blob: 6bbb988dd8daab88fa0de40ad2aaff8f96da1de6 [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
James Smart01649562017-02-12 13:52:32 -0800912 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -0800913 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
914 if (!phba->nvmet_support)
915 lpfc_nvme_update_localport(vports[i]);
916 /* todo: tgt: update targetport attributes */
917 }
James Smart549e55c2007-08-02 11:09:51 -0400918 }
James Smart895427b2017-02-12 13:52:30 -0800919 }
James Smart09372822008-01-11 01:52:54 -0500920 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500921 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500922 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
923 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400924 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500925 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500926 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400927 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500928 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500929 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500930 }
931 }
932
dea31012005-04-17 16:05:31 -0500933 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500934 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500935 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
936 if (mb) {
dea31012005-04-17 16:05:31 -0500937 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500938 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500939 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400940 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500941 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500942 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500943 }
944 }
James Smart2e0fef82007-06-17 19:56:36 -0500945 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500946 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500947 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500948 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500949 return 0;
dea31012005-04-17 16:05:31 -0500950}
951
James Smart92d7f7b2007-06-17 19:56:38 -0500952static void
953lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500954{
James Smart92d7f7b2007-06-17 19:56:38 -0500955 struct lpfc_nodelist *ndlp;
956
957 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500958 if (!NLP_CHK_NODE_ACT(ndlp))
959 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500960 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
961 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500962 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500963 /* On Linkup its safe to clean up the ndlp
964 * from Fabric connections.
965 */
James Smart92d7f7b2007-06-17 19:56:38 -0500966 if (ndlp->nlp_DID != Fabric_DID)
967 lpfc_unreg_rpi(vport, ndlp);
968 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
969 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500970 /* Fail outstanding IO now since device is
971 * marked for PLOGI.
972 */
James Smart92d7f7b2007-06-17 19:56:38 -0500973 lpfc_unreg_rpi(vport, ndlp);
974 }
975 }
976}
977
978static void
979lpfc_linkup_port(struct lpfc_vport *vport)
980{
981 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500982 struct lpfc_hba *phba = vport->phba;
983
984 if ((vport->load_flag & FC_UNLOADING) != 0)
985 return;
986
James Smart858c9f62007-06-17 19:56:39 -0500987 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
988 "Link Up: top:x%x speed:x%x flg:x%x",
989 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
990
James Smart92d7f7b2007-06-17 19:56:38 -0500991 /* If NPIV is not enabled, only bring the physical port up */
992 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
993 (vport != phba->pport))
994 return;
dea31012005-04-17 16:05:31 -0500995
James Smarta0f2d3e2017-02-12 13:52:31 -0800996 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
997 fc_host_post_event(shost, fc_get_event_number(),
998 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400999
James Smart2e0fef82007-06-17 19:56:36 -05001000 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001001 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1002 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1003 vport->fc_flag |= FC_NDISC_ACTIVE;
1004 vport->fc_ns_retry = 0;
1005 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001006
James Smart92d7f7b2007-06-17 19:56:38 -05001007 if (vport->fc_flag & FC_LBIT)
1008 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001009
James Smart92d7f7b2007-06-17 19:56:38 -05001010}
1011
1012static int
1013lpfc_linkup(struct lpfc_hba *phba)
1014{
James Smart549e55c2007-08-02 11:09:51 -04001015 struct lpfc_vport **vports;
1016 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001017
1018 phba->link_state = LPFC_LINK_UP;
1019
1020 /* Unblock fabric iocbs if they are blocked */
1021 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1022 del_timer_sync(&phba->fabric_block_timer);
1023
James Smart549e55c2007-08-02 11:09:51 -04001024 vports = lpfc_create_vport_work_array(phba);
1025 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001026 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001027 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001028 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001029
1030 return 0;
1031}
1032
1033/*
1034 * This routine handles processing a CLEAR_LA mailbox
1035 * command upon completion. It is setup in the LPFC_MBOXQ
1036 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001037 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001038 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001039static void
James Smart2e0fef82007-06-17 19:56:36 -05001040lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001041{
James Smart2e0fef82007-06-17 19:56:36 -05001042 struct lpfc_vport *vport = pmb->vport;
1043 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1044 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001045 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001046 uint32_t control;
1047
dea31012005-04-17 16:05:31 -05001048 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001049 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1050 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001051
1052 /* Check for error */
1053 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001054 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001055 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1056 "0320 CLEAR_LA mbxStatus error x%x hba "
1057 "state x%x\n",
1058 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001059 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001060 goto out;
1061 }
1062
James Smart92d7f7b2007-06-17 19:56:38 -05001063 if (vport->port_type == LPFC_PHYSICAL_PORT)
1064 phba->link_state = LPFC_HBA_READY;
1065
1066 spin_lock_irq(&phba->hbalock);
1067 psli->sli_flag |= LPFC_PROCESS_LA;
1068 control = readl(phba->HCregaddr);
1069 control |= HC_LAINT_ENA;
1070 writel(control, phba->HCregaddr);
1071 readl(phba->HCregaddr); /* flush */
1072 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001073 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001074 return;
dea31012005-04-17 16:05:31 -05001075
dea31012005-04-17 16:05:31 -05001076out:
1077 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001078 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1079 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001080 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001081
James Smart2e0fef82007-06-17 19:56:36 -05001082 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001083 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001084 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001085
James Smart2e0fef82007-06-17 19:56:36 -05001086 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001087
1088 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001089
1090 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001091 psli->sli_flag |= LPFC_PROCESS_LA;
1092 control = readl(phba->HCregaddr);
1093 control |= HC_LAINT_ENA;
1094 writel(control, phba->HCregaddr);
1095 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001096 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001097
1098 return;
1099}
1100
James Smart2e0fef82007-06-17 19:56:36 -05001101
James Smartd6de08c2015-12-16 18:11:53 -05001102void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001103lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001104{
James Smart2e0fef82007-06-17 19:56:36 -05001105 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001106
James Smart04c68492009-05-22 14:52:52 -04001107 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001108 goto out;
dea31012005-04-17 16:05:31 -05001109
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001110 mempool_free(pmb, phba->mbox_mem_pool);
1111
James Smart1b511972011-12-13 13:23:09 -05001112 /* don't perform discovery for SLI4 loopback diagnostic test */
1113 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1114 !(phba->hba_flag & HBA_FCOE_MODE) &&
1115 (phba->link_flag & LS_LOOPBACK_MODE))
1116 return;
1117
James Smart76a95d72010-11-20 23:11:48 -05001118 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001119 vport->fc_flag & FC_PUBLIC_LOOP &&
1120 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001121 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001122 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001123 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1124 */
James Smart2e0fef82007-06-17 19:56:36 -05001125 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001126 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001127 }
dea31012005-04-17 16:05:31 -05001128
James Smart2e0fef82007-06-17 19:56:36 -05001129 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001130 * LPFC_FLOGI while waiting for FLOGI cmpl
1131 */
James Smartd6de08c2015-12-16 18:11:53 -05001132 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001133 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001134 else if (vport->fc_flag & FC_PT2PT)
1135 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001136 return;
dea31012005-04-17 16:05:31 -05001137
1138out:
James Smarte8b62012007-08-02 11:10:09 -04001139 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1140 "0306 CONFIG_LINK mbxStatus error x%x "
1141 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001142 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001143 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001144
1145 lpfc_linkdown(phba);
1146
James Smarte8b62012007-08-02 11:10:09 -04001147 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1148 "0200 CONFIG_LINK bad hba state x%x\n",
1149 vport->port_state);
dea31012005-04-17 16:05:31 -05001150
James Smart92d7f7b2007-06-17 19:56:38 -05001151 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001152 return;
1153}
1154
James Smart7d791df2011-07-22 18:37:52 -04001155/**
1156 * lpfc_sli4_clear_fcf_rr_bmask
1157 * @phba pointer to the struct lpfc_hba for this port.
1158 * This fucnction resets the round robin bit mask and clears the
1159 * fcf priority list. The list deletions are done while holding the
1160 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1161 * from the lpfc_fcf_pri record.
1162 **/
1163void
1164lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1165{
1166 struct lpfc_fcf_pri *fcf_pri;
1167 struct lpfc_fcf_pri *next_fcf_pri;
1168 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1169 spin_lock_irq(&phba->hbalock);
1170 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1171 &phba->fcf.fcf_pri_list, list) {
1172 list_del_init(&fcf_pri->list);
1173 fcf_pri->fcf_rec.flag = 0;
1174 }
1175 spin_unlock_irq(&phba->hbalock);
1176}
dea31012005-04-17 16:05:31 -05001177static void
James Smart6fb120a2009-05-22 14:52:59 -04001178lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1179{
1180 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001181
1182 if (mboxq->u.mb.mbxStatus) {
1183 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1184 "2017 REG_FCFI mbxStatus error x%x "
1185 "HBA state x%x\n",
1186 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001187 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001188 }
1189
1190 /* Start FCoE discovery by sending a FLOGI. */
1191 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1192 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001193 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001194 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001195 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001196
James Smart32b97932009-07-19 10:01:21 -04001197 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001198 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1199 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001200 goto fail_out;
1201
1202 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001203 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001204 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001205 phba->hba_flag &= ~FCF_TS_INPROG;
1206 if (vport->port_state != LPFC_FLOGI) {
1207 phba->hba_flag |= FCF_RR_INPROG;
1208 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001209 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001210 goto out;
1211 }
1212 spin_unlock_irq(&phba->hbalock);
1213 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001214
James Smarta93ff372010-10-22 11:06:08 -04001215fail_out:
1216 spin_lock_irq(&phba->hbalock);
1217 phba->hba_flag &= ~FCF_RR_INPROG;
1218 spin_unlock_irq(&phba->hbalock);
1219out:
James Smart6fb120a2009-05-22 14:52:59 -04001220 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001221}
1222
1223/**
1224 * lpfc_fab_name_match - Check if the fcf fabric name match.
1225 * @fab_name: pointer to fabric name.
1226 * @new_fcf_record: pointer to fcf record.
1227 *
1228 * This routine compare the fcf record's fabric name with provided
1229 * fabric name. If the fabric name are identical this function
1230 * returns 1 else return 0.
1231 **/
1232static uint32_t
1233lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1234{
James Smartecfd03c2010-02-12 14:41:27 -05001235 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001236 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001237 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1238 return 0;
1239 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1240 return 0;
1241 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1242 return 0;
1243 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1244 return 0;
1245 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1246 return 0;
1247 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1248 return 0;
1249 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1250 return 0;
1251 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001252}
1253
1254/**
James Smart8fa38512009-07-19 10:01:03 -04001255 * lpfc_sw_name_match - Check if the fcf switch name match.
1256 * @fab_name: pointer to fabric name.
1257 * @new_fcf_record: pointer to fcf record.
1258 *
1259 * This routine compare the fcf record's switch name with provided
1260 * switch name. If the switch name are identical this function
1261 * returns 1 else return 0.
1262 **/
1263static uint32_t
1264lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1265{
James Smartecfd03c2010-02-12 14:41:27 -05001266 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001267 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001268 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1269 return 0;
1270 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1271 return 0;
1272 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1273 return 0;
1274 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1275 return 0;
1276 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1277 return 0;
1278 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1279 return 0;
1280 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1281 return 0;
1282 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001283}
1284
1285/**
James Smart6fb120a2009-05-22 14:52:59 -04001286 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001287 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001288 * @new_fcf_record: pointer to fcf record.
1289 *
1290 * This routine compare the fcf record's mac address with HBA's
1291 * FCF mac address. If the mac addresses are identical this function
1292 * returns 1 else return 0.
1293 **/
1294static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001295lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001296{
James Smartecfd03c2010-02-12 14:41:27 -05001297 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001298 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001299 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1300 return 0;
1301 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1302 return 0;
1303 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1304 return 0;
1305 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1306 return 0;
1307 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1308 return 0;
1309 return 1;
1310}
1311
1312static bool
1313lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1314{
1315 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001316}
1317
1318/**
James Smart7d791df2011-07-22 18:37:52 -04001319 * lpfc_update_fcf_record - Update driver fcf record
1320 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1321 * @phba: pointer to lpfc hba data structure.
1322 * @fcf_index: Index for the lpfc_fcf_record.
1323 * @new_fcf_record: pointer to hba fcf record.
1324 *
1325 * This routine updates the driver FCF priority record from the new HBA FCF
1326 * record. This routine is called with the host lock held.
1327 **/
1328static void
1329__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1330 struct fcf_record *new_fcf_record
1331 )
1332{
1333 struct lpfc_fcf_pri *fcf_pri;
1334
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001335 lockdep_assert_held(&phba->hbalock);
1336
James Smart7d791df2011-07-22 18:37:52 -04001337 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1338 fcf_pri->fcf_rec.fcf_index = fcf_index;
1339 /* FCF record priority */
1340 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1341
1342}
1343
1344/**
James Smart6fb120a2009-05-22 14:52:59 -04001345 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001346 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001347 * @new_fcf_record: pointer to fcf record.
1348 *
1349 * This routine copies the FCF information from the FCF
1350 * record to lpfc_hba data structure.
1351 **/
1352static void
James Smartecfd03c2010-02-12 14:41:27 -05001353lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1354 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001355{
James Smartecfd03c2010-02-12 14:41:27 -05001356 /* Fabric name */
1357 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001358 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001359 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001360 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001361 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001362 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001363 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001364 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001365 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001366 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001367 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001368 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001369 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001370 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001371 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001372 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001373 /* Mac address */
1374 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1375 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1376 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1377 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1378 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1379 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1380 /* FCF record index */
1381 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1382 /* FCF record priority */
1383 fcf_rec->priority = new_fcf_record->fip_priority;
1384 /* Switch name */
1385 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001386 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001387 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001388 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001389 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001390 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001391 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001392 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001393 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001394 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001395 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001396 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001397 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001398 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001399 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001400 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001401}
1402
1403/**
James Smartecfd03c2010-02-12 14:41:27 -05001404 * lpfc_update_fcf_record - Update driver fcf record
1405 * @phba: pointer to lpfc hba data structure.
1406 * @fcf_rec: pointer to driver fcf record.
1407 * @new_fcf_record: pointer to hba fcf record.
1408 * @addr_mode: address mode to be set to the driver fcf record.
1409 * @vlan_id: vlan tag to be set to the driver fcf record.
1410 * @flag: flag bits to be set to the driver fcf record.
1411 *
1412 * This routine updates the driver FCF record from the new HBA FCF record
1413 * together with the address mode, vlan_id, and other informations. This
1414 * routine is called with the host lock held.
1415 **/
1416static void
1417__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1418 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1419 uint16_t vlan_id, uint32_t flag)
1420{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001421 lockdep_assert_held(&phba->hbalock);
1422
James Smartecfd03c2010-02-12 14:41:27 -05001423 /* Copy the fields from the HBA's FCF record */
1424 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1425 /* Update other fields of driver FCF record */
1426 fcf_rec->addr_mode = addr_mode;
1427 fcf_rec->vlan_id = vlan_id;
1428 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001429 __lpfc_update_fcf_record_pri(phba,
1430 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1431 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001432}
1433
1434/**
James Smart6fb120a2009-05-22 14:52:59 -04001435 * lpfc_register_fcf - Register the FCF with hba.
1436 * @phba: pointer to lpfc hba data structure.
1437 *
1438 * This routine issues a register fcfi mailbox command to register
1439 * the fcf with HBA.
1440 **/
1441static void
1442lpfc_register_fcf(struct lpfc_hba *phba)
1443{
1444 LPFC_MBOXQ_t *fcf_mbxq;
1445 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001446
James Smartd439d282010-09-29 11:18:45 -04001447 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001448 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001449 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001450 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001451 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001452 return;
1453 }
1454
1455 /* The FCF is already registered, start discovery */
1456 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001457 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001458 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001459 if (phba->pport->port_state != LPFC_FLOGI &&
1460 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001461 phba->hba_flag |= FCF_RR_INPROG;
1462 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001463 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001464 return;
1465 }
1466 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001467 return;
1468 }
James Smartd439d282010-09-29 11:18:45 -04001469 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001470
James Smarta93ff372010-10-22 11:06:08 -04001471 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001472 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001473 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001474 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001475 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001476 return;
James Smart4d9ab992009-10-02 15:16:39 -04001477 }
James Smart6fb120a2009-05-22 14:52:59 -04001478
1479 lpfc_reg_fcfi(phba, fcf_mbxq);
1480 fcf_mbxq->vport = phba->pport;
1481 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1482 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001483 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001484 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001485 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001486 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001487 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001488 }
James Smart6fb120a2009-05-22 14:52:59 -04001489
1490 return;
1491}
1492
1493/**
1494 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1495 * @phba: pointer to lpfc hba data structure.
1496 * @new_fcf_record: pointer to fcf record.
1497 * @boot_flag: Indicates if this record used by boot bios.
1498 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001499 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001500 *
1501 * This routine compare the fcf record with connect list obtained from the
1502 * config region to decide if this FCF can be used for SAN discovery. It returns
1503 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1504 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1505 * is used by boot bios and addr_mode will indicate the addressing mode to be
1506 * used for this FCF when the function returns.
1507 * If the FCF record need to be used with a particular vlan id, the vlan is
1508 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001509 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001510 **/
1511static int
1512lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1513 struct fcf_record *new_fcf_record,
1514 uint32_t *boot_flag, uint32_t *addr_mode,
1515 uint16_t *vlan_id)
1516{
1517 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001518 int i, j, fcf_vlan_id = 0;
1519
1520 /* Find the lowest VLAN id in the FCF record */
1521 for (i = 0; i < 512; i++) {
1522 if (new_fcf_record->vlan_bitmap[i]) {
1523 fcf_vlan_id = i * 8;
1524 j = 0;
1525 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1526 j++;
1527 fcf_vlan_id++;
1528 }
1529 break;
1530 }
1531 }
James Smart6fb120a2009-05-22 14:52:59 -04001532
James Smart26979ce2012-09-29 11:31:55 -04001533 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001534 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001535 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1536 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001537 return 0;
1538
James Smart45ed1192009-10-02 15:17:02 -04001539 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001540 *boot_flag = 0;
1541 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1542 new_fcf_record);
1543 if (phba->valid_vlan)
1544 *vlan_id = phba->vlan_id;
1545 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001546 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001547 return 1;
1548 }
1549
1550 /*
1551 * If there are no FCF connection table entry, driver connect to all
1552 * FCFs.
1553 */
1554 if (list_empty(&phba->fcf_conn_rec_list)) {
1555 *boot_flag = 0;
1556 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1557 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001558
1559 /*
1560 * When there are no FCF connect entries, use driver's default
1561 * addressing mode - FPMA.
1562 */
1563 if (*addr_mode & LPFC_FCF_FPMA)
1564 *addr_mode = LPFC_FCF_FPMA;
1565
James Smart4d9ab992009-10-02 15:16:39 -04001566 /* If FCF record report a vlan id use that vlan id */
1567 if (fcf_vlan_id)
1568 *vlan_id = fcf_vlan_id;
1569 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001570 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001571 return 1;
1572 }
1573
James Smartecfd03c2010-02-12 14:41:27 -05001574 list_for_each_entry(conn_entry,
1575 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001576 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1577 continue;
1578
1579 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1580 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001581 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001582 continue;
James Smart8fa38512009-07-19 10:01:03 -04001583 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1584 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1585 new_fcf_record))
1586 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001587 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1588 /*
1589 * If the vlan bit map does not have the bit set for the
1590 * vlan id to be used, then it is not a match.
1591 */
1592 if (!(new_fcf_record->vlan_bitmap
1593 [conn_entry->conn_rec.vlan_tag / 8] &
1594 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1595 continue;
1596 }
1597
1598 /*
James Smart0c287582009-06-10 17:22:56 -04001599 * If connection record does not support any addressing mode,
1600 * skip the FCF record.
1601 */
1602 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1603 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1604 continue;
1605
1606 /*
James Smart6fb120a2009-05-22 14:52:59 -04001607 * Check if the connection record specifies a required
1608 * addressing mode.
1609 */
1610 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1611 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1612
1613 /*
1614 * If SPMA required but FCF not support this continue.
1615 */
1616 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1617 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1618 new_fcf_record) & LPFC_FCF_SPMA))
1619 continue;
1620
1621 /*
1622 * If FPMA required but FCF not support this continue.
1623 */
1624 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1625 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1626 new_fcf_record) & LPFC_FCF_FPMA))
1627 continue;
1628 }
1629
1630 /*
1631 * This fcf record matches filtering criteria.
1632 */
1633 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1634 *boot_flag = 1;
1635 else
1636 *boot_flag = 0;
1637
James Smart0c287582009-06-10 17:22:56 -04001638 /*
1639 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001640 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001641 * by FCF, allow fabric to pick the addressing mode.
1642 */
James Smart6fb120a2009-05-22 14:52:59 -04001643 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1644 new_fcf_record);
1645 /*
1646 * If the user specified a required address mode, assign that
1647 * address mode
1648 */
1649 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1650 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1651 *addr_mode = (conn_entry->conn_rec.flags &
1652 FCFCNCT_AM_SPMA) ?
1653 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1654 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001655 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001656 * addr mode only if FCF support the addr_mode.
1657 */
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_SPMA))
1662 *addr_mode = LPFC_FCF_SPMA;
1663 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1664 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1665 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1666 (*addr_mode & LPFC_FCF_FPMA))
1667 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001668
James Smart4d9ab992009-10-02 15:16:39 -04001669 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001670 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1671 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001672 /*
1673 * If no vlan id is specified in connect list, use the vlan id
1674 * in the FCF record
1675 */
1676 else if (fcf_vlan_id)
1677 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001678 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001679 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001680
1681 return 1;
1682 }
1683
1684 return 0;
1685}
1686
1687/**
James Smart32b97932009-07-19 10:01:21 -04001688 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1689 * @phba: pointer to lpfc hba data structure.
1690 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1691 *
1692 * This function check if there is any fcoe event pending while driver
1693 * scan FCF entries. If there is any pending event, it will restart the
1694 * FCF saning and return 1 else return 0.
1695 */
1696int
1697lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1698{
James Smart32b97932009-07-19 10:01:21 -04001699 /*
1700 * If the Link is up and no FCoE events while in the
1701 * FCF discovery, no need to restart FCF discovery.
1702 */
1703 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001704 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001705 return 0;
1706
James Smart0c9ab6f2010-02-26 14:15:57 -05001707 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1708 "2768 Pending link or FCF event during current "
1709 "handling of the previous event: link_state:x%x, "
1710 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1711 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1712 phba->fcoe_eventtag);
1713
James Smart32b97932009-07-19 10:01:21 -04001714 spin_lock_irq(&phba->hbalock);
1715 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1716 spin_unlock_irq(&phba->hbalock);
1717
James Smart0c9ab6f2010-02-26 14:15:57 -05001718 if (phba->link_state >= LPFC_LINK_UP) {
1719 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1720 "2780 Restart FCF table scan due to "
1721 "pending FCF event:evt_tag_at_scan:x%x, "
1722 "evt_tag_current:x%x\n",
1723 phba->fcoe_eventtag_at_fcf_scan,
1724 phba->fcoe_eventtag);
1725 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1726 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001727 /*
James Smarta93ff372010-10-22 11:06:08 -04001728 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001729 * flag
1730 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001731 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1732 "2833 Stop FCF discovery process due to link "
1733 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001734 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001735 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001736 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001737 spin_unlock_irq(&phba->hbalock);
1738 }
James Smart32b97932009-07-19 10:01:21 -04001739
James Smart0c9ab6f2010-02-26 14:15:57 -05001740 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001741 if (unreg_fcf) {
1742 spin_lock_irq(&phba->hbalock);
1743 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1744 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001745 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001746 }
James Smart32b97932009-07-19 10:01:21 -04001747 return 1;
1748}
1749
1750/**
James Smart999d8132010-03-15 11:24:56 -04001751 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1752 * @phba: pointer to lpfc hba data structure.
1753 * @fcf_cnt: number of eligible fcf record seen so far.
1754 *
1755 * This function makes an running random selection decision on FCF record to
1756 * use through a sequence of @fcf_cnt eligible FCF records with equal
1757 * probability. To perform integer manunipulation of random numbers with
1758 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001759 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001760 *
1761 * Returns true when outcome is for the newly read FCF record should be
1762 * chosen; otherwise, return false when outcome is for keeping the previously
1763 * chosen FCF record.
1764 **/
1765static bool
1766lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1767{
1768 uint32_t rand_num;
1769
1770 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001771 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001772
1773 /* Decision with probability 1/fcf_cnt */
1774 if ((fcf_cnt * rand_num) < 0xFFFF)
1775 return true;
1776 else
1777 return false;
1778}
1779
1780/**
James Smart3804dc82010-07-14 15:31:37 -04001781 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001782 * @phba: pointer to lpfc hba data structure.
1783 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001784 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001785 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001786 * This routine parses the non-embedded fcf mailbox command by performing the
1787 * necessarily error checking, non-embedded read FCF record mailbox command
1788 * SGE parsing, and endianness swapping.
1789 *
1790 * Returns the pointer to the new FCF record in the non-embedded mailbox
1791 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001792 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001793static struct fcf_record *
1794lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1795 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001796{
1797 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001798 struct lpfc_mbx_sge sge;
1799 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001800 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001801 union lpfc_sli4_cfg_shdr *shdr;
1802 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001803
James Smart6fb120a2009-05-22 14:52:59 -04001804 /* Get the first SGE entry from the non-embedded DMA memory. This
1805 * routine only uses a single SGE.
1806 */
1807 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001808 if (unlikely(!mboxq->sge_array)) {
1809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1810 "2524 Failed to get the non-embedded SGE "
1811 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001812 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001813 }
1814 virt_addr = mboxq->sge_array->addr[0];
1815
1816 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001817 lpfc_sli_pcimem_bcopy(shdr, shdr,
1818 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001819 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001820 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001821 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001822 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001823 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1824 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001825 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001826 "2726 READ_FCF_RECORD Indicates empty "
1827 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001828 else
1829 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001830 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001831 "with status x%x add_status x%x, "
1832 "mbx\n", shdr_status, shdr_add_status);
1833 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001834 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001835
1836 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001837 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1838 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1839 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001840 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001841 new_fcf_record = (struct fcf_record *)(virt_addr +
1842 sizeof(struct lpfc_mbx_read_fcf_tbl));
1843 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001844 offsetof(struct fcf_record, vlan_bitmap));
1845 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1846 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001847
James Smart0c9ab6f2010-02-26 14:15:57 -05001848 return new_fcf_record;
1849}
1850
1851/**
1852 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1853 * @phba: pointer to lpfc hba data structure.
1854 * @fcf_record: pointer to the fcf record.
1855 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1856 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1857 *
1858 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1859 * enabled.
1860 **/
1861static void
1862lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1863 struct fcf_record *fcf_record,
1864 uint16_t vlan_id,
1865 uint16_t next_fcf_index)
1866{
1867 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1868 "2764 READ_FCF_RECORD:\n"
1869 "\tFCF_Index : x%x\n"
1870 "\tFCF_Avail : x%x\n"
1871 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001872 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001873 "\tFIP_Priority : x%x\n"
1874 "\tMAC_Provider : x%x\n"
1875 "\tLowest VLANID : x%x\n"
1876 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1877 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1878 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1879 "\tNext_FCF_Index: x%x\n",
1880 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1881 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1882 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001883 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001884 fcf_record->fip_priority,
1885 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1886 vlan_id,
1887 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1888 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1889 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1890 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1891 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1892 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1893 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1894 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1895 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1896 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1897 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1898 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1899 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1900 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1901 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1902 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1903 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1904 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1905 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1906 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1907 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1908 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1909 next_fcf_index);
1910}
1911
1912/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001913 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1914 * @phba: pointer to lpfc hba data structure.
1915 * @fcf_rec: pointer to an existing FCF record.
1916 * @new_fcf_record: pointer to a new FCF record.
1917 * @new_vlan_id: vlan id from the new FCF record.
1918 *
1919 * This function performs matching test of a new FCF record against an existing
1920 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1921 * will not be used as part of the FCF record matching criteria.
1922 *
1923 * Returns true if all the fields matching, otherwise returns false.
1924 */
1925static bool
1926lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1927 struct lpfc_fcf_rec *fcf_rec,
1928 struct fcf_record *new_fcf_record,
1929 uint16_t new_vlan_id)
1930{
1931 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1932 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1933 return false;
1934 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1935 return false;
1936 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1937 return false;
1938 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1939 return false;
James Smart7d791df2011-07-22 18:37:52 -04001940 if (fcf_rec->priority != new_fcf_record->fip_priority)
1941 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001942 return true;
1943}
1944
1945/**
James Smarta93ff372010-10-22 11:06:08 -04001946 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1947 * @vport: Pointer to vport object.
1948 * @fcf_index: index to next fcf.
1949 *
1950 * This function processing the roundrobin fcf failover to next fcf index.
1951 * When this function is invoked, there will be a current fcf registered
1952 * for flogi.
1953 * Return: 0 for continue retrying flogi on currently registered fcf;
1954 * 1 for stop flogi on currently registered fcf;
1955 */
1956int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1957{
1958 struct lpfc_hba *phba = vport->phba;
1959 int rc;
1960
1961 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1962 spin_lock_irq(&phba->hbalock);
1963 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1964 spin_unlock_irq(&phba->hbalock);
1965 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1966 "2872 Devloss tmo with no eligible "
1967 "FCF, unregister in-use FCF (x%x) "
1968 "and rescan FCF table\n",
1969 phba->fcf.current_rec.fcf_indx);
1970 lpfc_unregister_fcf_rescan(phba);
1971 goto stop_flogi_current_fcf;
1972 }
1973 /* Mark the end to FLOGI roundrobin failover */
1974 phba->hba_flag &= ~FCF_RR_INPROG;
1975 /* Allow action to new fcf asynchronous event */
1976 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1977 spin_unlock_irq(&phba->hbalock);
1978 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1979 "2865 No FCF available, stop roundrobin FCF "
1980 "failover and change port state:x%x/x%x\n",
1981 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1982 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1983 goto stop_flogi_current_fcf;
1984 } else {
1985 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1986 "2794 Try FLOGI roundrobin FCF failover to "
1987 "(x%x)\n", fcf_index);
1988 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1989 if (rc)
1990 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1991 "2761 FLOGI roundrobin FCF failover "
1992 "failed (rc:x%x) to read FCF (x%x)\n",
1993 rc, phba->fcf.current_rec.fcf_indx);
1994 else
1995 goto stop_flogi_current_fcf;
1996 }
1997 return 0;
1998
1999stop_flogi_current_fcf:
2000 lpfc_can_disctmo(vport);
2001 return 1;
2002}
2003
2004/**
James Smart7d791df2011-07-22 18:37:52 -04002005 * lpfc_sli4_fcf_pri_list_del
2006 * @phba: pointer to lpfc hba data structure.
2007 * @fcf_index the index of the fcf record to delete
2008 * This routine checks the on list flag of the fcf_index to be deleted.
2009 * If it is one the list then it is removed from the list, and the flag
2010 * is cleared. This routine grab the hbalock before removing the fcf
2011 * record from the list.
2012 **/
2013static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2014 uint16_t fcf_index)
2015{
2016 struct lpfc_fcf_pri *new_fcf_pri;
2017
2018 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2019 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2020 "3058 deleting idx x%x pri x%x flg x%x\n",
2021 fcf_index, new_fcf_pri->fcf_rec.priority,
2022 new_fcf_pri->fcf_rec.flag);
2023 spin_lock_irq(&phba->hbalock);
2024 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2025 if (phba->fcf.current_rec.priority ==
2026 new_fcf_pri->fcf_rec.priority)
2027 phba->fcf.eligible_fcf_cnt--;
2028 list_del_init(&new_fcf_pri->list);
2029 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2030 }
2031 spin_unlock_irq(&phba->hbalock);
2032}
2033
2034/**
2035 * lpfc_sli4_set_fcf_flogi_fail
2036 * @phba: pointer to lpfc hba data structure.
2037 * @fcf_index the index of the fcf record to update
2038 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2039 * flag so the the round robin slection for the particular priority level
2040 * will try a different fcf record that does not have this bit set.
2041 * If the fcf record is re-read for any reason this flag is cleared brfore
2042 * adding it to the priority list.
2043 **/
2044void
2045lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2046{
2047 struct lpfc_fcf_pri *new_fcf_pri;
2048 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2049 spin_lock_irq(&phba->hbalock);
2050 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2051 spin_unlock_irq(&phba->hbalock);
2052}
2053
2054/**
2055 * lpfc_sli4_fcf_pri_list_add
2056 * @phba: pointer to lpfc hba data structure.
2057 * @fcf_index the index of the fcf record to add
2058 * This routine checks the priority of the fcf_index to be added.
2059 * If it is a lower priority than the current head of the fcf_pri list
2060 * then it is added to the list in the right order.
2061 * If it is the same priority as the current head of the list then it
2062 * is added to the head of the list and its bit in the rr_bmask is set.
2063 * If the fcf_index to be added is of a higher priority than the current
2064 * head of the list then the rr_bmask is cleared, its bit is set in the
2065 * rr_bmask and it is added to the head of the list.
2066 * returns:
2067 * 0=success 1=failure
2068 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002069static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2070 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002071 struct fcf_record *new_fcf_record)
2072{
2073 uint16_t current_fcf_pri;
2074 uint16_t last_index;
2075 struct lpfc_fcf_pri *fcf_pri;
2076 struct lpfc_fcf_pri *next_fcf_pri;
2077 struct lpfc_fcf_pri *new_fcf_pri;
2078 int ret;
2079
2080 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2081 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2082 "3059 adding idx x%x pri x%x flg x%x\n",
2083 fcf_index, new_fcf_record->fip_priority,
2084 new_fcf_pri->fcf_rec.flag);
2085 spin_lock_irq(&phba->hbalock);
2086 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2087 list_del_init(&new_fcf_pri->list);
2088 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2089 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2090 if (list_empty(&phba->fcf.fcf_pri_list)) {
2091 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2092 ret = lpfc_sli4_fcf_rr_index_set(phba,
2093 new_fcf_pri->fcf_rec.fcf_index);
2094 goto out;
2095 }
2096
2097 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2098 LPFC_SLI4_FCF_TBL_INDX_MAX);
2099 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2100 ret = 0; /* Empty rr list */
2101 goto out;
2102 }
2103 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2104 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2105 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2106 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2107 memset(phba->fcf.fcf_rr_bmask, 0,
2108 sizeof(*phba->fcf.fcf_rr_bmask));
2109 /* fcfs_at_this_priority_level = 1; */
2110 phba->fcf.eligible_fcf_cnt = 1;
2111 } else
2112 /* fcfs_at_this_priority_level++; */
2113 phba->fcf.eligible_fcf_cnt++;
2114 ret = lpfc_sli4_fcf_rr_index_set(phba,
2115 new_fcf_pri->fcf_rec.fcf_index);
2116 goto out;
2117 }
2118
2119 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2120 &phba->fcf.fcf_pri_list, list) {
2121 if (new_fcf_pri->fcf_rec.priority <=
2122 fcf_pri->fcf_rec.priority) {
2123 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2124 list_add(&new_fcf_pri->list,
2125 &phba->fcf.fcf_pri_list);
2126 else
2127 list_add(&new_fcf_pri->list,
2128 &((struct lpfc_fcf_pri *)
2129 fcf_pri->list.prev)->list);
2130 ret = 0;
2131 goto out;
2132 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2133 || new_fcf_pri->fcf_rec.priority <
2134 next_fcf_pri->fcf_rec.priority) {
2135 list_add(&new_fcf_pri->list, &fcf_pri->list);
2136 ret = 0;
2137 goto out;
2138 }
2139 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2140 continue;
2141
2142 }
2143 ret = 1;
2144out:
2145 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2146 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2147 spin_unlock_irq(&phba->hbalock);
2148 return ret;
2149}
2150
2151/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002152 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2153 * @phba: pointer to lpfc hba data structure.
2154 * @mboxq: pointer to mailbox object.
2155 *
2156 * This function iterates through all the fcf records available in
2157 * HBA and chooses the optimal FCF record for discovery. After finding
2158 * the FCF for discovery it registers the FCF record and kicks start
2159 * discovery.
2160 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2161 * use an FCF record which matches fabric name and mac address of the
2162 * currently used FCF record.
2163 * If the driver supports only one FCF, it will try to use the FCF record
2164 * used by BOOT_BIOS.
2165 */
2166void
2167lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2168{
2169 struct fcf_record *new_fcf_record;
2170 uint32_t boot_flag, addr_mode;
2171 uint16_t fcf_index, next_fcf_index;
2172 struct lpfc_fcf_rec *fcf_rec = NULL;
2173 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002174 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002175 int rc;
2176
2177 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002178 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002179 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2180 return;
2181 }
2182
2183 /* Parse the FCF record from the non-embedded mailbox command */
2184 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2185 &next_fcf_index);
2186 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002187 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002188 "2765 Mailbox command READ_FCF_RECORD "
2189 "failed to retrieve a FCF record.\n");
2190 /* Let next new FCF event trigger fast failover */
2191 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002192 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002193 spin_unlock_irq(&phba->hbalock);
2194 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2195 return;
2196 }
2197
2198 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002199 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2200 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002201
2202 /* Log the FCF record information if turned on */
2203 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2204 next_fcf_index);
2205
James Smart6fb120a2009-05-22 14:52:59 -04002206 /*
2207 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002208 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002209 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002210 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002211 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002212 lpfc_sli4_fcf_pri_list_del(phba,
2213 bf_get(lpfc_fcf_record_fcf_index,
2214 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002215 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002216 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002217 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002218 bf_get(lpfc_fcf_record_fcf_index,
2219 new_fcf_record),
2220 bf_get(lpfc_fcf_record_fcf_avail,
2221 new_fcf_record),
2222 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002223 new_fcf_record),
2224 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002225 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002226 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2227 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2228 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002229 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2230 phba->fcf.current_rec.fcf_indx) {
2231 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2232 "2862 FCF (x%x) matches property "
2233 "of in-use FCF (x%x)\n",
2234 bf_get(lpfc_fcf_record_fcf_index,
2235 new_fcf_record),
2236 phba->fcf.current_rec.fcf_indx);
2237 goto read_next_fcf;
2238 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002239 /*
2240 * In case the current in-use FCF record becomes
2241 * invalid/unavailable during FCF discovery that
2242 * was not triggered by fast FCF failover process,
2243 * treat it as fast FCF failover.
2244 */
2245 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2246 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2247 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2248 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002249 "(x%x), enter FCF failover "
2250 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002251 phba->fcf.current_rec.fcf_indx);
2252 spin_lock_irq(&phba->hbalock);
2253 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2254 spin_unlock_irq(&phba->hbalock);
2255 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2256 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2257 LPFC_FCOE_FCF_GET_FIRST);
2258 return;
2259 }
2260 }
James Smart6fb120a2009-05-22 14:52:59 -04002261 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002262 } else {
2263 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002264 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2265 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002266 if (rc)
2267 goto read_next_fcf;
2268 }
2269
James Smart6fb120a2009-05-22 14:52:59 -04002270 /*
2271 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002272 * FCF record for the discovery. The condition that a rescan
2273 * matches the in-use FCF record: fabric name, switch name, mac
2274 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002275 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002276 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002277 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002278 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2279 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002280 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002281 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2282 phba->fcf.current_rec.fcf_indx) {
2283 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2284 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2285 /* Stop FCF redisc wait timer */
2286 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2287 phba);
2288 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2289 /* Fast failover, mark completed */
2290 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2291 spin_unlock_irq(&phba->hbalock);
2292 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2293 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002294 "FCF (x%x), port_state:x%x, "
2295 "fc_flag:x%x\n",
2296 phba->fcf.current_rec.fcf_indx,
2297 phba->pport->port_state,
2298 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002299 goto out;
2300 } else
2301 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2302 "2863 New FCF (x%x) matches "
2303 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002304 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002305 new_fcf_record),
2306 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002307 }
James Smartecfd03c2010-02-12 14:41:27 -05002308 /*
2309 * Read next FCF record from HBA searching for the matching
2310 * with in-use record only if not during the fast failover
2311 * period. In case of fast failover period, it shall try to
2312 * determine whether the FCF record just read should be the
2313 * next candidate.
2314 */
2315 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002316 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002317 goto read_next_fcf;
2318 }
James Smart6fb120a2009-05-22 14:52:59 -04002319 }
James Smartecfd03c2010-02-12 14:41:27 -05002320 /*
2321 * Update on failover FCF record only if it's in FCF fast-failover
2322 * period; otherwise, update on current FCF record.
2323 */
James Smartfc2b9892010-02-26 14:15:29 -05002324 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2325 fcf_rec = &phba->fcf.failover_rec;
2326 else
James Smartecfd03c2010-02-12 14:41:27 -05002327 fcf_rec = &phba->fcf.current_rec;
2328
James Smart6fb120a2009-05-22 14:52:59 -04002329 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2330 /*
James Smartecfd03c2010-02-12 14:41:27 -05002331 * If the driver FCF record does not have boot flag
2332 * set and new hba fcf record has boot flag set, use
2333 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002334 */
James Smartecfd03c2010-02-12 14:41:27 -05002335 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2336 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002337 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2338 "2837 Update current FCF record "
2339 "(x%x) with new FCF record (x%x)\n",
2340 fcf_rec->fcf_indx,
2341 bf_get(lpfc_fcf_record_fcf_index,
2342 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002343 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2344 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002345 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002346 goto read_next_fcf;
2347 }
2348 /*
James Smartecfd03c2010-02-12 14:41:27 -05002349 * If the driver FCF record has boot flag set and the
2350 * new hba FCF record does not have boot flag, read
2351 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002352 */
James Smartecfd03c2010-02-12 14:41:27 -05002353 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002354 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002355 goto read_next_fcf;
2356 }
2357 /*
James Smartecfd03c2010-02-12 14:41:27 -05002358 * If the new hba FCF record has lower priority value
2359 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002360 */
James Smartfc2b9892010-02-26 14:15:29 -05002361 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002362 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002363 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2364 "2838 Update current FCF record "
2365 "(x%x) with new FCF record (x%x)\n",
2366 fcf_rec->fcf_indx,
2367 bf_get(lpfc_fcf_record_fcf_index,
2368 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002369 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2370 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002371 /* Reset running random FCF selection count */
2372 phba->fcf.eligible_fcf_cnt = 1;
2373 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2374 /* Update running random FCF selection count */
2375 phba->fcf.eligible_fcf_cnt++;
2376 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2377 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002378 if (select_new_fcf) {
2379 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2380 "2839 Update current FCF record "
2381 "(x%x) with new FCF record (x%x)\n",
2382 fcf_rec->fcf_indx,
2383 bf_get(lpfc_fcf_record_fcf_index,
2384 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002385 /* Choose the new FCF by random selection */
2386 __lpfc_update_fcf_record(phba, fcf_rec,
2387 new_fcf_record,
2388 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002389 }
James Smart6fb120a2009-05-22 14:52:59 -04002390 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002391 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002392 goto read_next_fcf;
2393 }
2394 /*
James Smartecfd03c2010-02-12 14:41:27 -05002395 * This is the first suitable FCF record, choose this record for
2396 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002397 */
James Smartecfd03c2010-02-12 14:41:27 -05002398 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002399 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002400 "2840 Update initial FCF candidate "
2401 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002402 bf_get(lpfc_fcf_record_fcf_index,
2403 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002404 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2405 addr_mode, vlan_id, (boot_flag ?
2406 BOOT_ENABLE : 0));
2407 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002408 /* Setup initial running random FCF selection count */
2409 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002410 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002411 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002412 goto read_next_fcf;
2413
2414read_next_fcf:
2415 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002416 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2417 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2418 /*
2419 * Case of FCF fast failover scan
2420 */
2421
2422 /*
2423 * It has not found any suitable FCF record, cancel
2424 * FCF scan inprogress, and do nothing
2425 */
2426 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002427 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002428 "2782 No suitable FCF found: "
2429 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002430 phba->fcoe_eventtag_at_fcf_scan,
2431 bf_get(lpfc_fcf_record_fcf_index,
2432 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002433 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002434 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2435 phba->hba_flag &= ~FCF_TS_INPROG;
2436 spin_unlock_irq(&phba->hbalock);
2437 /* Unregister in-use FCF and rescan */
2438 lpfc_printf_log(phba, KERN_INFO,
2439 LOG_FIP,
2440 "2864 On devloss tmo "
2441 "unreg in-use FCF and "
2442 "rescan FCF table\n");
2443 lpfc_unregister_fcf_rescan(phba);
2444 return;
2445 }
2446 /*
2447 * Let next new FCF event trigger fast failover
2448 */
2449 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002450 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002451 return;
2452 }
2453 /*
2454 * It has found a suitable FCF record that is not
2455 * the same as in-use FCF record, unregister the
2456 * in-use FCF record, replace the in-use FCF record
2457 * with the new FCF record, mark FCF fast failover
2458 * completed, and then start register the new FCF
2459 * record.
2460 */
2461
James Smart0c9ab6f2010-02-26 14:15:57 -05002462 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002463 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002464
2465 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002466 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002467 "2842 Replace in-use FCF (x%x) "
2468 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002469 phba->fcf.current_rec.fcf_indx,
2470 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002471 memcpy(&phba->fcf.current_rec,
2472 &phba->fcf.failover_rec,
2473 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002474 /*
2475 * Mark the fast FCF failover rediscovery completed
2476 * and the start of the first round of the roundrobin
2477 * FCF failover.
2478 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002479 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002480 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002481 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002482 /* Register to the new FCF record */
2483 lpfc_register_fcf(phba);
2484 } else {
2485 /*
2486 * In case of transaction period to fast FCF failover,
2487 * do nothing when search to the end of the FCF table.
2488 */
2489 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2490 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2491 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002492
James Smart7d791df2011-07-22 18:37:52 -04002493 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2494 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002495 /*
2496 * In case the current in-use FCF record no
2497 * longer existed during FCF discovery that
2498 * was not triggered by fast FCF failover
2499 * process, treat it as fast FCF failover.
2500 */
2501 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2502 "2841 In-use FCF record (x%x) "
2503 "not reported, entering fast "
2504 "FCF failover mode scanning.\n",
2505 phba->fcf.current_rec.fcf_indx);
2506 spin_lock_irq(&phba->hbalock);
2507 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2508 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002509 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2510 LPFC_FCOE_FCF_GET_FIRST);
2511 return;
2512 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002513 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002514 lpfc_register_fcf(phba);
2515 }
2516 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002517 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002518 return;
2519
2520out:
2521 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2522 lpfc_register_fcf(phba);
2523
2524 return;
2525}
2526
2527/**
James Smarta93ff372010-10-22 11:06:08 -04002528 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002529 * @phba: pointer to lpfc hba data structure.
2530 * @mboxq: pointer to mailbox object.
2531 *
James Smarta93ff372010-10-22 11:06:08 -04002532 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002533 * read FCF record mailbox command from the eligible FCF record bmask for
2534 * performing the failover. If the FCF read back is not valid/available, it
2535 * fails through to retrying FLOGI to the currently registered FCF again.
2536 * Otherwise, if the FCF read back is valid and available, it will set the
2537 * newly read FCF record to the failover FCF record, unregister currently
2538 * registered FCF record, copy the failover FCF record to the current
2539 * FCF record, and then register the current FCF record before proceeding
2540 * to trying FLOGI on the new failover FCF.
2541 */
2542void
2543lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2544{
2545 struct fcf_record *new_fcf_record;
2546 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002547 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002548 uint16_t current_fcf_index;
2549 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002550 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002551
James Smarta93ff372010-10-22 11:06:08 -04002552 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002553 if (phba->link_state < LPFC_LINK_UP) {
2554 spin_lock_irq(&phba->hbalock);
2555 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002556 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002557 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002558 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002559 }
2560
2561 /* Parse the FCF record from the non-embedded mailbox command */
2562 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2563 &next_fcf_index);
2564 if (!new_fcf_record) {
2565 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2566 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002567 "failed to retrieve a FCF record. "
2568 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2569 phba->fcf.fcf_flag);
2570 lpfc_unregister_fcf_rescan(phba);
2571 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002572 }
2573
2574 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002575 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2576 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002577
2578 /* Log the FCF record information if turned on */
2579 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2580 next_fcf_index);
2581
James Smarta93ff372010-10-22 11:06:08 -04002582 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2583 if (!rc) {
2584 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2585 "2848 Remove ineligible FCF (x%x) from "
2586 "from roundrobin bmask\n", fcf_index);
2587 /* Clear roundrobin bmask bit for ineligible FCF */
2588 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2589 /* Perform next round of roundrobin FCF failover */
2590 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2591 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2592 if (rc)
2593 goto out;
2594 goto error_out;
2595 }
2596
2597 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2598 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2599 "2760 Perform FLOGI roundrobin FCF failover: "
2600 "FCF (x%x) back to FCF (x%x)\n",
2601 phba->fcf.current_rec.fcf_indx, fcf_index);
2602 /* Wait 500 ms before retrying FLOGI to current FCF */
2603 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002604 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002605 goto out;
2606 }
2607
James Smart0c9ab6f2010-02-26 14:15:57 -05002608 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002609 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002610 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2611 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002612 spin_lock_irq(&phba->hbalock);
2613 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2614 new_fcf_record, addr_mode, vlan_id,
2615 (boot_flag ? BOOT_ENABLE : 0));
2616 spin_unlock_irq(&phba->hbalock);
2617
2618 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2619
2620 /* Unregister the current in-use FCF record */
2621 lpfc_unregister_fcf(phba);
2622
2623 /* Replace in-use record with the new record */
2624 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2625 sizeof(struct lpfc_fcf_rec));
2626
2627 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002628 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2629 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002630
James Smarta93ff372010-10-22 11:06:08 -04002631error_out:
2632 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002633out:
2634 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002635}
2636
2637/**
2638 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2639 * @phba: pointer to lpfc hba data structure.
2640 * @mboxq: pointer to mailbox object.
2641 *
2642 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002643 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002644 * failover when a new FCF event happened. If the FCF read back is
2645 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002646 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002647 */
2648void
2649lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2650{
2651 struct fcf_record *new_fcf_record;
2652 uint32_t boot_flag, addr_mode;
2653 uint16_t fcf_index, next_fcf_index;
2654 uint16_t vlan_id;
2655 int rc;
2656
2657 /* If link state is not up, no need to proceed */
2658 if (phba->link_state < LPFC_LINK_UP)
2659 goto out;
2660
2661 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002662 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002663 goto out;
2664
2665 /* Parse the FCF record from the non-embedded mailbox command */
2666 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2667 &next_fcf_index);
2668 if (!new_fcf_record) {
2669 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2670 "2767 Mailbox command READ_FCF_RECORD "
2671 "failed to retrieve a FCF record.\n");
2672 goto out;
2673 }
2674
2675 /* Check the connection list for eligibility */
2676 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2677 &addr_mode, &vlan_id);
2678
2679 /* Log the FCF record information if turned on */
2680 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2681 next_fcf_index);
2682
2683 if (!rc)
2684 goto out;
2685
2686 /* Update the eligible FCF record index bmask */
2687 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002688
2689 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002690
2691out:
2692 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2693}
2694
2695/**
James Smart76a95d72010-11-20 23:11:48 -05002696 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2697 * @phba: pointer to lpfc hba data structure.
2698 * @mboxq: pointer to mailbox data structure.
2699 *
2700 * This function handles completion of init vfi mailbox command.
2701 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002702static void
James Smart76a95d72010-11-20 23:11:48 -05002703lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2704{
2705 struct lpfc_vport *vport = mboxq->vport;
2706
James Smartdf9e1b52011-12-13 13:22:17 -05002707 /*
2708 * VFI not supported on interface type 0, just do the flogi
2709 * Also continue if the VFI is in use - just use the same one.
2710 */
2711 if (mboxq->u.mb.mbxStatus &&
2712 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2713 LPFC_SLI_INTF_IF_TYPE_0) &&
2714 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002715 lpfc_printf_vlog(vport, KERN_ERR,
2716 LOG_MBOX,
2717 "2891 Init VFI mailbox failed 0x%x\n",
2718 mboxq->u.mb.mbxStatus);
2719 mempool_free(mboxq, phba->mbox_mem_pool);
2720 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2721 return;
2722 }
James Smart73d91e52011-10-10 21:32:10 -04002723
James Smart76a95d72010-11-20 23:11:48 -05002724 lpfc_initial_flogi(vport);
2725 mempool_free(mboxq, phba->mbox_mem_pool);
2726 return;
2727}
2728
2729/**
2730 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2731 * @vport: pointer to lpfc_vport data structure.
2732 *
2733 * This function issue a init_vfi mailbox command to initialize the VFI and
2734 * VPI for the physical port.
2735 */
2736void
2737lpfc_issue_init_vfi(struct lpfc_vport *vport)
2738{
2739 LPFC_MBOXQ_t *mboxq;
2740 int rc;
2741 struct lpfc_hba *phba = vport->phba;
2742
2743 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2744 if (!mboxq) {
2745 lpfc_printf_vlog(vport, KERN_ERR,
2746 LOG_MBOX, "2892 Failed to allocate "
2747 "init_vfi mailbox\n");
2748 return;
2749 }
2750 lpfc_init_vfi(mboxq, vport);
2751 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2752 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2753 if (rc == MBX_NOT_FINISHED) {
2754 lpfc_printf_vlog(vport, KERN_ERR,
2755 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2756 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2757 }
2758}
2759
2760/**
James Smart1c6834a2009-07-19 10:01:26 -04002761 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2762 * @phba: pointer to lpfc hba data structure.
2763 * @mboxq: pointer to mailbox data structure.
2764 *
2765 * This function handles completion of init vpi mailbox command.
2766 */
James Smart695a8142010-01-26 23:08:03 -05002767void
James Smart1c6834a2009-07-19 10:01:26 -04002768lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2769{
2770 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002771 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002772 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2773
James Smart1c6834a2009-07-19 10:01:26 -04002774 if (mboxq->u.mb.mbxStatus) {
2775 lpfc_printf_vlog(vport, KERN_ERR,
2776 LOG_MBOX,
2777 "2609 Init VPI mailbox failed 0x%x\n",
2778 mboxq->u.mb.mbxStatus);
2779 mempool_free(mboxq, phba->mbox_mem_pool);
2780 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2781 return;
2782 }
James Smart72100cc2010-02-12 14:43:01 -05002783 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002784 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002785 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002786
James Smart695a8142010-01-26 23:08:03 -05002787 /* If this port is physical port or FDISC is done, do reg_vpi */
2788 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2789 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2790 if (!ndlp)
2791 lpfc_printf_vlog(vport, KERN_ERR,
2792 LOG_DISCOVERY,
2793 "2731 Cannot find fabric "
2794 "controller node\n");
2795 else
2796 lpfc_register_new_vport(phba, vport, ndlp);
2797 mempool_free(mboxq, phba->mbox_mem_pool);
2798 return;
2799 }
2800
James Smart1c6834a2009-07-19 10:01:26 -04002801 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2802 lpfc_initial_fdisc(vport);
2803 else {
2804 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002805 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2806 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002807 }
James Smart695a8142010-01-26 23:08:03 -05002808 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002809 return;
2810}
2811
2812/**
James Smartecfd03c2010-02-12 14:41:27 -05002813 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2814 * @vport: pointer to lpfc_vport data structure.
2815 *
2816 * This function issue a init_vpi mailbox command to initialize
2817 * VPI for the vport.
2818 */
2819void
2820lpfc_issue_init_vpi(struct lpfc_vport *vport)
2821{
2822 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002823 int rc, vpi;
2824
2825 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2826 vpi = lpfc_alloc_vpi(vport->phba);
2827 if (!vpi) {
2828 lpfc_printf_vlog(vport, KERN_ERR,
2829 LOG_MBOX,
2830 "3303 Failed to obtain vport vpi\n");
2831 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2832 return;
2833 }
2834 vport->vpi = vpi;
2835 }
James Smartecfd03c2010-02-12 14:41:27 -05002836
2837 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2838 if (!mboxq) {
2839 lpfc_printf_vlog(vport, KERN_ERR,
2840 LOG_MBOX, "2607 Failed to allocate "
2841 "init_vpi mailbox\n");
2842 return;
2843 }
2844 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2845 mboxq->vport = vport;
2846 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2847 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2848 if (rc == MBX_NOT_FINISHED) {
2849 lpfc_printf_vlog(vport, KERN_ERR,
2850 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2851 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2852 }
2853}
2854
2855/**
James Smart6fb120a2009-05-22 14:52:59 -04002856 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2857 * @phba: pointer to lpfc hba data structure.
2858 *
2859 * This function loops through the list of vports on the @phba and issues an
2860 * FDISC if possible.
2861 */
2862void
2863lpfc_start_fdiscs(struct lpfc_hba *phba)
2864{
2865 struct lpfc_vport **vports;
2866 int i;
2867
2868 vports = lpfc_create_vport_work_array(phba);
2869 if (vports != NULL) {
2870 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2871 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2872 continue;
2873 /* There are no vpi for this vport */
2874 if (vports[i]->vpi > phba->max_vpi) {
2875 lpfc_vport_set_state(vports[i],
2876 FC_VPORT_FAILED);
2877 continue;
2878 }
James Smart76a95d72010-11-20 23:11:48 -05002879 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002880 lpfc_vport_set_state(vports[i],
2881 FC_VPORT_LINKDOWN);
2882 continue;
2883 }
James Smart1c6834a2009-07-19 10:01:26 -04002884 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002885 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002886 continue;
2887 }
James Smart6fb120a2009-05-22 14:52:59 -04002888 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2889 lpfc_initial_fdisc(vports[i]);
2890 else {
2891 lpfc_vport_set_state(vports[i],
2892 FC_VPORT_NO_FABRIC_SUPP);
2893 lpfc_printf_vlog(vports[i], KERN_ERR,
2894 LOG_ELS,
2895 "0259 No NPIV "
2896 "Fabric support\n");
2897 }
2898 }
2899 }
2900 lpfc_destroy_vport_work_array(phba, vports);
2901}
2902
2903void
2904lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2905{
2906 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2907 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002908 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002909
James Smartf5eca9b2012-03-01 22:37:54 -05002910 /*
2911 * VFI not supported for interface type 0, so ignore any mailbox
2912 * error (except VFI in use) and continue with the discovery.
2913 */
2914 if (mboxq->u.mb.mbxStatus &&
2915 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2916 LPFC_SLI_INTF_IF_TYPE_0) &&
2917 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002918 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2919 "2018 REG_VFI mbxStatus error x%x "
2920 "HBA state x%x\n",
2921 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002922 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002923 /* FLOGI failed, use loop map to make discovery list */
2924 lpfc_disc_list_loopmap(vport);
2925 /* Start discovery */
2926 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002927 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002928 }
2929 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002930 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002931 }
James Smartae05ebe2013-03-01 16:35:38 -05002932
James Smarte74c03c2013-04-17 20:15:19 -04002933 /* If the VFI is already registered, there is nothing else to do
2934 * Unless this was a VFI update and we are in PT2PT mode, then
2935 * we should drop through to set the port state to ready.
2936 */
James Smartae05ebe2013-03-01 16:35:38 -05002937 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002938 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2939 vport->fc_flag & FC_PT2PT))
2940 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002941
James Smartc8685952009-11-18 15:39:16 -05002942 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002943 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002944 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002945 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002946 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002947 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002948 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002949
James Smart1b511972011-12-13 13:23:09 -05002950 /* In case SLI4 FC loopback test, we are ready */
2951 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2952 (phba->link_flag & LS_LOOPBACK_MODE)) {
2953 phba->link_state = LPFC_HBA_READY;
2954 goto out_free_mem;
2955 }
2956
James Smarte74c03c2013-04-17 20:15:19 -04002957 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2958 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2959 "alpacnt:%d LinkState:%x topology:%x\n",
2960 vport->port_state, vport->fc_flag, vport->fc_myDID,
2961 vport->phba->alpa_map[0],
2962 phba->link_state, phba->fc_topology);
2963
James Smart6fb120a2009-05-22 14:52:59 -04002964 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002965 /*
2966 * For private loop or for NPort pt2pt,
2967 * just start discovery and we are done.
2968 */
2969 if ((vport->fc_flag & FC_PT2PT) ||
2970 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2971 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2972
James Smart76a95d72010-11-20 23:11:48 -05002973 /* Use loop map to make discovery list */
2974 lpfc_disc_list_loopmap(vport);
2975 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002976 if (vport->fc_flag & FC_PT2PT)
2977 vport->port_state = LPFC_VPORT_READY;
2978 else
2979 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002980 } else {
2981 lpfc_start_fdiscs(phba);
2982 lpfc_do_scr_ns_plogi(phba, vport);
2983 }
James Smart6fb120a2009-05-22 14:52:59 -04002984 }
2985
James Smart1b511972011-12-13 13:23:09 -05002986out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002987 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002988 if (dmabuf) {
2989 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2990 kfree(dmabuf);
2991 }
James Smart6fb120a2009-05-22 14:52:59 -04002992 return;
2993}
2994
2995static void
James Smart2e0fef82007-06-17 19:56:36 -05002996lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002997{
James Smart6fb120a2009-05-22 14:52:59 -04002998 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002999 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003000 struct lpfc_vport *vport = pmb->vport;
James Smart21bf0b92015-08-31 16:48:21 -04003001 struct serv_parm *sp = &vport->fc_sparam;
3002 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003003
3004 /* Check for error */
3005 if (mb->mbxStatus) {
3006 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003007 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3008 "0319 READ_SPARAM mbxStatus error x%x "
3009 "hba state x%x>\n",
3010 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003011 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003012 goto out;
3013 }
3014
James Smart2e0fef82007-06-17 19:56:36 -05003015 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003016 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003017
3018 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3019 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3020 ed_tov = (ed_tov + 999999) / 1000000;
3021
3022 phba->fc_edtov = ed_tov;
3023 phba->fc_ratov = (2 * ed_tov) / 1000;
3024 if (phba->fc_ratov < FF_DEF_RATOV) {
3025 /* RA_TOV should be atleast 10sec for initial flogi */
3026 phba->fc_ratov = FF_DEF_RATOV;
3027 }
3028
James Smart05580562011-05-24 11:40:48 -04003029 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003030 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3031 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3032 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3033 }
3034
dea31012005-04-17 16:05:31 -05003035 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3036 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003037 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003038 return;
3039
3040out:
3041 pmb->context1 = NULL;
3042 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3043 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003044 lpfc_issue_clear_la(phba, vport);
3045 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003046 return;
3047}
3048
3049static void
James Smart76a95d72010-11-20 23:11:48 -05003050lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003051{
James Smart92d7f7b2007-06-17 19:56:38 -05003052 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003053 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003054 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003055 int i;
James Smart14691152006-12-02 13:34:28 -05003056 struct lpfc_dmabuf *mp;
3057 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003058 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003059 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003060
James Smart92d7f7b2007-06-17 19:56:38 -05003061 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003062 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3063
3064 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3065 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3066 case LPFC_LINK_SPEED_1GHZ:
3067 case LPFC_LINK_SPEED_2GHZ:
3068 case LPFC_LINK_SPEED_4GHZ:
3069 case LPFC_LINK_SPEED_8GHZ:
3070 case LPFC_LINK_SPEED_10GHZ:
3071 case LPFC_LINK_SPEED_16GHZ:
3072 case LPFC_LINK_SPEED_32GHZ:
3073 break;
3074 default:
3075 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3076 break;
3077 }
dea31012005-04-17 16:05:31 -05003078 }
3079
James Smarte74c03c2013-04-17 20:15:19 -04003080 if (phba->fc_topology &&
3081 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3082 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3083 "3314 Toplogy changed was 0x%x is 0x%x\n",
3084 phba->fc_topology,
3085 bf_get(lpfc_mbx_read_top_topology, la));
3086 phba->fc_topology_changed = 1;
3087 }
3088
James Smart76a95d72010-11-20 23:11:48 -05003089 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003090 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003091
James Smartdf9e1b52011-12-13 13:22:17 -05003092 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003093 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003094 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003095
James Smart6a9c52c2009-10-02 15:16:51 -04003096 /* if npiv is enabled and this adapter supports npiv log
3097 * a message that npiv is not supported in this topology
3098 */
3099 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003100 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3101 "1309 Link Up Event npiv not supported in loop "
3102 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003103 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003104 if (bf_get(lpfc_mbx_read_top_il, la))
3105 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003106
James Smart76a95d72010-11-20 23:11:48 -05003107 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3108 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003109
3110 if (i == 0) {
3111 phba->alpa_map[0] = 0;
3112 } else {
James Smarte8b62012007-08-02 11:10:09 -04003113 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003114 int numalpa, j, k;
3115 union {
3116 uint8_t pamap[16];
3117 struct {
3118 uint32_t wd1;
3119 uint32_t wd2;
3120 uint32_t wd3;
3121 uint32_t wd4;
3122 } pa;
3123 } un;
3124 numalpa = phba->alpa_map[0];
3125 j = 0;
3126 while (j < numalpa) {
3127 memset(un.pamap, 0, 16);
3128 for (k = 1; j < numalpa; k++) {
3129 un.pamap[k - 1] =
3130 phba->alpa_map[j + 1];
3131 j++;
3132 if (k == 16)
3133 break;
3134 }
3135 /* Link Up Event ALPA map */
3136 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003137 KERN_WARNING,
3138 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003139 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003140 "ALPA map Data: x%x "
3141 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003142 un.pa.wd1, un.pa.wd2,
3143 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003144 }
3145 }
3146 }
3147 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003148 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003149 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003150 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003151 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3152 }
James Smart2e0fef82007-06-17 19:56:36 -05003153 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003154 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003155 }
James Smart92d7f7b2007-06-17 19:56:38 -05003156 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003157
James Smart38c20672013-03-01 16:37:44 -05003158 if (fc_flags) {
3159 spin_lock_irq(shost->host_lock);
3160 vport->fc_flag |= fc_flags;
3161 spin_unlock_irq(shost->host_lock);
3162 }
3163
dea31012005-04-17 16:05:31 -05003164 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003165 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3166 if (!sparam_mbox)
3167 goto out;
3168
3169 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3170 if (rc) {
3171 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3172 goto out;
3173 }
3174 sparam_mbox->vport = vport;
3175 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3176 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3177 if (rc == MBX_NOT_FINISHED) {
3178 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3179 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3180 kfree(mp);
3181 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3182 goto out;
dea31012005-04-17 16:05:31 -05003183 }
3184
James Smart76a95d72010-11-20 23:11:48 -05003185 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003186 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3187 if (!cfglink_mbox)
3188 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003189 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003190 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003191 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003192 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003193 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003194 if (rc == MBX_NOT_FINISHED) {
3195 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3196 goto out;
3197 }
3198 } else {
James Smart32b97932009-07-19 10:01:21 -04003199 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003200 /*
3201 * Add the driver's default FCF record at FCF index 0 now. This
3202 * is phase 1 implementation that support FCF index 0 and driver
3203 * defaults.
3204 */
James Smart45ed1192009-10-02 15:17:02 -04003205 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003206 fcf_record = kzalloc(sizeof(struct fcf_record),
3207 GFP_KERNEL);
3208 if (unlikely(!fcf_record)) {
3209 lpfc_printf_log(phba, KERN_ERR,
3210 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003211 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003212 "fcf record\n");
3213 rc = -ENODEV;
3214 goto out;
3215 }
3216
3217 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3218 LPFC_FCOE_FCF_DEF_INDEX);
3219 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3220 if (unlikely(rc)) {
3221 lpfc_printf_log(phba, KERN_ERR,
3222 LOG_MBOX | LOG_SLI,
3223 "2013 Could not manually add FCF "
3224 "record 0, status %d\n", rc);
3225 rc = -ENODEV;
3226 kfree(fcf_record);
3227 goto out;
3228 }
3229 kfree(fcf_record);
3230 }
3231 /*
3232 * The driver is expected to do FIP/FCF. Call the port
3233 * and get the FCF Table.
3234 */
James Smart32b97932009-07-19 10:01:21 -04003235 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003236 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003237 spin_unlock_irq(&phba->hbalock);
3238 return;
3239 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003240 /* This is the initial FCF discovery scan */
3241 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003242 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003243 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3244 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003245 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3246 LPFC_FCOE_FCF_GET_FIRST);
3247 if (rc) {
3248 spin_lock_irq(&phba->hbalock);
3249 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3250 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003251 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003252 }
James Smart38b92ef2010-08-04 16:11:39 -04003253 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003254 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003255 }
James Smart6fb120a2009-05-22 14:52:59 -04003256
3257 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003258out:
3259 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003260 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3261 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3262 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003263 lpfc_issue_clear_la(phba, vport);
3264 return;
dea31012005-04-17 16:05:31 -05003265}
3266
3267static void
James Smart84774a42008-08-24 21:50:06 -04003268lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003269{
dea31012005-04-17 16:05:31 -05003270 uint32_t control;
3271 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003272 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003273 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003274 if (phba->sli_rev <= LPFC_SLI_REV3) {
3275 control = readl(phba->HCregaddr);
3276 control |= HC_LAINT_ENA;
3277 writel(control, phba->HCregaddr);
3278 readl(phba->HCregaddr); /* flush */
3279 }
James Smart2e0fef82007-06-17 19:56:36 -05003280 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003281}
3282
James Smart84774a42008-08-24 21:50:06 -04003283static void
3284lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3285{
3286 lpfc_linkdown(phba);
3287 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003288 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003289 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3290}
3291
3292
dea31012005-04-17 16:05:31 -05003293/*
James Smart76a95d72010-11-20 23:11:48 -05003294 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003295 * command upon completion. It is setup in the LPFC_MBOXQ
3296 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003297 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003298 */
3299void
James Smart76a95d72010-11-20 23:11:48 -05003300lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003301{
James Smart2e0fef82007-06-17 19:56:36 -05003302 struct lpfc_vport *vport = pmb->vport;
3303 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003304 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003305 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003306 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003307 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3308
James Smart0d2b6b82008-06-14 22:52:47 -04003309 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003310 pring = lpfc_phba_elsring(phba);
3311 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3312
dea31012005-04-17 16:05:31 -05003313 /* Check for error */
3314 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003315 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003316 "1307 READ_LA mbox error x%x state x%x\n",
3317 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003318 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003319 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003320 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003321 }
3322
James Smart76a95d72010-11-20 23:11:48 -05003323 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003324
3325 memcpy(&phba->alpa_map[0], mp->virt, 128);
3326
James Smart2e0fef82007-06-17 19:56:36 -05003327 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003328 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003329 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003330 else
James Smart2e0fef82007-06-17 19:56:36 -05003331 vport->fc_flag &= ~FC_BYPASSED_MODE;
3332 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003333
James Smartbe0c0082013-03-01 16:38:14 -05003334 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003335 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003336 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003337 if (phba->fc_eventTag != 0)
3338 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003339 }
dea31012005-04-17 16:05:31 -05003340
3341 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003342 if (phba->sli_rev < LPFC_SLI_REV4) {
3343 spin_lock_irq(&phba->hbalock);
3344 if (bf_get(lpfc_mbx_read_top_mm, la))
3345 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3346 else
3347 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3348 spin_unlock_irq(&phba->hbalock);
3349 }
dea31012005-04-17 16:05:31 -05003350
James Smart4d9ab992009-10-02 15:16:39 -04003351 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003352 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003353 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003354 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003355 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003356 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003357 "1306 Link Up Event in loop back mode "
3358 "x%x received Data: x%x x%x x%x x%x\n",
3359 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003360 bf_get(lpfc_mbx_read_top_alpa_granted,
3361 la),
3362 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003363 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003364 } else {
3365 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003366 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003367 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003368 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003369 bf_get(lpfc_mbx_read_top_alpa_granted,
3370 la),
3371 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003372 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003373 bf_get(lpfc_mbx_read_top_mm, la),
3374 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003375 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003376 }
James Smart92d7f7b2007-06-17 19:56:38 -05003377 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003378 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3379 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003380 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003381 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003382 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3383 "1308 Link Down Event in loop back mode "
3384 "x%x received "
3385 "Data: x%x x%x x%x\n",
3386 la->eventTag, phba->fc_eventTag,
3387 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003388 else
James Smart3163f722008-02-08 18:50:25 -05003389 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003390 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003391 "Data: x%x x%x x%x x%x x%x\n",
3392 la->eventTag, phba->fc_eventTag,
3393 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003394 bf_get(lpfc_mbx_read_top_mm, la),
3395 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003396 lpfc_mbx_issue_link_down(phba);
3397 }
James Smartbe0c0082013-03-01 16:38:14 -05003398 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3399 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003400 if (phba->link_state != LPFC_LINK_DOWN) {
3401 phba->fc_stat.LinkDown++;
3402 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3403 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003404 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003405 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003406 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003407 lpfc_mbx_issue_link_down(phba);
3408 } else
3409 lpfc_enable_la(phba);
3410
3411 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3412 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3413 "Data: x%x x%x x%x\n",
3414 la->eventTag, phba->fc_eventTag,
3415 phba->pport->port_state, vport->fc_flag);
3416 /*
3417 * The cmnd that triggered this will be waiting for this
3418 * signal.
3419 */
3420 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3421 if (phba->wait_4_mlo_maint_flg) {
3422 phba->wait_4_mlo_maint_flg = 0;
3423 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003424 }
James Smart84774a42008-08-24 21:50:06 -04003425 }
3426
James Smartbe0c0082013-03-01 16:38:14 -05003427 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3428 bf_get(lpfc_mbx_read_top_fa, la)) {
3429 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003430 lpfc_issue_clear_la(phba, vport);
3431 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003432 "1311 fa %d\n",
3433 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003434 }
3435
James Smart76a95d72010-11-20 23:11:48 -05003436lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003437 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3438 kfree(mp);
3439 mempool_free(pmb, phba->mbox_mem_pool);
3440 return;
3441}
3442
3443/*
3444 * This routine handles processing a REG_LOGIN mailbox
3445 * command upon completion. It is setup in the LPFC_MBOXQ
3446 * as the completion routine when the command is
3447 * handed off to the SLI layer.
3448 */
3449void
James Smart2e0fef82007-06-17 19:56:36 -05003450lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003451{
James Smart2e0fef82007-06-17 19:56:36 -05003452 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003453 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003454 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003455 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003456
dea31012005-04-17 16:05:31 -05003457 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003458 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003459
James Smartbe6bb942015-04-07 15:07:22 -04003460 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3461 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3462 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003463 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003464 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003465 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3466 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3467
James Smart40426292010-12-15 17:58:10 -05003468 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3469 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003470 /* We rcvd a rscn after issuing this
3471 * mbox reg login, we may have cycled
3472 * back through the state and be
3473 * back at reg login state so this
3474 * mbox needs to be ignored becase
3475 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003476 * process.
James Smartffc95492010-06-07 15:23:17 -04003477 */
3478 spin_lock_irq(shost->host_lock);
3479 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3480 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003481
3482 /*
3483 * We cannot leave the RPI registered because
3484 * if we go thru discovery again for this ndlp
3485 * a subsequent REG_RPI will fail.
3486 */
3487 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3488 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003489 }
3490
3491 /* Call state machine */
3492 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003493
dea31012005-04-17 16:05:31 -05003494 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3495 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003496 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003497 /* decrement the node reference count held for this callback
3498 * function.
3499 */
James Smart329f9bc2007-04-25 09:53:01 -04003500 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003501
3502 return;
3503}
3504
James Smart92d7f7b2007-06-17 19:56:38 -05003505static void
3506lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3507{
James Smart04c68492009-05-22 14:52:52 -04003508 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003509 struct lpfc_vport *vport = pmb->vport;
3510 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3511
3512 switch (mb->mbxStatus) {
3513 case 0x0011:
3514 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003515 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3516 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3517 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003518 break;
James Smart78730cf2010-04-06 15:06:30 -04003519 /* If VPI is busy, reset the HBA */
3520 case 0x9700:
3521 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3522 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3523 vport->vpi, mb->mbxStatus);
3524 if (!(phba->pport->load_flag & FC_UNLOADING))
3525 lpfc_workq_post_event(phba, NULL, NULL,
3526 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003527 }
James Smart72100cc2010-02-12 14:43:01 -05003528 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003529 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003530 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003531 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003532 vport->unreg_vpi_cmpl = VPORT_OK;
3533 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003534 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003535 /*
3536 * This shost reference might have been taken at the beginning of
3537 * lpfc_vport_delete()
3538 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003539 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003540 scsi_host_put(shost);
3541}
3542
James Smartd7c255b2008-08-24 21:50:00 -04003543int
James Smart92d7f7b2007-06-17 19:56:38 -05003544lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3545{
3546 struct lpfc_hba *phba = vport->phba;
3547 LPFC_MBOXQ_t *mbox;
3548 int rc;
3549
3550 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3551 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003552 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003553
3554 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3555 mbox->vport = vport;
3556 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003557 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003558 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003559 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3560 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003561 mempool_free(mbox, phba->mbox_mem_pool);
3562 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003563 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003564 }
James Smartd7c255b2008-08-24 21:50:00 -04003565 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003566}
3567
3568static void
3569lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3570{
3571 struct lpfc_vport *vport = pmb->vport;
3572 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003573 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003574
3575 switch (mb->mbxStatus) {
3576 case 0x0011:
3577 case 0x9601:
3578 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003579 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3580 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3581 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003582 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3583 spin_lock_irq(shost->host_lock);
3584 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3585 spin_unlock_irq(shost->host_lock);
3586 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003587
James Smart01649562017-02-12 13:52:32 -08003588 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -08003589 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3590 if (!phba->nvmet_support)
3591 lpfc_nvme_update_localport(vport);
3592 /* todo: update targetport attributes */
3593 }
James Smart92d7f7b2007-06-17 19:56:38 -05003594 goto out;
3595 }
James Smart92d7f7b2007-06-17 19:56:38 -05003596
James Smart72100cc2010-02-12 14:43:01 -05003597 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003598 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003599 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003600 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003601 vport->num_disc_nodes = 0;
3602 /* go thru NPR list and issue ELS PLOGIs */
3603 if (vport->fc_npr_cnt)
3604 lpfc_els_disc_plogi(vport);
3605
3606 if (!vport->num_disc_nodes) {
3607 spin_lock_irq(shost->host_lock);
3608 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3609 spin_unlock_irq(shost->host_lock);
3610 lpfc_can_disctmo(vport);
3611 }
3612 vport->port_state = LPFC_VPORT_READY;
3613
3614out:
3615 mempool_free(pmb, phba->mbox_mem_pool);
3616 return;
3617}
3618
James Smart21e9a0a2009-05-22 14:53:21 -04003619/**
3620 * lpfc_create_static_vport - Read HBA config region to create static vports.
3621 * @phba: pointer to lpfc hba data structure.
3622 *
3623 * This routine issue a DUMP mailbox command for config region 22 to get
3624 * the list of static vports to be created. The function create vports
3625 * based on the information returned from the HBA.
3626 **/
3627void
3628lpfc_create_static_vport(struct lpfc_hba *phba)
3629{
3630 LPFC_MBOXQ_t *pmb = NULL;
3631 MAILBOX_t *mb;
3632 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003633 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003634 struct fc_vport_identifiers vport_id;
3635 struct fc_vport *new_fc_vport;
3636 struct Scsi_Host *shost;
3637 struct lpfc_vport *vport;
3638 uint16_t offset = 0;
3639 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003640 struct lpfc_dmabuf *mp;
3641 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003642
3643 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3644 if (!pmb) {
3645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3646 "0542 lpfc_create_static_vport failed to"
3647 " allocate mailbox memory\n");
3648 return;
3649 }
James Smartcdcc2342012-08-14 14:26:35 -04003650 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003651 mb = &pmb->u.mb;
3652
3653 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3654 if (!vport_info) {
3655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3656 "0543 lpfc_create_static_vport failed to"
3657 " allocate vport_info\n");
3658 mempool_free(pmb, phba->mbox_mem_pool);
3659 return;
3660 }
3661
3662 vport_buff = (uint8_t *) vport_info;
3663 do {
James Smartcdcc2342012-08-14 14:26:35 -04003664 /* free dma buffer from previous round */
3665 if (pmb->context1) {
3666 mp = (struct lpfc_dmabuf *)pmb->context1;
3667 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3668 kfree(mp);
3669 }
James Smart1c6834a2009-07-19 10:01:26 -04003670 if (lpfc_dump_static_vport(phba, pmb, offset))
3671 goto out;
3672
James Smart21e9a0a2009-05-22 14:53:21 -04003673 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003674 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3675 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003676
James Smartcdcc2342012-08-14 14:26:35 -04003677 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003678 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3679 "0544 lpfc_create_static_vport failed to"
3680 " issue dump mailbox command ret 0x%x "
3681 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003682 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003683 goto out;
3684 }
3685
James Smart1c6834a2009-07-19 10:01:26 -04003686 if (phba->sli_rev == LPFC_SLI_REV4) {
3687 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003688 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003689 if (byte_count > sizeof(struct static_vport_info) -
3690 offset)
3691 byte_count = sizeof(struct static_vport_info)
3692 - offset;
3693 memcpy(vport_buff + offset, mp->virt, byte_count);
3694 offset += byte_count;
3695 } else {
3696 if (mb->un.varDmp.word_cnt >
3697 sizeof(struct static_vport_info) - offset)
3698 mb->un.varDmp.word_cnt =
3699 sizeof(struct static_vport_info)
3700 - offset;
3701 byte_count = mb->un.varDmp.word_cnt;
3702 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3703 vport_buff + offset,
3704 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003705
James Smart1c6834a2009-07-19 10:01:26 -04003706 offset += byte_count;
3707 }
James Smart21e9a0a2009-05-22 14:53:21 -04003708
James Smart1c6834a2009-07-19 10:01:26 -04003709 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003710 offset < sizeof(struct static_vport_info));
3711
3712
3713 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3714 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3715 != VPORT_INFO_REV)) {
3716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3717 "0545 lpfc_create_static_vport bad"
3718 " information header 0x%x 0x%x\n",
3719 le32_to_cpu(vport_info->signature),
3720 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3721
3722 goto out;
3723 }
3724
3725 shost = lpfc_shost_from_vport(phba->pport);
3726
3727 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3728 memset(&vport_id, 0, sizeof(vport_id));
3729 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3730 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3731 if (!vport_id.port_name || !vport_id.node_name)
3732 continue;
3733
3734 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3735 vport_id.vport_type = FC_PORTTYPE_NPIV;
3736 vport_id.disable = false;
3737 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3738
3739 if (!new_fc_vport) {
3740 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3741 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003742 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003743 continue;
3744 }
3745
3746 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3747 vport->vport_flag |= STATIC_VPORT;
3748 }
3749
3750out:
James Smart21e9a0a2009-05-22 14:53:21 -04003751 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003752 if (mbx_wait_rc != MBX_TIMEOUT) {
3753 if (pmb->context1) {
3754 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003755 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3756 kfree(mp);
3757 }
James Smart21e9a0a2009-05-22 14:53:21 -04003758 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003759 }
James Smart21e9a0a2009-05-22 14:53:21 -04003760
3761 return;
3762}
3763
dea31012005-04-17 16:05:31 -05003764/*
3765 * This routine handles processing a Fabric REG_LOGIN mailbox
3766 * command upon completion. It is setup in the LPFC_MBOXQ
3767 * as the completion routine when the command is
3768 * handed off to the SLI layer.
3769 */
3770void
James Smart2e0fef82007-06-17 19:56:36 -05003771lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003772{
James Smart92d7f7b2007-06-17 19:56:38 -05003773 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003774 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003775 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003776 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003777 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003778
James Smart549e55c2007-08-02 11:09:51 -04003779 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003780 pmb->context1 = NULL;
3781 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003782
dea31012005-04-17 16:05:31 -05003783 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003784 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3785 "0258 Register Fabric login error: 0x%x\n",
3786 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003787 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3788 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003789 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003790
James Smart76a95d72010-11-20 23:11:48 -05003791 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003792 /* FLOGI failed, use loop map to make discovery list */
3793 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003794
James Smart92d7f7b2007-06-17 19:56:38 -05003795 /* Start discovery */
3796 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003797 /* Decrement the reference count to ndlp after the
3798 * reference to the ndlp are done.
3799 */
3800 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003801 return;
3802 }
3803
3804 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003805 /* Decrement the reference count to ndlp after the reference
3806 * to the ndlp are done.
3807 */
3808 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003809 return;
3810 }
3811
James Smart6d368e52011-05-24 11:44:12 -04003812 if (phba->sli_rev < LPFC_SLI_REV4)
3813 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003814 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003815 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003816 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003817
James Smart2e0fef82007-06-17 19:56:36 -05003818 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003819 /* when physical port receive logo donot start
3820 * vport discovery */
3821 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3822 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003823 else {
3824 shost = lpfc_shost_from_vport(vport);
3825 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003826 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003827 spin_unlock_irq(shost->host_lock);
3828 }
James Smart92d7f7b2007-06-17 19:56:38 -05003829 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003830 }
3831
3832 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3833 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003834 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003835
3836 /* Drop the reference count from the mbox at the end after
3837 * all the current reference to the ndlp have been done.
3838 */
3839 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003840 return;
3841}
3842
James Smarta0f2d3e2017-02-12 13:52:31 -08003843 /*
3844 * This routine will issue a GID_FT for each FC4 Type supported
3845 * by the driver. ALL GID_FTs must complete before discovery is started.
3846 */
3847int
3848lpfc_issue_gidft(struct lpfc_vport *vport)
3849{
3850 struct lpfc_hba *phba = vport->phba;
3851
3852 /* Good status, issue CT Request to NameServer */
3853 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3854 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3855 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3856 /* Cannot issue NameServer FCP Query, so finish up
3857 * discovery
3858 */
3859 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3860 "0604 %s FC TYPE %x %s\n",
3861 "Failed to issue GID_FT to ",
3862 FC_TYPE_FCP,
3863 "Finishing discovery.");
3864 return 0;
3865 }
3866 vport->gidft_inp++;
3867 }
3868
3869 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3870 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3871 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3872 /* Cannot issue NameServer NVME Query, so finish up
3873 * discovery
3874 */
3875 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3876 "0605 %s FC_TYPE %x %s %d\n",
3877 "Failed to issue GID_FT to ",
3878 FC_TYPE_NVME,
3879 "Finishing discovery: gidftinp ",
3880 vport->gidft_inp);
3881 if (vport->gidft_inp == 0)
3882 return 0;
3883 } else
3884 vport->gidft_inp++;
3885 }
3886 return vport->gidft_inp;
3887}
3888
dea31012005-04-17 16:05:31 -05003889/*
3890 * This routine handles processing a NameServer REG_LOGIN mailbox
3891 * command upon completion. It is setup in the LPFC_MBOXQ
3892 * as the completion routine when the command is
3893 * handed off to the SLI layer.
3894 */
3895void
James Smart2e0fef82007-06-17 19:56:36 -05003896lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003897{
James Smart21e9a0a2009-05-22 14:53:21 -04003898 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003899 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3900 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3901 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003902
James Smartd439d282010-09-29 11:18:45 -04003903 pmb->context1 = NULL;
3904 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003905 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003906
dea31012005-04-17 16:05:31 -05003907 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003908 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3909 "0260 Register NameServer error: 0x%x\n",
3910 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003911
3912out:
James Smartfa4066b2008-01-11 01:53:27 -05003913 /* decrement the node reference count held for this
3914 * callback function.
3915 */
James Smart329f9bc2007-04-25 09:53:01 -04003916 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003917 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3918 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003919 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003920
3921 /* If no other thread is using the ndlp, free it */
3922 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003923
James Smart76a95d72010-11-20 23:11:48 -05003924 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003925 /*
3926 * RegLogin failed, use loop map to make discovery
3927 * list
3928 */
3929 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003930
James Smart92d7f7b2007-06-17 19:56:38 -05003931 /* Start discovery */
3932 lpfc_disc_start(vport);
3933 return;
3934 }
3935 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003936 return;
3937 }
3938
James Smart6d368e52011-05-24 11:44:12 -04003939 if (phba->sli_rev < LPFC_SLI_REV4)
3940 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003941 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003942 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003943 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003944 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3945 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3946 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003947 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003948 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003949
James Smart2e0fef82007-06-17 19:56:36 -05003950 if (vport->port_state < LPFC_VPORT_READY) {
3951 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003952 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3953 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3954 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3955 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003956
3957 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3958 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3959 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3960
3961 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3962 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08003963 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3964 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003965
3966 /* Issue SCR just before NameServer GID_FT Query */
3967 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003968 }
3969
James Smart2e0fef82007-06-17 19:56:36 -05003970 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003971 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003972 goto out;
dea31012005-04-17 16:05:31 -05003973
James Smarta0f2d3e2017-02-12 13:52:31 -08003974 /*
3975 * At this point in time we may need to wait for multiple
3976 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
3977 *
3978 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05003979 * callback function.
3980 */
James Smart329f9bc2007-04-25 09:53:01 -04003981 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003982 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3983 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003984 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003985
3986 return;
3987}
3988
3989static void
James Smart2e0fef82007-06-17 19:56:36 -05003990lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003991{
James Smart2e0fef82007-06-17 19:56:36 -05003992 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3993 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003994 struct lpfc_rport_data *rdata;
3995 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003996 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003997
James Smart895427b2017-02-12 13:52:30 -08003998 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
3999 return;
4000
dea31012005-04-17 16:05:31 -05004001 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004002 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4003 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004004 rport_ids.port_id = ndlp->nlp_DID;
4005 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004006
James Smart329f9bc2007-04-25 09:53:01 -04004007 /*
4008 * We leave our node pointer in rport->dd_data when we unregister a
4009 * FCP target port. But fc_remote_port_add zeros the space to which
4010 * rport->dd_data points. So, if we're reusing a previously
4011 * registered port, drop the reference that we took the last time we
4012 * registered the port.
4013 */
James Smart466e8402015-05-21 13:55:28 -04004014 rport = ndlp->rport;
4015 if (rport) {
4016 rdata = rport->dd_data;
4017 /* break the link before dropping the ref */
4018 ndlp->rport = NULL;
4019 if (rdata && rdata->pnode == ndlp)
4020 lpfc_nlp_put(ndlp);
4021 rdata->pnode = NULL;
4022 /* drop reference for earlier registeration */
4023 put_device(&rport->dev);
4024 }
James Smart858c9f62007-06-17 19:56:39 -05004025
4026 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4027 "rport add: did:x%x flg:x%x type x%x",
4028 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4029
James Smart9589b0622011-04-16 11:03:17 -04004030 /* Don't add the remote port if unloading. */
4031 if (vport->load_flag & FC_UNLOADING)
4032 return;
4033
James Smart2e0fef82007-06-17 19:56:36 -05004034 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004035 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004036 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4037 "Warning: fc_remote_port_add failed\n");
4038 return;
4039 }
4040
4041 /* initialize static port data */
4042 rport->maxframe_size = ndlp->nlp_maxframe;
4043 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004044 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004045 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004046
4047 if (ndlp->nlp_type & NLP_FCP_TARGET)
4048 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4049 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4050 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4051
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004052 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4053 fc_remote_port_rolechg(rport, rport_ids.roles);
4054
James Smart34f5ad82012-08-03 12:35:03 -04004055 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4056 "3183 rport register x%06x, rport %p role x%x\n",
4057 ndlp->nlp_DID, rport, rport_ids.roles);
4058
James Smart071fbd3d2006-04-15 11:53:20 -04004059 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004060 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004061 ndlp->nlp_sid = rport->scsi_target_id;
4062 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004063 return;
4064}
4065
4066static void
James Smart2e0fef82007-06-17 19:56:36 -05004067lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004068{
4069 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004070 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004071 struct lpfc_hba *phba = vport->phba;
4072
4073 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4074 return;
James Smartc01f3202006-08-18 17:47:08 -04004075
James Smart2ea259e2017-02-12 13:52:27 -08004076 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004077 "rport delete: did:x%x flg:x%x type x%x",
4078 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4079
James Smart2ea259e2017-02-12 13:52:27 -08004080 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004081 "3184 rport unregister x%06x, rport %p\n",
4082 ndlp->nlp_DID, rport);
4083
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004084 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004085
4086 return;
4087}
4088
James Smartde0c5b32007-04-25 09:52:27 -04004089static void
James Smart2e0fef82007-06-17 19:56:36 -05004090lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004091{
James Smart2e0fef82007-06-17 19:56:36 -05004092 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4093
4094 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004095 switch (state) {
4096 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004097 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004098 break;
4099 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004100 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004101 break;
4102 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004103 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004104 break;
4105 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004106 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004107 break;
4108 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004109 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004110 break;
4111 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004112 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004113 break;
4114 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004115 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004116 break;
4117 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004118 if (vport->fc_npr_cnt == 0 && count == -1)
4119 vport->fc_npr_cnt = 0;
4120 else
4121 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004122 break;
4123 }
James Smart2e0fef82007-06-17 19:56:36 -05004124 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004125}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004126
James Smartde0c5b32007-04-25 09:52:27 -04004127static void
James Smart2e0fef82007-06-17 19:56:36 -05004128lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004129 int old_state, int new_state)
4130{
James Smart2e0fef82007-06-17 19:56:36 -05004131 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smarta0f2d3e2017-02-12 13:52:31 -08004132 struct lpfc_hba *phba = vport->phba;
James Smart2e0fef82007-06-17 19:56:36 -05004133
James Smartde0c5b32007-04-25 09:52:27 -04004134 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004135 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4136 ndlp->nlp_type |= NLP_FC_NODE;
4137 }
4138 if (new_state == NLP_STE_MAPPED_NODE)
4139 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4140 if (new_state == NLP_STE_NPR_NODE)
4141 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004142
James Smarta0f2d3e2017-02-12 13:52:31 -08004143 /* FCP and NVME Transport interface */
4144 if ((old_state == NLP_STE_MAPPED_NODE ||
4145 old_state == NLP_STE_UNMAPPED_NODE)) {
4146 if (ndlp->rport) {
4147 vport->phba->nport_event_cnt++;
4148 lpfc_unregister_remote_port(ndlp);
4149 }
4150
4151 /* Notify the NVME transport of this rport's loss */
4152 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4153 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
4154 (vport->phba->nvmet_support == 0) &&
4155 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) ||
4156 (ndlp->nlp_DID == Fabric_DID))) {
4157 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004158 lpfc_nvme_unregister_port(vport, ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004159 }
James Smartde0c5b32007-04-25 09:52:27 -04004160 }
dea31012005-04-17 16:05:31 -05004161
James Smarta0f2d3e2017-02-12 13:52:31 -08004162 /* FCP and NVME Transport interfaces */
4163
James Smartde0c5b32007-04-25 09:52:27 -04004164 if (new_state == NLP_STE_MAPPED_NODE ||
4165 new_state == NLP_STE_UNMAPPED_NODE) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004166 if ((ndlp->nlp_fc4_type & NLP_FC4_FCP) ||
4167 (ndlp->nlp_DID == Fabric_DID)) {
4168 vport->phba->nport_event_cnt++;
4169 /*
4170 * Tell the fc transport about the port, if we haven't
4171 * already. If we have, and it's a scsi entity, be
4172 */
4173 lpfc_register_remote_port(vport, ndlp);
4174 }
4175 /* Notify the NVME transport of this new rport. */
4176 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4177 if (vport->phba->nvmet_support == 0) {
4178 /* Register this rport with the transport.
4179 * Initiators take the NDLP ref count in
4180 * the register.
4181 */
4182 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004183 lpfc_nvme_register_port(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08004184 } else {
4185 /* Just take an NDLP ref count since the
4186 * target does not register rports.
4187 */
4188 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004189 }
4190 }
James Smartde0c5b32007-04-25 09:52:27 -04004191 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004192
James Smartea2151b2008-09-07 11:52:10 -04004193 if ((new_state == NLP_STE_MAPPED_NODE) &&
4194 (vport->stat_data_enabled)) {
4195 /*
4196 * A new target is discovered, if there is no buffer for
4197 * statistical data collection allocate buffer.
4198 */
4199 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4200 sizeof(struct lpfc_scsicmd_bkt),
4201 GFP_KERNEL);
4202
4203 if (!ndlp->lat_data)
4204 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4205 "0286 lpfc_nlp_state_cleanup failed to "
4206 "allocate statistical data buffer DID "
4207 "0x%x\n", ndlp->nlp_DID);
4208 }
James Smart858c9f62007-06-17 19:56:39 -05004209 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004210 * If the node just added to Mapped list was an FCP target,
4211 * but the remote port registration failed or assigned a target
4212 * id outside the presentable range - move the node to the
4213 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004214 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004215 if ((new_state == NLP_STE_MAPPED_NODE) &&
4216 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004217 (!ndlp->rport ||
4218 ndlp->rport->scsi_target_id == -1 ||
4219 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004220 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004221 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004222 spin_unlock_irq(shost->host_lock);
4223 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004224 }
James Smartde0c5b32007-04-25 09:52:27 -04004225}
4226
James Smart685f0bf2007-04-25 09:53:08 -04004227static char *
4228lpfc_nlp_state_name(char *buffer, size_t size, int state)
4229{
4230 static char *states[] = {
4231 [NLP_STE_UNUSED_NODE] = "UNUSED",
4232 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4233 [NLP_STE_ADISC_ISSUE] = "ADISC",
4234 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4235 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004236 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004237 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4238 [NLP_STE_MAPPED_NODE] = "MAPPED",
4239 [NLP_STE_NPR_NODE] = "NPR",
4240 };
4241
James Smart311464e2007-08-02 11:10:37 -04004242 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004243 strlcpy(buffer, states[state], size);
4244 else
4245 snprintf(buffer, size, "unknown (%d)", state);
4246 return buffer;
4247}
4248
James Smartde0c5b32007-04-25 09:52:27 -04004249void
James Smart2e0fef82007-06-17 19:56:36 -05004250lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4251 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004252{
James Smart2e0fef82007-06-17 19:56:36 -05004253 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004254 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004255 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004256
James Smarte8b62012007-08-02 11:10:09 -04004257 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4258 "0904 NPort state transition x%06x, %s -> %s\n",
4259 ndlp->nlp_DID,
4260 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4261 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004262
4263 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4264 "node statechg did:x%x old:%d ste:%d",
4265 ndlp->nlp_DID, old_state, state);
4266
James Smartde0c5b32007-04-25 09:52:27 -04004267 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004268 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004269 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004270 if (old_state == NLP_STE_UNMAPPED_NODE) {
4271 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4272 ndlp->nlp_type &= ~NLP_FC_NODE;
4273 }
4274
James Smart685f0bf2007-04-25 09:53:08 -04004275 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004276 spin_lock_irq(shost->host_lock);
4277 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4278 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004279 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004280 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004281
4282 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004283 lpfc_nlp_counters(vport, state, 1);
4284 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004285}
4286
4287void
James Smarte47c9092008-02-08 18:49:26 -05004288lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4289{
4290 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4291
4292 if (list_empty(&ndlp->nlp_listp)) {
4293 spin_lock_irq(shost->host_lock);
4294 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4295 spin_unlock_irq(shost->host_lock);
4296 }
4297}
4298
4299void
James Smart2e0fef82007-06-17 19:56:36 -05004300lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004301{
James Smart2e0fef82007-06-17 19:56:36 -05004302 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4303
James Smart0d2b6b82008-06-14 22:52:47 -04004304 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004305 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004306 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4307 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004308 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004309 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004310 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004311 NLP_STE_UNUSED_NODE);
4312}
4313
Adrian Bunk4d9db012008-02-14 23:24:02 +02004314static void
James Smarte47c9092008-02-08 18:49:26 -05004315lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4316{
James Smart0d2b6b82008-06-14 22:52:47 -04004317 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004318 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4319 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4320 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4321 NLP_STE_UNUSED_NODE);
4322}
James Smart109f6ed2008-12-04 22:39:08 -05004323/**
James Smart3621a712009-04-06 18:47:14 -04004324 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004325 * @vport: Pointer to Virtual Port object.
4326 * @ndlp: Pointer to FC node object.
4327 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004328 *
4329 * This function is always called when node object need to be initialized.
4330 * It initializes all the fields of the node object. Although the reference
4331 * to phba from @ndlp can be obtained indirectly through it's reference to
4332 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4333 * to the life-span of the @ndlp might go beyond the existence of @vport as
4334 * the final release of ndlp is determined by its reference count. And, the
4335 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004336 **/
4337static inline void
4338lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4339 uint32_t did)
4340{
4341 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4342 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4343 init_timer(&ndlp->nlp_delayfunc);
4344 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4345 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4346 ndlp->nlp_DID = did;
4347 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004348 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004349 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004350 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004351 kref_init(&ndlp->kref);
4352 NLP_INT_NODE_ACT(ndlp);
4353 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004354 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004355}
James Smarte47c9092008-02-08 18:49:26 -05004356
4357struct lpfc_nodelist *
4358lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4359 int state)
4360{
4361 struct lpfc_hba *phba = vport->phba;
4362 uint32_t did;
4363 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004364 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004365
4366 if (!ndlp)
4367 return NULL;
4368
4369 spin_lock_irqsave(&phba->ndlp_lock, flags);
4370 /* The ndlp should not be in memory free mode */
4371 if (NLP_CHK_FREE_REQ(ndlp)) {
4372 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4373 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4374 "0277 lpfc_enable_node: ndlp:x%p "
4375 "usgmap:x%x refcnt:%d\n",
4376 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004377 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004378 return NULL;
4379 }
4380 /* The ndlp should not already be in active mode */
4381 if (NLP_CHK_NODE_ACT(ndlp)) {
4382 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4383 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4384 "0278 lpfc_enable_node: ndlp:x%p "
4385 "usgmap:x%x refcnt:%d\n",
4386 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004387 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004388 return NULL;
4389 }
4390
4391 /* Keep the original DID */
4392 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004393 if (phba->sli_rev == LPFC_SLI_REV4)
4394 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004395
4396 /* re-initialize ndlp except of ndlp linked list pointer */
4397 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4398 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004399 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004400
James Smartcff261f2013-12-17 20:29:47 -05004401 if (phba->sli_rev == LPFC_SLI_REV4)
4402 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4403
James Smarte47c9092008-02-08 18:49:26 -05004404 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004405 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004406 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004407 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4408 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4409 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4410 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004411 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004412 ndlp->nlp_usg_map, ndlp);
4413 }
James Smart725dd392013-10-10 12:23:30 -04004414
James Smarte47c9092008-02-08 18:49:26 -05004415
4416 if (state != NLP_STE_UNUSED_NODE)
4417 lpfc_nlp_set_state(vport, ndlp, state);
4418
4419 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4420 "node enable: did:x%x",
4421 ndlp->nlp_DID, 0, 0);
4422 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004423}
4424
4425void
James Smart2e0fef82007-06-17 19:56:36 -05004426lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004427{
James Smart87af33f2007-10-27 13:37:43 -04004428 /*
James Smartfa4066b2008-01-11 01:53:27 -05004429 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004430 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004431 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4432 * until ALL other outstanding threads have completed. We check
4433 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004434 */
James Smartfa4066b2008-01-11 01:53:27 -05004435 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4436 return;
James Smart51ef4c22007-08-02 11:10:31 -04004437 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004438 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004439 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004440 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004441 }
James Smart02902172015-05-21 13:55:17 -04004442
4443 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004444 return;
dea31012005-04-17 16:05:31 -05004445}
4446
4447/*
4448 * Start / ReStart rescue timer for Discovery / RSCN handling
4449 */
4450void
James Smart2e0fef82007-06-17 19:56:36 -05004451lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004452{
James Smart2e0fef82007-06-17 19:56:36 -05004453 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4454 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004455 uint32_t tmo;
4456
James Smart2e0fef82007-06-17 19:56:36 -05004457 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004458 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004459 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4460 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004461 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004462 * FC spec states we need 3 * ratov for CT requests
4463 */
4464 tmo = ((phba->fc_ratov * 3) + 3);
4465 }
dea31012005-04-17 16:05:31 -05004466
James Smart858c9f62007-06-17 19:56:39 -05004467
4468 if (!timer_pending(&vport->fc_disctmo)) {
4469 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4470 "set disc timer: tmo:x%x state:x%x flg:x%x",
4471 tmo, vport->port_state, vport->fc_flag);
4472 }
4473
James Smart256ec0d2013-04-17 20:14:58 -04004474 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004475 spin_lock_irq(shost->host_lock);
4476 vport->fc_flag |= FC_DISC_TMO;
4477 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004478
4479 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004480 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4481 "0247 Start Discovery Timer state x%x "
4482 "Data: x%x x%lx x%x x%x\n",
4483 vport->port_state, tmo,
4484 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4485 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004486
4487 return;
4488}
4489
4490/*
4491 * Cancel rescue timer for Discovery / RSCN handling
4492 */
4493int
James Smart2e0fef82007-06-17 19:56:36 -05004494lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004495{
James Smart2e0fef82007-06-17 19:56:36 -05004496 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004497 unsigned long iflags;
4498
James Smart858c9f62007-06-17 19:56:39 -05004499 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4500 "can disc timer: state:x%x rtry:x%x flg:x%x",
4501 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4502
dea31012005-04-17 16:05:31 -05004503 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004504 if (vport->fc_flag & FC_DISC_TMO) {
4505 spin_lock_irqsave(shost->host_lock, iflags);
4506 vport->fc_flag &= ~FC_DISC_TMO;
4507 spin_unlock_irqrestore(shost->host_lock, iflags);
4508 del_timer_sync(&vport->fc_disctmo);
4509 spin_lock_irqsave(&vport->work_port_lock, iflags);
4510 vport->work_port_events &= ~WORKER_DISC_TMO;
4511 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004512 }
4513
4514 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004515 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4516 "0248 Cancel Discovery Timer state x%x "
4517 "Data: x%x x%x x%x\n",
4518 vport->port_state, vport->fc_flag,
4519 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004520 return 0;
dea31012005-04-17 16:05:31 -05004521}
4522
4523/*
4524 * Check specified ring for outstanding IOCB on the SLI queue
4525 * Return true if iocb matches the specified nport
4526 */
4527int
James Smart2e0fef82007-06-17 19:56:36 -05004528lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4529 struct lpfc_sli_ring *pring,
4530 struct lpfc_iocbq *iocb,
4531 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004532{
James Smart2e0fef82007-06-17 19:56:36 -05004533 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004534 struct lpfc_vport *vport = ndlp->vport;
4535
4536 if (iocb->vport != vport)
4537 return 0;
4538
dea31012005-04-17 16:05:31 -05004539 if (pring->ringno == LPFC_ELS_RING) {
4540 switch (icmd->ulpCommand) {
4541 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004542 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004543 return 1;
dea31012005-04-17 16:05:31 -05004544 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004545 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4546 return 1;
dea31012005-04-17 16:05:31 -05004547 case CMD_XMIT_ELS_RSP64_CX:
4548 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004549 return 1;
dea31012005-04-17 16:05:31 -05004550 }
James Smart895427b2017-02-12 13:52:30 -08004551 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004552 /* Skip match check if waiting to relogin to FCP target */
4553 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004554 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004555 return 0;
dea31012005-04-17 16:05:31 -05004556 }
4557 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004558 return 1;
dea31012005-04-17 16:05:31 -05004559 }
dea31012005-04-17 16:05:31 -05004560 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004561 return 0;
dea31012005-04-17 16:05:31 -05004562}
4563
James Smart895427b2017-02-12 13:52:30 -08004564static void
4565__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4566 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4567 struct list_head *dequeue_list)
4568{
4569 struct lpfc_iocbq *iocb, *next_iocb;
4570
4571 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4572 /* Check to see if iocb matches the nport */
4573 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4574 /* match, dequeue */
4575 list_move_tail(&iocb->list, dequeue_list);
4576 }
4577}
4578
4579static void
4580lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4581 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4582{
4583 struct lpfc_sli *psli = &phba->sli;
4584 uint32_t i;
4585
4586 spin_lock_irq(&phba->hbalock);
4587 for (i = 0; i < psli->num_rings; i++)
4588 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4589 dequeue_list);
4590 spin_unlock_irq(&phba->hbalock);
4591}
4592
4593static void
4594lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4595 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4596{
4597 struct lpfc_sli_ring *pring;
4598 struct lpfc_queue *qp = NULL;
4599
4600 spin_lock_irq(&phba->hbalock);
4601 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4602 pring = qp->pring;
4603 if (!pring)
4604 continue;
4605 spin_lock_irq(&pring->ring_lock);
4606 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4607 spin_unlock_irq(&pring->ring_lock);
4608 }
4609 spin_unlock_irq(&phba->hbalock);
4610}
4611
dea31012005-04-17 16:05:31 -05004612/*
4613 * Free resources / clean up outstanding I/Os
4614 * associated with nlp_rpi in the LPFC_NODELIST entry.
4615 */
4616static int
James Smart2e0fef82007-06-17 19:56:36 -05004617lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004618{
James Smart2534ba72007-04-25 09:52:20 -04004619 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004620
James Smart92d7f7b2007-06-17 19:56:38 -05004621 lpfc_fabric_abort_nport(ndlp);
4622
dea31012005-04-17 16:05:31 -05004623 /*
4624 * Everything that matches on txcmplq will be returned
4625 * by firmware with a no rpi error.
4626 */
James Smart40426292010-12-15 17:58:10 -05004627 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004628 if (phba->sli_rev != LPFC_SLI_REV4)
4629 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4630 else
4631 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004632 }
James Smart2534ba72007-04-25 09:52:20 -04004633
James Smarta257bf92009-04-06 18:48:10 -04004634 /* Cancel all the IOCBs from the completions list */
4635 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4636 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004637
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004638 return 0;
dea31012005-04-17 16:05:31 -05004639}
4640
James Smart086a3452012-08-14 14:25:21 -04004641/**
4642 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4643 * @phba: Pointer to HBA context object.
4644 * @pmb: Pointer to mailbox object.
4645 *
4646 * This function will issue an ELS LOGO command after completing
4647 * the UNREG_RPI.
4648 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004649static void
James Smart086a3452012-08-14 14:25:21 -04004650lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4651{
4652 struct lpfc_vport *vport = pmb->vport;
4653 struct lpfc_nodelist *ndlp;
4654
4655 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4656 if (!ndlp)
4657 return;
4658 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004659 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004660}
4661
dea31012005-04-17 16:05:31 -05004662/*
4663 * Free rpi associated with LPFC_NODELIST entry.
4664 * This routine is called from lpfc_freenode(), when we are removing
4665 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4666 * LOGO that completes successfully, and we are waiting to PLOGI back
4667 * to the remote NPort. In addition, it is called after we receive
4668 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4669 * we are waiting to PLOGI back to the remote NPort.
4670 */
4671int
James Smart2e0fef82007-06-17 19:56:36 -05004672lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004673{
James Smart2e0fef82007-06-17 19:56:36 -05004674 struct lpfc_hba *phba = vport->phba;
4675 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004676 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004677 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004678
James Smartf454a9a2013-09-06 12:20:07 -04004679 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4680 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4681 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4682 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4683 "3366 RPI x%x needs to be "
4684 "unregistered nlp_flag x%x "
4685 "did x%x\n",
4686 ndlp->nlp_rpi, ndlp->nlp_flag,
4687 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004688 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4689 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004690 /* SLI4 ports require the physical rpi value. */
4691 rpi = ndlp->nlp_rpi;
4692 if (phba->sli_rev == LPFC_SLI_REV4)
4693 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004694
James Smart6d368e52011-05-24 11:44:12 -04004695 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004696 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004697 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4698 mbox->context1 = ndlp;
4699 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4700 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004701 if (phba->sli_rev == LPFC_SLI_REV4 &&
4702 (!(vport->load_flag & FC_UNLOADING)) &&
4703 (bf_get(lpfc_sli_intf_if_type,
4704 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004705 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004706 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004707 mbox->context1 = lpfc_nlp_get(ndlp);
4708 mbox->mbox_cmpl =
4709 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004710 /*
4711 * accept PLOGIs after unreg_rpi_cmpl
4712 */
4713 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004714 } else
4715 mbox->mbox_cmpl =
4716 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004717 }
4718
James Smart0b727fe2007-10-27 13:37:25 -04004719 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004720 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004721 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004722 acc_plogi = 1;
4723 }
dea31012005-04-17 16:05:31 -05004724 }
dea31012005-04-17 16:05:31 -05004725 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004726
James Smart40426292010-12-15 17:58:10 -05004727 if (phba->sli_rev != LPFC_SLI_REV4)
4728 ndlp->nlp_rpi = 0;
4729 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004730 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004731 if (acc_plogi)
4732 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004733 return 1;
4734 }
James Smart7c5e5182015-05-22 10:42:43 -04004735 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004736 return 0;
4737}
4738
James Smartecfd03c2010-02-12 14:41:27 -05004739/**
4740 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4741 * @phba: pointer to lpfc hba data structure.
4742 *
4743 * This routine is invoked to unregister all the currently registered RPIs
4744 * to the HBA.
4745 **/
4746void
4747lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4748{
4749 struct lpfc_vport **vports;
4750 struct lpfc_nodelist *ndlp;
4751 struct Scsi_Host *shost;
4752 int i;
4753
4754 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004755 if (!vports) {
4756 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4757 "2884 Vport array allocation failed \n");
4758 return;
4759 }
James Smartecfd03c2010-02-12 14:41:27 -05004760 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4761 shost = lpfc_shost_from_vport(vports[i]);
4762 spin_lock_irq(shost->host_lock);
4763 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004764 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004765 /* The mempool_alloc might sleep */
4766 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004767 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004768 spin_lock_irq(shost->host_lock);
4769 }
James Smartecfd03c2010-02-12 14:41:27 -05004770 }
4771 spin_unlock_irq(shost->host_lock);
4772 }
4773 lpfc_destroy_vport_work_array(phba, vports);
4774}
4775
James Smart92d7f7b2007-06-17 19:56:38 -05004776void
4777lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4778{
4779 struct lpfc_hba *phba = vport->phba;
4780 LPFC_MBOXQ_t *mbox;
4781 int rc;
4782
James Smart5af5eee2010-10-22 11:06:38 -04004783 if (phba->sli_rev == LPFC_SLI_REV4) {
4784 lpfc_sli4_unreg_all_rpis(vport);
4785 return;
4786 }
4787
James Smart92d7f7b2007-06-17 19:56:38 -05004788 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4789 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004790 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4791 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004792 mbox->vport = vport;
4793 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004794 mbox->context1 = NULL;
4795 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004796 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004797 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004798
4799 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4800 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4801 "1836 Could not issue "
4802 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004803 }
4804}
4805
4806void
4807lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4808{
4809 struct lpfc_hba *phba = vport->phba;
4810 LPFC_MBOXQ_t *mbox;
4811 int rc;
4812
4813 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4814 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004815 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4816 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004817 mbox->vport = vport;
4818 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004819 mbox->context1 = NULL;
4820 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004821 if (rc != MBX_TIMEOUT)
4822 mempool_free(mbox, phba->mbox_mem_pool);
4823
4824 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004825 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4826 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004827 "unreg_did (default rpis) status %d\n",
4828 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004829 }
4830}
4831
dea31012005-04-17 16:05:31 -05004832/*
4833 * Free resources associated with LPFC_NODELIST entry
4834 * so it can be freed.
4835 */
4836static int
James Smart2e0fef82007-06-17 19:56:36 -05004837lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004838{
James Smart2e0fef82007-06-17 19:56:36 -05004839 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4840 struct lpfc_hba *phba = vport->phba;
4841 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004842 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004843
4844 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004845 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4846 "0900 Cleanup node for NPort x%x "
4847 "Data: x%x x%x x%x\n",
4848 ndlp->nlp_DID, ndlp->nlp_flag,
4849 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004850 if (NLP_CHK_FREE_REQ(ndlp)) {
4851 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4852 "0280 lpfc_cleanup_node: ndlp:x%p "
4853 "usgmap:x%x refcnt:%d\n",
4854 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004855 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004856 lpfc_dequeue_node(vport, ndlp);
4857 } else {
4858 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4859 "0281 lpfc_cleanup_node: ndlp:x%p "
4860 "usgmap:x%x refcnt:%d\n",
4861 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004862 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004863 lpfc_disable_node(vport, ndlp);
4864 }
dea31012005-04-17 16:05:31 -05004865
James Smart086a3452012-08-14 14:25:21 -04004866
4867 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4868
dea31012005-04-17 16:05:31 -05004869 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4870 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004871 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004872 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004873 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4874 mb->context2 = NULL;
4875 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4876 }
4877 }
James Smart33ccf8d2006-08-17 11:57:58 -04004878
James Smart2e0fef82007-06-17 19:56:36 -05004879 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004880 /* Cleanup REG_LOGIN completions which are not yet processed */
4881 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4882 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004883 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004884 (ndlp != (struct lpfc_nodelist *) mb->context2))
4885 continue;
4886
4887 mb->context2 = NULL;
4888 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4889 }
4890
dea31012005-04-17 16:05:31 -05004891 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004892 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004893 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004894 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004895 mp = (struct lpfc_dmabuf *) (mb->context1);
4896 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004897 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004898 kfree(mp);
4899 }
4900 list_del(&mb->list);
4901 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004902 /* We shall not invoke the lpfc_nlp_put to decrement
4903 * the ndlp reference count as we are in the process
4904 * of lpfc_nlp_release.
4905 */
dea31012005-04-17 16:05:31 -05004906 }
4907 }
James Smart2e0fef82007-06-17 19:56:36 -05004908 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004909
James Smarte47c9092008-02-08 18:49:26 -05004910 lpfc_els_abort(phba, ndlp);
4911
James Smart2e0fef82007-06-17 19:56:36 -05004912 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004913 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004914 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004915
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004916 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004917 del_timer_sync(&ndlp->nlp_delayfunc);
4918
James Smart0d2b6b82008-06-14 22:52:47 -04004919 list_del_init(&ndlp->els_retry_evt.evt_listp);
4920 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004921 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004922 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004923
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004924 return 0;
dea31012005-04-17 16:05:31 -05004925}
4926
4927/*
4928 * Check to see if we can free the nlp back to the freelist.
4929 * If we are in the middle of using the nlp in the discovery state
4930 * machine, defer the free till we reach the end of the state machine.
4931 */
James Smart329f9bc2007-04-25 09:53:01 -04004932static void
James Smart2e0fef82007-06-17 19:56:36 -05004933lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004934{
James Smarta8adb832007-10-27 13:37:53 -04004935 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004936 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004937 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004938 LPFC_MBOXQ_t *mbox;
4939 int rc;
dea31012005-04-17 16:05:31 -05004940
James Smart0d2b6b82008-06-14 22:52:47 -04004941 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004942 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004943 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4944 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004945 /* For this case we need to cleanup the default rpi
4946 * allocated by the firmware.
4947 */
James Smartbe6bb942015-04-07 15:07:22 -04004948 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4949 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4950 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004951 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004952 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004953 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4954 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004955 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004956 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004957 if (rc) {
4958 mempool_free(mbox, phba->mbox_mem_pool);
4959 }
4960 else {
4961 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4962 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4963 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004964 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004965 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4966 if (rc == MBX_NOT_FINISHED) {
4967 mempool_free(mbox, phba->mbox_mem_pool);
4968 }
4969 }
4970 }
4971 }
James Smart2e0fef82007-06-17 19:56:36 -05004972 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004973
James Smart2e0fef82007-06-17 19:56:36 -05004974 /*
James Smart466e8402015-05-21 13:55:28 -04004975 * ndlp->rport must be set to NULL before it reaches here
4976 * i.e. break rport/node link before doing lpfc_nlp_put for
4977 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05004978 */
James Smart92d7f7b2007-06-17 19:56:38 -05004979 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04004980 /*
4981 * extra lpfc_nlp_put dropped the reference of ndlp
4982 * for registered rport so need to cleanup rport
4983 */
4984 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4985 "0940 removed node x%p DID x%x "
4986 " rport not null %p\n",
4987 ndlp, ndlp->nlp_DID, ndlp->rport);
4988 rport = ndlp->rport;
4989 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004990 rdata->pnode = NULL;
4991 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04004992 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05004993 }
dea31012005-04-17 16:05:31 -05004994}
4995
4996static int
James Smart2e0fef82007-06-17 19:56:36 -05004997lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4998 uint32_t did)
dea31012005-04-17 16:05:31 -05004999{
James Smart2e0fef82007-06-17 19:56:36 -05005000 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005001
5002 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005003 return 0;
dea31012005-04-17 16:05:31 -05005004
dea31012005-04-17 16:05:31 -05005005 /* First check for Direct match */
5006 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005007 return 1;
dea31012005-04-17 16:05:31 -05005008
5009 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005010 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005011 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005012 return 0;
dea31012005-04-17 16:05:31 -05005013 }
5014
5015 matchdid.un.word = did;
5016 ndlpdid.un.word = ndlp->nlp_DID;
5017 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5018 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5019 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005020 /* This code is supposed to match the ID
5021 * for a private loop device that is
5022 * connect to fl_port. But we need to
5023 * check that the port did not just go
5024 * from pt2pt to fabric or we could end
5025 * up matching ndlp->nlp_DID 000001 to
5026 * fabric DID 0x20101
5027 */
dea31012005-04-17 16:05:31 -05005028 if ((ndlpdid.un.b.domain == 0) &&
5029 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005030 if (ndlpdid.un.b.id &&
5031 vport->phba->fc_topology ==
5032 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005033 return 1;
dea31012005-04-17 16:05:31 -05005034 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005035 return 0;
dea31012005-04-17 16:05:31 -05005036 }
5037
5038 matchdid.un.word = ndlp->nlp_DID;
5039 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5040 (mydid.un.b.area == ndlpdid.un.b.area)) {
5041 if ((matchdid.un.b.domain == 0) &&
5042 (matchdid.un.b.area == 0)) {
5043 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005044 return 1;
dea31012005-04-17 16:05:31 -05005045 }
5046 }
5047 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005048 return 0;
dea31012005-04-17 16:05:31 -05005049}
5050
James Smart685f0bf2007-04-25 09:53:08 -04005051/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005052static struct lpfc_nodelist *
5053__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005054{
James Smart2fb9bd82006-12-02 13:33:57 -05005055 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005056 uint32_t data1;
5057
James Smart2e0fef82007-06-17 19:56:36 -05005058 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5059 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005060 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5061 ((uint32_t) ndlp->nlp_xri << 16) |
5062 ((uint32_t) ndlp->nlp_type << 8) |
5063 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005064 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5065 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005066 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005067 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005068 ndlp->nlp_flag, data1,
5069 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005070 return ndlp;
dea31012005-04-17 16:05:31 -05005071 }
5072 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005073
dea31012005-04-17 16:05:31 -05005074 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005075 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5076 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005077 return NULL;
5078}
5079
5080struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005081lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005082{
James Smart2e0fef82007-06-17 19:56:36 -05005083 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005084 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005085 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005086
James Smartfedd3b72011-02-16 12:39:24 -05005087 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005088 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005089 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005090 return ndlp;
5091}
5092
5093struct lpfc_nodelist *
5094lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5095{
5096 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5097 struct lpfc_nodelist *ndlp;
5098
5099 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005100 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05005101 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5102 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005103 return NULL;
5104 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05005105 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005106 if (!ndlp)
5107 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005108 lpfc_nlp_init(vport, ndlp, did);
5109 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
James Smart8c258642017-02-12 13:52:36 -08005110 if (vport->phba->nvmet_support)
5111 return ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005112 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005113 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005114 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005115 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005116 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5117 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5118 if (!ndlp)
5119 return NULL;
James Smart8c258642017-02-12 13:52:36 -08005120 if (vport->phba->nvmet_support)
5121 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005122 spin_lock_irq(shost->host_lock);
5123 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5124 spin_unlock_irq(shost->host_lock);
5125 return ndlp;
dea31012005-04-17 16:05:31 -05005126 }
James Smarte47c9092008-02-08 18:49:26 -05005127
James Smart58da1ff2008-04-07 10:15:56 -04005128 if ((vport->fc_flag & FC_RSCN_MODE) &&
5129 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005130 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005131 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005132 * we don't need to try to discover it again.
5133 */
5134 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5135 return NULL;
5136
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005137 /* Since this node is marked for discovery,
5138 * delay timeout is not needed.
5139 */
James Smart0d2b6b82008-06-14 22:52:47 -04005140 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08005141 if (vport->phba->nvmet_support)
5142 return ndlp;
James Smarta257bf92009-04-06 18:48:10 -04005143 spin_lock_irq(shost->host_lock);
5144 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5145 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005146 } else
dea31012005-04-17 16:05:31 -05005147 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005148 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005149 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04005150 * or we are already in the process of discovery on it,
5151 * we don't need to try to discover it again.
5152 */
James Smart685f0bf2007-04-25 09:53:08 -04005153 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005154 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5155 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05005156 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005157 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
James Smart8c258642017-02-12 13:52:36 -08005158 if (vport->phba->nvmet_support)
5159 return ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005160 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005161 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005162 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005163 }
5164 return ndlp;
5165}
5166
5167/* Build a list of nodes to discover based on the loopmap */
5168void
James Smart2e0fef82007-06-17 19:56:36 -05005169lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005170{
James Smart2e0fef82007-06-17 19:56:36 -05005171 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005172 int j;
5173 uint32_t alpa, index;
5174
James Smart2e0fef82007-06-17 19:56:36 -05005175 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005176 return;
James Smart2e0fef82007-06-17 19:56:36 -05005177
James Smart76a95d72010-11-20 23:11:48 -05005178 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005179 return;
dea31012005-04-17 16:05:31 -05005180
5181 /* Check for loop map present or not */
5182 if (phba->alpa_map[0]) {
5183 for (j = 1; j <= phba->alpa_map[0]; j++) {
5184 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005185 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005186 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005187 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005188 }
5189 } else {
5190 /* No alpamap, so try all alpa's */
5191 for (j = 0; j < FC_MAXLOOP; j++) {
5192 /* If cfg_scan_down is set, start from highest
5193 * ALPA (0xef) to lowest (0x1).
5194 */
James Smart3de2a652007-08-02 11:09:59 -04005195 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005196 index = j;
5197 else
5198 index = FC_MAXLOOP - j - 1;
5199 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005200 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005201 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005202 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005203 }
5204 }
5205 return;
5206}
5207
James Smart895427b2017-02-12 13:52:30 -08005208/* SLI3 only */
dea31012005-04-17 16:05:31 -05005209void
James Smart2e0fef82007-06-17 19:56:36 -05005210lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005211{
dea31012005-04-17 16:05:31 -05005212 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005213 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005214 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5215 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005216 int rc;
5217
James Smart92d7f7b2007-06-17 19:56:38 -05005218 /*
5219 * if it's not a physical port or if we already send
5220 * clear_la then don't send it.
5221 */
5222 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005223 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5224 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005225 return;
5226
James Smart2e0fef82007-06-17 19:56:36 -05005227 /* Link up discovery */
5228 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5229 phba->link_state = LPFC_CLEAR_LA;
5230 lpfc_clear_la(phba, mbox);
5231 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5232 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005233 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005234 if (rc == MBX_NOT_FINISHED) {
5235 mempool_free(mbox, phba->mbox_mem_pool);
5236 lpfc_disc_flush_list(vport);
5237 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5238 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005239 phba->link_state = LPFC_HBA_ERROR;
5240 }
5241 }
5242}
5243
5244/* Reg_vpi to tell firmware to resume normal operations */
5245void
5246lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5247{
5248 LPFC_MBOXQ_t *regvpimbox;
5249
5250 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5251 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005252 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005253 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5254 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005255 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005256 == MBX_NOT_FINISHED) {
5257 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005258 }
5259 }
5260}
5261
5262/* Start Link up / RSCN discovery on NPR nodes */
5263void
5264lpfc_disc_start(struct lpfc_vport *vport)
5265{
5266 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5267 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005268 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005269 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005270
James Smarte74c03c2013-04-17 20:15:19 -04005271 if (!lpfc_is_link_up(phba)) {
5272 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5273 "3315 Link is not up %x\n",
5274 phba->link_state);
dea31012005-04-17 16:05:31 -05005275 return;
James Smarte74c03c2013-04-17 20:15:19 -04005276 }
James Smart2e0fef82007-06-17 19:56:36 -05005277
5278 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005279 clear_la_pending = 1;
5280 else
5281 clear_la_pending = 0;
5282
James Smart2e0fef82007-06-17 19:56:36 -05005283 if (vport->port_state < LPFC_VPORT_READY)
5284 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005285
James Smart2e0fef82007-06-17 19:56:36 -05005286 lpfc_set_disctmo(vport);
5287
James Smart2e0fef82007-06-17 19:56:36 -05005288 vport->fc_prevDID = vport->fc_myDID;
5289 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005290
5291 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005292 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5293 "0202 Start Discovery hba state x%x "
5294 "Data: x%x x%x x%x\n",
5295 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5296 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005297
5298 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005299 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005300
5301 if (num_sent)
5302 return;
5303
James Smart6fa139f2013-04-17 20:20:00 -04005304 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005305 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005306 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005307 !(vport->fc_flag & FC_RSCN_MODE) &&
5308 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005309 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005310 lpfc_issue_reg_vpi(phba, vport);
5311 return;
5312 }
James Smart2e0fef82007-06-17 19:56:36 -05005313
James Smart92d7f7b2007-06-17 19:56:38 -05005314 /*
5315 * For SLI2, we need to set port_state to READY and continue
5316 * discovery.
5317 */
5318 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5319 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005320 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005321
5322 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005323 vport->num_disc_nodes = 0;
5324 /* go thru NPR nodes and issue ELS PLOGIs */
5325 if (vport->fc_npr_cnt)
5326 lpfc_els_disc_plogi(vport);
5327
5328 if (!vport->num_disc_nodes) {
5329 spin_lock_irq(shost->host_lock);
5330 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5331 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005332 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005333 }
5334 }
James Smart92d7f7b2007-06-17 19:56:38 -05005335 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005336 } else {
5337 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005338 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005339
5340 if (num_sent)
5341 return;
5342
James Smart2e0fef82007-06-17 19:56:36 -05005343 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005344 /* Check to see if more RSCNs came in while we
5345 * were processing this one.
5346 */
James Smart2e0fef82007-06-17 19:56:36 -05005347 if ((vport->fc_rscn_id_cnt == 0) &&
5348 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5349 spin_lock_irq(shost->host_lock);
5350 vport->fc_flag &= ~FC_RSCN_MODE;
5351 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005352 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005353 } else
James Smart2e0fef82007-06-17 19:56:36 -05005354 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005355 }
5356 }
5357 return;
5358}
5359
5360/*
5361 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5362 * ring the match the sppecified nodelist.
5363 */
5364static void
James Smart2e0fef82007-06-17 19:56:36 -05005365lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005366{
James Smart2534ba72007-04-25 09:52:20 -04005367 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005368 struct lpfc_sli *psli;
5369 IOCB_t *icmd;
5370 struct lpfc_iocbq *iocb, *next_iocb;
5371 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005372
5373 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005374 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005375
5376 /* Error matching iocb on txq or txcmplq
5377 * First check the txq.
5378 */
James Smart2e0fef82007-06-17 19:56:36 -05005379 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005380 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5381 if (iocb->context1 != ndlp) {
5382 continue;
5383 }
5384 icmd = &iocb->iocb;
5385 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5386 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5387
James Smart2534ba72007-04-25 09:52:20 -04005388 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005389 }
5390 }
5391
5392 /* Next check the txcmplq */
5393 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5394 if (iocb->context1 != ndlp) {
5395 continue;
5396 }
5397 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005398 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5399 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005400 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005401 }
5402 }
James Smart2e0fef82007-06-17 19:56:36 -05005403 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005404
James Smarta257bf92009-04-06 18:48:10 -04005405 /* Cancel all the IOCBs from the completions list */
5406 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5407 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005408}
5409
Adrian Bunka6ababd2007-11-05 18:07:33 +01005410static void
James Smart2e0fef82007-06-17 19:56:36 -05005411lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005412{
5413 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005414 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005415
James Smart2e0fef82007-06-17 19:56:36 -05005416 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5417 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005418 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005419 if (!NLP_CHK_NODE_ACT(ndlp))
5420 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005421 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5422 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5423 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005424 }
dea31012005-04-17 16:05:31 -05005425 }
5426 }
dea31012005-04-17 16:05:31 -05005427}
5428
James Smart92d7f7b2007-06-17 19:56:38 -05005429void
5430lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5431{
5432 lpfc_els_flush_rscn(vport);
5433 lpfc_els_flush_cmd(vport);
5434 lpfc_disc_flush_list(vport);
5435}
5436
dea31012005-04-17 16:05:31 -05005437/*****************************************************************************/
5438/*
5439 * NAME: lpfc_disc_timeout
5440 *
5441 * FUNCTION: Fibre Channel driver discovery timeout routine.
5442 *
5443 * EXECUTION ENVIRONMENT: interrupt only
5444 *
5445 * CALLED FROM:
5446 * Timer function
5447 *
5448 * RETURNS:
5449 * none
5450 */
5451/*****************************************************************************/
5452void
5453lpfc_disc_timeout(unsigned long ptr)
5454{
James Smart2e0fef82007-06-17 19:56:36 -05005455 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5456 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005457 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005458 unsigned long flags = 0;
5459
5460 if (unlikely(!phba))
5461 return;
5462
James Smart5e9d9b82008-06-14 22:52:53 -04005463 spin_lock_irqsave(&vport->work_port_lock, flags);
5464 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5465 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005466 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005467 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005468
James Smart5e9d9b82008-06-14 22:52:53 -04005469 if (!tmo_posted)
5470 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005471 return;
5472}
5473
5474static void
James Smart2e0fef82007-06-17 19:56:36 -05005475lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005476{
James Smart2e0fef82007-06-17 19:56:36 -05005477 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5478 struct lpfc_hba *phba = vport->phba;
5479 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005480 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005481 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005482 int rc, clrlaerr = 0;
5483
James Smart2e0fef82007-06-17 19:56:36 -05005484 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005485 return;
5486
James Smart2e0fef82007-06-17 19:56:36 -05005487 spin_lock_irq(shost->host_lock);
5488 vport->fc_flag &= ~FC_DISC_TMO;
5489 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005490
James Smart858c9f62007-06-17 19:56:39 -05005491 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5492 "disc timeout: state:x%x rtry:x%x flg:x%x",
5493 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5494
James Smart2e0fef82007-06-17 19:56:36 -05005495 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005496
5497 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005498 /*
5499 * port_state is identically LPFC_LOCAL_CFG_LINK while
5500 * waiting for FAN timeout
5501 */
James Smarte8b62012007-08-02 11:10:09 -04005502 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5503 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005504
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005505 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005506 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005507 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005508 if (!NLP_CHK_NODE_ACT(ndlp))
5509 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005510 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5511 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005512 if (ndlp->nlp_type & NLP_FABRIC) {
5513 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005514 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005515
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005516 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005517 /* Fail outstanding IO now since device
5518 * is marked for PLOGI.
5519 */
James Smart2e0fef82007-06-17 19:56:36 -05005520 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005521 }
5522 }
James Smart92d7f7b2007-06-17 19:56:38 -05005523 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005524 if (phba->sli_rev <= LPFC_SLI_REV3)
5525 lpfc_initial_flogi(vport);
5526 else
5527 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005528 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005529 }
dea31012005-04-17 16:05:31 -05005530 break;
5531
James Smart92d7f7b2007-06-17 19:56:38 -05005532 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005533 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005534 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005535 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005536 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5537 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005538 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005539
5540 /* Assume no Fabric and go on with discovery.
5541 * Check for outstanding ELS FLOGI to abort.
5542 */
5543
5544 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005545 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005546
5547 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005548 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005549 break;
5550
5551 case LPFC_FABRIC_CFG_LINK:
5552 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5553 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005554 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5555 "0223 Timeout while waiting for "
5556 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005557 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005558 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005559 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005560 lpfc_els_abort(phba, ndlp);
5561
5562 /* ReStart discovery */
5563 goto restart_disc;
dea31012005-04-17 16:05:31 -05005564
5565 case LPFC_NS_QRY:
5566 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005567 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5568 "0224 NameServer Query timeout "
5569 "Data: x%x x%x\n",
5570 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005571
James Smart92d7f7b2007-06-17 19:56:38 -05005572 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5573 /* Try it one more time */
5574 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005575 vport->gidft_inp = 0;
5576 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005577 if (rc == 0)
5578 break;
dea31012005-04-17 16:05:31 -05005579 }
James Smart92d7f7b2007-06-17 19:56:38 -05005580 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005581
James Smart87af33f2007-10-27 13:37:43 -04005582restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005583 /*
5584 * Discovery is over.
5585 * set port_state to PORT_READY if SLI2.
5586 * cmpl_reg_vpi will set port_state to READY for SLI3.
5587 */
James Smart3772a992009-05-22 14:50:54 -04005588 if (phba->sli_rev < LPFC_SLI_REV4) {
5589 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5590 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005591 else {
James Smart3772a992009-05-22 14:50:54 -04005592 lpfc_issue_clear_la(phba, vport);
5593 vport->port_state = LPFC_VPORT_READY;
5594 }
dea31012005-04-17 16:05:31 -05005595 }
5596
5597 /* Setup and issue mailbox INITIALIZE LINK command */
5598 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5599 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005600 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5601 "0206 Device Discovery "
5602 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005603 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005604 break;
5605 }
5606
5607 lpfc_linkdown(phba);
5608 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5609 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005610 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005611 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005612 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005613 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005614 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005615 if (rc == MBX_NOT_FINISHED)
5616 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5617
5618 break;
5619
5620 case LPFC_DISC_AUTH:
5621 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005622 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5623 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005624 lpfc_disc_flush_list(vport);
5625
James Smart92d7f7b2007-06-17 19:56:38 -05005626 /*
5627 * set port_state to PORT_READY if SLI2.
5628 * cmpl_reg_vpi will set port_state to READY for SLI3.
5629 */
James Smart3772a992009-05-22 14:50:54 -04005630 if (phba->sli_rev < LPFC_SLI_REV4) {
5631 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5632 lpfc_issue_reg_vpi(phba, vport);
5633 else { /* NPIV Not enabled */
5634 lpfc_issue_clear_la(phba, vport);
5635 vport->port_state = LPFC_VPORT_READY;
5636 }
dea31012005-04-17 16:05:31 -05005637 }
5638 break;
5639
James Smart2e0fef82007-06-17 19:56:36 -05005640 case LPFC_VPORT_READY:
5641 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005642 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5643 "0231 RSCN timeout Data: x%x "
5644 "x%x\n",
5645 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005646
5647 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005648 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005649
James Smart2e0fef82007-06-17 19:56:36 -05005650 lpfc_els_flush_rscn(vport);
5651 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005652 }
5653 break;
James Smart2e0fef82007-06-17 19:56:36 -05005654
James Smart92d7f7b2007-06-17 19:56:38 -05005655 default:
James Smarte8b62012007-08-02 11:10:09 -04005656 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005657 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005658 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005659 break;
5660 }
5661
5662 switch (phba->link_state) {
5663 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005664 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005665 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5666 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005667 clrlaerr = 1;
5668 break;
5669
James Smart09372822008-01-11 01:52:54 -05005670 case LPFC_LINK_UP:
5671 lpfc_issue_clear_la(phba, vport);
5672 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005673 case LPFC_LINK_UNKNOWN:
5674 case LPFC_WARM_START:
5675 case LPFC_INIT_START:
5676 case LPFC_INIT_MBX_CMDS:
5677 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005678 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005679 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5680 "0230 Unexpected timeout, hba link "
5681 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005682 clrlaerr = 1;
5683 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005684
5685 case LPFC_HBA_READY:
5686 break;
dea31012005-04-17 16:05:31 -05005687 }
5688
5689 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005690 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005691 if (phba->sli_rev != LPFC_SLI_REV4) {
5692 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5693 ~LPFC_STOP_IOCB_EVENT;
5694 psli->sli3_ring[LPFC_FCP_RING].flag &=
5695 ~LPFC_STOP_IOCB_EVENT;
5696 }
James Smart2e0fef82007-06-17 19:56:36 -05005697 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005698 }
dea31012005-04-17 16:05:31 -05005699 return;
5700}
5701
dea31012005-04-17 16:05:31 -05005702/*
5703 * This routine handles processing a NameServer REG_LOGIN mailbox
5704 * command upon completion. It is setup in the LPFC_MBOXQ
5705 * as the completion routine when the command is
5706 * handed off to the SLI layer.
5707 */
5708void
James Smart2e0fef82007-06-17 19:56:36 -05005709lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005710{
James Smart04c68492009-05-22 14:52:52 -04005711 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005712 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5713 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5714 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005715
5716 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005717 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005718
James Smart6d368e52011-05-24 11:44:12 -04005719 if (phba->sli_rev < LPFC_SLI_REV4)
5720 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005721 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005722 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005723 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005724 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5725 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5726 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005727 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005728 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005729 /*
5730 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005731 * 0xfffffa (FDMI well known port).
5732 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5733 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005734 */
James Smart4258e982015-12-16 18:11:58 -05005735 if (vport->port_type == LPFC_PHYSICAL_PORT)
5736 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005737 else
James Smart4258e982015-12-16 18:11:58 -05005738 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5739
dea31012005-04-17 16:05:31 -05005740
James Smartfa4066b2008-01-11 01:53:27 -05005741 /* decrement the node reference count held for this callback
5742 * function.
5743 */
James Smart329f9bc2007-04-25 09:53:01 -04005744 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005745 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5746 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005747 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005748
5749 return;
5750}
5751
James Smart685f0bf2007-04-25 09:53:08 -04005752static int
5753lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5754{
5755 uint16_t *rpi = param;
5756
James Smarteff4a012012-01-18 16:25:25 -05005757 /* check for active node */
5758 if (!NLP_CHK_NODE_ACT(ndlp))
5759 return 0;
5760
James Smart685f0bf2007-04-25 09:53:08 -04005761 return ndlp->nlp_rpi == *rpi;
5762}
5763
5764static int
5765lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5766{
5767 return memcmp(&ndlp->nlp_portname, param,
5768 sizeof(ndlp->nlp_portname)) == 0;
5769}
5770
Adrian Bunka6ababd2007-11-05 18:07:33 +01005771static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005772__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005773{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005774 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005775
James Smart2e0fef82007-06-17 19:56:36 -05005776 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005777 if (filter(ndlp, param)) {
5778 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5779 "3185 FIND node filter %p DID "
5780 "Data: x%p x%x x%x\n",
5781 filter, ndlp, ndlp->nlp_DID,
5782 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005783 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005784 }
James Smart685f0bf2007-04-25 09:53:08 -04005785 }
James Smart34f5ad82012-08-03 12:35:03 -04005786 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5787 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005788 return NULL;
dea31012005-04-17 16:05:31 -05005789}
5790
James Smart685f0bf2007-04-25 09:53:08 -04005791/*
5792 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005793 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005794 */
5795struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005796__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005797{
James Smart2e0fef82007-06-17 19:56:36 -05005798 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005799}
5800
James Smart488d1462006-03-07 15:02:37 -05005801/*
James Smart685f0bf2007-04-25 09:53:08 -04005802 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005803 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005804 */
5805struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005806lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005807{
James Smart2e0fef82007-06-17 19:56:36 -05005808 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005809 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005810
James Smart2e0fef82007-06-17 19:56:36 -05005811 spin_lock_irq(shost->host_lock);
5812 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5813 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005814 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005815}
5816
James Smartcb69f7d2011-12-13 13:21:57 -05005817/*
5818 * This routine looks up the ndlp lists for the given RPI. If the rpi
5819 * is found, the routine returns the node element list pointer else
5820 * return NULL.
5821 */
5822struct lpfc_nodelist *
5823lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5824{
5825 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5826 struct lpfc_nodelist *ndlp;
5827
5828 spin_lock_irq(shost->host_lock);
5829 ndlp = __lpfc_findnode_rpi(vport, rpi);
5830 spin_unlock_irq(shost->host_lock);
5831 return ndlp;
5832}
5833
5834/**
5835 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5836 * @phba: pointer to lpfc hba data structure.
5837 * @vpi: the physical host virtual N_Port identifier.
5838 *
5839 * This routine finds a vport on a HBA (referred by @phba) through a
5840 * @vpi. The function walks the HBA's vport list and returns the address
5841 * of the vport with the matching @vpi.
5842 *
5843 * Return code
5844 * NULL - No vport with the matching @vpi found
5845 * Otherwise - Address to the vport with the matching @vpi.
5846 **/
5847struct lpfc_vport *
5848lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5849{
5850 struct lpfc_vport *vport;
5851 unsigned long flags;
5852 int i = 0;
5853
5854 /* The physical ports are always vpi 0 - translate is unnecessary. */
5855 if (vpi > 0) {
5856 /*
5857 * Translate the physical vpi to the logical vpi. The
5858 * vport stores the logical vpi.
5859 */
5860 for (i = 0; i < phba->max_vpi; i++) {
5861 if (vpi == phba->vpi_ids[i])
5862 break;
5863 }
5864
5865 if (i >= phba->max_vpi) {
5866 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5867 "2936 Could not find Vport mapped "
5868 "to vpi %d\n", vpi);
5869 return NULL;
5870 }
5871 }
5872
5873 spin_lock_irqsave(&phba->hbalock, flags);
5874 list_for_each_entry(vport, &phba->port_list, listentry) {
5875 if (vport->vpi == i) {
5876 spin_unlock_irqrestore(&phba->hbalock, flags);
5877 return vport;
5878 }
5879 }
5880 spin_unlock_irqrestore(&phba->hbalock, flags);
5881 return NULL;
5882}
5883
dea31012005-04-17 16:05:31 -05005884void
James Smart2e0fef82007-06-17 19:56:36 -05005885lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5886 uint32_t did)
dea31012005-04-17 16:05:31 -05005887{
5888 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005889
5890 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005891 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005892 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005893 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005894 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5895 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5896 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5897 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005898 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005899 ndlp->nlp_usg_map, ndlp);
5900
James Smartcff261f2013-12-17 20:29:47 -05005901 ndlp->active_rrqs_xri_bitmap =
5902 mempool_alloc(vport->phba->active_rrq_pool,
5903 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005904 if (ndlp->active_rrqs_xri_bitmap)
5905 memset(ndlp->active_rrqs_xri_bitmap, 0,
5906 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005907 }
5908
James Smart725dd392013-10-10 12:23:30 -04005909
James Smart858c9f62007-06-17 19:56:39 -05005910
5911 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5912 "node init: did:x%x",
5913 ndlp->nlp_DID, 0, 0);
5914
dea31012005-04-17 16:05:31 -05005915 return;
5916}
James Smart329f9bc2007-04-25 09:53:01 -04005917
James Smart98c9ea52007-10-27 13:37:33 -04005918/* This routine releases all resources associated with a specifc NPort's ndlp
5919 * and mempool_free's the nodelist.
5920 */
James Smart311464e2007-08-02 11:10:37 -04005921static void
James Smart329f9bc2007-04-25 09:53:01 -04005922lpfc_nlp_release(struct kref *kref)
5923{
James Smarte47c9092008-02-08 18:49:26 -05005924 struct lpfc_hba *phba;
5925 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005926 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5927 kref);
James Smart858c9f62007-06-17 19:56:39 -05005928
5929 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5930 "node release: did:x%x flg:x%x type:x%x",
5931 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5932
James Smarte47c9092008-02-08 18:49:26 -05005933 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005934 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005935 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005936 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005937 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005938
5939 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005940 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005941
5942 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005943 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005944 spin_lock_irqsave(&phba->ndlp_lock, flags);
5945 NLP_CLR_NODE_ACT(ndlp);
5946 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005947 if (phba->sli_rev == LPFC_SLI_REV4)
5948 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005949
5950 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005951 if (NLP_CHK_FREE_REQ(ndlp)) {
5952 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005953 if (phba->sli_rev == LPFC_SLI_REV4)
5954 mempool_free(ndlp->active_rrqs_xri_bitmap,
5955 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005956 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005957 }
James Smart329f9bc2007-04-25 09:53:01 -04005958}
5959
James Smart98c9ea52007-10-27 13:37:33 -04005960/* This routine bumps the reference count for a ndlp structure to ensure
5961 * that one discovery thread won't free a ndlp while another discovery thread
5962 * is using it.
5963 */
James Smart329f9bc2007-04-25 09:53:01 -04005964struct lpfc_nodelist *
5965lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5966{
James Smarte47c9092008-02-08 18:49:26 -05005967 struct lpfc_hba *phba;
5968 unsigned long flags;
5969
James Smart98c9ea52007-10-27 13:37:33 -04005970 if (ndlp) {
5971 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5972 "node get: did:x%x flg:x%x refcnt:x%x",
5973 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005974 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005975 /* The check of ndlp usage to prevent incrementing the
5976 * ndlp reference count that is in the process of being
5977 * released.
5978 */
James Smarta257bf92009-04-06 18:48:10 -04005979 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005980 spin_lock_irqsave(&phba->ndlp_lock, flags);
5981 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5982 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5983 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5984 "0276 lpfc_nlp_get: ndlp:x%p "
5985 "usgmap:x%x refcnt:%d\n",
5986 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005987 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005988 return NULL;
5989 } else
5990 kref_get(&ndlp->kref);
5991 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005992 }
James Smart329f9bc2007-04-25 09:53:01 -04005993 return ndlp;
5994}
5995
James Smart98c9ea52007-10-27 13:37:33 -04005996/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005997 * count goes to 0, this indicates the the associated nodelist should be
5998 * freed. Returning 1 indicates the ndlp resource has been released; on the
5999 * other hand, returning 0 indicates the ndlp resource has not been released
6000 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006001 */
James Smart329f9bc2007-04-25 09:53:01 -04006002int
6003lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6004{
James Smarte47c9092008-02-08 18:49:26 -05006005 struct lpfc_hba *phba;
6006 unsigned long flags;
6007
6008 if (!ndlp)
6009 return 1;
6010
6011 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6012 "node put: did:x%x flg:x%x refcnt:x%x",
6013 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006014 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006015 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006016 spin_lock_irqsave(&phba->ndlp_lock, flags);
6017 /* Check the ndlp memory free acknowledge flag to avoid the
6018 * possible race condition that kref_put got invoked again
6019 * after previous one has done ndlp memory free.
6020 */
6021 if (NLP_CHK_FREE_ACK(ndlp)) {
6022 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6023 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6024 "0274 lpfc_nlp_put: ndlp:x%p "
6025 "usgmap:x%x refcnt:%d\n",
6026 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006027 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006028 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006029 }
James Smarte47c9092008-02-08 18:49:26 -05006030 /* Check the ndlp inactivate log flag to avoid the possible
6031 * race condition that kref_put got invoked again after ndlp
6032 * is already in inactivating state.
6033 */
6034 if (NLP_CHK_IACT_REQ(ndlp)) {
6035 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6036 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6037 "0275 lpfc_nlp_put: ndlp:x%p "
6038 "usgmap:x%x refcnt:%d\n",
6039 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006040 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006041 return 1;
6042 }
6043 /* For last put, mark the ndlp usage flags to make sure no
6044 * other kref_get and kref_put on the same ndlp shall get
6045 * in between the process when the final kref_put has been
6046 * invoked on this ndlp.
6047 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006048 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006049 /* Indicate ndlp is put to inactive state. */
6050 NLP_SET_IACT_REQ(ndlp);
6051 /* Acknowledge ndlp memory free has been seen. */
6052 if (NLP_CHK_FREE_REQ(ndlp))
6053 NLP_SET_FREE_ACK(ndlp);
6054 }
6055 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6056 /* Note, the kref_put returns 1 when decrementing a reference
6057 * count that was 1, it invokes the release callback function,
6058 * but it still left the reference count as 1 (not actually
6059 * performs the last decrementation). Otherwise, it actually
6060 * decrements the reference count and returns 0.
6061 */
6062 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006063}
James Smart98c9ea52007-10-27 13:37:33 -04006064
6065/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006066 * by any other discovery thread. This routine returns 1 if the
6067 * ndlp has been freed. A return value of 0 indicates the ndlp is
6068 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006069 */
6070int
6071lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6072{
6073 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6074 "node not used: did:x%x flg:x%x refcnt:x%x",
6075 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006076 kref_read(&ndlp->kref));
6077 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006078 if (lpfc_nlp_put(ndlp))
6079 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006080 return 0;
6081}
James Smart6fb120a2009-05-22 14:52:59 -04006082
6083/**
6084 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6085 * @phba: Pointer to hba context object.
6086 *
6087 * This function iterate through all FC nodes associated
6088 * will all vports to check if there is any node with
6089 * fc_rports associated with it. If there is an fc_rport
6090 * associated with the node, then the node is either in
6091 * discovered state or its devloss_timer is pending.
6092 */
6093static int
6094lpfc_fcf_inuse(struct lpfc_hba *phba)
6095{
6096 struct lpfc_vport **vports;
6097 int i, ret = 0;
6098 struct lpfc_nodelist *ndlp;
6099 struct Scsi_Host *shost;
6100
6101 vports = lpfc_create_vport_work_array(phba);
6102
James Smart63e801c2010-11-20 23:14:19 -05006103 /* If driver cannot allocate memory, indicate fcf is in use */
6104 if (!vports)
6105 return 1;
6106
James Smart6fb120a2009-05-22 14:52:59 -04006107 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6108 shost = lpfc_shost_from_vport(vports[i]);
6109 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006110 /*
6111 * IF the CVL_RCVD bit is not set then we have sent the
6112 * flogi.
6113 * If dev_loss fires while we are waiting we do not want to
6114 * unreg the fcf.
6115 */
6116 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6117 spin_unlock_irq(shost->host_lock);
6118 ret = 1;
6119 goto out;
6120 }
James Smart6fb120a2009-05-22 14:52:59 -04006121 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6122 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6123 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6124 ret = 1;
6125 spin_unlock_irq(shost->host_lock);
6126 goto out;
James Smart80c17842012-03-01 22:35:45 -05006127 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6128 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006129 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006130 "2624 RPI %x DID %x flag %x "
6131 "still logged in\n",
6132 ndlp->nlp_rpi, ndlp->nlp_DID,
6133 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006134 }
6135 }
6136 spin_unlock_irq(shost->host_lock);
6137 }
6138out:
6139 lpfc_destroy_vport_work_array(phba, vports);
6140 return ret;
6141}
6142
6143/**
6144 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6145 * @phba: Pointer to hba context object.
6146 * @mboxq: Pointer to mailbox object.
6147 *
6148 * This function frees memory associated with the mailbox command.
6149 */
James Smart1b511972011-12-13 13:23:09 -05006150void
James Smart6fb120a2009-05-22 14:52:59 -04006151lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6152{
6153 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006154 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006155
6156 if (mboxq->u.mb.mbxStatus) {
6157 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6158 "2555 UNREG_VFI mbxStatus error x%x "
6159 "HBA state x%x\n",
6160 mboxq->u.mb.mbxStatus, vport->port_state);
6161 }
James Smart38b92ef2010-08-04 16:11:39 -04006162 spin_lock_irq(shost->host_lock);
6163 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6164 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006165 mempool_free(mboxq, phba->mbox_mem_pool);
6166 return;
6167}
6168
6169/**
6170 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6171 * @phba: Pointer to hba context object.
6172 * @mboxq: Pointer to mailbox object.
6173 *
6174 * This function frees memory associated with the mailbox command.
6175 */
6176static void
6177lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6178{
6179 struct lpfc_vport *vport = mboxq->vport;
6180
6181 if (mboxq->u.mb.mbxStatus) {
6182 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6183 "2550 UNREG_FCFI mbxStatus error x%x "
6184 "HBA state x%x\n",
6185 mboxq->u.mb.mbxStatus, vport->port_state);
6186 }
6187 mempool_free(mboxq, phba->mbox_mem_pool);
6188 return;
6189}
6190
6191/**
James Smartecfd03c2010-02-12 14:41:27 -05006192 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006193 * @phba: Pointer to hba context object.
6194 *
James Smartecfd03c2010-02-12 14:41:27 -05006195 * This function prepare the HBA for unregistering the currently registered
6196 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6197 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006198 */
James Smartecfd03c2010-02-12 14:41:27 -05006199int
6200lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006201{
James Smart6fb120a2009-05-22 14:52:59 -04006202 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006203 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006204 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006205 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006206
James Smartecfd03c2010-02-12 14:41:27 -05006207 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006208 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006209 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006210
James Smart4d9ab992009-10-02 15:16:39 -04006211 /* At this point, all discovery is aborted */
6212 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006213
6214 /* Unregister VPIs */
6215 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006216 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006217 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006218 /* Stop FLOGI/FDISC retries */
6219 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6220 if (ndlp)
6221 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006222 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006223 if (phba->sli_rev == LPFC_SLI_REV4)
6224 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006225 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006226 shost = lpfc_shost_from_vport(vports[i]);
6227 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006228 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006229 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006230 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006231 }
6232 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006233 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6234 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6235 if (ndlp)
6236 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6237 lpfc_cleanup_pending_mbox(phba->pport);
6238 if (phba->sli_rev == LPFC_SLI_REV4)
6239 lpfc_sli4_unreg_all_rpis(phba->pport);
6240 lpfc_mbx_unreg_vpi(phba->pport);
6241 shost = lpfc_shost_from_vport(phba->pport);
6242 spin_lock_irq(shost->host_lock);
6243 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6244 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6245 spin_unlock_irq(shost->host_lock);
6246 }
James Smart6fb120a2009-05-22 14:52:59 -04006247
James Smart695a8142010-01-26 23:08:03 -05006248 /* Cleanup any outstanding ELS commands */
6249 lpfc_els_flush_all_cmd(phba);
6250
James Smart1b511972011-12-13 13:23:09 -05006251 /* Unregister the physical port VFI */
6252 rc = lpfc_issue_unreg_vfi(phba->pport);
6253 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006254}
6255
6256/**
6257 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6258 * @phba: Pointer to hba context object.
6259 *
6260 * This function issues synchronous unregister FCF mailbox command to HBA to
6261 * unregister the currently registered FCF record. The driver does not reset
6262 * the driver FCF usage state flags.
6263 *
6264 * Return 0 if successfully issued, none-zero otherwise.
6265 */
6266int
6267lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6268{
6269 LPFC_MBOXQ_t *mbox;
6270 int rc;
6271
James Smart6fb120a2009-05-22 14:52:59 -04006272 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6273 if (!mbox) {
6274 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006275 "2551 UNREG_FCFI mbox allocation failed"
6276 "HBA state x%x\n", phba->pport->port_state);
6277 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006278 }
James Smart6fb120a2009-05-22 14:52:59 -04006279 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6280 mbox->vport = phba->pport;
6281 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6282 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6283
6284 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006285 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6286 "2552 Unregister FCFI command failed rc x%x "
6287 "HBA state x%x\n",
6288 rc, phba->pport->port_state);
6289 return -EINVAL;
6290 }
6291 return 0;
6292}
6293
6294/**
6295 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6296 * @phba: Pointer to hba context object.
6297 *
6298 * This function unregisters the currently reigstered FCF. This function
6299 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6300 */
6301void
6302lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6303{
6304 int rc;
6305
6306 /* Preparation for unregistering fcf */
6307 rc = lpfc_unregister_fcf_prep(phba);
6308 if (rc) {
6309 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6310 "2748 Failed to prepare for unregistering "
6311 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006312 return;
6313 }
6314
James Smartecfd03c2010-02-12 14:41:27 -05006315 /* Now, unregister FCF record and reset HBA FCF state */
6316 rc = lpfc_sli4_unregister_fcf(phba);
6317 if (rc)
6318 return;
6319 /* Reset HBA FCF states after successful unregister FCF */
6320 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006321 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006322
6323 /*
6324 * If driver is not unloading, check if there is any other
6325 * FCF record that can be used for discovery.
6326 */
6327 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006328 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006329 return;
6330
James Smart0c9ab6f2010-02-26 14:15:57 -05006331 /* This is considered as the initial FCF discovery scan */
6332 spin_lock_irq(&phba->hbalock);
6333 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6334 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006335
6336 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006337 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006338
James Smart0c9ab6f2010-02-26 14:15:57 -05006339 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006340
James Smart0c9ab6f2010-02-26 14:15:57 -05006341 if (rc) {
6342 spin_lock_irq(&phba->hbalock);
6343 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6344 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006345 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006346 "2553 lpfc_unregister_unused_fcf failed "
6347 "to read FCF record HBA state x%x\n",
6348 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006349 }
James Smartecfd03c2010-02-12 14:41:27 -05006350}
6351
6352/**
6353 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6354 * @phba: Pointer to hba context object.
6355 *
6356 * This function just unregisters the currently reigstered FCF. It does not
6357 * try to find another FCF for discovery.
6358 */
6359void
6360lpfc_unregister_fcf(struct lpfc_hba *phba)
6361{
6362 int rc;
6363
6364 /* Preparation for unregistering fcf */
6365 rc = lpfc_unregister_fcf_prep(phba);
6366 if (rc) {
6367 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6368 "2749 Failed to prepare for unregistering "
6369 "HBA's FCF record: rc=%d\n", rc);
6370 return;
6371 }
6372
6373 /* Now, unregister FCF record and reset HBA FCF state */
6374 rc = lpfc_sli4_unregister_fcf(phba);
6375 if (rc)
6376 return;
6377 /* Set proper HBA FCF states after successful unregister FCF */
6378 spin_lock_irq(&phba->hbalock);
6379 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6380 spin_unlock_irq(&phba->hbalock);
6381}
6382
6383/**
6384 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6385 * @phba: Pointer to hba context object.
6386 *
6387 * This function check if there are any connected remote port for the FCF and
6388 * if all the devices are disconnected, this function unregister FCFI.
6389 * This function also tries to use another FCF for discovery.
6390 */
6391void
6392lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6393{
6394 /*
James Smart3804dc82010-07-14 15:31:37 -04006395 * If HBA is not running in FIP mode, if HBA does not support
6396 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6397 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006398 */
6399 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006400 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006401 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006402 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006403 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006404 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006405 spin_unlock_irq(&phba->hbalock);
6406 return;
6407 }
6408 spin_unlock_irq(&phba->hbalock);
6409
6410 if (lpfc_fcf_inuse(phba))
6411 return;
6412
6413 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006414}
6415
6416/**
6417 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6418 * @phba: Pointer to hba context object.
6419 * @buff: Buffer containing the FCF connection table as in the config
6420 * region.
6421 * This function create driver data structure for the FCF connection
6422 * record table read from config region 23.
6423 */
6424static void
6425lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6426 uint8_t *buff)
6427{
6428 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6429 struct lpfc_fcf_conn_hdr *conn_hdr;
6430 struct lpfc_fcf_conn_rec *conn_rec;
6431 uint32_t record_count;
6432 int i;
6433
6434 /* Free the current connect table */
6435 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006436 &phba->fcf_conn_rec_list, list) {
6437 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006438 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006439 }
James Smart6fb120a2009-05-22 14:52:59 -04006440
6441 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6442 record_count = conn_hdr->length * sizeof(uint32_t)/
6443 sizeof(struct lpfc_fcf_conn_rec);
6444
6445 conn_rec = (struct lpfc_fcf_conn_rec *)
6446 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6447
6448 for (i = 0; i < record_count; i++) {
6449 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6450 continue;
6451 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6452 GFP_KERNEL);
6453 if (!conn_entry) {
6454 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6455 "2566 Failed to allocate connection"
6456 " table entry\n");
6457 return;
6458 }
6459
6460 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6461 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006462 list_add_tail(&conn_entry->list,
6463 &phba->fcf_conn_rec_list);
6464 }
James Smartdf0d0852013-05-31 17:05:08 -04006465
6466 if (!list_empty(&phba->fcf_conn_rec_list)) {
6467 i = 0;
6468 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6469 list) {
6470 conn_rec = &conn_entry->conn_rec;
6471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6472 "3345 FCF connection list rec[%02d]: "
6473 "flags:x%04x, vtag:x%04x, "
6474 "fabric_name:x%02x:%02x:%02x:%02x:"
6475 "%02x:%02x:%02x:%02x, "
6476 "switch_name:x%02x:%02x:%02x:%02x:"
6477 "%02x:%02x:%02x:%02x\n", i++,
6478 conn_rec->flags, conn_rec->vlan_tag,
6479 conn_rec->fabric_name[0],
6480 conn_rec->fabric_name[1],
6481 conn_rec->fabric_name[2],
6482 conn_rec->fabric_name[3],
6483 conn_rec->fabric_name[4],
6484 conn_rec->fabric_name[5],
6485 conn_rec->fabric_name[6],
6486 conn_rec->fabric_name[7],
6487 conn_rec->switch_name[0],
6488 conn_rec->switch_name[1],
6489 conn_rec->switch_name[2],
6490 conn_rec->switch_name[3],
6491 conn_rec->switch_name[4],
6492 conn_rec->switch_name[5],
6493 conn_rec->switch_name[6],
6494 conn_rec->switch_name[7]);
6495 }
6496 }
James Smart6fb120a2009-05-22 14:52:59 -04006497}
6498
6499/**
6500 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6501 * @phba: Pointer to hba context object.
6502 * @buff: Buffer containing the FCoE parameter data structure.
6503 *
6504 * This function update driver data structure with config
6505 * parameters read from config region 23.
6506 */
6507static void
6508lpfc_read_fcoe_param(struct lpfc_hba *phba,
6509 uint8_t *buff)
6510{
6511 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6512 struct lpfc_fcoe_params *fcoe_param;
6513
6514 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6515 buff;
6516 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006517 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006518
6519 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6520 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6521 return;
6522
James Smart6fb120a2009-05-22 14:52:59 -04006523 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6524 phba->valid_vlan = 1;
6525 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6526 0xFFF;
6527 }
6528
6529 phba->fc_map[0] = fcoe_param->fc_map[0];
6530 phba->fc_map[1] = fcoe_param->fc_map[1];
6531 phba->fc_map[2] = fcoe_param->fc_map[2];
6532 return;
6533}
6534
6535/**
6536 * lpfc_get_rec_conf23 - Get a record type in config region data.
6537 * @buff: Buffer containing config region 23 data.
6538 * @size: Size of the data buffer.
6539 * @rec_type: Record type to be searched.
6540 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006541 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006542 * of the record specified by record_type. If record found, this
6543 * function return pointer to the record else return NULL.
6544 */
6545static uint8_t *
6546lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6547{
6548 uint32_t offset = 0, rec_length;
6549
6550 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6551 (size < sizeof(uint32_t)))
6552 return NULL;
6553
6554 rec_length = buff[offset + 1];
6555
6556 /*
6557 * One TLV record has one word header and number of data words
6558 * specified in the rec_length field of the record header.
6559 */
6560 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6561 <= size) {
6562 if (buff[offset] == rec_type)
6563 return &buff[offset];
6564
6565 if (buff[offset] == LPFC_REGION23_LAST_REC)
6566 return NULL;
6567
6568 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6569 rec_length = buff[offset + 1];
6570 }
6571 return NULL;
6572}
6573
6574/**
6575 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6576 * @phba: Pointer to lpfc_hba data structure.
6577 * @buff: Buffer containing config region 23 data.
6578 * @size: Size of the data buffer.
6579 *
Stefan Weileef35c22010-08-06 21:11:15 +02006580 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006581 * populate driver data structure with the parameters.
6582 */
6583void
6584lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6585 uint8_t *buff,
6586 uint32_t size)
6587{
James Smarteb016562014-09-03 12:58:06 -04006588 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006589 uint8_t *rec_ptr;
6590
6591 /*
6592 * If data size is less than 2 words signature and version cannot be
6593 * verified.
6594 */
6595 if (size < 2*sizeof(uint32_t))
6596 return;
6597
6598 /* Check the region signature first */
6599 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6601 "2567 Config region 23 has bad signature\n");
6602 return;
6603 }
6604
6605 offset += 4;
6606
6607 /* Check the data structure version */
6608 if (buff[offset] != LPFC_REGION23_VERSION) {
6609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6610 "2568 Config region 23 has bad version\n");
6611 return;
6612 }
6613 offset += 4;
6614
James Smart6fb120a2009-05-22 14:52:59 -04006615 /* Read FCoE param record */
6616 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6617 size - offset, FCOE_PARAM_TYPE);
6618 if (rec_ptr)
6619 lpfc_read_fcoe_param(phba, rec_ptr);
6620
6621 /* Read FCF connection table */
6622 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6623 size - offset, FCOE_CONN_TBL_TYPE);
6624 if (rec_ptr)
6625 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6626
6627}