blob: 542797bd3134ba7fb0f0cd1fe978e0c4087dea2f [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)) {
James Smartd613b6a2017-02-12 13:52:37 -0800914 if (phba->nvmet_support)
915 lpfc_nvmet_update_targetport(phba);
916 else
James Smart8c258642017-02-12 13:52:36 -0800917 lpfc_nvme_update_localport(vports[i]);
James Smart8c258642017-02-12 13:52:36 -0800918 }
James Smart549e55c2007-08-02 11:09:51 -0400919 }
James Smart895427b2017-02-12 13:52:30 -0800920 }
James Smart09372822008-01-11 01:52:54 -0500921 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500922 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500923 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
924 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400925 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500926 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500927 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400928 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500929 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500930 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500931 }
932 }
933
dea31012005-04-17 16:05:31 -0500934 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500935 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500936 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
937 if (mb) {
dea31012005-04-17 16:05:31 -0500938 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500939 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500940 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400941 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500942 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500943 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500944 }
945 }
James Smart2e0fef82007-06-17 19:56:36 -0500946 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500947 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500948 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500949 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500950 return 0;
dea31012005-04-17 16:05:31 -0500951}
952
James Smart92d7f7b2007-06-17 19:56:38 -0500953static void
954lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500955{
James Smart92d7f7b2007-06-17 19:56:38 -0500956 struct lpfc_nodelist *ndlp;
957
958 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500959 if (!NLP_CHK_NODE_ACT(ndlp))
960 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500961 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
962 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500963 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500964 /* On Linkup its safe to clean up the ndlp
965 * from Fabric connections.
966 */
James Smart92d7f7b2007-06-17 19:56:38 -0500967 if (ndlp->nlp_DID != Fabric_DID)
968 lpfc_unreg_rpi(vport, ndlp);
969 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
970 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500971 /* Fail outstanding IO now since device is
972 * marked for PLOGI.
973 */
James Smart92d7f7b2007-06-17 19:56:38 -0500974 lpfc_unreg_rpi(vport, ndlp);
975 }
976 }
977}
978
979static void
980lpfc_linkup_port(struct lpfc_vport *vport)
981{
982 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500983 struct lpfc_hba *phba = vport->phba;
984
985 if ((vport->load_flag & FC_UNLOADING) != 0)
986 return;
987
James Smart858c9f62007-06-17 19:56:39 -0500988 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
989 "Link Up: top:x%x speed:x%x flg:x%x",
990 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
991
James Smart92d7f7b2007-06-17 19:56:38 -0500992 /* If NPIV is not enabled, only bring the physical port up */
993 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
994 (vport != phba->pport))
995 return;
dea31012005-04-17 16:05:31 -0500996
James Smarta0f2d3e2017-02-12 13:52:31 -0800997 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
998 fc_host_post_event(shost, fc_get_event_number(),
999 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -04001000
James Smart2e0fef82007-06-17 19:56:36 -05001001 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001002 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1003 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1004 vport->fc_flag |= FC_NDISC_ACTIVE;
1005 vport->fc_ns_retry = 0;
1006 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001007
James Smart92d7f7b2007-06-17 19:56:38 -05001008 if (vport->fc_flag & FC_LBIT)
1009 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001010
James Smart92d7f7b2007-06-17 19:56:38 -05001011}
1012
1013static int
1014lpfc_linkup(struct lpfc_hba *phba)
1015{
James Smart549e55c2007-08-02 11:09:51 -04001016 struct lpfc_vport **vports;
1017 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001018
1019 phba->link_state = LPFC_LINK_UP;
1020
1021 /* Unblock fabric iocbs if they are blocked */
1022 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1023 del_timer_sync(&phba->fabric_block_timer);
1024
James Smart549e55c2007-08-02 11:09:51 -04001025 vports = lpfc_create_vport_work_array(phba);
1026 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001027 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001028 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001029 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001030
1031 return 0;
1032}
1033
1034/*
1035 * This routine handles processing a CLEAR_LA mailbox
1036 * command upon completion. It is setup in the LPFC_MBOXQ
1037 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001038 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001039 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001040static void
James Smart2e0fef82007-06-17 19:56:36 -05001041lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001042{
James Smart2e0fef82007-06-17 19:56:36 -05001043 struct lpfc_vport *vport = pmb->vport;
1044 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1045 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001046 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001047 uint32_t control;
1048
dea31012005-04-17 16:05:31 -05001049 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001050 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1051 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001052
1053 /* Check for error */
1054 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001055 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001056 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1057 "0320 CLEAR_LA mbxStatus error x%x hba "
1058 "state x%x\n",
1059 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001060 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001061 goto out;
1062 }
1063
James Smart92d7f7b2007-06-17 19:56:38 -05001064 if (vport->port_type == LPFC_PHYSICAL_PORT)
1065 phba->link_state = LPFC_HBA_READY;
1066
1067 spin_lock_irq(&phba->hbalock);
1068 psli->sli_flag |= LPFC_PROCESS_LA;
1069 control = readl(phba->HCregaddr);
1070 control |= HC_LAINT_ENA;
1071 writel(control, phba->HCregaddr);
1072 readl(phba->HCregaddr); /* flush */
1073 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001074 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001075 return;
dea31012005-04-17 16:05:31 -05001076
dea31012005-04-17 16:05:31 -05001077out:
1078 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001079 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1080 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001081 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001082
James Smart2e0fef82007-06-17 19:56:36 -05001083 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001084 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001085 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001086
James Smart2e0fef82007-06-17 19:56:36 -05001087 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001088
1089 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001090
1091 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001092 psli->sli_flag |= LPFC_PROCESS_LA;
1093 control = readl(phba->HCregaddr);
1094 control |= HC_LAINT_ENA;
1095 writel(control, phba->HCregaddr);
1096 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001097 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001098
1099 return;
1100}
1101
James Smart2e0fef82007-06-17 19:56:36 -05001102
James Smartd6de08c2015-12-16 18:11:53 -05001103void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001104lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001105{
James Smart2e0fef82007-06-17 19:56:36 -05001106 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001107
James Smart04c68492009-05-22 14:52:52 -04001108 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001109 goto out;
dea31012005-04-17 16:05:31 -05001110
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001111 mempool_free(pmb, phba->mbox_mem_pool);
1112
James Smart1b511972011-12-13 13:23:09 -05001113 /* don't perform discovery for SLI4 loopback diagnostic test */
1114 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1115 !(phba->hba_flag & HBA_FCOE_MODE) &&
1116 (phba->link_flag & LS_LOOPBACK_MODE))
1117 return;
1118
James Smart76a95d72010-11-20 23:11:48 -05001119 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001120 vport->fc_flag & FC_PUBLIC_LOOP &&
1121 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001122 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001123 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001124 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1125 */
James Smart2e0fef82007-06-17 19:56:36 -05001126 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001127 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001128 }
dea31012005-04-17 16:05:31 -05001129
James Smart2e0fef82007-06-17 19:56:36 -05001130 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001131 * LPFC_FLOGI while waiting for FLOGI cmpl
1132 */
James Smartd6de08c2015-12-16 18:11:53 -05001133 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001134 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001135 else if (vport->fc_flag & FC_PT2PT)
1136 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001137 return;
dea31012005-04-17 16:05:31 -05001138
1139out:
James Smarte8b62012007-08-02 11:10:09 -04001140 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1141 "0306 CONFIG_LINK mbxStatus error x%x "
1142 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001143 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001144 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001145
1146 lpfc_linkdown(phba);
1147
James Smarte8b62012007-08-02 11:10:09 -04001148 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1149 "0200 CONFIG_LINK bad hba state x%x\n",
1150 vport->port_state);
dea31012005-04-17 16:05:31 -05001151
James Smart92d7f7b2007-06-17 19:56:38 -05001152 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001153 return;
1154}
1155
James Smart7d791df2011-07-22 18:37:52 -04001156/**
1157 * lpfc_sli4_clear_fcf_rr_bmask
1158 * @phba pointer to the struct lpfc_hba for this port.
1159 * This fucnction resets the round robin bit mask and clears the
1160 * fcf priority list. The list deletions are done while holding the
1161 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1162 * from the lpfc_fcf_pri record.
1163 **/
1164void
1165lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1166{
1167 struct lpfc_fcf_pri *fcf_pri;
1168 struct lpfc_fcf_pri *next_fcf_pri;
1169 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1170 spin_lock_irq(&phba->hbalock);
1171 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1172 &phba->fcf.fcf_pri_list, list) {
1173 list_del_init(&fcf_pri->list);
1174 fcf_pri->fcf_rec.flag = 0;
1175 }
1176 spin_unlock_irq(&phba->hbalock);
1177}
dea31012005-04-17 16:05:31 -05001178static void
James Smart6fb120a2009-05-22 14:52:59 -04001179lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1180{
1181 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001182
1183 if (mboxq->u.mb.mbxStatus) {
1184 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1185 "2017 REG_FCFI mbxStatus error x%x "
1186 "HBA state x%x\n",
1187 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001188 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001189 }
1190
1191 /* Start FCoE discovery by sending a FLOGI. */
1192 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1193 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001194 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001195 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001196 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001197
James Smart32b97932009-07-19 10:01:21 -04001198 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001199 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1200 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001201 goto fail_out;
1202
1203 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001204 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001205 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001206 phba->hba_flag &= ~FCF_TS_INPROG;
1207 if (vport->port_state != LPFC_FLOGI) {
1208 phba->hba_flag |= FCF_RR_INPROG;
1209 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001210 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001211 goto out;
1212 }
1213 spin_unlock_irq(&phba->hbalock);
1214 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001215
James Smarta93ff372010-10-22 11:06:08 -04001216fail_out:
1217 spin_lock_irq(&phba->hbalock);
1218 phba->hba_flag &= ~FCF_RR_INPROG;
1219 spin_unlock_irq(&phba->hbalock);
1220out:
James Smart6fb120a2009-05-22 14:52:59 -04001221 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001222}
1223
1224/**
1225 * lpfc_fab_name_match - Check if the fcf fabric name match.
1226 * @fab_name: pointer to fabric name.
1227 * @new_fcf_record: pointer to fcf record.
1228 *
1229 * This routine compare the fcf record's fabric name with provided
1230 * fabric name. If the fabric name are identical this function
1231 * returns 1 else return 0.
1232 **/
1233static uint32_t
1234lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1235{
James Smartecfd03c2010-02-12 14:41:27 -05001236 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001237 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001238 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1239 return 0;
1240 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1241 return 0;
1242 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1243 return 0;
1244 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1245 return 0;
1246 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1247 return 0;
1248 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1249 return 0;
1250 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1251 return 0;
1252 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001253}
1254
1255/**
James Smart8fa38512009-07-19 10:01:03 -04001256 * lpfc_sw_name_match - Check if the fcf switch name match.
1257 * @fab_name: pointer to fabric name.
1258 * @new_fcf_record: pointer to fcf record.
1259 *
1260 * This routine compare the fcf record's switch name with provided
1261 * switch name. If the switch name are identical this function
1262 * returns 1 else return 0.
1263 **/
1264static uint32_t
1265lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1266{
James Smartecfd03c2010-02-12 14:41:27 -05001267 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001268 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001269 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1270 return 0;
1271 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1272 return 0;
1273 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1274 return 0;
1275 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1276 return 0;
1277 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1278 return 0;
1279 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1280 return 0;
1281 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1282 return 0;
1283 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001284}
1285
1286/**
James Smart6fb120a2009-05-22 14:52:59 -04001287 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001288 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001289 * @new_fcf_record: pointer to fcf record.
1290 *
1291 * This routine compare the fcf record's mac address with HBA's
1292 * FCF mac address. If the mac addresses are identical this function
1293 * returns 1 else return 0.
1294 **/
1295static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001296lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001297{
James Smartecfd03c2010-02-12 14:41:27 -05001298 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001299 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001300 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1301 return 0;
1302 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1303 return 0;
1304 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1305 return 0;
1306 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1307 return 0;
1308 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1309 return 0;
1310 return 1;
1311}
1312
1313static bool
1314lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1315{
1316 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001317}
1318
1319/**
James Smart7d791df2011-07-22 18:37:52 -04001320 * lpfc_update_fcf_record - Update driver fcf record
1321 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1322 * @phba: pointer to lpfc hba data structure.
1323 * @fcf_index: Index for the lpfc_fcf_record.
1324 * @new_fcf_record: pointer to hba fcf record.
1325 *
1326 * This routine updates the driver FCF priority record from the new HBA FCF
1327 * record. This routine is called with the host lock held.
1328 **/
1329static void
1330__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1331 struct fcf_record *new_fcf_record
1332 )
1333{
1334 struct lpfc_fcf_pri *fcf_pri;
1335
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001336 lockdep_assert_held(&phba->hbalock);
1337
James Smart7d791df2011-07-22 18:37:52 -04001338 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1339 fcf_pri->fcf_rec.fcf_index = fcf_index;
1340 /* FCF record priority */
1341 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1342
1343}
1344
1345/**
James Smart6fb120a2009-05-22 14:52:59 -04001346 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001347 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001348 * @new_fcf_record: pointer to fcf record.
1349 *
1350 * This routine copies the FCF information from the FCF
1351 * record to lpfc_hba data structure.
1352 **/
1353static void
James Smartecfd03c2010-02-12 14:41:27 -05001354lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1355 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001356{
James Smartecfd03c2010-02-12 14:41:27 -05001357 /* Fabric name */
1358 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001359 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001360 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001361 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001362 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001363 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001364 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001365 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001366 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001367 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001368 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001369 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001370 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001371 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001372 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001373 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001374 /* Mac address */
1375 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1376 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1377 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1378 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1379 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1380 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1381 /* FCF record index */
1382 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1383 /* FCF record priority */
1384 fcf_rec->priority = new_fcf_record->fip_priority;
1385 /* Switch name */
1386 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001387 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001388 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001389 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001390 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001391 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001392 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001393 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001394 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001395 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001396 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001397 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001398 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001399 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001400 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001401 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001402}
1403
1404/**
James Smartecfd03c2010-02-12 14:41:27 -05001405 * lpfc_update_fcf_record - Update driver fcf record
1406 * @phba: pointer to lpfc hba data structure.
1407 * @fcf_rec: pointer to driver fcf record.
1408 * @new_fcf_record: pointer to hba fcf record.
1409 * @addr_mode: address mode to be set to the driver fcf record.
1410 * @vlan_id: vlan tag to be set to the driver fcf record.
1411 * @flag: flag bits to be set to the driver fcf record.
1412 *
1413 * This routine updates the driver FCF record from the new HBA FCF record
1414 * together with the address mode, vlan_id, and other informations. This
1415 * routine is called with the host lock held.
1416 **/
1417static void
1418__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1419 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1420 uint16_t vlan_id, uint32_t flag)
1421{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001422 lockdep_assert_held(&phba->hbalock);
1423
James Smartecfd03c2010-02-12 14:41:27 -05001424 /* Copy the fields from the HBA's FCF record */
1425 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1426 /* Update other fields of driver FCF record */
1427 fcf_rec->addr_mode = addr_mode;
1428 fcf_rec->vlan_id = vlan_id;
1429 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001430 __lpfc_update_fcf_record_pri(phba,
1431 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1432 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001433}
1434
1435/**
James Smart6fb120a2009-05-22 14:52:59 -04001436 * lpfc_register_fcf - Register the FCF with hba.
1437 * @phba: pointer to lpfc hba data structure.
1438 *
1439 * This routine issues a register fcfi mailbox command to register
1440 * the fcf with HBA.
1441 **/
1442static void
1443lpfc_register_fcf(struct lpfc_hba *phba)
1444{
1445 LPFC_MBOXQ_t *fcf_mbxq;
1446 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001447
James Smartd439d282010-09-29 11:18:45 -04001448 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001449 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001450 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001451 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001452 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001453 return;
1454 }
1455
1456 /* The FCF is already registered, start discovery */
1457 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001458 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001459 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001460 if (phba->pport->port_state != LPFC_FLOGI &&
1461 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001462 phba->hba_flag |= FCF_RR_INPROG;
1463 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001464 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001465 return;
1466 }
1467 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001468 return;
1469 }
James Smartd439d282010-09-29 11:18:45 -04001470 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001471
James Smarta93ff372010-10-22 11:06:08 -04001472 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001473 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001474 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001475 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001476 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001477 return;
James Smart4d9ab992009-10-02 15:16:39 -04001478 }
James Smart6fb120a2009-05-22 14:52:59 -04001479
1480 lpfc_reg_fcfi(phba, fcf_mbxq);
1481 fcf_mbxq->vport = phba->pport;
1482 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1483 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001484 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001485 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001486 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001487 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001488 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001489 }
James Smart6fb120a2009-05-22 14:52:59 -04001490
1491 return;
1492}
1493
1494/**
1495 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1496 * @phba: pointer to lpfc hba data structure.
1497 * @new_fcf_record: pointer to fcf record.
1498 * @boot_flag: Indicates if this record used by boot bios.
1499 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001500 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001501 *
1502 * This routine compare the fcf record with connect list obtained from the
1503 * config region to decide if this FCF can be used for SAN discovery. It returns
1504 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1505 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1506 * is used by boot bios and addr_mode will indicate the addressing mode to be
1507 * used for this FCF when the function returns.
1508 * If the FCF record need to be used with a particular vlan id, the vlan is
1509 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001510 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001511 **/
1512static int
1513lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1514 struct fcf_record *new_fcf_record,
1515 uint32_t *boot_flag, uint32_t *addr_mode,
1516 uint16_t *vlan_id)
1517{
1518 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001519 int i, j, fcf_vlan_id = 0;
1520
1521 /* Find the lowest VLAN id in the FCF record */
1522 for (i = 0; i < 512; i++) {
1523 if (new_fcf_record->vlan_bitmap[i]) {
1524 fcf_vlan_id = i * 8;
1525 j = 0;
1526 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1527 j++;
1528 fcf_vlan_id++;
1529 }
1530 break;
1531 }
1532 }
James Smart6fb120a2009-05-22 14:52:59 -04001533
James Smart26979ce2012-09-29 11:31:55 -04001534 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001535 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001536 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1537 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001538 return 0;
1539
James Smart45ed1192009-10-02 15:17:02 -04001540 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001541 *boot_flag = 0;
1542 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1543 new_fcf_record);
1544 if (phba->valid_vlan)
1545 *vlan_id = phba->vlan_id;
1546 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001547 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001548 return 1;
1549 }
1550
1551 /*
1552 * If there are no FCF connection table entry, driver connect to all
1553 * FCFs.
1554 */
1555 if (list_empty(&phba->fcf_conn_rec_list)) {
1556 *boot_flag = 0;
1557 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1558 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001559
1560 /*
1561 * When there are no FCF connect entries, use driver's default
1562 * addressing mode - FPMA.
1563 */
1564 if (*addr_mode & LPFC_FCF_FPMA)
1565 *addr_mode = LPFC_FCF_FPMA;
1566
James Smart4d9ab992009-10-02 15:16:39 -04001567 /* If FCF record report a vlan id use that vlan id */
1568 if (fcf_vlan_id)
1569 *vlan_id = fcf_vlan_id;
1570 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001571 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001572 return 1;
1573 }
1574
James Smartecfd03c2010-02-12 14:41:27 -05001575 list_for_each_entry(conn_entry,
1576 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001577 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1578 continue;
1579
1580 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1581 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001582 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001583 continue;
James Smart8fa38512009-07-19 10:01:03 -04001584 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1585 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1586 new_fcf_record))
1587 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001588 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1589 /*
1590 * If the vlan bit map does not have the bit set for the
1591 * vlan id to be used, then it is not a match.
1592 */
1593 if (!(new_fcf_record->vlan_bitmap
1594 [conn_entry->conn_rec.vlan_tag / 8] &
1595 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1596 continue;
1597 }
1598
1599 /*
James Smart0c287582009-06-10 17:22:56 -04001600 * If connection record does not support any addressing mode,
1601 * skip the FCF record.
1602 */
1603 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1604 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1605 continue;
1606
1607 /*
James Smart6fb120a2009-05-22 14:52:59 -04001608 * Check if the connection record specifies a required
1609 * addressing mode.
1610 */
1611 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1612 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1613
1614 /*
1615 * If SPMA required but FCF not support this continue.
1616 */
1617 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1618 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1619 new_fcf_record) & LPFC_FCF_SPMA))
1620 continue;
1621
1622 /*
1623 * If FPMA required but FCF not support this continue.
1624 */
1625 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1626 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1627 new_fcf_record) & LPFC_FCF_FPMA))
1628 continue;
1629 }
1630
1631 /*
1632 * This fcf record matches filtering criteria.
1633 */
1634 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1635 *boot_flag = 1;
1636 else
1637 *boot_flag = 0;
1638
James Smart0c287582009-06-10 17:22:56 -04001639 /*
1640 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001641 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001642 * by FCF, allow fabric to pick the addressing mode.
1643 */
James Smart6fb120a2009-05-22 14:52:59 -04001644 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1645 new_fcf_record);
1646 /*
1647 * If the user specified a required address mode, assign that
1648 * address mode
1649 */
1650 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1651 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1652 *addr_mode = (conn_entry->conn_rec.flags &
1653 FCFCNCT_AM_SPMA) ?
1654 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1655 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001656 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001657 * addr mode only if FCF support the addr_mode.
1658 */
1659 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1660 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1661 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1662 (*addr_mode & LPFC_FCF_SPMA))
1663 *addr_mode = LPFC_FCF_SPMA;
1664 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1665 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1666 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1667 (*addr_mode & LPFC_FCF_FPMA))
1668 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001669
James Smart4d9ab992009-10-02 15:16:39 -04001670 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001671 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1672 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001673 /*
1674 * If no vlan id is specified in connect list, use the vlan id
1675 * in the FCF record
1676 */
1677 else if (fcf_vlan_id)
1678 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001679 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001680 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001681
1682 return 1;
1683 }
1684
1685 return 0;
1686}
1687
1688/**
James Smart32b97932009-07-19 10:01:21 -04001689 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1690 * @phba: pointer to lpfc hba data structure.
1691 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1692 *
1693 * This function check if there is any fcoe event pending while driver
1694 * scan FCF entries. If there is any pending event, it will restart the
1695 * FCF saning and return 1 else return 0.
1696 */
1697int
1698lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1699{
James Smart32b97932009-07-19 10:01:21 -04001700 /*
1701 * If the Link is up and no FCoE events while in the
1702 * FCF discovery, no need to restart FCF discovery.
1703 */
1704 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001705 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001706 return 0;
1707
James Smart0c9ab6f2010-02-26 14:15:57 -05001708 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1709 "2768 Pending link or FCF event during current "
1710 "handling of the previous event: link_state:x%x, "
1711 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1712 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1713 phba->fcoe_eventtag);
1714
James Smart32b97932009-07-19 10:01:21 -04001715 spin_lock_irq(&phba->hbalock);
1716 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1717 spin_unlock_irq(&phba->hbalock);
1718
James Smart0c9ab6f2010-02-26 14:15:57 -05001719 if (phba->link_state >= LPFC_LINK_UP) {
1720 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1721 "2780 Restart FCF table scan due to "
1722 "pending FCF event:evt_tag_at_scan:x%x, "
1723 "evt_tag_current:x%x\n",
1724 phba->fcoe_eventtag_at_fcf_scan,
1725 phba->fcoe_eventtag);
1726 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1727 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001728 /*
James Smarta93ff372010-10-22 11:06:08 -04001729 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001730 * flag
1731 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001732 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1733 "2833 Stop FCF discovery process due to link "
1734 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001735 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001736 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001737 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001738 spin_unlock_irq(&phba->hbalock);
1739 }
James Smart32b97932009-07-19 10:01:21 -04001740
James Smart0c9ab6f2010-02-26 14:15:57 -05001741 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001742 if (unreg_fcf) {
1743 spin_lock_irq(&phba->hbalock);
1744 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1745 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001746 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001747 }
James Smart32b97932009-07-19 10:01:21 -04001748 return 1;
1749}
1750
1751/**
James Smart999d8132010-03-15 11:24:56 -04001752 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1753 * @phba: pointer to lpfc hba data structure.
1754 * @fcf_cnt: number of eligible fcf record seen so far.
1755 *
1756 * This function makes an running random selection decision on FCF record to
1757 * use through a sequence of @fcf_cnt eligible FCF records with equal
1758 * probability. To perform integer manunipulation of random numbers with
1759 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001760 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001761 *
1762 * Returns true when outcome is for the newly read FCF record should be
1763 * chosen; otherwise, return false when outcome is for keeping the previously
1764 * chosen FCF record.
1765 **/
1766static bool
1767lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1768{
1769 uint32_t rand_num;
1770
1771 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001772 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001773
1774 /* Decision with probability 1/fcf_cnt */
1775 if ((fcf_cnt * rand_num) < 0xFFFF)
1776 return true;
1777 else
1778 return false;
1779}
1780
1781/**
James Smart3804dc82010-07-14 15:31:37 -04001782 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001783 * @phba: pointer to lpfc hba data structure.
1784 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001785 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001786 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001787 * This routine parses the non-embedded fcf mailbox command by performing the
1788 * necessarily error checking, non-embedded read FCF record mailbox command
1789 * SGE parsing, and endianness swapping.
1790 *
1791 * Returns the pointer to the new FCF record in the non-embedded mailbox
1792 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001793 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001794static struct fcf_record *
1795lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1796 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001797{
1798 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001799 struct lpfc_mbx_sge sge;
1800 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001801 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001802 union lpfc_sli4_cfg_shdr *shdr;
1803 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001804
James Smart6fb120a2009-05-22 14:52:59 -04001805 /* Get the first SGE entry from the non-embedded DMA memory. This
1806 * routine only uses a single SGE.
1807 */
1808 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001809 if (unlikely(!mboxq->sge_array)) {
1810 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1811 "2524 Failed to get the non-embedded SGE "
1812 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001813 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001814 }
1815 virt_addr = mboxq->sge_array->addr[0];
1816
1817 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001818 lpfc_sli_pcimem_bcopy(shdr, shdr,
1819 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001820 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001821 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001822 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001823 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001824 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1825 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001826 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001827 "2726 READ_FCF_RECORD Indicates empty "
1828 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001829 else
1830 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001831 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001832 "with status x%x add_status x%x, "
1833 "mbx\n", shdr_status, shdr_add_status);
1834 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001835 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001836
1837 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001838 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1839 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1840 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001841 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001842 new_fcf_record = (struct fcf_record *)(virt_addr +
1843 sizeof(struct lpfc_mbx_read_fcf_tbl));
1844 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001845 offsetof(struct fcf_record, vlan_bitmap));
1846 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1847 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001848
James Smart0c9ab6f2010-02-26 14:15:57 -05001849 return new_fcf_record;
1850}
1851
1852/**
1853 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1854 * @phba: pointer to lpfc hba data structure.
1855 * @fcf_record: pointer to the fcf record.
1856 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1857 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1858 *
1859 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1860 * enabled.
1861 **/
1862static void
1863lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1864 struct fcf_record *fcf_record,
1865 uint16_t vlan_id,
1866 uint16_t next_fcf_index)
1867{
1868 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1869 "2764 READ_FCF_RECORD:\n"
1870 "\tFCF_Index : x%x\n"
1871 "\tFCF_Avail : x%x\n"
1872 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001873 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001874 "\tFIP_Priority : x%x\n"
1875 "\tMAC_Provider : x%x\n"
1876 "\tLowest VLANID : x%x\n"
1877 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1878 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1879 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1880 "\tNext_FCF_Index: x%x\n",
1881 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1882 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1883 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001884 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001885 fcf_record->fip_priority,
1886 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1887 vlan_id,
1888 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1889 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1890 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1891 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1892 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1893 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1894 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1895 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1896 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1897 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1898 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1899 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1900 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1901 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1902 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1903 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1904 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1905 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1906 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1907 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1908 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1909 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1910 next_fcf_index);
1911}
1912
1913/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001914 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1915 * @phba: pointer to lpfc hba data structure.
1916 * @fcf_rec: pointer to an existing FCF record.
1917 * @new_fcf_record: pointer to a new FCF record.
1918 * @new_vlan_id: vlan id from the new FCF record.
1919 *
1920 * This function performs matching test of a new FCF record against an existing
1921 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1922 * will not be used as part of the FCF record matching criteria.
1923 *
1924 * Returns true if all the fields matching, otherwise returns false.
1925 */
1926static bool
1927lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1928 struct lpfc_fcf_rec *fcf_rec,
1929 struct fcf_record *new_fcf_record,
1930 uint16_t new_vlan_id)
1931{
1932 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1933 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1934 return false;
1935 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1936 return false;
1937 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1938 return false;
1939 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1940 return false;
James Smart7d791df2011-07-22 18:37:52 -04001941 if (fcf_rec->priority != new_fcf_record->fip_priority)
1942 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001943 return true;
1944}
1945
1946/**
James Smarta93ff372010-10-22 11:06:08 -04001947 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1948 * @vport: Pointer to vport object.
1949 * @fcf_index: index to next fcf.
1950 *
1951 * This function processing the roundrobin fcf failover to next fcf index.
1952 * When this function is invoked, there will be a current fcf registered
1953 * for flogi.
1954 * Return: 0 for continue retrying flogi on currently registered fcf;
1955 * 1 for stop flogi on currently registered fcf;
1956 */
1957int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1958{
1959 struct lpfc_hba *phba = vport->phba;
1960 int rc;
1961
1962 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1963 spin_lock_irq(&phba->hbalock);
1964 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1965 spin_unlock_irq(&phba->hbalock);
1966 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1967 "2872 Devloss tmo with no eligible "
1968 "FCF, unregister in-use FCF (x%x) "
1969 "and rescan FCF table\n",
1970 phba->fcf.current_rec.fcf_indx);
1971 lpfc_unregister_fcf_rescan(phba);
1972 goto stop_flogi_current_fcf;
1973 }
1974 /* Mark the end to FLOGI roundrobin failover */
1975 phba->hba_flag &= ~FCF_RR_INPROG;
1976 /* Allow action to new fcf asynchronous event */
1977 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1978 spin_unlock_irq(&phba->hbalock);
1979 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1980 "2865 No FCF available, stop roundrobin FCF "
1981 "failover and change port state:x%x/x%x\n",
1982 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1983 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1984 goto stop_flogi_current_fcf;
1985 } else {
1986 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1987 "2794 Try FLOGI roundrobin FCF failover to "
1988 "(x%x)\n", fcf_index);
1989 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1990 if (rc)
1991 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1992 "2761 FLOGI roundrobin FCF failover "
1993 "failed (rc:x%x) to read FCF (x%x)\n",
1994 rc, phba->fcf.current_rec.fcf_indx);
1995 else
1996 goto stop_flogi_current_fcf;
1997 }
1998 return 0;
1999
2000stop_flogi_current_fcf:
2001 lpfc_can_disctmo(vport);
2002 return 1;
2003}
2004
2005/**
James Smart7d791df2011-07-22 18:37:52 -04002006 * lpfc_sli4_fcf_pri_list_del
2007 * @phba: pointer to lpfc hba data structure.
2008 * @fcf_index the index of the fcf record to delete
2009 * This routine checks the on list flag of the fcf_index to be deleted.
2010 * If it is one the list then it is removed from the list, and the flag
2011 * is cleared. This routine grab the hbalock before removing the fcf
2012 * record from the list.
2013 **/
2014static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2015 uint16_t fcf_index)
2016{
2017 struct lpfc_fcf_pri *new_fcf_pri;
2018
2019 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2020 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2021 "3058 deleting idx x%x pri x%x flg x%x\n",
2022 fcf_index, new_fcf_pri->fcf_rec.priority,
2023 new_fcf_pri->fcf_rec.flag);
2024 spin_lock_irq(&phba->hbalock);
2025 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2026 if (phba->fcf.current_rec.priority ==
2027 new_fcf_pri->fcf_rec.priority)
2028 phba->fcf.eligible_fcf_cnt--;
2029 list_del_init(&new_fcf_pri->list);
2030 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2031 }
2032 spin_unlock_irq(&phba->hbalock);
2033}
2034
2035/**
2036 * lpfc_sli4_set_fcf_flogi_fail
2037 * @phba: pointer to lpfc hba data structure.
2038 * @fcf_index the index of the fcf record to update
2039 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2040 * flag so the the round robin slection for the particular priority level
2041 * will try a different fcf record that does not have this bit set.
2042 * If the fcf record is re-read for any reason this flag is cleared brfore
2043 * adding it to the priority list.
2044 **/
2045void
2046lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2047{
2048 struct lpfc_fcf_pri *new_fcf_pri;
2049 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2050 spin_lock_irq(&phba->hbalock);
2051 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2052 spin_unlock_irq(&phba->hbalock);
2053}
2054
2055/**
2056 * lpfc_sli4_fcf_pri_list_add
2057 * @phba: pointer to lpfc hba data structure.
2058 * @fcf_index the index of the fcf record to add
2059 * This routine checks the priority of the fcf_index to be added.
2060 * If it is a lower priority than the current head of the fcf_pri list
2061 * then it is added to the list in the right order.
2062 * If it is the same priority as the current head of the list then it
2063 * is added to the head of the list and its bit in the rr_bmask is set.
2064 * If the fcf_index to be added is of a higher priority than the current
2065 * head of the list then the rr_bmask is cleared, its bit is set in the
2066 * rr_bmask and it is added to the head of the list.
2067 * returns:
2068 * 0=success 1=failure
2069 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002070static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2071 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002072 struct fcf_record *new_fcf_record)
2073{
2074 uint16_t current_fcf_pri;
2075 uint16_t last_index;
2076 struct lpfc_fcf_pri *fcf_pri;
2077 struct lpfc_fcf_pri *next_fcf_pri;
2078 struct lpfc_fcf_pri *new_fcf_pri;
2079 int ret;
2080
2081 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2082 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2083 "3059 adding idx x%x pri x%x flg x%x\n",
2084 fcf_index, new_fcf_record->fip_priority,
2085 new_fcf_pri->fcf_rec.flag);
2086 spin_lock_irq(&phba->hbalock);
2087 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2088 list_del_init(&new_fcf_pri->list);
2089 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2090 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2091 if (list_empty(&phba->fcf.fcf_pri_list)) {
2092 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2093 ret = lpfc_sli4_fcf_rr_index_set(phba,
2094 new_fcf_pri->fcf_rec.fcf_index);
2095 goto out;
2096 }
2097
2098 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2099 LPFC_SLI4_FCF_TBL_INDX_MAX);
2100 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2101 ret = 0; /* Empty rr list */
2102 goto out;
2103 }
2104 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2105 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2106 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2107 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2108 memset(phba->fcf.fcf_rr_bmask, 0,
2109 sizeof(*phba->fcf.fcf_rr_bmask));
2110 /* fcfs_at_this_priority_level = 1; */
2111 phba->fcf.eligible_fcf_cnt = 1;
2112 } else
2113 /* fcfs_at_this_priority_level++; */
2114 phba->fcf.eligible_fcf_cnt++;
2115 ret = lpfc_sli4_fcf_rr_index_set(phba,
2116 new_fcf_pri->fcf_rec.fcf_index);
2117 goto out;
2118 }
2119
2120 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2121 &phba->fcf.fcf_pri_list, list) {
2122 if (new_fcf_pri->fcf_rec.priority <=
2123 fcf_pri->fcf_rec.priority) {
2124 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2125 list_add(&new_fcf_pri->list,
2126 &phba->fcf.fcf_pri_list);
2127 else
2128 list_add(&new_fcf_pri->list,
2129 &((struct lpfc_fcf_pri *)
2130 fcf_pri->list.prev)->list);
2131 ret = 0;
2132 goto out;
2133 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2134 || new_fcf_pri->fcf_rec.priority <
2135 next_fcf_pri->fcf_rec.priority) {
2136 list_add(&new_fcf_pri->list, &fcf_pri->list);
2137 ret = 0;
2138 goto out;
2139 }
2140 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2141 continue;
2142
2143 }
2144 ret = 1;
2145out:
2146 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2147 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2148 spin_unlock_irq(&phba->hbalock);
2149 return ret;
2150}
2151
2152/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002153 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2154 * @phba: pointer to lpfc hba data structure.
2155 * @mboxq: pointer to mailbox object.
2156 *
2157 * This function iterates through all the fcf records available in
2158 * HBA and chooses the optimal FCF record for discovery. After finding
2159 * the FCF for discovery it registers the FCF record and kicks start
2160 * discovery.
2161 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2162 * use an FCF record which matches fabric name and mac address of the
2163 * currently used FCF record.
2164 * If the driver supports only one FCF, it will try to use the FCF record
2165 * used by BOOT_BIOS.
2166 */
2167void
2168lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2169{
2170 struct fcf_record *new_fcf_record;
2171 uint32_t boot_flag, addr_mode;
2172 uint16_t fcf_index, next_fcf_index;
2173 struct lpfc_fcf_rec *fcf_rec = NULL;
2174 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002175 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002176 int rc;
2177
2178 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002179 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002180 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2181 return;
2182 }
2183
2184 /* Parse the FCF record from the non-embedded mailbox command */
2185 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2186 &next_fcf_index);
2187 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002188 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002189 "2765 Mailbox command READ_FCF_RECORD "
2190 "failed to retrieve a FCF record.\n");
2191 /* Let next new FCF event trigger fast failover */
2192 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002193 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002194 spin_unlock_irq(&phba->hbalock);
2195 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2196 return;
2197 }
2198
2199 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002200 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2201 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002202
2203 /* Log the FCF record information if turned on */
2204 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2205 next_fcf_index);
2206
James Smart6fb120a2009-05-22 14:52:59 -04002207 /*
2208 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002209 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002210 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002211 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002213 lpfc_sli4_fcf_pri_list_del(phba,
2214 bf_get(lpfc_fcf_record_fcf_index,
2215 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002216 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002217 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002218 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002219 bf_get(lpfc_fcf_record_fcf_index,
2220 new_fcf_record),
2221 bf_get(lpfc_fcf_record_fcf_avail,
2222 new_fcf_record),
2223 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002224 new_fcf_record),
2225 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002226 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002227 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2228 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2229 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002230 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2231 phba->fcf.current_rec.fcf_indx) {
2232 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2233 "2862 FCF (x%x) matches property "
2234 "of in-use FCF (x%x)\n",
2235 bf_get(lpfc_fcf_record_fcf_index,
2236 new_fcf_record),
2237 phba->fcf.current_rec.fcf_indx);
2238 goto read_next_fcf;
2239 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002240 /*
2241 * In case the current in-use FCF record becomes
2242 * invalid/unavailable during FCF discovery that
2243 * was not triggered by fast FCF failover process,
2244 * treat it as fast FCF failover.
2245 */
2246 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2247 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2248 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2249 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002250 "(x%x), enter FCF failover "
2251 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002252 phba->fcf.current_rec.fcf_indx);
2253 spin_lock_irq(&phba->hbalock);
2254 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2255 spin_unlock_irq(&phba->hbalock);
2256 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2257 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2258 LPFC_FCOE_FCF_GET_FIRST);
2259 return;
2260 }
2261 }
James Smart6fb120a2009-05-22 14:52:59 -04002262 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002263 } else {
2264 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002265 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2266 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002267 if (rc)
2268 goto read_next_fcf;
2269 }
2270
James Smart6fb120a2009-05-22 14:52:59 -04002271 /*
2272 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002273 * FCF record for the discovery. The condition that a rescan
2274 * matches the in-use FCF record: fabric name, switch name, mac
2275 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002276 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002277 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002278 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002279 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2280 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002281 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002282 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2283 phba->fcf.current_rec.fcf_indx) {
2284 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2285 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2286 /* Stop FCF redisc wait timer */
2287 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2288 phba);
2289 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2290 /* Fast failover, mark completed */
2291 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2292 spin_unlock_irq(&phba->hbalock);
2293 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2294 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002295 "FCF (x%x), port_state:x%x, "
2296 "fc_flag:x%x\n",
2297 phba->fcf.current_rec.fcf_indx,
2298 phba->pport->port_state,
2299 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002300 goto out;
2301 } else
2302 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2303 "2863 New FCF (x%x) matches "
2304 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002305 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002306 new_fcf_record),
2307 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002308 }
James Smartecfd03c2010-02-12 14:41:27 -05002309 /*
2310 * Read next FCF record from HBA searching for the matching
2311 * with in-use record only if not during the fast failover
2312 * period. In case of fast failover period, it shall try to
2313 * determine whether the FCF record just read should be the
2314 * next candidate.
2315 */
2316 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002317 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002318 goto read_next_fcf;
2319 }
James Smart6fb120a2009-05-22 14:52:59 -04002320 }
James Smartecfd03c2010-02-12 14:41:27 -05002321 /*
2322 * Update on failover FCF record only if it's in FCF fast-failover
2323 * period; otherwise, update on current FCF record.
2324 */
James Smartfc2b9892010-02-26 14:15:29 -05002325 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2326 fcf_rec = &phba->fcf.failover_rec;
2327 else
James Smartecfd03c2010-02-12 14:41:27 -05002328 fcf_rec = &phba->fcf.current_rec;
2329
James Smart6fb120a2009-05-22 14:52:59 -04002330 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2331 /*
James Smartecfd03c2010-02-12 14:41:27 -05002332 * If the driver FCF record does not have boot flag
2333 * set and new hba fcf record has boot flag set, use
2334 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002335 */
James Smartecfd03c2010-02-12 14:41:27 -05002336 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2337 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002338 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2339 "2837 Update current FCF record "
2340 "(x%x) with new FCF record (x%x)\n",
2341 fcf_rec->fcf_indx,
2342 bf_get(lpfc_fcf_record_fcf_index,
2343 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002344 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2345 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002346 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002347 goto read_next_fcf;
2348 }
2349 /*
James Smartecfd03c2010-02-12 14:41:27 -05002350 * If the driver FCF record has boot flag set and the
2351 * new hba FCF record does not have boot flag, read
2352 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002353 */
James Smartecfd03c2010-02-12 14:41:27 -05002354 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002355 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002356 goto read_next_fcf;
2357 }
2358 /*
James Smartecfd03c2010-02-12 14:41:27 -05002359 * If the new hba FCF record has lower priority value
2360 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002361 */
James Smartfc2b9892010-02-26 14:15:29 -05002362 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002363 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002364 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2365 "2838 Update current FCF record "
2366 "(x%x) with new FCF record (x%x)\n",
2367 fcf_rec->fcf_indx,
2368 bf_get(lpfc_fcf_record_fcf_index,
2369 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002370 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2371 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002372 /* Reset running random FCF selection count */
2373 phba->fcf.eligible_fcf_cnt = 1;
2374 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2375 /* Update running random FCF selection count */
2376 phba->fcf.eligible_fcf_cnt++;
2377 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2378 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002379 if (select_new_fcf) {
2380 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2381 "2839 Update current FCF record "
2382 "(x%x) with new FCF record (x%x)\n",
2383 fcf_rec->fcf_indx,
2384 bf_get(lpfc_fcf_record_fcf_index,
2385 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002386 /* Choose the new FCF by random selection */
2387 __lpfc_update_fcf_record(phba, fcf_rec,
2388 new_fcf_record,
2389 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002390 }
James Smart6fb120a2009-05-22 14:52:59 -04002391 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002392 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002393 goto read_next_fcf;
2394 }
2395 /*
James Smartecfd03c2010-02-12 14:41:27 -05002396 * This is the first suitable FCF record, choose this record for
2397 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002398 */
James Smartecfd03c2010-02-12 14:41:27 -05002399 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002400 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002401 "2840 Update initial FCF candidate "
2402 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002403 bf_get(lpfc_fcf_record_fcf_index,
2404 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002405 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2406 addr_mode, vlan_id, (boot_flag ?
2407 BOOT_ENABLE : 0));
2408 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002409 /* Setup initial running random FCF selection count */
2410 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002411 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002412 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002413 goto read_next_fcf;
2414
2415read_next_fcf:
2416 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002417 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2418 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2419 /*
2420 * Case of FCF fast failover scan
2421 */
2422
2423 /*
2424 * It has not found any suitable FCF record, cancel
2425 * FCF scan inprogress, and do nothing
2426 */
2427 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002428 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002429 "2782 No suitable FCF found: "
2430 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002431 phba->fcoe_eventtag_at_fcf_scan,
2432 bf_get(lpfc_fcf_record_fcf_index,
2433 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002434 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002435 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2436 phba->hba_flag &= ~FCF_TS_INPROG;
2437 spin_unlock_irq(&phba->hbalock);
2438 /* Unregister in-use FCF and rescan */
2439 lpfc_printf_log(phba, KERN_INFO,
2440 LOG_FIP,
2441 "2864 On devloss tmo "
2442 "unreg in-use FCF and "
2443 "rescan FCF table\n");
2444 lpfc_unregister_fcf_rescan(phba);
2445 return;
2446 }
2447 /*
2448 * Let next new FCF event trigger fast failover
2449 */
2450 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002451 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002452 return;
2453 }
2454 /*
2455 * It has found a suitable FCF record that is not
2456 * the same as in-use FCF record, unregister the
2457 * in-use FCF record, replace the in-use FCF record
2458 * with the new FCF record, mark FCF fast failover
2459 * completed, and then start register the new FCF
2460 * record.
2461 */
2462
James Smart0c9ab6f2010-02-26 14:15:57 -05002463 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002464 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002465
2466 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002467 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002468 "2842 Replace in-use FCF (x%x) "
2469 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002470 phba->fcf.current_rec.fcf_indx,
2471 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002472 memcpy(&phba->fcf.current_rec,
2473 &phba->fcf.failover_rec,
2474 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002475 /*
2476 * Mark the fast FCF failover rediscovery completed
2477 * and the start of the first round of the roundrobin
2478 * FCF failover.
2479 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002480 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002481 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002482 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002483 /* Register to the new FCF record */
2484 lpfc_register_fcf(phba);
2485 } else {
2486 /*
2487 * In case of transaction period to fast FCF failover,
2488 * do nothing when search to the end of the FCF table.
2489 */
2490 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2491 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2492 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002493
James Smart7d791df2011-07-22 18:37:52 -04002494 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2495 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002496 /*
2497 * In case the current in-use FCF record no
2498 * longer existed during FCF discovery that
2499 * was not triggered by fast FCF failover
2500 * process, treat it as fast FCF failover.
2501 */
2502 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2503 "2841 In-use FCF record (x%x) "
2504 "not reported, entering fast "
2505 "FCF failover mode scanning.\n",
2506 phba->fcf.current_rec.fcf_indx);
2507 spin_lock_irq(&phba->hbalock);
2508 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2509 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002510 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2511 LPFC_FCOE_FCF_GET_FIRST);
2512 return;
2513 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002514 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002515 lpfc_register_fcf(phba);
2516 }
2517 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002518 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002519 return;
2520
2521out:
2522 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2523 lpfc_register_fcf(phba);
2524
2525 return;
2526}
2527
2528/**
James Smarta93ff372010-10-22 11:06:08 -04002529 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002530 * @phba: pointer to lpfc hba data structure.
2531 * @mboxq: pointer to mailbox object.
2532 *
James Smarta93ff372010-10-22 11:06:08 -04002533 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002534 * read FCF record mailbox command from the eligible FCF record bmask for
2535 * performing the failover. If the FCF read back is not valid/available, it
2536 * fails through to retrying FLOGI to the currently registered FCF again.
2537 * Otherwise, if the FCF read back is valid and available, it will set the
2538 * newly read FCF record to the failover FCF record, unregister currently
2539 * registered FCF record, copy the failover FCF record to the current
2540 * FCF record, and then register the current FCF record before proceeding
2541 * to trying FLOGI on the new failover FCF.
2542 */
2543void
2544lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2545{
2546 struct fcf_record *new_fcf_record;
2547 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002548 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002549 uint16_t current_fcf_index;
2550 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002551 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002552
James Smarta93ff372010-10-22 11:06:08 -04002553 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002554 if (phba->link_state < LPFC_LINK_UP) {
2555 spin_lock_irq(&phba->hbalock);
2556 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002557 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002558 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002559 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002560 }
2561
2562 /* Parse the FCF record from the non-embedded mailbox command */
2563 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2564 &next_fcf_index);
2565 if (!new_fcf_record) {
2566 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2567 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002568 "failed to retrieve a FCF record. "
2569 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2570 phba->fcf.fcf_flag);
2571 lpfc_unregister_fcf_rescan(phba);
2572 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002573 }
2574
2575 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002576 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2577 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002578
2579 /* Log the FCF record information if turned on */
2580 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2581 next_fcf_index);
2582
James Smarta93ff372010-10-22 11:06:08 -04002583 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2584 if (!rc) {
2585 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2586 "2848 Remove ineligible FCF (x%x) from "
2587 "from roundrobin bmask\n", fcf_index);
2588 /* Clear roundrobin bmask bit for ineligible FCF */
2589 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2590 /* Perform next round of roundrobin FCF failover */
2591 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2592 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2593 if (rc)
2594 goto out;
2595 goto error_out;
2596 }
2597
2598 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2599 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2600 "2760 Perform FLOGI roundrobin FCF failover: "
2601 "FCF (x%x) back to FCF (x%x)\n",
2602 phba->fcf.current_rec.fcf_indx, fcf_index);
2603 /* Wait 500 ms before retrying FLOGI to current FCF */
2604 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002605 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002606 goto out;
2607 }
2608
James Smart0c9ab6f2010-02-26 14:15:57 -05002609 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002610 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002611 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2612 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002613 spin_lock_irq(&phba->hbalock);
2614 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2615 new_fcf_record, addr_mode, vlan_id,
2616 (boot_flag ? BOOT_ENABLE : 0));
2617 spin_unlock_irq(&phba->hbalock);
2618
2619 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2620
2621 /* Unregister the current in-use FCF record */
2622 lpfc_unregister_fcf(phba);
2623
2624 /* Replace in-use record with the new record */
2625 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2626 sizeof(struct lpfc_fcf_rec));
2627
2628 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002629 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2630 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002631
James Smarta93ff372010-10-22 11:06:08 -04002632error_out:
2633 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002634out:
2635 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002636}
2637
2638/**
2639 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2640 * @phba: pointer to lpfc hba data structure.
2641 * @mboxq: pointer to mailbox object.
2642 *
2643 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002644 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002645 * failover when a new FCF event happened. If the FCF read back is
2646 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002647 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002648 */
2649void
2650lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2651{
2652 struct fcf_record *new_fcf_record;
2653 uint32_t boot_flag, addr_mode;
2654 uint16_t fcf_index, next_fcf_index;
2655 uint16_t vlan_id;
2656 int rc;
2657
2658 /* If link state is not up, no need to proceed */
2659 if (phba->link_state < LPFC_LINK_UP)
2660 goto out;
2661
2662 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002663 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002664 goto out;
2665
2666 /* Parse the FCF record from the non-embedded mailbox command */
2667 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2668 &next_fcf_index);
2669 if (!new_fcf_record) {
2670 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2671 "2767 Mailbox command READ_FCF_RECORD "
2672 "failed to retrieve a FCF record.\n");
2673 goto out;
2674 }
2675
2676 /* Check the connection list for eligibility */
2677 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2678 &addr_mode, &vlan_id);
2679
2680 /* Log the FCF record information if turned on */
2681 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2682 next_fcf_index);
2683
2684 if (!rc)
2685 goto out;
2686
2687 /* Update the eligible FCF record index bmask */
2688 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002689
2690 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002691
2692out:
2693 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2694}
2695
2696/**
James Smart76a95d72010-11-20 23:11:48 -05002697 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2698 * @phba: pointer to lpfc hba data structure.
2699 * @mboxq: pointer to mailbox data structure.
2700 *
2701 * This function handles completion of init vfi mailbox command.
2702 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002703static void
James Smart76a95d72010-11-20 23:11:48 -05002704lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2705{
2706 struct lpfc_vport *vport = mboxq->vport;
2707
James Smartdf9e1b52011-12-13 13:22:17 -05002708 /*
2709 * VFI not supported on interface type 0, just do the flogi
2710 * Also continue if the VFI is in use - just use the same one.
2711 */
2712 if (mboxq->u.mb.mbxStatus &&
2713 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2714 LPFC_SLI_INTF_IF_TYPE_0) &&
2715 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002716 lpfc_printf_vlog(vport, KERN_ERR,
2717 LOG_MBOX,
2718 "2891 Init VFI mailbox failed 0x%x\n",
2719 mboxq->u.mb.mbxStatus);
2720 mempool_free(mboxq, phba->mbox_mem_pool);
2721 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2722 return;
2723 }
James Smart73d91e52011-10-10 21:32:10 -04002724
James Smart76a95d72010-11-20 23:11:48 -05002725 lpfc_initial_flogi(vport);
2726 mempool_free(mboxq, phba->mbox_mem_pool);
2727 return;
2728}
2729
2730/**
2731 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2732 * @vport: pointer to lpfc_vport data structure.
2733 *
2734 * This function issue a init_vfi mailbox command to initialize the VFI and
2735 * VPI for the physical port.
2736 */
2737void
2738lpfc_issue_init_vfi(struct lpfc_vport *vport)
2739{
2740 LPFC_MBOXQ_t *mboxq;
2741 int rc;
2742 struct lpfc_hba *phba = vport->phba;
2743
2744 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2745 if (!mboxq) {
2746 lpfc_printf_vlog(vport, KERN_ERR,
2747 LOG_MBOX, "2892 Failed to allocate "
2748 "init_vfi mailbox\n");
2749 return;
2750 }
2751 lpfc_init_vfi(mboxq, vport);
2752 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2753 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2754 if (rc == MBX_NOT_FINISHED) {
2755 lpfc_printf_vlog(vport, KERN_ERR,
2756 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2757 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2758 }
2759}
2760
2761/**
James Smart1c6834a2009-07-19 10:01:26 -04002762 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2763 * @phba: pointer to lpfc hba data structure.
2764 * @mboxq: pointer to mailbox data structure.
2765 *
2766 * This function handles completion of init vpi mailbox command.
2767 */
James Smart695a8142010-01-26 23:08:03 -05002768void
James Smart1c6834a2009-07-19 10:01:26 -04002769lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2770{
2771 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002772 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002773 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2774
James Smart1c6834a2009-07-19 10:01:26 -04002775 if (mboxq->u.mb.mbxStatus) {
2776 lpfc_printf_vlog(vport, KERN_ERR,
2777 LOG_MBOX,
2778 "2609 Init VPI mailbox failed 0x%x\n",
2779 mboxq->u.mb.mbxStatus);
2780 mempool_free(mboxq, phba->mbox_mem_pool);
2781 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2782 return;
2783 }
James Smart72100cc2010-02-12 14:43:01 -05002784 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002785 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002786 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002787
James Smart695a8142010-01-26 23:08:03 -05002788 /* If this port is physical port or FDISC is done, do reg_vpi */
2789 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2790 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2791 if (!ndlp)
2792 lpfc_printf_vlog(vport, KERN_ERR,
2793 LOG_DISCOVERY,
2794 "2731 Cannot find fabric "
2795 "controller node\n");
2796 else
2797 lpfc_register_new_vport(phba, vport, ndlp);
2798 mempool_free(mboxq, phba->mbox_mem_pool);
2799 return;
2800 }
2801
James Smart1c6834a2009-07-19 10:01:26 -04002802 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2803 lpfc_initial_fdisc(vport);
2804 else {
2805 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002806 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2807 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002808 }
James Smart695a8142010-01-26 23:08:03 -05002809 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002810 return;
2811}
2812
2813/**
James Smartecfd03c2010-02-12 14:41:27 -05002814 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2815 * @vport: pointer to lpfc_vport data structure.
2816 *
2817 * This function issue a init_vpi mailbox command to initialize
2818 * VPI for the vport.
2819 */
2820void
2821lpfc_issue_init_vpi(struct lpfc_vport *vport)
2822{
2823 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002824 int rc, vpi;
2825
2826 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2827 vpi = lpfc_alloc_vpi(vport->phba);
2828 if (!vpi) {
2829 lpfc_printf_vlog(vport, KERN_ERR,
2830 LOG_MBOX,
2831 "3303 Failed to obtain vport vpi\n");
2832 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2833 return;
2834 }
2835 vport->vpi = vpi;
2836 }
James Smartecfd03c2010-02-12 14:41:27 -05002837
2838 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2839 if (!mboxq) {
2840 lpfc_printf_vlog(vport, KERN_ERR,
2841 LOG_MBOX, "2607 Failed to allocate "
2842 "init_vpi mailbox\n");
2843 return;
2844 }
2845 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2846 mboxq->vport = vport;
2847 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2848 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2849 if (rc == MBX_NOT_FINISHED) {
2850 lpfc_printf_vlog(vport, KERN_ERR,
2851 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2852 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2853 }
2854}
2855
2856/**
James Smart6fb120a2009-05-22 14:52:59 -04002857 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2858 * @phba: pointer to lpfc hba data structure.
2859 *
2860 * This function loops through the list of vports on the @phba and issues an
2861 * FDISC if possible.
2862 */
2863void
2864lpfc_start_fdiscs(struct lpfc_hba *phba)
2865{
2866 struct lpfc_vport **vports;
2867 int i;
2868
2869 vports = lpfc_create_vport_work_array(phba);
2870 if (vports != NULL) {
2871 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2872 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2873 continue;
2874 /* There are no vpi for this vport */
2875 if (vports[i]->vpi > phba->max_vpi) {
2876 lpfc_vport_set_state(vports[i],
2877 FC_VPORT_FAILED);
2878 continue;
2879 }
James Smart76a95d72010-11-20 23:11:48 -05002880 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002881 lpfc_vport_set_state(vports[i],
2882 FC_VPORT_LINKDOWN);
2883 continue;
2884 }
James Smart1c6834a2009-07-19 10:01:26 -04002885 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002886 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002887 continue;
2888 }
James Smart6fb120a2009-05-22 14:52:59 -04002889 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2890 lpfc_initial_fdisc(vports[i]);
2891 else {
2892 lpfc_vport_set_state(vports[i],
2893 FC_VPORT_NO_FABRIC_SUPP);
2894 lpfc_printf_vlog(vports[i], KERN_ERR,
2895 LOG_ELS,
2896 "0259 No NPIV "
2897 "Fabric support\n");
2898 }
2899 }
2900 }
2901 lpfc_destroy_vport_work_array(phba, vports);
2902}
2903
2904void
2905lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2906{
2907 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2908 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002909 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002910
James Smartf5eca9b2012-03-01 22:37:54 -05002911 /*
2912 * VFI not supported for interface type 0, so ignore any mailbox
2913 * error (except VFI in use) and continue with the discovery.
2914 */
2915 if (mboxq->u.mb.mbxStatus &&
2916 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2917 LPFC_SLI_INTF_IF_TYPE_0) &&
2918 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002919 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2920 "2018 REG_VFI mbxStatus error x%x "
2921 "HBA state x%x\n",
2922 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002923 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002924 /* FLOGI failed, use loop map to make discovery list */
2925 lpfc_disc_list_loopmap(vport);
2926 /* Start discovery */
2927 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002928 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002929 }
2930 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002931 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002932 }
James Smartae05ebe2013-03-01 16:35:38 -05002933
James Smarte74c03c2013-04-17 20:15:19 -04002934 /* If the VFI is already registered, there is nothing else to do
2935 * Unless this was a VFI update and we are in PT2PT mode, then
2936 * we should drop through to set the port state to ready.
2937 */
James Smartae05ebe2013-03-01 16:35:38 -05002938 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002939 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2940 vport->fc_flag & FC_PT2PT))
2941 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002942
James Smartc8685952009-11-18 15:39:16 -05002943 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002944 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002945 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002946 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002947 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002948 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002949 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002950
James Smart1b511972011-12-13 13:23:09 -05002951 /* In case SLI4 FC loopback test, we are ready */
2952 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2953 (phba->link_flag & LS_LOOPBACK_MODE)) {
2954 phba->link_state = LPFC_HBA_READY;
2955 goto out_free_mem;
2956 }
2957
James Smarte74c03c2013-04-17 20:15:19 -04002958 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2959 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2960 "alpacnt:%d LinkState:%x topology:%x\n",
2961 vport->port_state, vport->fc_flag, vport->fc_myDID,
2962 vport->phba->alpa_map[0],
2963 phba->link_state, phba->fc_topology);
2964
James Smart6fb120a2009-05-22 14:52:59 -04002965 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002966 /*
2967 * For private loop or for NPort pt2pt,
2968 * just start discovery and we are done.
2969 */
2970 if ((vport->fc_flag & FC_PT2PT) ||
2971 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2972 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2973
James Smart76a95d72010-11-20 23:11:48 -05002974 /* Use loop map to make discovery list */
2975 lpfc_disc_list_loopmap(vport);
2976 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002977 if (vport->fc_flag & FC_PT2PT)
2978 vport->port_state = LPFC_VPORT_READY;
2979 else
2980 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002981 } else {
2982 lpfc_start_fdiscs(phba);
2983 lpfc_do_scr_ns_plogi(phba, vport);
2984 }
James Smart6fb120a2009-05-22 14:52:59 -04002985 }
2986
James Smart1b511972011-12-13 13:23:09 -05002987out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002988 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002989 if (dmabuf) {
2990 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2991 kfree(dmabuf);
2992 }
James Smart6fb120a2009-05-22 14:52:59 -04002993 return;
2994}
2995
2996static void
James Smart2e0fef82007-06-17 19:56:36 -05002997lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002998{
James Smart6fb120a2009-05-22 14:52:59 -04002999 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003000 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003001 struct lpfc_vport *vport = pmb->vport;
James Smart21bf0b92015-08-31 16:48:21 -04003002 struct serv_parm *sp = &vport->fc_sparam;
3003 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003004
3005 /* Check for error */
3006 if (mb->mbxStatus) {
3007 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003008 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3009 "0319 READ_SPARAM mbxStatus error x%x "
3010 "hba state x%x>\n",
3011 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003012 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003013 goto out;
3014 }
3015
James Smart2e0fef82007-06-17 19:56:36 -05003016 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003017 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003018
3019 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3020 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3021 ed_tov = (ed_tov + 999999) / 1000000;
3022
3023 phba->fc_edtov = ed_tov;
3024 phba->fc_ratov = (2 * ed_tov) / 1000;
3025 if (phba->fc_ratov < FF_DEF_RATOV) {
3026 /* RA_TOV should be atleast 10sec for initial flogi */
3027 phba->fc_ratov = FF_DEF_RATOV;
3028 }
3029
James Smart05580562011-05-24 11:40:48 -04003030 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003031 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3032 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3033 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3034 }
3035
dea31012005-04-17 16:05:31 -05003036 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3037 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003038 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003039 return;
3040
3041out:
3042 pmb->context1 = NULL;
3043 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3044 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003045 lpfc_issue_clear_la(phba, vport);
3046 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003047 return;
3048}
3049
3050static void
James Smart76a95d72010-11-20 23:11:48 -05003051lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003052{
James Smart92d7f7b2007-06-17 19:56:38 -05003053 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003054 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003055 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003056 int i;
James Smart14691152006-12-02 13:34:28 -05003057 struct lpfc_dmabuf *mp;
3058 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003059 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003060 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003061
James Smart92d7f7b2007-06-17 19:56:38 -05003062 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003063 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3064
3065 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3066 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3067 case LPFC_LINK_SPEED_1GHZ:
3068 case LPFC_LINK_SPEED_2GHZ:
3069 case LPFC_LINK_SPEED_4GHZ:
3070 case LPFC_LINK_SPEED_8GHZ:
3071 case LPFC_LINK_SPEED_10GHZ:
3072 case LPFC_LINK_SPEED_16GHZ:
3073 case LPFC_LINK_SPEED_32GHZ:
3074 break;
3075 default:
3076 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3077 break;
3078 }
dea31012005-04-17 16:05:31 -05003079 }
3080
James Smarte74c03c2013-04-17 20:15:19 -04003081 if (phba->fc_topology &&
3082 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3083 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3084 "3314 Toplogy changed was 0x%x is 0x%x\n",
3085 phba->fc_topology,
3086 bf_get(lpfc_mbx_read_top_topology, la));
3087 phba->fc_topology_changed = 1;
3088 }
3089
James Smart76a95d72010-11-20 23:11:48 -05003090 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003091 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003092
James Smartdf9e1b52011-12-13 13:22:17 -05003093 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003094 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003095 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003096
James Smart6a9c52c2009-10-02 15:16:51 -04003097 /* if npiv is enabled and this adapter supports npiv log
3098 * a message that npiv is not supported in this topology
3099 */
3100 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003101 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3102 "1309 Link Up Event npiv not supported in loop "
3103 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003104 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003105 if (bf_get(lpfc_mbx_read_top_il, la))
3106 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003107
James Smart76a95d72010-11-20 23:11:48 -05003108 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3109 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003110
3111 if (i == 0) {
3112 phba->alpa_map[0] = 0;
3113 } else {
James Smarte8b62012007-08-02 11:10:09 -04003114 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003115 int numalpa, j, k;
3116 union {
3117 uint8_t pamap[16];
3118 struct {
3119 uint32_t wd1;
3120 uint32_t wd2;
3121 uint32_t wd3;
3122 uint32_t wd4;
3123 } pa;
3124 } un;
3125 numalpa = phba->alpa_map[0];
3126 j = 0;
3127 while (j < numalpa) {
3128 memset(un.pamap, 0, 16);
3129 for (k = 1; j < numalpa; k++) {
3130 un.pamap[k - 1] =
3131 phba->alpa_map[j + 1];
3132 j++;
3133 if (k == 16)
3134 break;
3135 }
3136 /* Link Up Event ALPA map */
3137 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003138 KERN_WARNING,
3139 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003140 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003141 "ALPA map Data: x%x "
3142 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003143 un.pa.wd1, un.pa.wd2,
3144 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003145 }
3146 }
3147 }
3148 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003149 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003150 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003151 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003152 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3153 }
James Smart2e0fef82007-06-17 19:56:36 -05003154 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003155 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003156 }
James Smart92d7f7b2007-06-17 19:56:38 -05003157 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003158
James Smart38c20672013-03-01 16:37:44 -05003159 if (fc_flags) {
3160 spin_lock_irq(shost->host_lock);
3161 vport->fc_flag |= fc_flags;
3162 spin_unlock_irq(shost->host_lock);
3163 }
3164
dea31012005-04-17 16:05:31 -05003165 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003166 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3167 if (!sparam_mbox)
3168 goto out;
3169
3170 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3171 if (rc) {
3172 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3173 goto out;
3174 }
3175 sparam_mbox->vport = vport;
3176 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3177 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3178 if (rc == MBX_NOT_FINISHED) {
3179 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3180 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3181 kfree(mp);
3182 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3183 goto out;
dea31012005-04-17 16:05:31 -05003184 }
3185
James Smart76a95d72010-11-20 23:11:48 -05003186 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003187 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3188 if (!cfglink_mbox)
3189 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003190 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003191 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003192 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003193 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003194 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003195 if (rc == MBX_NOT_FINISHED) {
3196 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3197 goto out;
3198 }
3199 } else {
James Smart32b97932009-07-19 10:01:21 -04003200 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003201 /*
3202 * Add the driver's default FCF record at FCF index 0 now. This
3203 * is phase 1 implementation that support FCF index 0 and driver
3204 * defaults.
3205 */
James Smart45ed1192009-10-02 15:17:02 -04003206 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003207 fcf_record = kzalloc(sizeof(struct fcf_record),
3208 GFP_KERNEL);
3209 if (unlikely(!fcf_record)) {
3210 lpfc_printf_log(phba, KERN_ERR,
3211 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003212 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003213 "fcf record\n");
3214 rc = -ENODEV;
3215 goto out;
3216 }
3217
3218 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3219 LPFC_FCOE_FCF_DEF_INDEX);
3220 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3221 if (unlikely(rc)) {
3222 lpfc_printf_log(phba, KERN_ERR,
3223 LOG_MBOX | LOG_SLI,
3224 "2013 Could not manually add FCF "
3225 "record 0, status %d\n", rc);
3226 rc = -ENODEV;
3227 kfree(fcf_record);
3228 goto out;
3229 }
3230 kfree(fcf_record);
3231 }
3232 /*
3233 * The driver is expected to do FIP/FCF. Call the port
3234 * and get the FCF Table.
3235 */
James Smart32b97932009-07-19 10:01:21 -04003236 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003237 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003238 spin_unlock_irq(&phba->hbalock);
3239 return;
3240 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003241 /* This is the initial FCF discovery scan */
3242 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003243 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003244 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3245 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003246 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3247 LPFC_FCOE_FCF_GET_FIRST);
3248 if (rc) {
3249 spin_lock_irq(&phba->hbalock);
3250 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3251 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003252 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003253 }
James Smart38b92ef2010-08-04 16:11:39 -04003254 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003255 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003256 }
James Smart6fb120a2009-05-22 14:52:59 -04003257
3258 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003259out:
3260 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003261 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3262 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3263 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003264 lpfc_issue_clear_la(phba, vport);
3265 return;
dea31012005-04-17 16:05:31 -05003266}
3267
3268static void
James Smart84774a42008-08-24 21:50:06 -04003269lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003270{
dea31012005-04-17 16:05:31 -05003271 uint32_t control;
3272 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003273 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003274 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003275 if (phba->sli_rev <= LPFC_SLI_REV3) {
3276 control = readl(phba->HCregaddr);
3277 control |= HC_LAINT_ENA;
3278 writel(control, phba->HCregaddr);
3279 readl(phba->HCregaddr); /* flush */
3280 }
James Smart2e0fef82007-06-17 19:56:36 -05003281 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003282}
3283
James Smart84774a42008-08-24 21:50:06 -04003284static void
3285lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3286{
3287 lpfc_linkdown(phba);
3288 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003289 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003290 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3291}
3292
3293
dea31012005-04-17 16:05:31 -05003294/*
James Smart76a95d72010-11-20 23:11:48 -05003295 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003296 * command upon completion. It is setup in the LPFC_MBOXQ
3297 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003298 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003299 */
3300void
James Smart76a95d72010-11-20 23:11:48 -05003301lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003302{
James Smart2e0fef82007-06-17 19:56:36 -05003303 struct lpfc_vport *vport = pmb->vport;
3304 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003305 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003306 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003307 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003308 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3309
James Smart0d2b6b82008-06-14 22:52:47 -04003310 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003311 pring = lpfc_phba_elsring(phba);
3312 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3313
dea31012005-04-17 16:05:31 -05003314 /* Check for error */
3315 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003316 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003317 "1307 READ_LA mbox error x%x state x%x\n",
3318 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003319 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003320 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003321 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003322 }
3323
James Smart76a95d72010-11-20 23:11:48 -05003324 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003325
3326 memcpy(&phba->alpa_map[0], mp->virt, 128);
3327
James Smart2e0fef82007-06-17 19:56:36 -05003328 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003329 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003330 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003331 else
James Smart2e0fef82007-06-17 19:56:36 -05003332 vport->fc_flag &= ~FC_BYPASSED_MODE;
3333 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003334
James Smartbe0c0082013-03-01 16:38:14 -05003335 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003336 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003337 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003338 if (phba->fc_eventTag != 0)
3339 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003340 }
dea31012005-04-17 16:05:31 -05003341
3342 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003343 if (phba->sli_rev < LPFC_SLI_REV4) {
3344 spin_lock_irq(&phba->hbalock);
3345 if (bf_get(lpfc_mbx_read_top_mm, la))
3346 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3347 else
3348 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3349 spin_unlock_irq(&phba->hbalock);
3350 }
dea31012005-04-17 16:05:31 -05003351
James Smart4d9ab992009-10-02 15:16:39 -04003352 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003353 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003354 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003355 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003356 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003357 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003358 "1306 Link Up Event in loop back mode "
3359 "x%x received Data: x%x x%x x%x x%x\n",
3360 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003361 bf_get(lpfc_mbx_read_top_alpa_granted,
3362 la),
3363 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003364 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003365 } else {
3366 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003367 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003368 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003369 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003370 bf_get(lpfc_mbx_read_top_alpa_granted,
3371 la),
3372 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003373 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003374 bf_get(lpfc_mbx_read_top_mm, la),
3375 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003376 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003377 }
James Smart92d7f7b2007-06-17 19:56:38 -05003378 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003379 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3380 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003381 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003382 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003383 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3384 "1308 Link Down Event in loop back mode "
3385 "x%x received "
3386 "Data: x%x x%x x%x\n",
3387 la->eventTag, phba->fc_eventTag,
3388 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003389 else
James Smart3163f722008-02-08 18:50:25 -05003390 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003391 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003392 "Data: x%x x%x x%x x%x x%x\n",
3393 la->eventTag, phba->fc_eventTag,
3394 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003395 bf_get(lpfc_mbx_read_top_mm, la),
3396 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003397 lpfc_mbx_issue_link_down(phba);
3398 }
James Smartbe0c0082013-03-01 16:38:14 -05003399 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3400 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003401 if (phba->link_state != LPFC_LINK_DOWN) {
3402 phba->fc_stat.LinkDown++;
3403 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3404 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003405 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003406 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003407 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003408 lpfc_mbx_issue_link_down(phba);
3409 } else
3410 lpfc_enable_la(phba);
3411
3412 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3413 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3414 "Data: x%x x%x x%x\n",
3415 la->eventTag, phba->fc_eventTag,
3416 phba->pport->port_state, vport->fc_flag);
3417 /*
3418 * The cmnd that triggered this will be waiting for this
3419 * signal.
3420 */
3421 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3422 if (phba->wait_4_mlo_maint_flg) {
3423 phba->wait_4_mlo_maint_flg = 0;
3424 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003425 }
James Smart84774a42008-08-24 21:50:06 -04003426 }
3427
James Smartbe0c0082013-03-01 16:38:14 -05003428 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3429 bf_get(lpfc_mbx_read_top_fa, la)) {
3430 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003431 lpfc_issue_clear_la(phba, vport);
3432 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003433 "1311 fa %d\n",
3434 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003435 }
3436
James Smart76a95d72010-11-20 23:11:48 -05003437lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003438 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3439 kfree(mp);
3440 mempool_free(pmb, phba->mbox_mem_pool);
3441 return;
3442}
3443
3444/*
3445 * This routine handles processing a REG_LOGIN mailbox
3446 * command upon completion. It is setup in the LPFC_MBOXQ
3447 * as the completion routine when the command is
3448 * handed off to the SLI layer.
3449 */
3450void
James Smart2e0fef82007-06-17 19:56:36 -05003451lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003452{
James Smart2e0fef82007-06-17 19:56:36 -05003453 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003454 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003455 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003456 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003457
dea31012005-04-17 16:05:31 -05003458 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003459 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003460
James Smartbe6bb942015-04-07 15:07:22 -04003461 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3462 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3463 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003464 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003465 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003466 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3467 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3468
James Smart40426292010-12-15 17:58:10 -05003469 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3470 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003471 /* We rcvd a rscn after issuing this
3472 * mbox reg login, we may have cycled
3473 * back through the state and be
3474 * back at reg login state so this
3475 * mbox needs to be ignored becase
3476 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003477 * process.
James Smartffc95492010-06-07 15:23:17 -04003478 */
3479 spin_lock_irq(shost->host_lock);
3480 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3481 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003482
3483 /*
3484 * We cannot leave the RPI registered because
3485 * if we go thru discovery again for this ndlp
3486 * a subsequent REG_RPI will fail.
3487 */
3488 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3489 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003490 }
3491
3492 /* Call state machine */
3493 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003494
dea31012005-04-17 16:05:31 -05003495 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3496 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003497 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003498 /* decrement the node reference count held for this callback
3499 * function.
3500 */
James Smart329f9bc2007-04-25 09:53:01 -04003501 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003502
3503 return;
3504}
3505
James Smart92d7f7b2007-06-17 19:56:38 -05003506static void
3507lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3508{
James Smart04c68492009-05-22 14:52:52 -04003509 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003510 struct lpfc_vport *vport = pmb->vport;
3511 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3512
3513 switch (mb->mbxStatus) {
3514 case 0x0011:
3515 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003516 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3517 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3518 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003519 break;
James Smart78730cf2010-04-06 15:06:30 -04003520 /* If VPI is busy, reset the HBA */
3521 case 0x9700:
3522 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3523 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3524 vport->vpi, mb->mbxStatus);
3525 if (!(phba->pport->load_flag & FC_UNLOADING))
3526 lpfc_workq_post_event(phba, NULL, NULL,
3527 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003528 }
James Smart72100cc2010-02-12 14:43:01 -05003529 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003530 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003531 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003532 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003533 vport->unreg_vpi_cmpl = VPORT_OK;
3534 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003535 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003536 /*
3537 * This shost reference might have been taken at the beginning of
3538 * lpfc_vport_delete()
3539 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003540 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003541 scsi_host_put(shost);
3542}
3543
James Smartd7c255b2008-08-24 21:50:00 -04003544int
James Smart92d7f7b2007-06-17 19:56:38 -05003545lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3546{
3547 struct lpfc_hba *phba = vport->phba;
3548 LPFC_MBOXQ_t *mbox;
3549 int rc;
3550
3551 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3552 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003553 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003554
3555 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3556 mbox->vport = vport;
3557 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003558 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003559 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003560 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3561 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003562 mempool_free(mbox, phba->mbox_mem_pool);
3563 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003564 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003565 }
James Smartd7c255b2008-08-24 21:50:00 -04003566 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003567}
3568
3569static void
3570lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3571{
3572 struct lpfc_vport *vport = pmb->vport;
3573 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003574 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003575
3576 switch (mb->mbxStatus) {
3577 case 0x0011:
3578 case 0x9601:
3579 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003580 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3581 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3582 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003583 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3584 spin_lock_irq(shost->host_lock);
3585 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3586 spin_unlock_irq(shost->host_lock);
3587 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003588
James Smart01649562017-02-12 13:52:32 -08003589 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -08003590 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -08003591 if (phba->nvmet_support)
3592 lpfc_nvmet_update_targetport(phba);
3593 else
James Smart8c258642017-02-12 13:52:36 -08003594 lpfc_nvme_update_localport(vport);
James Smart8c258642017-02-12 13:52:36 -08003595 }
James Smart92d7f7b2007-06-17 19:56:38 -05003596 goto out;
3597 }
James Smart92d7f7b2007-06-17 19:56:38 -05003598
James Smart72100cc2010-02-12 14:43:01 -05003599 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003600 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003601 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003602 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003603 vport->num_disc_nodes = 0;
3604 /* go thru NPR list and issue ELS PLOGIs */
3605 if (vport->fc_npr_cnt)
3606 lpfc_els_disc_plogi(vport);
3607
3608 if (!vport->num_disc_nodes) {
3609 spin_lock_irq(shost->host_lock);
3610 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3611 spin_unlock_irq(shost->host_lock);
3612 lpfc_can_disctmo(vport);
3613 }
3614 vport->port_state = LPFC_VPORT_READY;
3615
3616out:
3617 mempool_free(pmb, phba->mbox_mem_pool);
3618 return;
3619}
3620
James Smart21e9a0a2009-05-22 14:53:21 -04003621/**
3622 * lpfc_create_static_vport - Read HBA config region to create static vports.
3623 * @phba: pointer to lpfc hba data structure.
3624 *
3625 * This routine issue a DUMP mailbox command for config region 22 to get
3626 * the list of static vports to be created. The function create vports
3627 * based on the information returned from the HBA.
3628 **/
3629void
3630lpfc_create_static_vport(struct lpfc_hba *phba)
3631{
3632 LPFC_MBOXQ_t *pmb = NULL;
3633 MAILBOX_t *mb;
3634 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003635 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003636 struct fc_vport_identifiers vport_id;
3637 struct fc_vport *new_fc_vport;
3638 struct Scsi_Host *shost;
3639 struct lpfc_vport *vport;
3640 uint16_t offset = 0;
3641 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003642 struct lpfc_dmabuf *mp;
3643 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003644
3645 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3646 if (!pmb) {
3647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3648 "0542 lpfc_create_static_vport failed to"
3649 " allocate mailbox memory\n");
3650 return;
3651 }
James Smartcdcc2342012-08-14 14:26:35 -04003652 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003653 mb = &pmb->u.mb;
3654
3655 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3656 if (!vport_info) {
3657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3658 "0543 lpfc_create_static_vport failed to"
3659 " allocate vport_info\n");
3660 mempool_free(pmb, phba->mbox_mem_pool);
3661 return;
3662 }
3663
3664 vport_buff = (uint8_t *) vport_info;
3665 do {
James Smartcdcc2342012-08-14 14:26:35 -04003666 /* free dma buffer from previous round */
3667 if (pmb->context1) {
3668 mp = (struct lpfc_dmabuf *)pmb->context1;
3669 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3670 kfree(mp);
3671 }
James Smart1c6834a2009-07-19 10:01:26 -04003672 if (lpfc_dump_static_vport(phba, pmb, offset))
3673 goto out;
3674
James Smart21e9a0a2009-05-22 14:53:21 -04003675 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003676 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3677 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003678
James Smartcdcc2342012-08-14 14:26:35 -04003679 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003680 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3681 "0544 lpfc_create_static_vport failed to"
3682 " issue dump mailbox command ret 0x%x "
3683 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003684 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003685 goto out;
3686 }
3687
James Smart1c6834a2009-07-19 10:01:26 -04003688 if (phba->sli_rev == LPFC_SLI_REV4) {
3689 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003690 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003691 if (byte_count > sizeof(struct static_vport_info) -
3692 offset)
3693 byte_count = sizeof(struct static_vport_info)
3694 - offset;
3695 memcpy(vport_buff + offset, mp->virt, byte_count);
3696 offset += byte_count;
3697 } else {
3698 if (mb->un.varDmp.word_cnt >
3699 sizeof(struct static_vport_info) - offset)
3700 mb->un.varDmp.word_cnt =
3701 sizeof(struct static_vport_info)
3702 - offset;
3703 byte_count = mb->un.varDmp.word_cnt;
3704 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3705 vport_buff + offset,
3706 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003707
James Smart1c6834a2009-07-19 10:01:26 -04003708 offset += byte_count;
3709 }
James Smart21e9a0a2009-05-22 14:53:21 -04003710
James Smart1c6834a2009-07-19 10:01:26 -04003711 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003712 offset < sizeof(struct static_vport_info));
3713
3714
3715 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3716 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3717 != VPORT_INFO_REV)) {
3718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3719 "0545 lpfc_create_static_vport bad"
3720 " information header 0x%x 0x%x\n",
3721 le32_to_cpu(vport_info->signature),
3722 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3723
3724 goto out;
3725 }
3726
3727 shost = lpfc_shost_from_vport(phba->pport);
3728
3729 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3730 memset(&vport_id, 0, sizeof(vport_id));
3731 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3732 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3733 if (!vport_id.port_name || !vport_id.node_name)
3734 continue;
3735
3736 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3737 vport_id.vport_type = FC_PORTTYPE_NPIV;
3738 vport_id.disable = false;
3739 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3740
3741 if (!new_fc_vport) {
3742 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3743 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003744 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003745 continue;
3746 }
3747
3748 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3749 vport->vport_flag |= STATIC_VPORT;
3750 }
3751
3752out:
James Smart21e9a0a2009-05-22 14:53:21 -04003753 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003754 if (mbx_wait_rc != MBX_TIMEOUT) {
3755 if (pmb->context1) {
3756 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003757 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3758 kfree(mp);
3759 }
James Smart21e9a0a2009-05-22 14:53:21 -04003760 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003761 }
James Smart21e9a0a2009-05-22 14:53:21 -04003762
3763 return;
3764}
3765
dea31012005-04-17 16:05:31 -05003766/*
3767 * This routine handles processing a Fabric REG_LOGIN mailbox
3768 * command upon completion. It is setup in the LPFC_MBOXQ
3769 * as the completion routine when the command is
3770 * handed off to the SLI layer.
3771 */
3772void
James Smart2e0fef82007-06-17 19:56:36 -05003773lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003774{
James Smart92d7f7b2007-06-17 19:56:38 -05003775 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003776 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003777 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003778 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003779 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003780
James Smart549e55c2007-08-02 11:09:51 -04003781 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003782 pmb->context1 = NULL;
3783 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003784
dea31012005-04-17 16:05:31 -05003785 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003786 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3787 "0258 Register Fabric login error: 0x%x\n",
3788 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003789 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3790 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003791 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003792
James Smart76a95d72010-11-20 23:11:48 -05003793 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003794 /* FLOGI failed, use loop map to make discovery list */
3795 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003796
James Smart92d7f7b2007-06-17 19:56:38 -05003797 /* Start discovery */
3798 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003799 /* Decrement the reference count to ndlp after the
3800 * reference to the ndlp are done.
3801 */
3802 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003803 return;
3804 }
3805
3806 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003807 /* Decrement the reference count to ndlp after the reference
3808 * to the ndlp are done.
3809 */
3810 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003811 return;
3812 }
3813
James Smart6d368e52011-05-24 11:44:12 -04003814 if (phba->sli_rev < LPFC_SLI_REV4)
3815 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003816 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003817 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003818 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003819
James Smart2e0fef82007-06-17 19:56:36 -05003820 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003821 /* when physical port receive logo donot start
3822 * vport discovery */
3823 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3824 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003825 else {
3826 shost = lpfc_shost_from_vport(vport);
3827 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003828 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003829 spin_unlock_irq(shost->host_lock);
3830 }
James Smart92d7f7b2007-06-17 19:56:38 -05003831 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003832 }
3833
3834 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3835 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003836 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003837
3838 /* Drop the reference count from the mbox at the end after
3839 * all the current reference to the ndlp have been done.
3840 */
3841 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003842 return;
3843}
3844
James Smarta0f2d3e2017-02-12 13:52:31 -08003845 /*
3846 * This routine will issue a GID_FT for each FC4 Type supported
3847 * by the driver. ALL GID_FTs must complete before discovery is started.
3848 */
3849int
3850lpfc_issue_gidft(struct lpfc_vport *vport)
3851{
3852 struct lpfc_hba *phba = vport->phba;
3853
3854 /* Good status, issue CT Request to NameServer */
3855 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3856 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3857 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3858 /* Cannot issue NameServer FCP Query, so finish up
3859 * discovery
3860 */
3861 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3862 "0604 %s FC TYPE %x %s\n",
3863 "Failed to issue GID_FT to ",
3864 FC_TYPE_FCP,
3865 "Finishing discovery.");
3866 return 0;
3867 }
3868 vport->gidft_inp++;
3869 }
3870
3871 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3872 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3873 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3874 /* Cannot issue NameServer NVME Query, so finish up
3875 * discovery
3876 */
3877 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3878 "0605 %s FC_TYPE %x %s %d\n",
3879 "Failed to issue GID_FT to ",
3880 FC_TYPE_NVME,
3881 "Finishing discovery: gidftinp ",
3882 vport->gidft_inp);
3883 if (vport->gidft_inp == 0)
3884 return 0;
3885 } else
3886 vport->gidft_inp++;
3887 }
3888 return vport->gidft_inp;
3889}
3890
dea31012005-04-17 16:05:31 -05003891/*
3892 * This routine handles processing a NameServer REG_LOGIN mailbox
3893 * command upon completion. It is setup in the LPFC_MBOXQ
3894 * as the completion routine when the command is
3895 * handed off to the SLI layer.
3896 */
3897void
James Smart2e0fef82007-06-17 19:56:36 -05003898lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003899{
James Smart21e9a0a2009-05-22 14:53:21 -04003900 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003901 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3902 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3903 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003904
James Smartd439d282010-09-29 11:18:45 -04003905 pmb->context1 = NULL;
3906 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003907 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003908
dea31012005-04-17 16:05:31 -05003909 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003910 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3911 "0260 Register NameServer error: 0x%x\n",
3912 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003913
3914out:
James Smartfa4066b2008-01-11 01:53:27 -05003915 /* decrement the node reference count held for this
3916 * callback function.
3917 */
James Smart329f9bc2007-04-25 09:53:01 -04003918 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003919 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3920 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003921 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003922
3923 /* If no other thread is using the ndlp, free it */
3924 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003925
James Smart76a95d72010-11-20 23:11:48 -05003926 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003927 /*
3928 * RegLogin failed, use loop map to make discovery
3929 * list
3930 */
3931 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003932
James Smart92d7f7b2007-06-17 19:56:38 -05003933 /* Start discovery */
3934 lpfc_disc_start(vport);
3935 return;
3936 }
3937 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003938 return;
3939 }
3940
James Smart6d368e52011-05-24 11:44:12 -04003941 if (phba->sli_rev < LPFC_SLI_REV4)
3942 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003943 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003944 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003945 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003946 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3947 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3948 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003949 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003950 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003951
James Smart2e0fef82007-06-17 19:56:36 -05003952 if (vport->port_state < LPFC_VPORT_READY) {
3953 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003954 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3955 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3956 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3957 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003958
3959 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3960 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3961 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3962
3963 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3964 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08003965 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3966 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003967
3968 /* Issue SCR just before NameServer GID_FT Query */
3969 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003970 }
3971
James Smart2e0fef82007-06-17 19:56:36 -05003972 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003973 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003974 goto out;
dea31012005-04-17 16:05:31 -05003975
James Smarta0f2d3e2017-02-12 13:52:31 -08003976 /*
3977 * At this point in time we may need to wait for multiple
3978 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
3979 *
3980 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05003981 * callback function.
3982 */
James Smart329f9bc2007-04-25 09:53:01 -04003983 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003984 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3985 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003986 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003987
3988 return;
3989}
3990
3991static void
James Smart2e0fef82007-06-17 19:56:36 -05003992lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003993{
James Smart2e0fef82007-06-17 19:56:36 -05003994 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3995 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003996 struct lpfc_rport_data *rdata;
3997 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003998 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003999
James Smart895427b2017-02-12 13:52:30 -08004000 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4001 return;
4002
dea31012005-04-17 16:05:31 -05004003 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004004 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4005 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004006 rport_ids.port_id = ndlp->nlp_DID;
4007 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004008
James Smart329f9bc2007-04-25 09:53:01 -04004009 /*
4010 * We leave our node pointer in rport->dd_data when we unregister a
4011 * FCP target port. But fc_remote_port_add zeros the space to which
4012 * rport->dd_data points. So, if we're reusing a previously
4013 * registered port, drop the reference that we took the last time we
4014 * registered the port.
4015 */
James Smart466e8402015-05-21 13:55:28 -04004016 rport = ndlp->rport;
4017 if (rport) {
4018 rdata = rport->dd_data;
4019 /* break the link before dropping the ref */
4020 ndlp->rport = NULL;
4021 if (rdata && rdata->pnode == ndlp)
4022 lpfc_nlp_put(ndlp);
4023 rdata->pnode = NULL;
4024 /* drop reference for earlier registeration */
4025 put_device(&rport->dev);
4026 }
James Smart858c9f62007-06-17 19:56:39 -05004027
4028 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4029 "rport add: did:x%x flg:x%x type x%x",
4030 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4031
James Smart9589b0622011-04-16 11:03:17 -04004032 /* Don't add the remote port if unloading. */
4033 if (vport->load_flag & FC_UNLOADING)
4034 return;
4035
James Smart2e0fef82007-06-17 19:56:36 -05004036 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004037 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004038 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4039 "Warning: fc_remote_port_add failed\n");
4040 return;
4041 }
4042
4043 /* initialize static port data */
4044 rport->maxframe_size = ndlp->nlp_maxframe;
4045 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004046 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004047 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004048
4049 if (ndlp->nlp_type & NLP_FCP_TARGET)
4050 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4051 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4052 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4053
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004054 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4055 fc_remote_port_rolechg(rport, rport_ids.roles);
4056
James Smart34f5ad82012-08-03 12:35:03 -04004057 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4058 "3183 rport register x%06x, rport %p role x%x\n",
4059 ndlp->nlp_DID, rport, rport_ids.roles);
4060
James Smart071fbd3d2006-04-15 11:53:20 -04004061 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004062 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004063 ndlp->nlp_sid = rport->scsi_target_id;
4064 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004065 return;
4066}
4067
4068static void
James Smart2e0fef82007-06-17 19:56:36 -05004069lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004070{
4071 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004072 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004073 struct lpfc_hba *phba = vport->phba;
4074
4075 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4076 return;
James Smartc01f3202006-08-18 17:47:08 -04004077
James Smart2ea259e2017-02-12 13:52:27 -08004078 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004079 "rport delete: did:x%x flg:x%x type x%x",
4080 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4081
James Smart2ea259e2017-02-12 13:52:27 -08004082 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004083 "3184 rport unregister x%06x, rport %p\n",
4084 ndlp->nlp_DID, rport);
4085
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004086 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004087
4088 return;
4089}
4090
James Smartde0c5b32007-04-25 09:52:27 -04004091static void
James Smart2e0fef82007-06-17 19:56:36 -05004092lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004093{
James Smart2e0fef82007-06-17 19:56:36 -05004094 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4095
4096 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004097 switch (state) {
4098 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004099 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004100 break;
4101 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004102 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004103 break;
4104 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004105 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004106 break;
4107 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004108 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004109 break;
4110 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004111 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004112 break;
4113 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004114 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004115 break;
4116 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004117 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004118 break;
4119 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004120 if (vport->fc_npr_cnt == 0 && count == -1)
4121 vport->fc_npr_cnt = 0;
4122 else
4123 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004124 break;
4125 }
James Smart2e0fef82007-06-17 19:56:36 -05004126 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004127}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004128
James Smartde0c5b32007-04-25 09:52:27 -04004129static void
James Smart2e0fef82007-06-17 19:56:36 -05004130lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004131 int old_state, int new_state)
4132{
James Smart2e0fef82007-06-17 19:56:36 -05004133 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smarta0f2d3e2017-02-12 13:52:31 -08004134 struct lpfc_hba *phba = vport->phba;
James Smart2e0fef82007-06-17 19:56:36 -05004135
James Smartde0c5b32007-04-25 09:52:27 -04004136 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004137 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4138 ndlp->nlp_type |= NLP_FC_NODE;
4139 }
4140 if (new_state == NLP_STE_MAPPED_NODE)
4141 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4142 if (new_state == NLP_STE_NPR_NODE)
4143 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004144
James Smarta0f2d3e2017-02-12 13:52:31 -08004145 /* FCP and NVME Transport interface */
4146 if ((old_state == NLP_STE_MAPPED_NODE ||
4147 old_state == NLP_STE_UNMAPPED_NODE)) {
4148 if (ndlp->rport) {
4149 vport->phba->nport_event_cnt++;
4150 lpfc_unregister_remote_port(ndlp);
4151 }
4152
4153 /* Notify the NVME transport of this rport's loss */
4154 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4155 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
4156 (vport->phba->nvmet_support == 0) &&
4157 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) ||
4158 (ndlp->nlp_DID == Fabric_DID))) {
4159 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004160 lpfc_nvme_unregister_port(vport, ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004161 }
James Smartde0c5b32007-04-25 09:52:27 -04004162 }
dea31012005-04-17 16:05:31 -05004163
James Smarta0f2d3e2017-02-12 13:52:31 -08004164 /* FCP and NVME Transport interfaces */
4165
James Smartde0c5b32007-04-25 09:52:27 -04004166 if (new_state == NLP_STE_MAPPED_NODE ||
4167 new_state == NLP_STE_UNMAPPED_NODE) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004168 if ((ndlp->nlp_fc4_type & NLP_FC4_FCP) ||
4169 (ndlp->nlp_DID == Fabric_DID)) {
4170 vport->phba->nport_event_cnt++;
4171 /*
4172 * Tell the fc transport about the port, if we haven't
4173 * already. If we have, and it's a scsi entity, be
4174 */
4175 lpfc_register_remote_port(vport, ndlp);
4176 }
4177 /* Notify the NVME transport of this new rport. */
4178 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4179 if (vport->phba->nvmet_support == 0) {
4180 /* Register this rport with the transport.
4181 * Initiators take the NDLP ref count in
4182 * the register.
4183 */
4184 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004185 lpfc_nvme_register_port(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08004186 } else {
4187 /* Just take an NDLP ref count since the
4188 * target does not register rports.
4189 */
4190 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004191 }
4192 }
James Smartde0c5b32007-04-25 09:52:27 -04004193 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004194
James Smartea2151b2008-09-07 11:52:10 -04004195 if ((new_state == NLP_STE_MAPPED_NODE) &&
4196 (vport->stat_data_enabled)) {
4197 /*
4198 * A new target is discovered, if there is no buffer for
4199 * statistical data collection allocate buffer.
4200 */
4201 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4202 sizeof(struct lpfc_scsicmd_bkt),
4203 GFP_KERNEL);
4204
4205 if (!ndlp->lat_data)
4206 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4207 "0286 lpfc_nlp_state_cleanup failed to "
4208 "allocate statistical data buffer DID "
4209 "0x%x\n", ndlp->nlp_DID);
4210 }
James Smart858c9f62007-06-17 19:56:39 -05004211 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004212 * If the node just added to Mapped list was an FCP target,
4213 * but the remote port registration failed or assigned a target
4214 * id outside the presentable range - move the node to the
4215 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004216 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004217 if ((new_state == NLP_STE_MAPPED_NODE) &&
4218 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004219 (!ndlp->rport ||
4220 ndlp->rport->scsi_target_id == -1 ||
4221 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004222 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004223 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004224 spin_unlock_irq(shost->host_lock);
4225 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004226 }
James Smartde0c5b32007-04-25 09:52:27 -04004227}
4228
James Smart685f0bf2007-04-25 09:53:08 -04004229static char *
4230lpfc_nlp_state_name(char *buffer, size_t size, int state)
4231{
4232 static char *states[] = {
4233 [NLP_STE_UNUSED_NODE] = "UNUSED",
4234 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4235 [NLP_STE_ADISC_ISSUE] = "ADISC",
4236 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4237 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004238 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004239 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4240 [NLP_STE_MAPPED_NODE] = "MAPPED",
4241 [NLP_STE_NPR_NODE] = "NPR",
4242 };
4243
James Smart311464e2007-08-02 11:10:37 -04004244 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004245 strlcpy(buffer, states[state], size);
4246 else
4247 snprintf(buffer, size, "unknown (%d)", state);
4248 return buffer;
4249}
4250
James Smartde0c5b32007-04-25 09:52:27 -04004251void
James Smart2e0fef82007-06-17 19:56:36 -05004252lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4253 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004254{
James Smart2e0fef82007-06-17 19:56:36 -05004255 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004256 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004257 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004258
James Smarte8b62012007-08-02 11:10:09 -04004259 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4260 "0904 NPort state transition x%06x, %s -> %s\n",
4261 ndlp->nlp_DID,
4262 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4263 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004264
4265 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4266 "node statechg did:x%x old:%d ste:%d",
4267 ndlp->nlp_DID, old_state, state);
4268
James Smartde0c5b32007-04-25 09:52:27 -04004269 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004270 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004271 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004272 if (old_state == NLP_STE_UNMAPPED_NODE) {
4273 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4274 ndlp->nlp_type &= ~NLP_FC_NODE;
4275 }
4276
James Smart685f0bf2007-04-25 09:53:08 -04004277 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004278 spin_lock_irq(shost->host_lock);
4279 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4280 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004281 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004282 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004283
4284 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004285 lpfc_nlp_counters(vport, state, 1);
4286 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004287}
4288
4289void
James Smarte47c9092008-02-08 18:49:26 -05004290lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4291{
4292 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4293
4294 if (list_empty(&ndlp->nlp_listp)) {
4295 spin_lock_irq(shost->host_lock);
4296 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4297 spin_unlock_irq(shost->host_lock);
4298 }
4299}
4300
4301void
James Smart2e0fef82007-06-17 19:56:36 -05004302lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004303{
James Smart2e0fef82007-06-17 19:56:36 -05004304 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4305
James Smart0d2b6b82008-06-14 22:52:47 -04004306 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004307 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004308 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4309 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004310 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004311 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004312 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004313 NLP_STE_UNUSED_NODE);
4314}
4315
Adrian Bunk4d9db012008-02-14 23:24:02 +02004316static void
James Smarte47c9092008-02-08 18:49:26 -05004317lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4318{
James Smart0d2b6b82008-06-14 22:52:47 -04004319 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004320 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4321 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4322 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4323 NLP_STE_UNUSED_NODE);
4324}
James Smart109f6ed2008-12-04 22:39:08 -05004325/**
James Smart3621a712009-04-06 18:47:14 -04004326 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004327 * @vport: Pointer to Virtual Port object.
4328 * @ndlp: Pointer to FC node object.
4329 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004330 *
4331 * This function is always called when node object need to be initialized.
4332 * It initializes all the fields of the node object. Although the reference
4333 * to phba from @ndlp can be obtained indirectly through it's reference to
4334 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4335 * to the life-span of the @ndlp might go beyond the existence of @vport as
4336 * the final release of ndlp is determined by its reference count. And, the
4337 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004338 **/
4339static inline void
4340lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4341 uint32_t did)
4342{
4343 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4344 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4345 init_timer(&ndlp->nlp_delayfunc);
4346 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4347 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4348 ndlp->nlp_DID = did;
4349 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004350 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004351 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004352 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004353 kref_init(&ndlp->kref);
4354 NLP_INT_NODE_ACT(ndlp);
4355 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004356 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004357}
James Smarte47c9092008-02-08 18:49:26 -05004358
4359struct lpfc_nodelist *
4360lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4361 int state)
4362{
4363 struct lpfc_hba *phba = vport->phba;
4364 uint32_t did;
4365 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004366 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004367
4368 if (!ndlp)
4369 return NULL;
4370
4371 spin_lock_irqsave(&phba->ndlp_lock, flags);
4372 /* The ndlp should not be in memory free mode */
4373 if (NLP_CHK_FREE_REQ(ndlp)) {
4374 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4375 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4376 "0277 lpfc_enable_node: ndlp:x%p "
4377 "usgmap:x%x refcnt:%d\n",
4378 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004379 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004380 return NULL;
4381 }
4382 /* The ndlp should not already be in active mode */
4383 if (NLP_CHK_NODE_ACT(ndlp)) {
4384 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4385 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4386 "0278 lpfc_enable_node: ndlp:x%p "
4387 "usgmap:x%x refcnt:%d\n",
4388 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004389 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004390 return NULL;
4391 }
4392
4393 /* Keep the original DID */
4394 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004395 if (phba->sli_rev == LPFC_SLI_REV4)
4396 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004397
4398 /* re-initialize ndlp except of ndlp linked list pointer */
4399 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4400 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004401 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004402
James Smartcff261f2013-12-17 20:29:47 -05004403 if (phba->sli_rev == LPFC_SLI_REV4)
4404 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4405
James Smarte47c9092008-02-08 18:49:26 -05004406 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004407 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004408 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004409 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4410 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4411 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4412 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004413 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004414 ndlp->nlp_usg_map, ndlp);
4415 }
James Smart725dd392013-10-10 12:23:30 -04004416
James Smarte47c9092008-02-08 18:49:26 -05004417
4418 if (state != NLP_STE_UNUSED_NODE)
4419 lpfc_nlp_set_state(vport, ndlp, state);
4420
4421 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4422 "node enable: did:x%x",
4423 ndlp->nlp_DID, 0, 0);
4424 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004425}
4426
4427void
James Smart2e0fef82007-06-17 19:56:36 -05004428lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004429{
James Smart87af33f2007-10-27 13:37:43 -04004430 /*
James Smartfa4066b2008-01-11 01:53:27 -05004431 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004432 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004433 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4434 * until ALL other outstanding threads have completed. We check
4435 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004436 */
James Smartfa4066b2008-01-11 01:53:27 -05004437 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4438 return;
James Smart51ef4c22007-08-02 11:10:31 -04004439 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004440 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004441 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004442 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004443 }
James Smart02902172015-05-21 13:55:17 -04004444
4445 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004446 return;
dea31012005-04-17 16:05:31 -05004447}
4448
4449/*
4450 * Start / ReStart rescue timer for Discovery / RSCN handling
4451 */
4452void
James Smart2e0fef82007-06-17 19:56:36 -05004453lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004454{
James Smart2e0fef82007-06-17 19:56:36 -05004455 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4456 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004457 uint32_t tmo;
4458
James Smart2e0fef82007-06-17 19:56:36 -05004459 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004460 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004461 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4462 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004463 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004464 * FC spec states we need 3 * ratov for CT requests
4465 */
4466 tmo = ((phba->fc_ratov * 3) + 3);
4467 }
dea31012005-04-17 16:05:31 -05004468
James Smart858c9f62007-06-17 19:56:39 -05004469
4470 if (!timer_pending(&vport->fc_disctmo)) {
4471 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4472 "set disc timer: tmo:x%x state:x%x flg:x%x",
4473 tmo, vport->port_state, vport->fc_flag);
4474 }
4475
James Smart256ec0d2013-04-17 20:14:58 -04004476 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004477 spin_lock_irq(shost->host_lock);
4478 vport->fc_flag |= FC_DISC_TMO;
4479 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004480
4481 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004482 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4483 "0247 Start Discovery Timer state x%x "
4484 "Data: x%x x%lx x%x x%x\n",
4485 vport->port_state, tmo,
4486 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4487 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004488
4489 return;
4490}
4491
4492/*
4493 * Cancel rescue timer for Discovery / RSCN handling
4494 */
4495int
James Smart2e0fef82007-06-17 19:56:36 -05004496lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004497{
James Smart2e0fef82007-06-17 19:56:36 -05004498 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004499 unsigned long iflags;
4500
James Smart858c9f62007-06-17 19:56:39 -05004501 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4502 "can disc timer: state:x%x rtry:x%x flg:x%x",
4503 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4504
dea31012005-04-17 16:05:31 -05004505 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004506 if (vport->fc_flag & FC_DISC_TMO) {
4507 spin_lock_irqsave(shost->host_lock, iflags);
4508 vport->fc_flag &= ~FC_DISC_TMO;
4509 spin_unlock_irqrestore(shost->host_lock, iflags);
4510 del_timer_sync(&vport->fc_disctmo);
4511 spin_lock_irqsave(&vport->work_port_lock, iflags);
4512 vport->work_port_events &= ~WORKER_DISC_TMO;
4513 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004514 }
4515
4516 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004517 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4518 "0248 Cancel Discovery Timer state x%x "
4519 "Data: x%x x%x x%x\n",
4520 vport->port_state, vport->fc_flag,
4521 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004522 return 0;
dea31012005-04-17 16:05:31 -05004523}
4524
4525/*
4526 * Check specified ring for outstanding IOCB on the SLI queue
4527 * Return true if iocb matches the specified nport
4528 */
4529int
James Smart2e0fef82007-06-17 19:56:36 -05004530lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4531 struct lpfc_sli_ring *pring,
4532 struct lpfc_iocbq *iocb,
4533 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004534{
James Smart2e0fef82007-06-17 19:56:36 -05004535 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004536 struct lpfc_vport *vport = ndlp->vport;
4537
4538 if (iocb->vport != vport)
4539 return 0;
4540
dea31012005-04-17 16:05:31 -05004541 if (pring->ringno == LPFC_ELS_RING) {
4542 switch (icmd->ulpCommand) {
4543 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004544 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004545 return 1;
dea31012005-04-17 16:05:31 -05004546 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004547 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4548 return 1;
dea31012005-04-17 16:05:31 -05004549 case CMD_XMIT_ELS_RSP64_CX:
4550 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004551 return 1;
dea31012005-04-17 16:05:31 -05004552 }
James Smart895427b2017-02-12 13:52:30 -08004553 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004554 /* Skip match check if waiting to relogin to FCP target */
4555 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004556 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004557 return 0;
dea31012005-04-17 16:05:31 -05004558 }
4559 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004560 return 1;
dea31012005-04-17 16:05:31 -05004561 }
dea31012005-04-17 16:05:31 -05004562 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004563 return 0;
dea31012005-04-17 16:05:31 -05004564}
4565
James Smart895427b2017-02-12 13:52:30 -08004566static void
4567__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4568 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4569 struct list_head *dequeue_list)
4570{
4571 struct lpfc_iocbq *iocb, *next_iocb;
4572
4573 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4574 /* Check to see if iocb matches the nport */
4575 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4576 /* match, dequeue */
4577 list_move_tail(&iocb->list, dequeue_list);
4578 }
4579}
4580
4581static void
4582lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4583 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4584{
4585 struct lpfc_sli *psli = &phba->sli;
4586 uint32_t i;
4587
4588 spin_lock_irq(&phba->hbalock);
4589 for (i = 0; i < psli->num_rings; i++)
4590 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4591 dequeue_list);
4592 spin_unlock_irq(&phba->hbalock);
4593}
4594
4595static void
4596lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4597 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4598{
4599 struct lpfc_sli_ring *pring;
4600 struct lpfc_queue *qp = NULL;
4601
4602 spin_lock_irq(&phba->hbalock);
4603 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4604 pring = qp->pring;
4605 if (!pring)
4606 continue;
4607 spin_lock_irq(&pring->ring_lock);
4608 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4609 spin_unlock_irq(&pring->ring_lock);
4610 }
4611 spin_unlock_irq(&phba->hbalock);
4612}
4613
dea31012005-04-17 16:05:31 -05004614/*
4615 * Free resources / clean up outstanding I/Os
4616 * associated with nlp_rpi in the LPFC_NODELIST entry.
4617 */
4618static int
James Smart2e0fef82007-06-17 19:56:36 -05004619lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004620{
James Smart2534ba72007-04-25 09:52:20 -04004621 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004622
James Smart92d7f7b2007-06-17 19:56:38 -05004623 lpfc_fabric_abort_nport(ndlp);
4624
dea31012005-04-17 16:05:31 -05004625 /*
4626 * Everything that matches on txcmplq will be returned
4627 * by firmware with a no rpi error.
4628 */
James Smart40426292010-12-15 17:58:10 -05004629 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004630 if (phba->sli_rev != LPFC_SLI_REV4)
4631 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4632 else
4633 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004634 }
James Smart2534ba72007-04-25 09:52:20 -04004635
James Smarta257bf92009-04-06 18:48:10 -04004636 /* Cancel all the IOCBs from the completions list */
4637 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4638 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004639
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004640 return 0;
dea31012005-04-17 16:05:31 -05004641}
4642
James Smart086a3452012-08-14 14:25:21 -04004643/**
4644 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4645 * @phba: Pointer to HBA context object.
4646 * @pmb: Pointer to mailbox object.
4647 *
4648 * This function will issue an ELS LOGO command after completing
4649 * the UNREG_RPI.
4650 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004651static void
James Smart086a3452012-08-14 14:25:21 -04004652lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4653{
4654 struct lpfc_vport *vport = pmb->vport;
4655 struct lpfc_nodelist *ndlp;
4656
4657 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4658 if (!ndlp)
4659 return;
4660 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004661 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004662}
4663
dea31012005-04-17 16:05:31 -05004664/*
4665 * Free rpi associated with LPFC_NODELIST entry.
4666 * This routine is called from lpfc_freenode(), when we are removing
4667 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4668 * LOGO that completes successfully, and we are waiting to PLOGI back
4669 * to the remote NPort. In addition, it is called after we receive
4670 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4671 * we are waiting to PLOGI back to the remote NPort.
4672 */
4673int
James Smart2e0fef82007-06-17 19:56:36 -05004674lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004675{
James Smart2e0fef82007-06-17 19:56:36 -05004676 struct lpfc_hba *phba = vport->phba;
4677 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004678 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004679 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004680
James Smartf454a9a2013-09-06 12:20:07 -04004681 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4682 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4683 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4684 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4685 "3366 RPI x%x needs to be "
4686 "unregistered nlp_flag x%x "
4687 "did x%x\n",
4688 ndlp->nlp_rpi, ndlp->nlp_flag,
4689 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004690 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4691 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004692 /* SLI4 ports require the physical rpi value. */
4693 rpi = ndlp->nlp_rpi;
4694 if (phba->sli_rev == LPFC_SLI_REV4)
4695 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004696
James Smart6d368e52011-05-24 11:44:12 -04004697 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004698 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004699 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4700 mbox->context1 = ndlp;
4701 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4702 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004703 if (phba->sli_rev == LPFC_SLI_REV4 &&
4704 (!(vport->load_flag & FC_UNLOADING)) &&
4705 (bf_get(lpfc_sli_intf_if_type,
4706 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004707 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004708 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004709 mbox->context1 = lpfc_nlp_get(ndlp);
4710 mbox->mbox_cmpl =
4711 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004712 /*
4713 * accept PLOGIs after unreg_rpi_cmpl
4714 */
4715 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004716 } else
4717 mbox->mbox_cmpl =
4718 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004719 }
4720
James Smart0b727fe2007-10-27 13:37:25 -04004721 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004722 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004723 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004724 acc_plogi = 1;
4725 }
dea31012005-04-17 16:05:31 -05004726 }
dea31012005-04-17 16:05:31 -05004727 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004728
James Smart40426292010-12-15 17:58:10 -05004729 if (phba->sli_rev != LPFC_SLI_REV4)
4730 ndlp->nlp_rpi = 0;
4731 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004732 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004733 if (acc_plogi)
4734 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004735 return 1;
4736 }
James Smart7c5e5182015-05-22 10:42:43 -04004737 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004738 return 0;
4739}
4740
James Smartecfd03c2010-02-12 14:41:27 -05004741/**
4742 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4743 * @phba: pointer to lpfc hba data structure.
4744 *
4745 * This routine is invoked to unregister all the currently registered RPIs
4746 * to the HBA.
4747 **/
4748void
4749lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4750{
4751 struct lpfc_vport **vports;
4752 struct lpfc_nodelist *ndlp;
4753 struct Scsi_Host *shost;
4754 int i;
4755
4756 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004757 if (!vports) {
4758 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4759 "2884 Vport array allocation failed \n");
4760 return;
4761 }
James Smartecfd03c2010-02-12 14:41:27 -05004762 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4763 shost = lpfc_shost_from_vport(vports[i]);
4764 spin_lock_irq(shost->host_lock);
4765 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004766 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004767 /* The mempool_alloc might sleep */
4768 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004769 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004770 spin_lock_irq(shost->host_lock);
4771 }
James Smartecfd03c2010-02-12 14:41:27 -05004772 }
4773 spin_unlock_irq(shost->host_lock);
4774 }
4775 lpfc_destroy_vport_work_array(phba, vports);
4776}
4777
James Smart92d7f7b2007-06-17 19:56:38 -05004778void
4779lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4780{
4781 struct lpfc_hba *phba = vport->phba;
4782 LPFC_MBOXQ_t *mbox;
4783 int rc;
4784
James Smart5af5eee2010-10-22 11:06:38 -04004785 if (phba->sli_rev == LPFC_SLI_REV4) {
4786 lpfc_sli4_unreg_all_rpis(vport);
4787 return;
4788 }
4789
James Smart92d7f7b2007-06-17 19:56:38 -05004790 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4791 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004792 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4793 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004794 mbox->vport = vport;
4795 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004796 mbox->context1 = NULL;
4797 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004798 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004799 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004800
4801 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4802 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4803 "1836 Could not issue "
4804 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004805 }
4806}
4807
4808void
4809lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4810{
4811 struct lpfc_hba *phba = vport->phba;
4812 LPFC_MBOXQ_t *mbox;
4813 int rc;
4814
4815 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4816 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004817 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4818 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004819 mbox->vport = vport;
4820 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004821 mbox->context1 = NULL;
4822 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004823 if (rc != MBX_TIMEOUT)
4824 mempool_free(mbox, phba->mbox_mem_pool);
4825
4826 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004827 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4828 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004829 "unreg_did (default rpis) status %d\n",
4830 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004831 }
4832}
4833
dea31012005-04-17 16:05:31 -05004834/*
4835 * Free resources associated with LPFC_NODELIST entry
4836 * so it can be freed.
4837 */
4838static int
James Smart2e0fef82007-06-17 19:56:36 -05004839lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004840{
James Smart2e0fef82007-06-17 19:56:36 -05004841 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4842 struct lpfc_hba *phba = vport->phba;
4843 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004844 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004845
4846 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004847 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4848 "0900 Cleanup node for NPort x%x "
4849 "Data: x%x x%x x%x\n",
4850 ndlp->nlp_DID, ndlp->nlp_flag,
4851 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004852 if (NLP_CHK_FREE_REQ(ndlp)) {
4853 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4854 "0280 lpfc_cleanup_node: ndlp:x%p "
4855 "usgmap:x%x refcnt:%d\n",
4856 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004857 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004858 lpfc_dequeue_node(vport, ndlp);
4859 } else {
4860 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4861 "0281 lpfc_cleanup_node: ndlp:x%p "
4862 "usgmap:x%x refcnt:%d\n",
4863 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004864 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004865 lpfc_disable_node(vport, ndlp);
4866 }
dea31012005-04-17 16:05:31 -05004867
James Smart086a3452012-08-14 14:25:21 -04004868
4869 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4870
dea31012005-04-17 16:05:31 -05004871 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4872 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004873 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004874 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004875 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4876 mb->context2 = NULL;
4877 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4878 }
4879 }
James Smart33ccf8d2006-08-17 11:57:58 -04004880
James Smart2e0fef82007-06-17 19:56:36 -05004881 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004882 /* Cleanup REG_LOGIN completions which are not yet processed */
4883 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4884 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004885 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004886 (ndlp != (struct lpfc_nodelist *) mb->context2))
4887 continue;
4888
4889 mb->context2 = NULL;
4890 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4891 }
4892
dea31012005-04-17 16:05:31 -05004893 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004894 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004895 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004896 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004897 mp = (struct lpfc_dmabuf *) (mb->context1);
4898 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004899 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004900 kfree(mp);
4901 }
4902 list_del(&mb->list);
4903 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004904 /* We shall not invoke the lpfc_nlp_put to decrement
4905 * the ndlp reference count as we are in the process
4906 * of lpfc_nlp_release.
4907 */
dea31012005-04-17 16:05:31 -05004908 }
4909 }
James Smart2e0fef82007-06-17 19:56:36 -05004910 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004911
James Smarte47c9092008-02-08 18:49:26 -05004912 lpfc_els_abort(phba, ndlp);
4913
James Smart2e0fef82007-06-17 19:56:36 -05004914 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004915 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004916 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004917
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004918 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004919 del_timer_sync(&ndlp->nlp_delayfunc);
4920
James Smart0d2b6b82008-06-14 22:52:47 -04004921 list_del_init(&ndlp->els_retry_evt.evt_listp);
4922 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004923 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004924 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004925
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004926 return 0;
dea31012005-04-17 16:05:31 -05004927}
4928
4929/*
4930 * Check to see if we can free the nlp back to the freelist.
4931 * If we are in the middle of using the nlp in the discovery state
4932 * machine, defer the free till we reach the end of the state machine.
4933 */
James Smart329f9bc2007-04-25 09:53:01 -04004934static void
James Smart2e0fef82007-06-17 19:56:36 -05004935lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004936{
James Smarta8adb832007-10-27 13:37:53 -04004937 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004938 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004939 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004940 LPFC_MBOXQ_t *mbox;
4941 int rc;
dea31012005-04-17 16:05:31 -05004942
James Smart0d2b6b82008-06-14 22:52:47 -04004943 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004944 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004945 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4946 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004947 /* For this case we need to cleanup the default rpi
4948 * allocated by the firmware.
4949 */
James Smartbe6bb942015-04-07 15:07:22 -04004950 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4951 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4952 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004953 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004954 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004955 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4956 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004957 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004958 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004959 if (rc) {
4960 mempool_free(mbox, phba->mbox_mem_pool);
4961 }
4962 else {
4963 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4964 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4965 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004966 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004967 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4968 if (rc == MBX_NOT_FINISHED) {
4969 mempool_free(mbox, phba->mbox_mem_pool);
4970 }
4971 }
4972 }
4973 }
James Smart2e0fef82007-06-17 19:56:36 -05004974 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004975
James Smart2e0fef82007-06-17 19:56:36 -05004976 /*
James Smart466e8402015-05-21 13:55:28 -04004977 * ndlp->rport must be set to NULL before it reaches here
4978 * i.e. break rport/node link before doing lpfc_nlp_put for
4979 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05004980 */
James Smart92d7f7b2007-06-17 19:56:38 -05004981 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04004982 /*
4983 * extra lpfc_nlp_put dropped the reference of ndlp
4984 * for registered rport so need to cleanup rport
4985 */
4986 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4987 "0940 removed node x%p DID x%x "
4988 " rport not null %p\n",
4989 ndlp, ndlp->nlp_DID, ndlp->rport);
4990 rport = ndlp->rport;
4991 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004992 rdata->pnode = NULL;
4993 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04004994 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05004995 }
dea31012005-04-17 16:05:31 -05004996}
4997
4998static int
James Smart2e0fef82007-06-17 19:56:36 -05004999lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5000 uint32_t did)
dea31012005-04-17 16:05:31 -05005001{
James Smart2e0fef82007-06-17 19:56:36 -05005002 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005003
5004 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005005 return 0;
dea31012005-04-17 16:05:31 -05005006
dea31012005-04-17 16:05:31 -05005007 /* First check for Direct match */
5008 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005009 return 1;
dea31012005-04-17 16:05:31 -05005010
5011 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005012 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005013 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005014 return 0;
dea31012005-04-17 16:05:31 -05005015 }
5016
5017 matchdid.un.word = did;
5018 ndlpdid.un.word = ndlp->nlp_DID;
5019 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5020 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5021 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005022 /* This code is supposed to match the ID
5023 * for a private loop device that is
5024 * connect to fl_port. But we need to
5025 * check that the port did not just go
5026 * from pt2pt to fabric or we could end
5027 * up matching ndlp->nlp_DID 000001 to
5028 * fabric DID 0x20101
5029 */
dea31012005-04-17 16:05:31 -05005030 if ((ndlpdid.un.b.domain == 0) &&
5031 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005032 if (ndlpdid.un.b.id &&
5033 vport->phba->fc_topology ==
5034 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005035 return 1;
dea31012005-04-17 16:05:31 -05005036 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005037 return 0;
dea31012005-04-17 16:05:31 -05005038 }
5039
5040 matchdid.un.word = ndlp->nlp_DID;
5041 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5042 (mydid.un.b.area == ndlpdid.un.b.area)) {
5043 if ((matchdid.un.b.domain == 0) &&
5044 (matchdid.un.b.area == 0)) {
5045 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005046 return 1;
dea31012005-04-17 16:05:31 -05005047 }
5048 }
5049 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005050 return 0;
dea31012005-04-17 16:05:31 -05005051}
5052
James Smart685f0bf2007-04-25 09:53:08 -04005053/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005054static struct lpfc_nodelist *
5055__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005056{
James Smart2fb9bd82006-12-02 13:33:57 -05005057 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005058 uint32_t data1;
5059
James Smart2e0fef82007-06-17 19:56:36 -05005060 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5061 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005062 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5063 ((uint32_t) ndlp->nlp_xri << 16) |
5064 ((uint32_t) ndlp->nlp_type << 8) |
5065 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005066 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5067 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005068 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005069 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005070 ndlp->nlp_flag, data1,
5071 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005072 return ndlp;
dea31012005-04-17 16:05:31 -05005073 }
5074 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005075
dea31012005-04-17 16:05:31 -05005076 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005077 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5078 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005079 return NULL;
5080}
5081
5082struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005083lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005084{
James Smart2e0fef82007-06-17 19:56:36 -05005085 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005086 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005087 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005088
James Smartfedd3b72011-02-16 12:39:24 -05005089 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005090 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005091 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005092 return ndlp;
5093}
5094
5095struct lpfc_nodelist *
5096lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5097{
5098 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5099 struct lpfc_nodelist *ndlp;
5100
5101 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005102 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05005103 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5104 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005105 return NULL;
5106 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05005107 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005108 if (!ndlp)
5109 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005110 lpfc_nlp_init(vport, ndlp, did);
5111 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
James Smart8c258642017-02-12 13:52:36 -08005112 if (vport->phba->nvmet_support)
5113 return ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005114 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005115 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005116 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005117 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005118 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5119 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5120 if (!ndlp)
5121 return NULL;
James Smart8c258642017-02-12 13:52:36 -08005122 if (vport->phba->nvmet_support)
5123 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005124 spin_lock_irq(shost->host_lock);
5125 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5126 spin_unlock_irq(shost->host_lock);
5127 return ndlp;
dea31012005-04-17 16:05:31 -05005128 }
James Smarte47c9092008-02-08 18:49:26 -05005129
James Smart58da1ff2008-04-07 10:15:56 -04005130 if ((vport->fc_flag & FC_RSCN_MODE) &&
5131 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005132 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005133 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005134 * we don't need to try to discover it again.
5135 */
5136 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5137 return NULL;
5138
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005139 /* Since this node is marked for discovery,
5140 * delay timeout is not needed.
5141 */
James Smart0d2b6b82008-06-14 22:52:47 -04005142 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08005143 if (vport->phba->nvmet_support)
5144 return ndlp;
James Smarta257bf92009-04-06 18:48:10 -04005145 spin_lock_irq(shost->host_lock);
5146 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5147 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005148 } else
dea31012005-04-17 16:05:31 -05005149 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005150 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005151 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04005152 * or we are already in the process of discovery on it,
5153 * we don't need to try to discover it again.
5154 */
James Smart685f0bf2007-04-25 09:53:08 -04005155 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005156 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5157 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05005158 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005159 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
James Smart8c258642017-02-12 13:52:36 -08005160 if (vport->phba->nvmet_support)
5161 return ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005162 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005163 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005164 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005165 }
5166 return ndlp;
5167}
5168
5169/* Build a list of nodes to discover based on the loopmap */
5170void
James Smart2e0fef82007-06-17 19:56:36 -05005171lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005172{
James Smart2e0fef82007-06-17 19:56:36 -05005173 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005174 int j;
5175 uint32_t alpa, index;
5176
James Smart2e0fef82007-06-17 19:56:36 -05005177 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005178 return;
James Smart2e0fef82007-06-17 19:56:36 -05005179
James Smart76a95d72010-11-20 23:11:48 -05005180 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005181 return;
dea31012005-04-17 16:05:31 -05005182
5183 /* Check for loop map present or not */
5184 if (phba->alpa_map[0]) {
5185 for (j = 1; j <= phba->alpa_map[0]; j++) {
5186 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005187 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005188 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005189 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005190 }
5191 } else {
5192 /* No alpamap, so try all alpa's */
5193 for (j = 0; j < FC_MAXLOOP; j++) {
5194 /* If cfg_scan_down is set, start from highest
5195 * ALPA (0xef) to lowest (0x1).
5196 */
James Smart3de2a652007-08-02 11:09:59 -04005197 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005198 index = j;
5199 else
5200 index = FC_MAXLOOP - j - 1;
5201 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005202 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005203 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005204 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005205 }
5206 }
5207 return;
5208}
5209
James Smart895427b2017-02-12 13:52:30 -08005210/* SLI3 only */
dea31012005-04-17 16:05:31 -05005211void
James Smart2e0fef82007-06-17 19:56:36 -05005212lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005213{
dea31012005-04-17 16:05:31 -05005214 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005215 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005216 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5217 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005218 int rc;
5219
James Smart92d7f7b2007-06-17 19:56:38 -05005220 /*
5221 * if it's not a physical port or if we already send
5222 * clear_la then don't send it.
5223 */
5224 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005225 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5226 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005227 return;
5228
James Smart2e0fef82007-06-17 19:56:36 -05005229 /* Link up discovery */
5230 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5231 phba->link_state = LPFC_CLEAR_LA;
5232 lpfc_clear_la(phba, mbox);
5233 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5234 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005235 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005236 if (rc == MBX_NOT_FINISHED) {
5237 mempool_free(mbox, phba->mbox_mem_pool);
5238 lpfc_disc_flush_list(vport);
5239 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5240 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005241 phba->link_state = LPFC_HBA_ERROR;
5242 }
5243 }
5244}
5245
5246/* Reg_vpi to tell firmware to resume normal operations */
5247void
5248lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5249{
5250 LPFC_MBOXQ_t *regvpimbox;
5251
5252 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5253 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005254 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005255 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5256 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005257 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005258 == MBX_NOT_FINISHED) {
5259 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005260 }
5261 }
5262}
5263
5264/* Start Link up / RSCN discovery on NPR nodes */
5265void
5266lpfc_disc_start(struct lpfc_vport *vport)
5267{
5268 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5269 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005270 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005271 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005272
James Smarte74c03c2013-04-17 20:15:19 -04005273 if (!lpfc_is_link_up(phba)) {
5274 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5275 "3315 Link is not up %x\n",
5276 phba->link_state);
dea31012005-04-17 16:05:31 -05005277 return;
James Smarte74c03c2013-04-17 20:15:19 -04005278 }
James Smart2e0fef82007-06-17 19:56:36 -05005279
5280 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005281 clear_la_pending = 1;
5282 else
5283 clear_la_pending = 0;
5284
James Smart2e0fef82007-06-17 19:56:36 -05005285 if (vport->port_state < LPFC_VPORT_READY)
5286 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005287
James Smart2e0fef82007-06-17 19:56:36 -05005288 lpfc_set_disctmo(vport);
5289
James Smart2e0fef82007-06-17 19:56:36 -05005290 vport->fc_prevDID = vport->fc_myDID;
5291 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005292
5293 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005294 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5295 "0202 Start Discovery hba state x%x "
5296 "Data: x%x x%x x%x\n",
5297 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5298 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005299
5300 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005301 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005302
5303 if (num_sent)
5304 return;
5305
James Smart6fa139f2013-04-17 20:20:00 -04005306 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005307 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005308 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005309 !(vport->fc_flag & FC_RSCN_MODE) &&
5310 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005311 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005312 lpfc_issue_reg_vpi(phba, vport);
5313 return;
5314 }
James Smart2e0fef82007-06-17 19:56:36 -05005315
James Smart92d7f7b2007-06-17 19:56:38 -05005316 /*
5317 * For SLI2, we need to set port_state to READY and continue
5318 * discovery.
5319 */
5320 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5321 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005322 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005323
5324 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005325 vport->num_disc_nodes = 0;
5326 /* go thru NPR nodes and issue ELS PLOGIs */
5327 if (vport->fc_npr_cnt)
5328 lpfc_els_disc_plogi(vport);
5329
5330 if (!vport->num_disc_nodes) {
5331 spin_lock_irq(shost->host_lock);
5332 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5333 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005334 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005335 }
5336 }
James Smart92d7f7b2007-06-17 19:56:38 -05005337 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005338 } else {
5339 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005340 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005341
5342 if (num_sent)
5343 return;
5344
James Smart2e0fef82007-06-17 19:56:36 -05005345 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005346 /* Check to see if more RSCNs came in while we
5347 * were processing this one.
5348 */
James Smart2e0fef82007-06-17 19:56:36 -05005349 if ((vport->fc_rscn_id_cnt == 0) &&
5350 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5351 spin_lock_irq(shost->host_lock);
5352 vport->fc_flag &= ~FC_RSCN_MODE;
5353 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005354 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005355 } else
James Smart2e0fef82007-06-17 19:56:36 -05005356 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005357 }
5358 }
5359 return;
5360}
5361
5362/*
5363 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5364 * ring the match the sppecified nodelist.
5365 */
5366static void
James Smart2e0fef82007-06-17 19:56:36 -05005367lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005368{
James Smart2534ba72007-04-25 09:52:20 -04005369 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005370 struct lpfc_sli *psli;
5371 IOCB_t *icmd;
5372 struct lpfc_iocbq *iocb, *next_iocb;
5373 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005374
5375 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005376 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005377
5378 /* Error matching iocb on txq or txcmplq
5379 * First check the txq.
5380 */
James Smart2e0fef82007-06-17 19:56:36 -05005381 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005382 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5383 if (iocb->context1 != ndlp) {
5384 continue;
5385 }
5386 icmd = &iocb->iocb;
5387 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5388 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5389
James Smart2534ba72007-04-25 09:52:20 -04005390 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005391 }
5392 }
5393
5394 /* Next check the txcmplq */
5395 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5396 if (iocb->context1 != ndlp) {
5397 continue;
5398 }
5399 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005400 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5401 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005402 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005403 }
5404 }
James Smart2e0fef82007-06-17 19:56:36 -05005405 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005406
James Smarta257bf92009-04-06 18:48:10 -04005407 /* Cancel all the IOCBs from the completions list */
5408 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5409 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005410}
5411
Adrian Bunka6ababd2007-11-05 18:07:33 +01005412static void
James Smart2e0fef82007-06-17 19:56:36 -05005413lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005414{
5415 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005416 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005417
James Smart2e0fef82007-06-17 19:56:36 -05005418 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5419 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005420 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005421 if (!NLP_CHK_NODE_ACT(ndlp))
5422 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005423 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5424 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5425 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005426 }
dea31012005-04-17 16:05:31 -05005427 }
5428 }
dea31012005-04-17 16:05:31 -05005429}
5430
James Smart92d7f7b2007-06-17 19:56:38 -05005431void
5432lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5433{
5434 lpfc_els_flush_rscn(vport);
5435 lpfc_els_flush_cmd(vport);
5436 lpfc_disc_flush_list(vport);
5437}
5438
dea31012005-04-17 16:05:31 -05005439/*****************************************************************************/
5440/*
5441 * NAME: lpfc_disc_timeout
5442 *
5443 * FUNCTION: Fibre Channel driver discovery timeout routine.
5444 *
5445 * EXECUTION ENVIRONMENT: interrupt only
5446 *
5447 * CALLED FROM:
5448 * Timer function
5449 *
5450 * RETURNS:
5451 * none
5452 */
5453/*****************************************************************************/
5454void
5455lpfc_disc_timeout(unsigned long ptr)
5456{
James Smart2e0fef82007-06-17 19:56:36 -05005457 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5458 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005459 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005460 unsigned long flags = 0;
5461
5462 if (unlikely(!phba))
5463 return;
5464
James Smart5e9d9b82008-06-14 22:52:53 -04005465 spin_lock_irqsave(&vport->work_port_lock, flags);
5466 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5467 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005468 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005469 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005470
James Smart5e9d9b82008-06-14 22:52:53 -04005471 if (!tmo_posted)
5472 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005473 return;
5474}
5475
5476static void
James Smart2e0fef82007-06-17 19:56:36 -05005477lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005478{
James Smart2e0fef82007-06-17 19:56:36 -05005479 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5480 struct lpfc_hba *phba = vport->phba;
5481 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005482 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005483 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005484 int rc, clrlaerr = 0;
5485
James Smart2e0fef82007-06-17 19:56:36 -05005486 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005487 return;
5488
James Smart2e0fef82007-06-17 19:56:36 -05005489 spin_lock_irq(shost->host_lock);
5490 vport->fc_flag &= ~FC_DISC_TMO;
5491 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005492
James Smart858c9f62007-06-17 19:56:39 -05005493 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5494 "disc timeout: state:x%x rtry:x%x flg:x%x",
5495 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5496
James Smart2e0fef82007-06-17 19:56:36 -05005497 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005498
5499 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005500 /*
5501 * port_state is identically LPFC_LOCAL_CFG_LINK while
5502 * waiting for FAN timeout
5503 */
James Smarte8b62012007-08-02 11:10:09 -04005504 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5505 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005506
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005507 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005508 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005509 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005510 if (!NLP_CHK_NODE_ACT(ndlp))
5511 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005512 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5513 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005514 if (ndlp->nlp_type & NLP_FABRIC) {
5515 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005516 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005517
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005518 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005519 /* Fail outstanding IO now since device
5520 * is marked for PLOGI.
5521 */
James Smart2e0fef82007-06-17 19:56:36 -05005522 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005523 }
5524 }
James Smart92d7f7b2007-06-17 19:56:38 -05005525 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005526 if (phba->sli_rev <= LPFC_SLI_REV3)
5527 lpfc_initial_flogi(vport);
5528 else
5529 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005530 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005531 }
dea31012005-04-17 16:05:31 -05005532 break;
5533
James Smart92d7f7b2007-06-17 19:56:38 -05005534 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005535 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005536 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005537 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005538 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5539 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005540 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005541
5542 /* Assume no Fabric and go on with discovery.
5543 * Check for outstanding ELS FLOGI to abort.
5544 */
5545
5546 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005547 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005548
5549 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005550 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005551 break;
5552
5553 case LPFC_FABRIC_CFG_LINK:
5554 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5555 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005556 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5557 "0223 Timeout while waiting for "
5558 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005559 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005560 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005561 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005562 lpfc_els_abort(phba, ndlp);
5563
5564 /* ReStart discovery */
5565 goto restart_disc;
dea31012005-04-17 16:05:31 -05005566
5567 case LPFC_NS_QRY:
5568 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005569 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5570 "0224 NameServer Query timeout "
5571 "Data: x%x x%x\n",
5572 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005573
James Smart92d7f7b2007-06-17 19:56:38 -05005574 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5575 /* Try it one more time */
5576 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005577 vport->gidft_inp = 0;
5578 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005579 if (rc == 0)
5580 break;
dea31012005-04-17 16:05:31 -05005581 }
James Smart92d7f7b2007-06-17 19:56:38 -05005582 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005583
James Smart87af33f2007-10-27 13:37:43 -04005584restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005585 /*
5586 * Discovery is over.
5587 * set port_state to PORT_READY if SLI2.
5588 * cmpl_reg_vpi will set port_state to READY for SLI3.
5589 */
James Smart3772a992009-05-22 14:50:54 -04005590 if (phba->sli_rev < LPFC_SLI_REV4) {
5591 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5592 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005593 else {
James Smart3772a992009-05-22 14:50:54 -04005594 lpfc_issue_clear_la(phba, vport);
5595 vport->port_state = LPFC_VPORT_READY;
5596 }
dea31012005-04-17 16:05:31 -05005597 }
5598
5599 /* Setup and issue mailbox INITIALIZE LINK command */
5600 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5601 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005602 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5603 "0206 Device Discovery "
5604 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005605 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005606 break;
5607 }
5608
5609 lpfc_linkdown(phba);
5610 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5611 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005612 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005613 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005614 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005615 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005616 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005617 if (rc == MBX_NOT_FINISHED)
5618 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5619
5620 break;
5621
5622 case LPFC_DISC_AUTH:
5623 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005624 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5625 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005626 lpfc_disc_flush_list(vport);
5627
James Smart92d7f7b2007-06-17 19:56:38 -05005628 /*
5629 * set port_state to PORT_READY if SLI2.
5630 * cmpl_reg_vpi will set port_state to READY for SLI3.
5631 */
James Smart3772a992009-05-22 14:50:54 -04005632 if (phba->sli_rev < LPFC_SLI_REV4) {
5633 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5634 lpfc_issue_reg_vpi(phba, vport);
5635 else { /* NPIV Not enabled */
5636 lpfc_issue_clear_la(phba, vport);
5637 vport->port_state = LPFC_VPORT_READY;
5638 }
dea31012005-04-17 16:05:31 -05005639 }
5640 break;
5641
James Smart2e0fef82007-06-17 19:56:36 -05005642 case LPFC_VPORT_READY:
5643 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005644 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5645 "0231 RSCN timeout Data: x%x "
5646 "x%x\n",
5647 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005648
5649 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005650 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005651
James Smart2e0fef82007-06-17 19:56:36 -05005652 lpfc_els_flush_rscn(vport);
5653 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005654 }
5655 break;
James Smart2e0fef82007-06-17 19:56:36 -05005656
James Smart92d7f7b2007-06-17 19:56:38 -05005657 default:
James Smarte8b62012007-08-02 11:10:09 -04005658 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005659 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005660 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005661 break;
5662 }
5663
5664 switch (phba->link_state) {
5665 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005666 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005667 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5668 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005669 clrlaerr = 1;
5670 break;
5671
James Smart09372822008-01-11 01:52:54 -05005672 case LPFC_LINK_UP:
5673 lpfc_issue_clear_la(phba, vport);
5674 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005675 case LPFC_LINK_UNKNOWN:
5676 case LPFC_WARM_START:
5677 case LPFC_INIT_START:
5678 case LPFC_INIT_MBX_CMDS:
5679 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005680 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005681 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5682 "0230 Unexpected timeout, hba link "
5683 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005684 clrlaerr = 1;
5685 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005686
5687 case LPFC_HBA_READY:
5688 break;
dea31012005-04-17 16:05:31 -05005689 }
5690
5691 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005692 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005693 if (phba->sli_rev != LPFC_SLI_REV4) {
5694 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5695 ~LPFC_STOP_IOCB_EVENT;
5696 psli->sli3_ring[LPFC_FCP_RING].flag &=
5697 ~LPFC_STOP_IOCB_EVENT;
5698 }
James Smart2e0fef82007-06-17 19:56:36 -05005699 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005700 }
dea31012005-04-17 16:05:31 -05005701 return;
5702}
5703
dea31012005-04-17 16:05:31 -05005704/*
5705 * This routine handles processing a NameServer REG_LOGIN mailbox
5706 * command upon completion. It is setup in the LPFC_MBOXQ
5707 * as the completion routine when the command is
5708 * handed off to the SLI layer.
5709 */
5710void
James Smart2e0fef82007-06-17 19:56:36 -05005711lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005712{
James Smart04c68492009-05-22 14:52:52 -04005713 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005714 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5715 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5716 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005717
5718 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005719 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005720
James Smart6d368e52011-05-24 11:44:12 -04005721 if (phba->sli_rev < LPFC_SLI_REV4)
5722 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005723 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005724 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005725 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005726 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5727 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5728 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005729 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005730 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005731 /*
5732 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005733 * 0xfffffa (FDMI well known port).
5734 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5735 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005736 */
James Smart4258e982015-12-16 18:11:58 -05005737 if (vport->port_type == LPFC_PHYSICAL_PORT)
5738 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005739 else
James Smart4258e982015-12-16 18:11:58 -05005740 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5741
dea31012005-04-17 16:05:31 -05005742
James Smartfa4066b2008-01-11 01:53:27 -05005743 /* decrement the node reference count held for this callback
5744 * function.
5745 */
James Smart329f9bc2007-04-25 09:53:01 -04005746 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005747 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5748 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005749 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005750
5751 return;
5752}
5753
James Smart685f0bf2007-04-25 09:53:08 -04005754static int
5755lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5756{
5757 uint16_t *rpi = param;
5758
James Smarteff4a012012-01-18 16:25:25 -05005759 /* check for active node */
5760 if (!NLP_CHK_NODE_ACT(ndlp))
5761 return 0;
5762
James Smart685f0bf2007-04-25 09:53:08 -04005763 return ndlp->nlp_rpi == *rpi;
5764}
5765
5766static int
5767lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5768{
5769 return memcmp(&ndlp->nlp_portname, param,
5770 sizeof(ndlp->nlp_portname)) == 0;
5771}
5772
Adrian Bunka6ababd2007-11-05 18:07:33 +01005773static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005774__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005775{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005776 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005777
James Smart2e0fef82007-06-17 19:56:36 -05005778 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005779 if (filter(ndlp, param)) {
5780 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5781 "3185 FIND node filter %p DID "
5782 "Data: x%p x%x x%x\n",
5783 filter, ndlp, ndlp->nlp_DID,
5784 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005785 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005786 }
James Smart685f0bf2007-04-25 09:53:08 -04005787 }
James Smart34f5ad82012-08-03 12:35:03 -04005788 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5789 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005790 return NULL;
dea31012005-04-17 16:05:31 -05005791}
5792
James Smart685f0bf2007-04-25 09:53:08 -04005793/*
5794 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005795 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005796 */
5797struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005798__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005799{
James Smart2e0fef82007-06-17 19:56:36 -05005800 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005801}
5802
James Smart488d1462006-03-07 15:02:37 -05005803/*
James Smart685f0bf2007-04-25 09:53:08 -04005804 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005805 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005806 */
5807struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005808lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005809{
James Smart2e0fef82007-06-17 19:56:36 -05005810 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005811 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005812
James Smart2e0fef82007-06-17 19:56:36 -05005813 spin_lock_irq(shost->host_lock);
5814 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5815 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005816 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005817}
5818
James Smartcb69f7d2011-12-13 13:21:57 -05005819/*
5820 * This routine looks up the ndlp lists for the given RPI. If the rpi
5821 * is found, the routine returns the node element list pointer else
5822 * return NULL.
5823 */
5824struct lpfc_nodelist *
5825lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5826{
5827 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5828 struct lpfc_nodelist *ndlp;
5829
5830 spin_lock_irq(shost->host_lock);
5831 ndlp = __lpfc_findnode_rpi(vport, rpi);
5832 spin_unlock_irq(shost->host_lock);
5833 return ndlp;
5834}
5835
5836/**
5837 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5838 * @phba: pointer to lpfc hba data structure.
5839 * @vpi: the physical host virtual N_Port identifier.
5840 *
5841 * This routine finds a vport on a HBA (referred by @phba) through a
5842 * @vpi. The function walks the HBA's vport list and returns the address
5843 * of the vport with the matching @vpi.
5844 *
5845 * Return code
5846 * NULL - No vport with the matching @vpi found
5847 * Otherwise - Address to the vport with the matching @vpi.
5848 **/
5849struct lpfc_vport *
5850lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5851{
5852 struct lpfc_vport *vport;
5853 unsigned long flags;
5854 int i = 0;
5855
5856 /* The physical ports are always vpi 0 - translate is unnecessary. */
5857 if (vpi > 0) {
5858 /*
5859 * Translate the physical vpi to the logical vpi. The
5860 * vport stores the logical vpi.
5861 */
5862 for (i = 0; i < phba->max_vpi; i++) {
5863 if (vpi == phba->vpi_ids[i])
5864 break;
5865 }
5866
5867 if (i >= phba->max_vpi) {
5868 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5869 "2936 Could not find Vport mapped "
5870 "to vpi %d\n", vpi);
5871 return NULL;
5872 }
5873 }
5874
5875 spin_lock_irqsave(&phba->hbalock, flags);
5876 list_for_each_entry(vport, &phba->port_list, listentry) {
5877 if (vport->vpi == i) {
5878 spin_unlock_irqrestore(&phba->hbalock, flags);
5879 return vport;
5880 }
5881 }
5882 spin_unlock_irqrestore(&phba->hbalock, flags);
5883 return NULL;
5884}
5885
dea31012005-04-17 16:05:31 -05005886void
James Smart2e0fef82007-06-17 19:56:36 -05005887lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5888 uint32_t did)
dea31012005-04-17 16:05:31 -05005889{
5890 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005891
5892 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005893 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005894 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005895 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005896 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5897 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5898 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5899 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005900 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005901 ndlp->nlp_usg_map, ndlp);
5902
James Smartcff261f2013-12-17 20:29:47 -05005903 ndlp->active_rrqs_xri_bitmap =
5904 mempool_alloc(vport->phba->active_rrq_pool,
5905 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005906 if (ndlp->active_rrqs_xri_bitmap)
5907 memset(ndlp->active_rrqs_xri_bitmap, 0,
5908 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005909 }
5910
James Smart725dd392013-10-10 12:23:30 -04005911
James Smart858c9f62007-06-17 19:56:39 -05005912
5913 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5914 "node init: did:x%x",
5915 ndlp->nlp_DID, 0, 0);
5916
dea31012005-04-17 16:05:31 -05005917 return;
5918}
James Smart329f9bc2007-04-25 09:53:01 -04005919
James Smart98c9ea52007-10-27 13:37:33 -04005920/* This routine releases all resources associated with a specifc NPort's ndlp
5921 * and mempool_free's the nodelist.
5922 */
James Smart311464e2007-08-02 11:10:37 -04005923static void
James Smart329f9bc2007-04-25 09:53:01 -04005924lpfc_nlp_release(struct kref *kref)
5925{
James Smarte47c9092008-02-08 18:49:26 -05005926 struct lpfc_hba *phba;
5927 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005928 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5929 kref);
James Smart858c9f62007-06-17 19:56:39 -05005930
5931 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5932 "node release: did:x%x flg:x%x type:x%x",
5933 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5934
James Smarte47c9092008-02-08 18:49:26 -05005935 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005936 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005937 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005938 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005939 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005940
5941 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005942 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005943
5944 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005945 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005946 spin_lock_irqsave(&phba->ndlp_lock, flags);
5947 NLP_CLR_NODE_ACT(ndlp);
5948 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005949 if (phba->sli_rev == LPFC_SLI_REV4)
5950 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005951
5952 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005953 if (NLP_CHK_FREE_REQ(ndlp)) {
5954 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005955 if (phba->sli_rev == LPFC_SLI_REV4)
5956 mempool_free(ndlp->active_rrqs_xri_bitmap,
5957 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005958 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005959 }
James Smart329f9bc2007-04-25 09:53:01 -04005960}
5961
James Smart98c9ea52007-10-27 13:37:33 -04005962/* This routine bumps the reference count for a ndlp structure to ensure
5963 * that one discovery thread won't free a ndlp while another discovery thread
5964 * is using it.
5965 */
James Smart329f9bc2007-04-25 09:53:01 -04005966struct lpfc_nodelist *
5967lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5968{
James Smarte47c9092008-02-08 18:49:26 -05005969 struct lpfc_hba *phba;
5970 unsigned long flags;
5971
James Smart98c9ea52007-10-27 13:37:33 -04005972 if (ndlp) {
5973 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5974 "node get: did:x%x flg:x%x refcnt:x%x",
5975 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005976 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005977 /* The check of ndlp usage to prevent incrementing the
5978 * ndlp reference count that is in the process of being
5979 * released.
5980 */
James Smarta257bf92009-04-06 18:48:10 -04005981 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005982 spin_lock_irqsave(&phba->ndlp_lock, flags);
5983 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5984 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5985 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5986 "0276 lpfc_nlp_get: ndlp:x%p "
5987 "usgmap:x%x refcnt:%d\n",
5988 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005989 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005990 return NULL;
5991 } else
5992 kref_get(&ndlp->kref);
5993 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005994 }
James Smart329f9bc2007-04-25 09:53:01 -04005995 return ndlp;
5996}
5997
James Smart98c9ea52007-10-27 13:37:33 -04005998/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005999 * count goes to 0, this indicates the the associated nodelist should be
6000 * freed. Returning 1 indicates the ndlp resource has been released; on the
6001 * other hand, returning 0 indicates the ndlp resource has not been released
6002 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006003 */
James Smart329f9bc2007-04-25 09:53:01 -04006004int
6005lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6006{
James Smarte47c9092008-02-08 18:49:26 -05006007 struct lpfc_hba *phba;
6008 unsigned long flags;
6009
6010 if (!ndlp)
6011 return 1;
6012
6013 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6014 "node put: did:x%x flg:x%x refcnt:x%x",
6015 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006016 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006017 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006018 spin_lock_irqsave(&phba->ndlp_lock, flags);
6019 /* Check the ndlp memory free acknowledge flag to avoid the
6020 * possible race condition that kref_put got invoked again
6021 * after previous one has done ndlp memory free.
6022 */
6023 if (NLP_CHK_FREE_ACK(ndlp)) {
6024 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6025 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6026 "0274 lpfc_nlp_put: ndlp:x%p "
6027 "usgmap:x%x refcnt:%d\n",
6028 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006029 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006030 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006031 }
James Smarte47c9092008-02-08 18:49:26 -05006032 /* Check the ndlp inactivate log flag to avoid the possible
6033 * race condition that kref_put got invoked again after ndlp
6034 * is already in inactivating state.
6035 */
6036 if (NLP_CHK_IACT_REQ(ndlp)) {
6037 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6038 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6039 "0275 lpfc_nlp_put: ndlp:x%p "
6040 "usgmap:x%x refcnt:%d\n",
6041 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006042 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006043 return 1;
6044 }
6045 /* For last put, mark the ndlp usage flags to make sure no
6046 * other kref_get and kref_put on the same ndlp shall get
6047 * in between the process when the final kref_put has been
6048 * invoked on this ndlp.
6049 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006050 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006051 /* Indicate ndlp is put to inactive state. */
6052 NLP_SET_IACT_REQ(ndlp);
6053 /* Acknowledge ndlp memory free has been seen. */
6054 if (NLP_CHK_FREE_REQ(ndlp))
6055 NLP_SET_FREE_ACK(ndlp);
6056 }
6057 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6058 /* Note, the kref_put returns 1 when decrementing a reference
6059 * count that was 1, it invokes the release callback function,
6060 * but it still left the reference count as 1 (not actually
6061 * performs the last decrementation). Otherwise, it actually
6062 * decrements the reference count and returns 0.
6063 */
6064 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006065}
James Smart98c9ea52007-10-27 13:37:33 -04006066
6067/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006068 * by any other discovery thread. This routine returns 1 if the
6069 * ndlp has been freed. A return value of 0 indicates the ndlp is
6070 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006071 */
6072int
6073lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6074{
6075 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6076 "node not used: did:x%x flg:x%x refcnt:x%x",
6077 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006078 kref_read(&ndlp->kref));
6079 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006080 if (lpfc_nlp_put(ndlp))
6081 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006082 return 0;
6083}
James Smart6fb120a2009-05-22 14:52:59 -04006084
6085/**
6086 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6087 * @phba: Pointer to hba context object.
6088 *
6089 * This function iterate through all FC nodes associated
6090 * will all vports to check if there is any node with
6091 * fc_rports associated with it. If there is an fc_rport
6092 * associated with the node, then the node is either in
6093 * discovered state or its devloss_timer is pending.
6094 */
6095static int
6096lpfc_fcf_inuse(struct lpfc_hba *phba)
6097{
6098 struct lpfc_vport **vports;
6099 int i, ret = 0;
6100 struct lpfc_nodelist *ndlp;
6101 struct Scsi_Host *shost;
6102
6103 vports = lpfc_create_vport_work_array(phba);
6104
James Smart63e801c2010-11-20 23:14:19 -05006105 /* If driver cannot allocate memory, indicate fcf is in use */
6106 if (!vports)
6107 return 1;
6108
James Smart6fb120a2009-05-22 14:52:59 -04006109 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6110 shost = lpfc_shost_from_vport(vports[i]);
6111 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006112 /*
6113 * IF the CVL_RCVD bit is not set then we have sent the
6114 * flogi.
6115 * If dev_loss fires while we are waiting we do not want to
6116 * unreg the fcf.
6117 */
6118 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6119 spin_unlock_irq(shost->host_lock);
6120 ret = 1;
6121 goto out;
6122 }
James Smart6fb120a2009-05-22 14:52:59 -04006123 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6124 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6125 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6126 ret = 1;
6127 spin_unlock_irq(shost->host_lock);
6128 goto out;
James Smart80c17842012-03-01 22:35:45 -05006129 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6130 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006131 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006132 "2624 RPI %x DID %x flag %x "
6133 "still logged in\n",
6134 ndlp->nlp_rpi, ndlp->nlp_DID,
6135 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006136 }
6137 }
6138 spin_unlock_irq(shost->host_lock);
6139 }
6140out:
6141 lpfc_destroy_vport_work_array(phba, vports);
6142 return ret;
6143}
6144
6145/**
6146 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6147 * @phba: Pointer to hba context object.
6148 * @mboxq: Pointer to mailbox object.
6149 *
6150 * This function frees memory associated with the mailbox command.
6151 */
James Smart1b511972011-12-13 13:23:09 -05006152void
James Smart6fb120a2009-05-22 14:52:59 -04006153lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6154{
6155 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006156 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006157
6158 if (mboxq->u.mb.mbxStatus) {
6159 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6160 "2555 UNREG_VFI mbxStatus error x%x "
6161 "HBA state x%x\n",
6162 mboxq->u.mb.mbxStatus, vport->port_state);
6163 }
James Smart38b92ef2010-08-04 16:11:39 -04006164 spin_lock_irq(shost->host_lock);
6165 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6166 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006167 mempool_free(mboxq, phba->mbox_mem_pool);
6168 return;
6169}
6170
6171/**
6172 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6173 * @phba: Pointer to hba context object.
6174 * @mboxq: Pointer to mailbox object.
6175 *
6176 * This function frees memory associated with the mailbox command.
6177 */
6178static void
6179lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6180{
6181 struct lpfc_vport *vport = mboxq->vport;
6182
6183 if (mboxq->u.mb.mbxStatus) {
6184 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6185 "2550 UNREG_FCFI mbxStatus error x%x "
6186 "HBA state x%x\n",
6187 mboxq->u.mb.mbxStatus, vport->port_state);
6188 }
6189 mempool_free(mboxq, phba->mbox_mem_pool);
6190 return;
6191}
6192
6193/**
James Smartecfd03c2010-02-12 14:41:27 -05006194 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006195 * @phba: Pointer to hba context object.
6196 *
James Smartecfd03c2010-02-12 14:41:27 -05006197 * This function prepare the HBA for unregistering the currently registered
6198 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6199 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006200 */
James Smartecfd03c2010-02-12 14:41:27 -05006201int
6202lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006203{
James Smart6fb120a2009-05-22 14:52:59 -04006204 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006205 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006206 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006207 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006208
James Smartecfd03c2010-02-12 14:41:27 -05006209 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006210 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006211 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006212
James Smart4d9ab992009-10-02 15:16:39 -04006213 /* At this point, all discovery is aborted */
6214 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006215
6216 /* Unregister VPIs */
6217 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006218 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006219 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006220 /* Stop FLOGI/FDISC retries */
6221 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6222 if (ndlp)
6223 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006224 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006225 if (phba->sli_rev == LPFC_SLI_REV4)
6226 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006227 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006228 shost = lpfc_shost_from_vport(vports[i]);
6229 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006230 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006231 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006232 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006233 }
6234 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006235 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6236 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6237 if (ndlp)
6238 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6239 lpfc_cleanup_pending_mbox(phba->pport);
6240 if (phba->sli_rev == LPFC_SLI_REV4)
6241 lpfc_sli4_unreg_all_rpis(phba->pport);
6242 lpfc_mbx_unreg_vpi(phba->pport);
6243 shost = lpfc_shost_from_vport(phba->pport);
6244 spin_lock_irq(shost->host_lock);
6245 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6246 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6247 spin_unlock_irq(shost->host_lock);
6248 }
James Smart6fb120a2009-05-22 14:52:59 -04006249
James Smart695a8142010-01-26 23:08:03 -05006250 /* Cleanup any outstanding ELS commands */
6251 lpfc_els_flush_all_cmd(phba);
6252
James Smart1b511972011-12-13 13:23:09 -05006253 /* Unregister the physical port VFI */
6254 rc = lpfc_issue_unreg_vfi(phba->pport);
6255 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006256}
6257
6258/**
6259 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6260 * @phba: Pointer to hba context object.
6261 *
6262 * This function issues synchronous unregister FCF mailbox command to HBA to
6263 * unregister the currently registered FCF record. The driver does not reset
6264 * the driver FCF usage state flags.
6265 *
6266 * Return 0 if successfully issued, none-zero otherwise.
6267 */
6268int
6269lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6270{
6271 LPFC_MBOXQ_t *mbox;
6272 int rc;
6273
James Smart6fb120a2009-05-22 14:52:59 -04006274 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6275 if (!mbox) {
6276 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006277 "2551 UNREG_FCFI mbox allocation failed"
6278 "HBA state x%x\n", phba->pport->port_state);
6279 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006280 }
James Smart6fb120a2009-05-22 14:52:59 -04006281 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6282 mbox->vport = phba->pport;
6283 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6284 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6285
6286 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6288 "2552 Unregister FCFI command failed rc x%x "
6289 "HBA state x%x\n",
6290 rc, phba->pport->port_state);
6291 return -EINVAL;
6292 }
6293 return 0;
6294}
6295
6296/**
6297 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6298 * @phba: Pointer to hba context object.
6299 *
6300 * This function unregisters the currently reigstered FCF. This function
6301 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6302 */
6303void
6304lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6305{
6306 int rc;
6307
6308 /* Preparation for unregistering fcf */
6309 rc = lpfc_unregister_fcf_prep(phba);
6310 if (rc) {
6311 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6312 "2748 Failed to prepare for unregistering "
6313 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006314 return;
6315 }
6316
James Smartecfd03c2010-02-12 14:41:27 -05006317 /* Now, unregister FCF record and reset HBA FCF state */
6318 rc = lpfc_sli4_unregister_fcf(phba);
6319 if (rc)
6320 return;
6321 /* Reset HBA FCF states after successful unregister FCF */
6322 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006323 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006324
6325 /*
6326 * If driver is not unloading, check if there is any other
6327 * FCF record that can be used for discovery.
6328 */
6329 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006330 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006331 return;
6332
James Smart0c9ab6f2010-02-26 14:15:57 -05006333 /* This is considered as the initial FCF discovery scan */
6334 spin_lock_irq(&phba->hbalock);
6335 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6336 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006337
6338 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006339 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006340
James Smart0c9ab6f2010-02-26 14:15:57 -05006341 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006342
James Smart0c9ab6f2010-02-26 14:15:57 -05006343 if (rc) {
6344 spin_lock_irq(&phba->hbalock);
6345 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6346 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006347 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006348 "2553 lpfc_unregister_unused_fcf failed "
6349 "to read FCF record HBA state x%x\n",
6350 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006351 }
James Smartecfd03c2010-02-12 14:41:27 -05006352}
6353
6354/**
6355 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6356 * @phba: Pointer to hba context object.
6357 *
6358 * This function just unregisters the currently reigstered FCF. It does not
6359 * try to find another FCF for discovery.
6360 */
6361void
6362lpfc_unregister_fcf(struct lpfc_hba *phba)
6363{
6364 int rc;
6365
6366 /* Preparation for unregistering fcf */
6367 rc = lpfc_unregister_fcf_prep(phba);
6368 if (rc) {
6369 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6370 "2749 Failed to prepare for unregistering "
6371 "HBA's FCF record: rc=%d\n", rc);
6372 return;
6373 }
6374
6375 /* Now, unregister FCF record and reset HBA FCF state */
6376 rc = lpfc_sli4_unregister_fcf(phba);
6377 if (rc)
6378 return;
6379 /* Set proper HBA FCF states after successful unregister FCF */
6380 spin_lock_irq(&phba->hbalock);
6381 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6382 spin_unlock_irq(&phba->hbalock);
6383}
6384
6385/**
6386 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6387 * @phba: Pointer to hba context object.
6388 *
6389 * This function check if there are any connected remote port for the FCF and
6390 * if all the devices are disconnected, this function unregister FCFI.
6391 * This function also tries to use another FCF for discovery.
6392 */
6393void
6394lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6395{
6396 /*
James Smart3804dc82010-07-14 15:31:37 -04006397 * If HBA is not running in FIP mode, if HBA does not support
6398 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6399 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006400 */
6401 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006402 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006403 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006404 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006405 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006406 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006407 spin_unlock_irq(&phba->hbalock);
6408 return;
6409 }
6410 spin_unlock_irq(&phba->hbalock);
6411
6412 if (lpfc_fcf_inuse(phba))
6413 return;
6414
6415 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006416}
6417
6418/**
6419 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6420 * @phba: Pointer to hba context object.
6421 * @buff: Buffer containing the FCF connection table as in the config
6422 * region.
6423 * This function create driver data structure for the FCF connection
6424 * record table read from config region 23.
6425 */
6426static void
6427lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6428 uint8_t *buff)
6429{
6430 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6431 struct lpfc_fcf_conn_hdr *conn_hdr;
6432 struct lpfc_fcf_conn_rec *conn_rec;
6433 uint32_t record_count;
6434 int i;
6435
6436 /* Free the current connect table */
6437 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006438 &phba->fcf_conn_rec_list, list) {
6439 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006440 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006441 }
James Smart6fb120a2009-05-22 14:52:59 -04006442
6443 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6444 record_count = conn_hdr->length * sizeof(uint32_t)/
6445 sizeof(struct lpfc_fcf_conn_rec);
6446
6447 conn_rec = (struct lpfc_fcf_conn_rec *)
6448 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6449
6450 for (i = 0; i < record_count; i++) {
6451 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6452 continue;
6453 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6454 GFP_KERNEL);
6455 if (!conn_entry) {
6456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6457 "2566 Failed to allocate connection"
6458 " table entry\n");
6459 return;
6460 }
6461
6462 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6463 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006464 list_add_tail(&conn_entry->list,
6465 &phba->fcf_conn_rec_list);
6466 }
James Smartdf0d0852013-05-31 17:05:08 -04006467
6468 if (!list_empty(&phba->fcf_conn_rec_list)) {
6469 i = 0;
6470 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6471 list) {
6472 conn_rec = &conn_entry->conn_rec;
6473 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6474 "3345 FCF connection list rec[%02d]: "
6475 "flags:x%04x, vtag:x%04x, "
6476 "fabric_name:x%02x:%02x:%02x:%02x:"
6477 "%02x:%02x:%02x:%02x, "
6478 "switch_name:x%02x:%02x:%02x:%02x:"
6479 "%02x:%02x:%02x:%02x\n", i++,
6480 conn_rec->flags, conn_rec->vlan_tag,
6481 conn_rec->fabric_name[0],
6482 conn_rec->fabric_name[1],
6483 conn_rec->fabric_name[2],
6484 conn_rec->fabric_name[3],
6485 conn_rec->fabric_name[4],
6486 conn_rec->fabric_name[5],
6487 conn_rec->fabric_name[6],
6488 conn_rec->fabric_name[7],
6489 conn_rec->switch_name[0],
6490 conn_rec->switch_name[1],
6491 conn_rec->switch_name[2],
6492 conn_rec->switch_name[3],
6493 conn_rec->switch_name[4],
6494 conn_rec->switch_name[5],
6495 conn_rec->switch_name[6],
6496 conn_rec->switch_name[7]);
6497 }
6498 }
James Smart6fb120a2009-05-22 14:52:59 -04006499}
6500
6501/**
6502 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6503 * @phba: Pointer to hba context object.
6504 * @buff: Buffer containing the FCoE parameter data structure.
6505 *
6506 * This function update driver data structure with config
6507 * parameters read from config region 23.
6508 */
6509static void
6510lpfc_read_fcoe_param(struct lpfc_hba *phba,
6511 uint8_t *buff)
6512{
6513 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6514 struct lpfc_fcoe_params *fcoe_param;
6515
6516 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6517 buff;
6518 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006519 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006520
6521 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6522 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6523 return;
6524
James Smart6fb120a2009-05-22 14:52:59 -04006525 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6526 phba->valid_vlan = 1;
6527 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6528 0xFFF;
6529 }
6530
6531 phba->fc_map[0] = fcoe_param->fc_map[0];
6532 phba->fc_map[1] = fcoe_param->fc_map[1];
6533 phba->fc_map[2] = fcoe_param->fc_map[2];
6534 return;
6535}
6536
6537/**
6538 * lpfc_get_rec_conf23 - Get a record type in config region data.
6539 * @buff: Buffer containing config region 23 data.
6540 * @size: Size of the data buffer.
6541 * @rec_type: Record type to be searched.
6542 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006543 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006544 * of the record specified by record_type. If record found, this
6545 * function return pointer to the record else return NULL.
6546 */
6547static uint8_t *
6548lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6549{
6550 uint32_t offset = 0, rec_length;
6551
6552 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6553 (size < sizeof(uint32_t)))
6554 return NULL;
6555
6556 rec_length = buff[offset + 1];
6557
6558 /*
6559 * One TLV record has one word header and number of data words
6560 * specified in the rec_length field of the record header.
6561 */
6562 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6563 <= size) {
6564 if (buff[offset] == rec_type)
6565 return &buff[offset];
6566
6567 if (buff[offset] == LPFC_REGION23_LAST_REC)
6568 return NULL;
6569
6570 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6571 rec_length = buff[offset + 1];
6572 }
6573 return NULL;
6574}
6575
6576/**
6577 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6578 * @phba: Pointer to lpfc_hba data structure.
6579 * @buff: Buffer containing config region 23 data.
6580 * @size: Size of the data buffer.
6581 *
Stefan Weileef35c22010-08-06 21:11:15 +02006582 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006583 * populate driver data structure with the parameters.
6584 */
6585void
6586lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6587 uint8_t *buff,
6588 uint32_t size)
6589{
James Smarteb016562014-09-03 12:58:06 -04006590 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006591 uint8_t *rec_ptr;
6592
6593 /*
6594 * If data size is less than 2 words signature and version cannot be
6595 * verified.
6596 */
6597 if (size < 2*sizeof(uint32_t))
6598 return;
6599
6600 /* Check the region signature first */
6601 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6603 "2567 Config region 23 has bad signature\n");
6604 return;
6605 }
6606
6607 offset += 4;
6608
6609 /* Check the data structure version */
6610 if (buff[offset] != LPFC_REGION23_VERSION) {
6611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6612 "2568 Config region 23 has bad version\n");
6613 return;
6614 }
6615 offset += 4;
6616
James Smart6fb120a2009-05-22 14:52:59 -04006617 /* Read FCoE param record */
6618 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6619 size - offset, FCOE_PARAM_TYPE);
6620 if (rec_ptr)
6621 lpfc_read_fcoe_param(phba, rec_ptr);
6622
6623 /* Read FCF connection table */
6624 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6625 size - offset, FCOE_CONN_TBL_TYPE);
6626 if (rec_ptr)
6627 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6628
6629}