blob: 8936f5d91c873ec237be8089f84a3a9a463a76b7 [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) ||
913 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
914 lpfc_nvme_update_localport(vports[i]);
James Smart549e55c2007-08-02 11:09:51 -0400915 }
James Smart895427b2017-02-12 13:52:30 -0800916 }
James Smart09372822008-01-11 01:52:54 -0500917 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500918 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500919 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
920 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400921 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500922 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500923 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400924 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500925 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500926 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500927 }
928 }
929
dea31012005-04-17 16:05:31 -0500930 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500931 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500932 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
933 if (mb) {
dea31012005-04-17 16:05:31 -0500934 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500935 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500936 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400937 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500938 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500939 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500940 }
941 }
James Smart2e0fef82007-06-17 19:56:36 -0500942 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500943 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500944 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500945 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500946 return 0;
dea31012005-04-17 16:05:31 -0500947}
948
James Smart92d7f7b2007-06-17 19:56:38 -0500949static void
950lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500951{
James Smart92d7f7b2007-06-17 19:56:38 -0500952 struct lpfc_nodelist *ndlp;
953
954 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500955 if (!NLP_CHK_NODE_ACT(ndlp))
956 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500957 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
958 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500959 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500960 /* On Linkup its safe to clean up the ndlp
961 * from Fabric connections.
962 */
James Smart92d7f7b2007-06-17 19:56:38 -0500963 if (ndlp->nlp_DID != Fabric_DID)
964 lpfc_unreg_rpi(vport, ndlp);
965 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
966 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500967 /* Fail outstanding IO now since device is
968 * marked for PLOGI.
969 */
James Smart92d7f7b2007-06-17 19:56:38 -0500970 lpfc_unreg_rpi(vport, ndlp);
971 }
972 }
973}
974
975static void
976lpfc_linkup_port(struct lpfc_vport *vport)
977{
978 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500979 struct lpfc_hba *phba = vport->phba;
980
981 if ((vport->load_flag & FC_UNLOADING) != 0)
982 return;
983
James Smart858c9f62007-06-17 19:56:39 -0500984 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
985 "Link Up: top:x%x speed:x%x flg:x%x",
986 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
987
James Smart92d7f7b2007-06-17 19:56:38 -0500988 /* If NPIV is not enabled, only bring the physical port up */
989 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
990 (vport != phba->pport))
991 return;
dea31012005-04-17 16:05:31 -0500992
James Smarta0f2d3e2017-02-12 13:52:31 -0800993 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
994 fc_host_post_event(shost, fc_get_event_number(),
995 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400996
James Smart2e0fef82007-06-17 19:56:36 -0500997 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500998 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
999 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1000 vport->fc_flag |= FC_NDISC_ACTIVE;
1001 vport->fc_ns_retry = 0;
1002 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001003
James Smart92d7f7b2007-06-17 19:56:38 -05001004 if (vport->fc_flag & FC_LBIT)
1005 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001006
James Smart92d7f7b2007-06-17 19:56:38 -05001007}
1008
1009static int
1010lpfc_linkup(struct lpfc_hba *phba)
1011{
James Smart549e55c2007-08-02 11:09:51 -04001012 struct lpfc_vport **vports;
1013 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001014
1015 phba->link_state = LPFC_LINK_UP;
1016
1017 /* Unblock fabric iocbs if they are blocked */
1018 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1019 del_timer_sync(&phba->fabric_block_timer);
1020
James Smart549e55c2007-08-02 11:09:51 -04001021 vports = lpfc_create_vport_work_array(phba);
1022 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001023 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001024 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001025 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001026
1027 return 0;
1028}
1029
1030/*
1031 * This routine handles processing a CLEAR_LA mailbox
1032 * command upon completion. It is setup in the LPFC_MBOXQ
1033 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001034 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001035 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001036static void
James Smart2e0fef82007-06-17 19:56:36 -05001037lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001038{
James Smart2e0fef82007-06-17 19:56:36 -05001039 struct lpfc_vport *vport = pmb->vport;
1040 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1041 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001042 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001043 uint32_t control;
1044
dea31012005-04-17 16:05:31 -05001045 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001046 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1047 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001048
1049 /* Check for error */
1050 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001051 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001052 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1053 "0320 CLEAR_LA mbxStatus error x%x hba "
1054 "state x%x\n",
1055 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001056 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001057 goto out;
1058 }
1059
James Smart92d7f7b2007-06-17 19:56:38 -05001060 if (vport->port_type == LPFC_PHYSICAL_PORT)
1061 phba->link_state = LPFC_HBA_READY;
1062
1063 spin_lock_irq(&phba->hbalock);
1064 psli->sli_flag |= LPFC_PROCESS_LA;
1065 control = readl(phba->HCregaddr);
1066 control |= HC_LAINT_ENA;
1067 writel(control, phba->HCregaddr);
1068 readl(phba->HCregaddr); /* flush */
1069 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001070 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001071 return;
dea31012005-04-17 16:05:31 -05001072
dea31012005-04-17 16:05:31 -05001073out:
1074 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001075 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1076 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001077 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001078
James Smart2e0fef82007-06-17 19:56:36 -05001079 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001080 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001081 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001082
James Smart2e0fef82007-06-17 19:56:36 -05001083 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001084
1085 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001086
1087 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001088 psli->sli_flag |= LPFC_PROCESS_LA;
1089 control = readl(phba->HCregaddr);
1090 control |= HC_LAINT_ENA;
1091 writel(control, phba->HCregaddr);
1092 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001093 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001094
1095 return;
1096}
1097
James Smart2e0fef82007-06-17 19:56:36 -05001098
James Smartd6de08c2015-12-16 18:11:53 -05001099void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001100lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001101{
James Smart2e0fef82007-06-17 19:56:36 -05001102 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001103
James Smart04c68492009-05-22 14:52:52 -04001104 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001105 goto out;
dea31012005-04-17 16:05:31 -05001106
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001107 mempool_free(pmb, phba->mbox_mem_pool);
1108
James Smart1b511972011-12-13 13:23:09 -05001109 /* don't perform discovery for SLI4 loopback diagnostic test */
1110 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1111 !(phba->hba_flag & HBA_FCOE_MODE) &&
1112 (phba->link_flag & LS_LOOPBACK_MODE))
1113 return;
1114
James Smart76a95d72010-11-20 23:11:48 -05001115 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001116 vport->fc_flag & FC_PUBLIC_LOOP &&
1117 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001118 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001119 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001120 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1121 */
James Smart2e0fef82007-06-17 19:56:36 -05001122 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001123 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001124 }
dea31012005-04-17 16:05:31 -05001125
James Smart2e0fef82007-06-17 19:56:36 -05001126 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001127 * LPFC_FLOGI while waiting for FLOGI cmpl
1128 */
James Smartd6de08c2015-12-16 18:11:53 -05001129 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001130 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001131 else if (vport->fc_flag & FC_PT2PT)
1132 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001133 return;
dea31012005-04-17 16:05:31 -05001134
1135out:
James Smarte8b62012007-08-02 11:10:09 -04001136 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1137 "0306 CONFIG_LINK mbxStatus error x%x "
1138 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001139 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001140 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001141
1142 lpfc_linkdown(phba);
1143
James Smarte8b62012007-08-02 11:10:09 -04001144 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1145 "0200 CONFIG_LINK bad hba state x%x\n",
1146 vport->port_state);
dea31012005-04-17 16:05:31 -05001147
James Smart92d7f7b2007-06-17 19:56:38 -05001148 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001149 return;
1150}
1151
James Smart7d791df2011-07-22 18:37:52 -04001152/**
1153 * lpfc_sli4_clear_fcf_rr_bmask
1154 * @phba pointer to the struct lpfc_hba for this port.
1155 * This fucnction resets the round robin bit mask and clears the
1156 * fcf priority list. The list deletions are done while holding the
1157 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1158 * from the lpfc_fcf_pri record.
1159 **/
1160void
1161lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1162{
1163 struct lpfc_fcf_pri *fcf_pri;
1164 struct lpfc_fcf_pri *next_fcf_pri;
1165 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1166 spin_lock_irq(&phba->hbalock);
1167 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1168 &phba->fcf.fcf_pri_list, list) {
1169 list_del_init(&fcf_pri->list);
1170 fcf_pri->fcf_rec.flag = 0;
1171 }
1172 spin_unlock_irq(&phba->hbalock);
1173}
dea31012005-04-17 16:05:31 -05001174static void
James Smart6fb120a2009-05-22 14:52:59 -04001175lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1176{
1177 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001178
1179 if (mboxq->u.mb.mbxStatus) {
1180 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1181 "2017 REG_FCFI mbxStatus error x%x "
1182 "HBA state x%x\n",
1183 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001184 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001185 }
1186
1187 /* Start FCoE discovery by sending a FLOGI. */
1188 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1189 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001190 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001191 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001192 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001193
James Smart32b97932009-07-19 10:01:21 -04001194 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001195 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1196 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001197 goto fail_out;
1198
1199 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001200 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001201 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001202 phba->hba_flag &= ~FCF_TS_INPROG;
1203 if (vport->port_state != LPFC_FLOGI) {
1204 phba->hba_flag |= FCF_RR_INPROG;
1205 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001206 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001207 goto out;
1208 }
1209 spin_unlock_irq(&phba->hbalock);
1210 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001211
James Smarta93ff372010-10-22 11:06:08 -04001212fail_out:
1213 spin_lock_irq(&phba->hbalock);
1214 phba->hba_flag &= ~FCF_RR_INPROG;
1215 spin_unlock_irq(&phba->hbalock);
1216out:
James Smart6fb120a2009-05-22 14:52:59 -04001217 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001218}
1219
1220/**
1221 * lpfc_fab_name_match - Check if the fcf fabric name match.
1222 * @fab_name: pointer to fabric name.
1223 * @new_fcf_record: pointer to fcf record.
1224 *
1225 * This routine compare the fcf record's fabric name with provided
1226 * fabric name. If the fabric name are identical this function
1227 * returns 1 else return 0.
1228 **/
1229static uint32_t
1230lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1231{
James Smartecfd03c2010-02-12 14:41:27 -05001232 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001233 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001234 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1235 return 0;
1236 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1237 return 0;
1238 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1239 return 0;
1240 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1241 return 0;
1242 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1243 return 0;
1244 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1245 return 0;
1246 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1247 return 0;
1248 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001249}
1250
1251/**
James Smart8fa38512009-07-19 10:01:03 -04001252 * lpfc_sw_name_match - Check if the fcf switch name match.
1253 * @fab_name: pointer to fabric name.
1254 * @new_fcf_record: pointer to fcf record.
1255 *
1256 * This routine compare the fcf record's switch name with provided
1257 * switch name. If the switch name are identical this function
1258 * returns 1 else return 0.
1259 **/
1260static uint32_t
1261lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1262{
James Smartecfd03c2010-02-12 14:41:27 -05001263 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001264 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001265 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1266 return 0;
1267 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1268 return 0;
1269 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1270 return 0;
1271 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1272 return 0;
1273 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1274 return 0;
1275 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1276 return 0;
1277 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1278 return 0;
1279 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001280}
1281
1282/**
James Smart6fb120a2009-05-22 14:52:59 -04001283 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001284 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001285 * @new_fcf_record: pointer to fcf record.
1286 *
1287 * This routine compare the fcf record's mac address with HBA's
1288 * FCF mac address. If the mac addresses are identical this function
1289 * returns 1 else return 0.
1290 **/
1291static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001292lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001293{
James Smartecfd03c2010-02-12 14:41:27 -05001294 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001295 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001296 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1297 return 0;
1298 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1299 return 0;
1300 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1301 return 0;
1302 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1303 return 0;
1304 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1305 return 0;
1306 return 1;
1307}
1308
1309static bool
1310lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1311{
1312 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001313}
1314
1315/**
James Smart7d791df2011-07-22 18:37:52 -04001316 * lpfc_update_fcf_record - Update driver fcf record
1317 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1318 * @phba: pointer to lpfc hba data structure.
1319 * @fcf_index: Index for the lpfc_fcf_record.
1320 * @new_fcf_record: pointer to hba fcf record.
1321 *
1322 * This routine updates the driver FCF priority record from the new HBA FCF
1323 * record. This routine is called with the host lock held.
1324 **/
1325static void
1326__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1327 struct fcf_record *new_fcf_record
1328 )
1329{
1330 struct lpfc_fcf_pri *fcf_pri;
1331
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001332 lockdep_assert_held(&phba->hbalock);
1333
James Smart7d791df2011-07-22 18:37:52 -04001334 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1335 fcf_pri->fcf_rec.fcf_index = fcf_index;
1336 /* FCF record priority */
1337 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1338
1339}
1340
1341/**
James Smart6fb120a2009-05-22 14:52:59 -04001342 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001343 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001344 * @new_fcf_record: pointer to fcf record.
1345 *
1346 * This routine copies the FCF information from the FCF
1347 * record to lpfc_hba data structure.
1348 **/
1349static void
James Smartecfd03c2010-02-12 14:41:27 -05001350lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1351 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001352{
James Smartecfd03c2010-02-12 14:41:27 -05001353 /* Fabric name */
1354 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001355 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001356 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001357 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001358 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001359 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001360 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001361 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001362 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001363 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001364 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001365 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001366 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001367 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001368 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001369 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001370 /* Mac address */
1371 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1372 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1373 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1374 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1375 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1376 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1377 /* FCF record index */
1378 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1379 /* FCF record priority */
1380 fcf_rec->priority = new_fcf_record->fip_priority;
1381 /* Switch name */
1382 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001383 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001384 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001385 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001386 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001387 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001388 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001389 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001390 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001391 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001392 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001393 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001394 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001395 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001396 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001397 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001398}
1399
1400/**
James Smartecfd03c2010-02-12 14:41:27 -05001401 * lpfc_update_fcf_record - Update driver fcf record
1402 * @phba: pointer to lpfc hba data structure.
1403 * @fcf_rec: pointer to driver fcf record.
1404 * @new_fcf_record: pointer to hba fcf record.
1405 * @addr_mode: address mode to be set to the driver fcf record.
1406 * @vlan_id: vlan tag to be set to the driver fcf record.
1407 * @flag: flag bits to be set to the driver fcf record.
1408 *
1409 * This routine updates the driver FCF record from the new HBA FCF record
1410 * together with the address mode, vlan_id, and other informations. This
1411 * routine is called with the host lock held.
1412 **/
1413static void
1414__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1415 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1416 uint16_t vlan_id, uint32_t flag)
1417{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001418 lockdep_assert_held(&phba->hbalock);
1419
James Smartecfd03c2010-02-12 14:41:27 -05001420 /* Copy the fields from the HBA's FCF record */
1421 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1422 /* Update other fields of driver FCF record */
1423 fcf_rec->addr_mode = addr_mode;
1424 fcf_rec->vlan_id = vlan_id;
1425 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001426 __lpfc_update_fcf_record_pri(phba,
1427 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1428 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001429}
1430
1431/**
James Smart6fb120a2009-05-22 14:52:59 -04001432 * lpfc_register_fcf - Register the FCF with hba.
1433 * @phba: pointer to lpfc hba data structure.
1434 *
1435 * This routine issues a register fcfi mailbox command to register
1436 * the fcf with HBA.
1437 **/
1438static void
1439lpfc_register_fcf(struct lpfc_hba *phba)
1440{
1441 LPFC_MBOXQ_t *fcf_mbxq;
1442 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001443
James Smartd439d282010-09-29 11:18:45 -04001444 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001445 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001446 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001447 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001448 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001449 return;
1450 }
1451
1452 /* The FCF is already registered, start discovery */
1453 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001454 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001455 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001456 if (phba->pport->port_state != LPFC_FLOGI &&
1457 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001458 phba->hba_flag |= FCF_RR_INPROG;
1459 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001460 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001461 return;
1462 }
1463 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001464 return;
1465 }
James Smartd439d282010-09-29 11:18:45 -04001466 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001467
James Smarta93ff372010-10-22 11:06:08 -04001468 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001469 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001470 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001471 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001472 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001473 return;
James Smart4d9ab992009-10-02 15:16:39 -04001474 }
James Smart6fb120a2009-05-22 14:52:59 -04001475
1476 lpfc_reg_fcfi(phba, fcf_mbxq);
1477 fcf_mbxq->vport = phba->pport;
1478 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1479 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001480 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001481 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001482 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001483 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001484 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001485 }
James Smart6fb120a2009-05-22 14:52:59 -04001486
1487 return;
1488}
1489
1490/**
1491 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1492 * @phba: pointer to lpfc hba data structure.
1493 * @new_fcf_record: pointer to fcf record.
1494 * @boot_flag: Indicates if this record used by boot bios.
1495 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001496 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001497 *
1498 * This routine compare the fcf record with connect list obtained from the
1499 * config region to decide if this FCF can be used for SAN discovery. It returns
1500 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1501 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1502 * is used by boot bios and addr_mode will indicate the addressing mode to be
1503 * used for this FCF when the function returns.
1504 * If the FCF record need to be used with a particular vlan id, the vlan is
1505 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001506 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001507 **/
1508static int
1509lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1510 struct fcf_record *new_fcf_record,
1511 uint32_t *boot_flag, uint32_t *addr_mode,
1512 uint16_t *vlan_id)
1513{
1514 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001515 int i, j, fcf_vlan_id = 0;
1516
1517 /* Find the lowest VLAN id in the FCF record */
1518 for (i = 0; i < 512; i++) {
1519 if (new_fcf_record->vlan_bitmap[i]) {
1520 fcf_vlan_id = i * 8;
1521 j = 0;
1522 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1523 j++;
1524 fcf_vlan_id++;
1525 }
1526 break;
1527 }
1528 }
James Smart6fb120a2009-05-22 14:52:59 -04001529
James Smart26979ce2012-09-29 11:31:55 -04001530 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001531 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001532 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1533 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001534 return 0;
1535
James Smart45ed1192009-10-02 15:17:02 -04001536 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001537 *boot_flag = 0;
1538 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1539 new_fcf_record);
1540 if (phba->valid_vlan)
1541 *vlan_id = phba->vlan_id;
1542 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001543 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001544 return 1;
1545 }
1546
1547 /*
1548 * If there are no FCF connection table entry, driver connect to all
1549 * FCFs.
1550 */
1551 if (list_empty(&phba->fcf_conn_rec_list)) {
1552 *boot_flag = 0;
1553 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1554 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001555
1556 /*
1557 * When there are no FCF connect entries, use driver's default
1558 * addressing mode - FPMA.
1559 */
1560 if (*addr_mode & LPFC_FCF_FPMA)
1561 *addr_mode = LPFC_FCF_FPMA;
1562
James Smart4d9ab992009-10-02 15:16:39 -04001563 /* If FCF record report a vlan id use that vlan id */
1564 if (fcf_vlan_id)
1565 *vlan_id = fcf_vlan_id;
1566 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001567 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001568 return 1;
1569 }
1570
James Smartecfd03c2010-02-12 14:41:27 -05001571 list_for_each_entry(conn_entry,
1572 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001573 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1574 continue;
1575
1576 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1577 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001578 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001579 continue;
James Smart8fa38512009-07-19 10:01:03 -04001580 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1581 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1582 new_fcf_record))
1583 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001584 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1585 /*
1586 * If the vlan bit map does not have the bit set for the
1587 * vlan id to be used, then it is not a match.
1588 */
1589 if (!(new_fcf_record->vlan_bitmap
1590 [conn_entry->conn_rec.vlan_tag / 8] &
1591 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1592 continue;
1593 }
1594
1595 /*
James Smart0c287582009-06-10 17:22:56 -04001596 * If connection record does not support any addressing mode,
1597 * skip the FCF record.
1598 */
1599 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1600 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1601 continue;
1602
1603 /*
James Smart6fb120a2009-05-22 14:52:59 -04001604 * Check if the connection record specifies a required
1605 * addressing mode.
1606 */
1607 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1608 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1609
1610 /*
1611 * If SPMA required but FCF not support this continue.
1612 */
1613 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1614 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1615 new_fcf_record) & LPFC_FCF_SPMA))
1616 continue;
1617
1618 /*
1619 * If FPMA required but FCF not support this continue.
1620 */
1621 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1622 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1623 new_fcf_record) & LPFC_FCF_FPMA))
1624 continue;
1625 }
1626
1627 /*
1628 * This fcf record matches filtering criteria.
1629 */
1630 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1631 *boot_flag = 1;
1632 else
1633 *boot_flag = 0;
1634
James Smart0c287582009-06-10 17:22:56 -04001635 /*
1636 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001637 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001638 * by FCF, allow fabric to pick the addressing mode.
1639 */
James Smart6fb120a2009-05-22 14:52:59 -04001640 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1641 new_fcf_record);
1642 /*
1643 * If the user specified a required address mode, assign that
1644 * address mode
1645 */
1646 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1647 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1648 *addr_mode = (conn_entry->conn_rec.flags &
1649 FCFCNCT_AM_SPMA) ?
1650 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1651 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001652 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001653 * addr mode only if FCF support the addr_mode.
1654 */
1655 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1656 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1657 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1658 (*addr_mode & LPFC_FCF_SPMA))
1659 *addr_mode = LPFC_FCF_SPMA;
1660 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1661 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1662 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1663 (*addr_mode & LPFC_FCF_FPMA))
1664 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001665
James Smart4d9ab992009-10-02 15:16:39 -04001666 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001667 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1668 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001669 /*
1670 * If no vlan id is specified in connect list, use the vlan id
1671 * in the FCF record
1672 */
1673 else if (fcf_vlan_id)
1674 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001675 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001676 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001677
1678 return 1;
1679 }
1680
1681 return 0;
1682}
1683
1684/**
James Smart32b97932009-07-19 10:01:21 -04001685 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1686 * @phba: pointer to lpfc hba data structure.
1687 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1688 *
1689 * This function check if there is any fcoe event pending while driver
1690 * scan FCF entries. If there is any pending event, it will restart the
1691 * FCF saning and return 1 else return 0.
1692 */
1693int
1694lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1695{
James Smart32b97932009-07-19 10:01:21 -04001696 /*
1697 * If the Link is up and no FCoE events while in the
1698 * FCF discovery, no need to restart FCF discovery.
1699 */
1700 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001701 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001702 return 0;
1703
James Smart0c9ab6f2010-02-26 14:15:57 -05001704 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1705 "2768 Pending link or FCF event during current "
1706 "handling of the previous event: link_state:x%x, "
1707 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1708 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1709 phba->fcoe_eventtag);
1710
James Smart32b97932009-07-19 10:01:21 -04001711 spin_lock_irq(&phba->hbalock);
1712 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1713 spin_unlock_irq(&phba->hbalock);
1714
James Smart0c9ab6f2010-02-26 14:15:57 -05001715 if (phba->link_state >= LPFC_LINK_UP) {
1716 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1717 "2780 Restart FCF table scan due to "
1718 "pending FCF event:evt_tag_at_scan:x%x, "
1719 "evt_tag_current:x%x\n",
1720 phba->fcoe_eventtag_at_fcf_scan,
1721 phba->fcoe_eventtag);
1722 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1723 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001724 /*
James Smarta93ff372010-10-22 11:06:08 -04001725 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001726 * flag
1727 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001728 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1729 "2833 Stop FCF discovery process due to link "
1730 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001731 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001732 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001733 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001734 spin_unlock_irq(&phba->hbalock);
1735 }
James Smart32b97932009-07-19 10:01:21 -04001736
James Smart0c9ab6f2010-02-26 14:15:57 -05001737 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001738 if (unreg_fcf) {
1739 spin_lock_irq(&phba->hbalock);
1740 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1741 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001742 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001743 }
James Smart32b97932009-07-19 10:01:21 -04001744 return 1;
1745}
1746
1747/**
James Smart999d8132010-03-15 11:24:56 -04001748 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1749 * @phba: pointer to lpfc hba data structure.
1750 * @fcf_cnt: number of eligible fcf record seen so far.
1751 *
1752 * This function makes an running random selection decision on FCF record to
1753 * use through a sequence of @fcf_cnt eligible FCF records with equal
1754 * probability. To perform integer manunipulation of random numbers with
1755 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001756 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001757 *
1758 * Returns true when outcome is for the newly read FCF record should be
1759 * chosen; otherwise, return false when outcome is for keeping the previously
1760 * chosen FCF record.
1761 **/
1762static bool
1763lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1764{
1765 uint32_t rand_num;
1766
1767 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001768 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001769
1770 /* Decision with probability 1/fcf_cnt */
1771 if ((fcf_cnt * rand_num) < 0xFFFF)
1772 return true;
1773 else
1774 return false;
1775}
1776
1777/**
James Smart3804dc82010-07-14 15:31:37 -04001778 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001779 * @phba: pointer to lpfc hba data structure.
1780 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001781 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001782 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001783 * This routine parses the non-embedded fcf mailbox command by performing the
1784 * necessarily error checking, non-embedded read FCF record mailbox command
1785 * SGE parsing, and endianness swapping.
1786 *
1787 * Returns the pointer to the new FCF record in the non-embedded mailbox
1788 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001789 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001790static struct fcf_record *
1791lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1792 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001793{
1794 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001795 struct lpfc_mbx_sge sge;
1796 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001797 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001798 union lpfc_sli4_cfg_shdr *shdr;
1799 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001800
James Smart6fb120a2009-05-22 14:52:59 -04001801 /* Get the first SGE entry from the non-embedded DMA memory. This
1802 * routine only uses a single SGE.
1803 */
1804 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001805 if (unlikely(!mboxq->sge_array)) {
1806 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1807 "2524 Failed to get the non-embedded SGE "
1808 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001809 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001810 }
1811 virt_addr = mboxq->sge_array->addr[0];
1812
1813 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001814 lpfc_sli_pcimem_bcopy(shdr, shdr,
1815 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001816 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001817 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001818 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001819 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001820 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1821 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001822 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001823 "2726 READ_FCF_RECORD Indicates empty "
1824 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001825 else
1826 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001827 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001828 "with status x%x add_status x%x, "
1829 "mbx\n", shdr_status, shdr_add_status);
1830 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001831 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001832
1833 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001834 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1835 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1836 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001837 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001838 new_fcf_record = (struct fcf_record *)(virt_addr +
1839 sizeof(struct lpfc_mbx_read_fcf_tbl));
1840 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001841 offsetof(struct fcf_record, vlan_bitmap));
1842 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1843 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001844
James Smart0c9ab6f2010-02-26 14:15:57 -05001845 return new_fcf_record;
1846}
1847
1848/**
1849 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1850 * @phba: pointer to lpfc hba data structure.
1851 * @fcf_record: pointer to the fcf record.
1852 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1853 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1854 *
1855 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1856 * enabled.
1857 **/
1858static void
1859lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1860 struct fcf_record *fcf_record,
1861 uint16_t vlan_id,
1862 uint16_t next_fcf_index)
1863{
1864 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1865 "2764 READ_FCF_RECORD:\n"
1866 "\tFCF_Index : x%x\n"
1867 "\tFCF_Avail : x%x\n"
1868 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001869 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001870 "\tFIP_Priority : x%x\n"
1871 "\tMAC_Provider : x%x\n"
1872 "\tLowest VLANID : x%x\n"
1873 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1874 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1875 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1876 "\tNext_FCF_Index: x%x\n",
1877 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1878 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1879 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001880 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001881 fcf_record->fip_priority,
1882 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1883 vlan_id,
1884 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1885 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1886 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1887 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1888 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1889 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1890 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1891 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1892 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1893 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1894 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1895 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1896 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1897 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1898 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1899 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1900 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1901 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1902 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1903 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1904 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1905 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1906 next_fcf_index);
1907}
1908
1909/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001910 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1911 * @phba: pointer to lpfc hba data structure.
1912 * @fcf_rec: pointer to an existing FCF record.
1913 * @new_fcf_record: pointer to a new FCF record.
1914 * @new_vlan_id: vlan id from the new FCF record.
1915 *
1916 * This function performs matching test of a new FCF record against an existing
1917 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1918 * will not be used as part of the FCF record matching criteria.
1919 *
1920 * Returns true if all the fields matching, otherwise returns false.
1921 */
1922static bool
1923lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1924 struct lpfc_fcf_rec *fcf_rec,
1925 struct fcf_record *new_fcf_record,
1926 uint16_t new_vlan_id)
1927{
1928 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1929 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1930 return false;
1931 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1932 return false;
1933 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1934 return false;
1935 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1936 return false;
James Smart7d791df2011-07-22 18:37:52 -04001937 if (fcf_rec->priority != new_fcf_record->fip_priority)
1938 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001939 return true;
1940}
1941
1942/**
James Smarta93ff372010-10-22 11:06:08 -04001943 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1944 * @vport: Pointer to vport object.
1945 * @fcf_index: index to next fcf.
1946 *
1947 * This function processing the roundrobin fcf failover to next fcf index.
1948 * When this function is invoked, there will be a current fcf registered
1949 * for flogi.
1950 * Return: 0 for continue retrying flogi on currently registered fcf;
1951 * 1 for stop flogi on currently registered fcf;
1952 */
1953int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1954{
1955 struct lpfc_hba *phba = vport->phba;
1956 int rc;
1957
1958 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1959 spin_lock_irq(&phba->hbalock);
1960 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1961 spin_unlock_irq(&phba->hbalock);
1962 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1963 "2872 Devloss tmo with no eligible "
1964 "FCF, unregister in-use FCF (x%x) "
1965 "and rescan FCF table\n",
1966 phba->fcf.current_rec.fcf_indx);
1967 lpfc_unregister_fcf_rescan(phba);
1968 goto stop_flogi_current_fcf;
1969 }
1970 /* Mark the end to FLOGI roundrobin failover */
1971 phba->hba_flag &= ~FCF_RR_INPROG;
1972 /* Allow action to new fcf asynchronous event */
1973 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1974 spin_unlock_irq(&phba->hbalock);
1975 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1976 "2865 No FCF available, stop roundrobin FCF "
1977 "failover and change port state:x%x/x%x\n",
1978 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1979 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1980 goto stop_flogi_current_fcf;
1981 } else {
1982 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1983 "2794 Try FLOGI roundrobin FCF failover to "
1984 "(x%x)\n", fcf_index);
1985 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1986 if (rc)
1987 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1988 "2761 FLOGI roundrobin FCF failover "
1989 "failed (rc:x%x) to read FCF (x%x)\n",
1990 rc, phba->fcf.current_rec.fcf_indx);
1991 else
1992 goto stop_flogi_current_fcf;
1993 }
1994 return 0;
1995
1996stop_flogi_current_fcf:
1997 lpfc_can_disctmo(vport);
1998 return 1;
1999}
2000
2001/**
James Smart7d791df2011-07-22 18:37:52 -04002002 * lpfc_sli4_fcf_pri_list_del
2003 * @phba: pointer to lpfc hba data structure.
2004 * @fcf_index the index of the fcf record to delete
2005 * This routine checks the on list flag of the fcf_index to be deleted.
2006 * If it is one the list then it is removed from the list, and the flag
2007 * is cleared. This routine grab the hbalock before removing the fcf
2008 * record from the list.
2009 **/
2010static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2011 uint16_t fcf_index)
2012{
2013 struct lpfc_fcf_pri *new_fcf_pri;
2014
2015 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2016 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2017 "3058 deleting idx x%x pri x%x flg x%x\n",
2018 fcf_index, new_fcf_pri->fcf_rec.priority,
2019 new_fcf_pri->fcf_rec.flag);
2020 spin_lock_irq(&phba->hbalock);
2021 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2022 if (phba->fcf.current_rec.priority ==
2023 new_fcf_pri->fcf_rec.priority)
2024 phba->fcf.eligible_fcf_cnt--;
2025 list_del_init(&new_fcf_pri->list);
2026 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2027 }
2028 spin_unlock_irq(&phba->hbalock);
2029}
2030
2031/**
2032 * lpfc_sli4_set_fcf_flogi_fail
2033 * @phba: pointer to lpfc hba data structure.
2034 * @fcf_index the index of the fcf record to update
2035 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2036 * flag so the the round robin slection for the particular priority level
2037 * will try a different fcf record that does not have this bit set.
2038 * If the fcf record is re-read for any reason this flag is cleared brfore
2039 * adding it to the priority list.
2040 **/
2041void
2042lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2043{
2044 struct lpfc_fcf_pri *new_fcf_pri;
2045 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2046 spin_lock_irq(&phba->hbalock);
2047 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2048 spin_unlock_irq(&phba->hbalock);
2049}
2050
2051/**
2052 * lpfc_sli4_fcf_pri_list_add
2053 * @phba: pointer to lpfc hba data structure.
2054 * @fcf_index the index of the fcf record to add
2055 * This routine checks the priority of the fcf_index to be added.
2056 * If it is a lower priority than the current head of the fcf_pri list
2057 * then it is added to the list in the right order.
2058 * If it is the same priority as the current head of the list then it
2059 * is added to the head of the list and its bit in the rr_bmask is set.
2060 * If the fcf_index to be added is of a higher priority than the current
2061 * head of the list then the rr_bmask is cleared, its bit is set in the
2062 * rr_bmask and it is added to the head of the list.
2063 * returns:
2064 * 0=success 1=failure
2065 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002066static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2067 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002068 struct fcf_record *new_fcf_record)
2069{
2070 uint16_t current_fcf_pri;
2071 uint16_t last_index;
2072 struct lpfc_fcf_pri *fcf_pri;
2073 struct lpfc_fcf_pri *next_fcf_pri;
2074 struct lpfc_fcf_pri *new_fcf_pri;
2075 int ret;
2076
2077 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2078 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2079 "3059 adding idx x%x pri x%x flg x%x\n",
2080 fcf_index, new_fcf_record->fip_priority,
2081 new_fcf_pri->fcf_rec.flag);
2082 spin_lock_irq(&phba->hbalock);
2083 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2084 list_del_init(&new_fcf_pri->list);
2085 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2086 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2087 if (list_empty(&phba->fcf.fcf_pri_list)) {
2088 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2089 ret = lpfc_sli4_fcf_rr_index_set(phba,
2090 new_fcf_pri->fcf_rec.fcf_index);
2091 goto out;
2092 }
2093
2094 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2095 LPFC_SLI4_FCF_TBL_INDX_MAX);
2096 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2097 ret = 0; /* Empty rr list */
2098 goto out;
2099 }
2100 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2101 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2102 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2103 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2104 memset(phba->fcf.fcf_rr_bmask, 0,
2105 sizeof(*phba->fcf.fcf_rr_bmask));
2106 /* fcfs_at_this_priority_level = 1; */
2107 phba->fcf.eligible_fcf_cnt = 1;
2108 } else
2109 /* fcfs_at_this_priority_level++; */
2110 phba->fcf.eligible_fcf_cnt++;
2111 ret = lpfc_sli4_fcf_rr_index_set(phba,
2112 new_fcf_pri->fcf_rec.fcf_index);
2113 goto out;
2114 }
2115
2116 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2117 &phba->fcf.fcf_pri_list, list) {
2118 if (new_fcf_pri->fcf_rec.priority <=
2119 fcf_pri->fcf_rec.priority) {
2120 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2121 list_add(&new_fcf_pri->list,
2122 &phba->fcf.fcf_pri_list);
2123 else
2124 list_add(&new_fcf_pri->list,
2125 &((struct lpfc_fcf_pri *)
2126 fcf_pri->list.prev)->list);
2127 ret = 0;
2128 goto out;
2129 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2130 || new_fcf_pri->fcf_rec.priority <
2131 next_fcf_pri->fcf_rec.priority) {
2132 list_add(&new_fcf_pri->list, &fcf_pri->list);
2133 ret = 0;
2134 goto out;
2135 }
2136 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2137 continue;
2138
2139 }
2140 ret = 1;
2141out:
2142 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2143 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2144 spin_unlock_irq(&phba->hbalock);
2145 return ret;
2146}
2147
2148/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002149 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2150 * @phba: pointer to lpfc hba data structure.
2151 * @mboxq: pointer to mailbox object.
2152 *
2153 * This function iterates through all the fcf records available in
2154 * HBA and chooses the optimal FCF record for discovery. After finding
2155 * the FCF for discovery it registers the FCF record and kicks start
2156 * discovery.
2157 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2158 * use an FCF record which matches fabric name and mac address of the
2159 * currently used FCF record.
2160 * If the driver supports only one FCF, it will try to use the FCF record
2161 * used by BOOT_BIOS.
2162 */
2163void
2164lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2165{
2166 struct fcf_record *new_fcf_record;
2167 uint32_t boot_flag, addr_mode;
2168 uint16_t fcf_index, next_fcf_index;
2169 struct lpfc_fcf_rec *fcf_rec = NULL;
2170 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002171 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002172 int rc;
2173
2174 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002175 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002176 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2177 return;
2178 }
2179
2180 /* Parse the FCF record from the non-embedded mailbox command */
2181 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2182 &next_fcf_index);
2183 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002184 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002185 "2765 Mailbox command READ_FCF_RECORD "
2186 "failed to retrieve a FCF record.\n");
2187 /* Let next new FCF event trigger fast failover */
2188 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002189 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002190 spin_unlock_irq(&phba->hbalock);
2191 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2192 return;
2193 }
2194
2195 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002196 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2197 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002198
2199 /* Log the FCF record information if turned on */
2200 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2201 next_fcf_index);
2202
James Smart6fb120a2009-05-22 14:52:59 -04002203 /*
2204 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002205 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002206 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002207 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002208 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002209 lpfc_sli4_fcf_pri_list_del(phba,
2210 bf_get(lpfc_fcf_record_fcf_index,
2211 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002213 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002214 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002215 bf_get(lpfc_fcf_record_fcf_index,
2216 new_fcf_record),
2217 bf_get(lpfc_fcf_record_fcf_avail,
2218 new_fcf_record),
2219 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002220 new_fcf_record),
2221 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002222 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002223 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2224 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2225 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002226 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2227 phba->fcf.current_rec.fcf_indx) {
2228 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2229 "2862 FCF (x%x) matches property "
2230 "of in-use FCF (x%x)\n",
2231 bf_get(lpfc_fcf_record_fcf_index,
2232 new_fcf_record),
2233 phba->fcf.current_rec.fcf_indx);
2234 goto read_next_fcf;
2235 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002236 /*
2237 * In case the current in-use FCF record becomes
2238 * invalid/unavailable during FCF discovery that
2239 * was not triggered by fast FCF failover process,
2240 * treat it as fast FCF failover.
2241 */
2242 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2243 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2244 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2245 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002246 "(x%x), enter FCF failover "
2247 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002248 phba->fcf.current_rec.fcf_indx);
2249 spin_lock_irq(&phba->hbalock);
2250 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2251 spin_unlock_irq(&phba->hbalock);
2252 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2253 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2254 LPFC_FCOE_FCF_GET_FIRST);
2255 return;
2256 }
2257 }
James Smart6fb120a2009-05-22 14:52:59 -04002258 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002259 } else {
2260 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002261 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2262 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002263 if (rc)
2264 goto read_next_fcf;
2265 }
2266
James Smart6fb120a2009-05-22 14:52:59 -04002267 /*
2268 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002269 * FCF record for the discovery. The condition that a rescan
2270 * matches the in-use FCF record: fabric name, switch name, mac
2271 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002272 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002273 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002274 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002275 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2276 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002277 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002278 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2279 phba->fcf.current_rec.fcf_indx) {
2280 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2281 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2282 /* Stop FCF redisc wait timer */
2283 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2284 phba);
2285 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2286 /* Fast failover, mark completed */
2287 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2288 spin_unlock_irq(&phba->hbalock);
2289 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2290 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002291 "FCF (x%x), port_state:x%x, "
2292 "fc_flag:x%x\n",
2293 phba->fcf.current_rec.fcf_indx,
2294 phba->pport->port_state,
2295 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002296 goto out;
2297 } else
2298 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2299 "2863 New FCF (x%x) matches "
2300 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002301 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002302 new_fcf_record),
2303 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002304 }
James Smartecfd03c2010-02-12 14:41:27 -05002305 /*
2306 * Read next FCF record from HBA searching for the matching
2307 * with in-use record only if not during the fast failover
2308 * period. In case of fast failover period, it shall try to
2309 * determine whether the FCF record just read should be the
2310 * next candidate.
2311 */
2312 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002313 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002314 goto read_next_fcf;
2315 }
James Smart6fb120a2009-05-22 14:52:59 -04002316 }
James Smartecfd03c2010-02-12 14:41:27 -05002317 /*
2318 * Update on failover FCF record only if it's in FCF fast-failover
2319 * period; otherwise, update on current FCF record.
2320 */
James Smartfc2b9892010-02-26 14:15:29 -05002321 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2322 fcf_rec = &phba->fcf.failover_rec;
2323 else
James Smartecfd03c2010-02-12 14:41:27 -05002324 fcf_rec = &phba->fcf.current_rec;
2325
James Smart6fb120a2009-05-22 14:52:59 -04002326 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2327 /*
James Smartecfd03c2010-02-12 14:41:27 -05002328 * If the driver FCF record does not have boot flag
2329 * set and new hba fcf record has boot flag set, use
2330 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002331 */
James Smartecfd03c2010-02-12 14:41:27 -05002332 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2333 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002334 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2335 "2837 Update current FCF record "
2336 "(x%x) with new FCF record (x%x)\n",
2337 fcf_rec->fcf_indx,
2338 bf_get(lpfc_fcf_record_fcf_index,
2339 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002340 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2341 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002342 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002343 goto read_next_fcf;
2344 }
2345 /*
James Smartecfd03c2010-02-12 14:41:27 -05002346 * If the driver FCF record has boot flag set and the
2347 * new hba FCF record does not have boot flag, read
2348 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002349 */
James Smartecfd03c2010-02-12 14:41:27 -05002350 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002351 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002352 goto read_next_fcf;
2353 }
2354 /*
James Smartecfd03c2010-02-12 14:41:27 -05002355 * If the new hba FCF record has lower priority value
2356 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002357 */
James Smartfc2b9892010-02-26 14:15:29 -05002358 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002359 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002360 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2361 "2838 Update current FCF record "
2362 "(x%x) with new FCF record (x%x)\n",
2363 fcf_rec->fcf_indx,
2364 bf_get(lpfc_fcf_record_fcf_index,
2365 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002366 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2367 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002368 /* Reset running random FCF selection count */
2369 phba->fcf.eligible_fcf_cnt = 1;
2370 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2371 /* Update running random FCF selection count */
2372 phba->fcf.eligible_fcf_cnt++;
2373 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2374 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002375 if (select_new_fcf) {
2376 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2377 "2839 Update current FCF record "
2378 "(x%x) with new FCF record (x%x)\n",
2379 fcf_rec->fcf_indx,
2380 bf_get(lpfc_fcf_record_fcf_index,
2381 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002382 /* Choose the new FCF by random selection */
2383 __lpfc_update_fcf_record(phba, fcf_rec,
2384 new_fcf_record,
2385 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002386 }
James Smart6fb120a2009-05-22 14:52:59 -04002387 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002388 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002389 goto read_next_fcf;
2390 }
2391 /*
James Smartecfd03c2010-02-12 14:41:27 -05002392 * This is the first suitable FCF record, choose this record for
2393 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002394 */
James Smartecfd03c2010-02-12 14:41:27 -05002395 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002396 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002397 "2840 Update initial FCF candidate "
2398 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002399 bf_get(lpfc_fcf_record_fcf_index,
2400 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002401 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2402 addr_mode, vlan_id, (boot_flag ?
2403 BOOT_ENABLE : 0));
2404 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002405 /* Setup initial running random FCF selection count */
2406 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002407 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002408 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002409 goto read_next_fcf;
2410
2411read_next_fcf:
2412 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002413 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2414 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2415 /*
2416 * Case of FCF fast failover scan
2417 */
2418
2419 /*
2420 * It has not found any suitable FCF record, cancel
2421 * FCF scan inprogress, and do nothing
2422 */
2423 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002424 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002425 "2782 No suitable FCF found: "
2426 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002427 phba->fcoe_eventtag_at_fcf_scan,
2428 bf_get(lpfc_fcf_record_fcf_index,
2429 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002430 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002431 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2432 phba->hba_flag &= ~FCF_TS_INPROG;
2433 spin_unlock_irq(&phba->hbalock);
2434 /* Unregister in-use FCF and rescan */
2435 lpfc_printf_log(phba, KERN_INFO,
2436 LOG_FIP,
2437 "2864 On devloss tmo "
2438 "unreg in-use FCF and "
2439 "rescan FCF table\n");
2440 lpfc_unregister_fcf_rescan(phba);
2441 return;
2442 }
2443 /*
2444 * Let next new FCF event trigger fast failover
2445 */
2446 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002447 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002448 return;
2449 }
2450 /*
2451 * It has found a suitable FCF record that is not
2452 * the same as in-use FCF record, unregister the
2453 * in-use FCF record, replace the in-use FCF record
2454 * with the new FCF record, mark FCF fast failover
2455 * completed, and then start register the new FCF
2456 * record.
2457 */
2458
James Smart0c9ab6f2010-02-26 14:15:57 -05002459 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002460 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002461
2462 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002463 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002464 "2842 Replace in-use FCF (x%x) "
2465 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002466 phba->fcf.current_rec.fcf_indx,
2467 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002468 memcpy(&phba->fcf.current_rec,
2469 &phba->fcf.failover_rec,
2470 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002471 /*
2472 * Mark the fast FCF failover rediscovery completed
2473 * and the start of the first round of the roundrobin
2474 * FCF failover.
2475 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002476 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002477 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002478 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002479 /* Register to the new FCF record */
2480 lpfc_register_fcf(phba);
2481 } else {
2482 /*
2483 * In case of transaction period to fast FCF failover,
2484 * do nothing when search to the end of the FCF table.
2485 */
2486 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2487 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2488 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002489
James Smart7d791df2011-07-22 18:37:52 -04002490 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2491 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002492 /*
2493 * In case the current in-use FCF record no
2494 * longer existed during FCF discovery that
2495 * was not triggered by fast FCF failover
2496 * process, treat it as fast FCF failover.
2497 */
2498 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2499 "2841 In-use FCF record (x%x) "
2500 "not reported, entering fast "
2501 "FCF failover mode scanning.\n",
2502 phba->fcf.current_rec.fcf_indx);
2503 spin_lock_irq(&phba->hbalock);
2504 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2505 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002506 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2507 LPFC_FCOE_FCF_GET_FIRST);
2508 return;
2509 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002510 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002511 lpfc_register_fcf(phba);
2512 }
2513 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002514 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002515 return;
2516
2517out:
2518 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2519 lpfc_register_fcf(phba);
2520
2521 return;
2522}
2523
2524/**
James Smarta93ff372010-10-22 11:06:08 -04002525 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002526 * @phba: pointer to lpfc hba data structure.
2527 * @mboxq: pointer to mailbox object.
2528 *
James Smarta93ff372010-10-22 11:06:08 -04002529 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002530 * read FCF record mailbox command from the eligible FCF record bmask for
2531 * performing the failover. If the FCF read back is not valid/available, it
2532 * fails through to retrying FLOGI to the currently registered FCF again.
2533 * Otherwise, if the FCF read back is valid and available, it will set the
2534 * newly read FCF record to the failover FCF record, unregister currently
2535 * registered FCF record, copy the failover FCF record to the current
2536 * FCF record, and then register the current FCF record before proceeding
2537 * to trying FLOGI on the new failover FCF.
2538 */
2539void
2540lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2541{
2542 struct fcf_record *new_fcf_record;
2543 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002544 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002545 uint16_t current_fcf_index;
2546 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002547 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002548
James Smarta93ff372010-10-22 11:06:08 -04002549 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002550 if (phba->link_state < LPFC_LINK_UP) {
2551 spin_lock_irq(&phba->hbalock);
2552 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002553 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002554 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002555 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002556 }
2557
2558 /* Parse the FCF record from the non-embedded mailbox command */
2559 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2560 &next_fcf_index);
2561 if (!new_fcf_record) {
2562 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2563 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002564 "failed to retrieve a FCF record. "
2565 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2566 phba->fcf.fcf_flag);
2567 lpfc_unregister_fcf_rescan(phba);
2568 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002569 }
2570
2571 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002572 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2573 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002574
2575 /* Log the FCF record information if turned on */
2576 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2577 next_fcf_index);
2578
James Smarta93ff372010-10-22 11:06:08 -04002579 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2580 if (!rc) {
2581 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2582 "2848 Remove ineligible FCF (x%x) from "
2583 "from roundrobin bmask\n", fcf_index);
2584 /* Clear roundrobin bmask bit for ineligible FCF */
2585 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2586 /* Perform next round of roundrobin FCF failover */
2587 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2588 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2589 if (rc)
2590 goto out;
2591 goto error_out;
2592 }
2593
2594 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2595 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2596 "2760 Perform FLOGI roundrobin FCF failover: "
2597 "FCF (x%x) back to FCF (x%x)\n",
2598 phba->fcf.current_rec.fcf_indx, fcf_index);
2599 /* Wait 500 ms before retrying FLOGI to current FCF */
2600 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002601 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002602 goto out;
2603 }
2604
James Smart0c9ab6f2010-02-26 14:15:57 -05002605 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002606 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002607 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2608 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002609 spin_lock_irq(&phba->hbalock);
2610 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2611 new_fcf_record, addr_mode, vlan_id,
2612 (boot_flag ? BOOT_ENABLE : 0));
2613 spin_unlock_irq(&phba->hbalock);
2614
2615 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2616
2617 /* Unregister the current in-use FCF record */
2618 lpfc_unregister_fcf(phba);
2619
2620 /* Replace in-use record with the new record */
2621 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2622 sizeof(struct lpfc_fcf_rec));
2623
2624 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002625 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2626 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002627
James Smarta93ff372010-10-22 11:06:08 -04002628error_out:
2629 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002630out:
2631 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002632}
2633
2634/**
2635 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2636 * @phba: pointer to lpfc hba data structure.
2637 * @mboxq: pointer to mailbox object.
2638 *
2639 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002640 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002641 * failover when a new FCF event happened. If the FCF read back is
2642 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002643 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002644 */
2645void
2646lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2647{
2648 struct fcf_record *new_fcf_record;
2649 uint32_t boot_flag, addr_mode;
2650 uint16_t fcf_index, next_fcf_index;
2651 uint16_t vlan_id;
2652 int rc;
2653
2654 /* If link state is not up, no need to proceed */
2655 if (phba->link_state < LPFC_LINK_UP)
2656 goto out;
2657
2658 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002659 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002660 goto out;
2661
2662 /* Parse the FCF record from the non-embedded mailbox command */
2663 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2664 &next_fcf_index);
2665 if (!new_fcf_record) {
2666 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2667 "2767 Mailbox command READ_FCF_RECORD "
2668 "failed to retrieve a FCF record.\n");
2669 goto out;
2670 }
2671
2672 /* Check the connection list for eligibility */
2673 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2674 &addr_mode, &vlan_id);
2675
2676 /* Log the FCF record information if turned on */
2677 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2678 next_fcf_index);
2679
2680 if (!rc)
2681 goto out;
2682
2683 /* Update the eligible FCF record index bmask */
2684 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002685
2686 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002687
2688out:
2689 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2690}
2691
2692/**
James Smart76a95d72010-11-20 23:11:48 -05002693 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2694 * @phba: pointer to lpfc hba data structure.
2695 * @mboxq: pointer to mailbox data structure.
2696 *
2697 * This function handles completion of init vfi mailbox command.
2698 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002699static void
James Smart76a95d72010-11-20 23:11:48 -05002700lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2701{
2702 struct lpfc_vport *vport = mboxq->vport;
2703
James Smartdf9e1b52011-12-13 13:22:17 -05002704 /*
2705 * VFI not supported on interface type 0, just do the flogi
2706 * Also continue if the VFI is in use - just use the same one.
2707 */
2708 if (mboxq->u.mb.mbxStatus &&
2709 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2710 LPFC_SLI_INTF_IF_TYPE_0) &&
2711 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002712 lpfc_printf_vlog(vport, KERN_ERR,
2713 LOG_MBOX,
2714 "2891 Init VFI mailbox failed 0x%x\n",
2715 mboxq->u.mb.mbxStatus);
2716 mempool_free(mboxq, phba->mbox_mem_pool);
2717 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2718 return;
2719 }
James Smart73d91e52011-10-10 21:32:10 -04002720
James Smart76a95d72010-11-20 23:11:48 -05002721 lpfc_initial_flogi(vport);
2722 mempool_free(mboxq, phba->mbox_mem_pool);
2723 return;
2724}
2725
2726/**
2727 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2728 * @vport: pointer to lpfc_vport data structure.
2729 *
2730 * This function issue a init_vfi mailbox command to initialize the VFI and
2731 * VPI for the physical port.
2732 */
2733void
2734lpfc_issue_init_vfi(struct lpfc_vport *vport)
2735{
2736 LPFC_MBOXQ_t *mboxq;
2737 int rc;
2738 struct lpfc_hba *phba = vport->phba;
2739
2740 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2741 if (!mboxq) {
2742 lpfc_printf_vlog(vport, KERN_ERR,
2743 LOG_MBOX, "2892 Failed to allocate "
2744 "init_vfi mailbox\n");
2745 return;
2746 }
2747 lpfc_init_vfi(mboxq, vport);
2748 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2749 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2750 if (rc == MBX_NOT_FINISHED) {
2751 lpfc_printf_vlog(vport, KERN_ERR,
2752 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2753 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2754 }
2755}
2756
2757/**
James Smart1c6834a2009-07-19 10:01:26 -04002758 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2759 * @phba: pointer to lpfc hba data structure.
2760 * @mboxq: pointer to mailbox data structure.
2761 *
2762 * This function handles completion of init vpi mailbox command.
2763 */
James Smart695a8142010-01-26 23:08:03 -05002764void
James Smart1c6834a2009-07-19 10:01:26 -04002765lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2766{
2767 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002768 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002769 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2770
James Smart1c6834a2009-07-19 10:01:26 -04002771 if (mboxq->u.mb.mbxStatus) {
2772 lpfc_printf_vlog(vport, KERN_ERR,
2773 LOG_MBOX,
2774 "2609 Init VPI mailbox failed 0x%x\n",
2775 mboxq->u.mb.mbxStatus);
2776 mempool_free(mboxq, phba->mbox_mem_pool);
2777 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2778 return;
2779 }
James Smart72100cc2010-02-12 14:43:01 -05002780 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002781 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002782 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002783
James Smart695a8142010-01-26 23:08:03 -05002784 /* If this port is physical port or FDISC is done, do reg_vpi */
2785 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2786 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2787 if (!ndlp)
2788 lpfc_printf_vlog(vport, KERN_ERR,
2789 LOG_DISCOVERY,
2790 "2731 Cannot find fabric "
2791 "controller node\n");
2792 else
2793 lpfc_register_new_vport(phba, vport, ndlp);
2794 mempool_free(mboxq, phba->mbox_mem_pool);
2795 return;
2796 }
2797
James Smart1c6834a2009-07-19 10:01:26 -04002798 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2799 lpfc_initial_fdisc(vport);
2800 else {
2801 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002802 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2803 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002804 }
James Smart695a8142010-01-26 23:08:03 -05002805 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002806 return;
2807}
2808
2809/**
James Smartecfd03c2010-02-12 14:41:27 -05002810 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2811 * @vport: pointer to lpfc_vport data structure.
2812 *
2813 * This function issue a init_vpi mailbox command to initialize
2814 * VPI for the vport.
2815 */
2816void
2817lpfc_issue_init_vpi(struct lpfc_vport *vport)
2818{
2819 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002820 int rc, vpi;
2821
2822 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2823 vpi = lpfc_alloc_vpi(vport->phba);
2824 if (!vpi) {
2825 lpfc_printf_vlog(vport, KERN_ERR,
2826 LOG_MBOX,
2827 "3303 Failed to obtain vport vpi\n");
2828 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2829 return;
2830 }
2831 vport->vpi = vpi;
2832 }
James Smartecfd03c2010-02-12 14:41:27 -05002833
2834 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2835 if (!mboxq) {
2836 lpfc_printf_vlog(vport, KERN_ERR,
2837 LOG_MBOX, "2607 Failed to allocate "
2838 "init_vpi mailbox\n");
2839 return;
2840 }
2841 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2842 mboxq->vport = vport;
2843 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2844 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2845 if (rc == MBX_NOT_FINISHED) {
2846 lpfc_printf_vlog(vport, KERN_ERR,
2847 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2848 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2849 }
2850}
2851
2852/**
James Smart6fb120a2009-05-22 14:52:59 -04002853 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2854 * @phba: pointer to lpfc hba data structure.
2855 *
2856 * This function loops through the list of vports on the @phba and issues an
2857 * FDISC if possible.
2858 */
2859void
2860lpfc_start_fdiscs(struct lpfc_hba *phba)
2861{
2862 struct lpfc_vport **vports;
2863 int i;
2864
2865 vports = lpfc_create_vport_work_array(phba);
2866 if (vports != NULL) {
2867 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2868 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2869 continue;
2870 /* There are no vpi for this vport */
2871 if (vports[i]->vpi > phba->max_vpi) {
2872 lpfc_vport_set_state(vports[i],
2873 FC_VPORT_FAILED);
2874 continue;
2875 }
James Smart76a95d72010-11-20 23:11:48 -05002876 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002877 lpfc_vport_set_state(vports[i],
2878 FC_VPORT_LINKDOWN);
2879 continue;
2880 }
James Smart1c6834a2009-07-19 10:01:26 -04002881 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002882 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002883 continue;
2884 }
James Smart6fb120a2009-05-22 14:52:59 -04002885 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2886 lpfc_initial_fdisc(vports[i]);
2887 else {
2888 lpfc_vport_set_state(vports[i],
2889 FC_VPORT_NO_FABRIC_SUPP);
2890 lpfc_printf_vlog(vports[i], KERN_ERR,
2891 LOG_ELS,
2892 "0259 No NPIV "
2893 "Fabric support\n");
2894 }
2895 }
2896 }
2897 lpfc_destroy_vport_work_array(phba, vports);
2898}
2899
2900void
2901lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2902{
2903 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2904 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002905 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002906
James Smartf5eca9b2012-03-01 22:37:54 -05002907 /*
2908 * VFI not supported for interface type 0, so ignore any mailbox
2909 * error (except VFI in use) and continue with the discovery.
2910 */
2911 if (mboxq->u.mb.mbxStatus &&
2912 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2913 LPFC_SLI_INTF_IF_TYPE_0) &&
2914 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002915 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2916 "2018 REG_VFI mbxStatus error x%x "
2917 "HBA state x%x\n",
2918 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002919 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002920 /* FLOGI failed, use loop map to make discovery list */
2921 lpfc_disc_list_loopmap(vport);
2922 /* Start discovery */
2923 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002924 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002925 }
2926 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002927 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002928 }
James Smartae05ebe2013-03-01 16:35:38 -05002929
James Smarte74c03c2013-04-17 20:15:19 -04002930 /* If the VFI is already registered, there is nothing else to do
2931 * Unless this was a VFI update and we are in PT2PT mode, then
2932 * we should drop through to set the port state to ready.
2933 */
James Smartae05ebe2013-03-01 16:35:38 -05002934 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002935 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2936 vport->fc_flag & FC_PT2PT))
2937 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002938
James Smartc8685952009-11-18 15:39:16 -05002939 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002940 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002941 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002942 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002943 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002944 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002945 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002946
James Smart1b511972011-12-13 13:23:09 -05002947 /* In case SLI4 FC loopback test, we are ready */
2948 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2949 (phba->link_flag & LS_LOOPBACK_MODE)) {
2950 phba->link_state = LPFC_HBA_READY;
2951 goto out_free_mem;
2952 }
2953
James Smarte74c03c2013-04-17 20:15:19 -04002954 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2955 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2956 "alpacnt:%d LinkState:%x topology:%x\n",
2957 vport->port_state, vport->fc_flag, vport->fc_myDID,
2958 vport->phba->alpa_map[0],
2959 phba->link_state, phba->fc_topology);
2960
James Smart6fb120a2009-05-22 14:52:59 -04002961 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002962 /*
2963 * For private loop or for NPort pt2pt,
2964 * just start discovery and we are done.
2965 */
2966 if ((vport->fc_flag & FC_PT2PT) ||
2967 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2968 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2969
James Smart76a95d72010-11-20 23:11:48 -05002970 /* Use loop map to make discovery list */
2971 lpfc_disc_list_loopmap(vport);
2972 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002973 if (vport->fc_flag & FC_PT2PT)
2974 vport->port_state = LPFC_VPORT_READY;
2975 else
2976 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002977 } else {
2978 lpfc_start_fdiscs(phba);
2979 lpfc_do_scr_ns_plogi(phba, vport);
2980 }
James Smart6fb120a2009-05-22 14:52:59 -04002981 }
2982
James Smart1b511972011-12-13 13:23:09 -05002983out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002984 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002985 if (dmabuf) {
2986 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2987 kfree(dmabuf);
2988 }
James Smart6fb120a2009-05-22 14:52:59 -04002989 return;
2990}
2991
2992static void
James Smart2e0fef82007-06-17 19:56:36 -05002993lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002994{
James Smart6fb120a2009-05-22 14:52:59 -04002995 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002996 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002997 struct lpfc_vport *vport = pmb->vport;
James Smart21bf0b92015-08-31 16:48:21 -04002998 struct serv_parm *sp = &vport->fc_sparam;
2999 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003000
3001 /* Check for error */
3002 if (mb->mbxStatus) {
3003 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003004 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3005 "0319 READ_SPARAM mbxStatus error x%x "
3006 "hba state x%x>\n",
3007 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003008 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003009 goto out;
3010 }
3011
James Smart2e0fef82007-06-17 19:56:36 -05003012 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003013 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003014
3015 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3016 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3017 ed_tov = (ed_tov + 999999) / 1000000;
3018
3019 phba->fc_edtov = ed_tov;
3020 phba->fc_ratov = (2 * ed_tov) / 1000;
3021 if (phba->fc_ratov < FF_DEF_RATOV) {
3022 /* RA_TOV should be atleast 10sec for initial flogi */
3023 phba->fc_ratov = FF_DEF_RATOV;
3024 }
3025
James Smart05580562011-05-24 11:40:48 -04003026 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003027 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3028 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3029 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3030 }
3031
dea31012005-04-17 16:05:31 -05003032 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3033 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003034 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003035 return;
3036
3037out:
3038 pmb->context1 = NULL;
3039 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3040 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003041 lpfc_issue_clear_la(phba, vport);
3042 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003043 return;
3044}
3045
3046static void
James Smart76a95d72010-11-20 23:11:48 -05003047lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003048{
James Smart92d7f7b2007-06-17 19:56:38 -05003049 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003050 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003051 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003052 int i;
James Smart14691152006-12-02 13:34:28 -05003053 struct lpfc_dmabuf *mp;
3054 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003055 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003056 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003057
James Smart92d7f7b2007-06-17 19:56:38 -05003058 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003059 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3060
3061 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3062 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3063 case LPFC_LINK_SPEED_1GHZ:
3064 case LPFC_LINK_SPEED_2GHZ:
3065 case LPFC_LINK_SPEED_4GHZ:
3066 case LPFC_LINK_SPEED_8GHZ:
3067 case LPFC_LINK_SPEED_10GHZ:
3068 case LPFC_LINK_SPEED_16GHZ:
3069 case LPFC_LINK_SPEED_32GHZ:
3070 break;
3071 default:
3072 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3073 break;
3074 }
dea31012005-04-17 16:05:31 -05003075 }
3076
James Smarte74c03c2013-04-17 20:15:19 -04003077 if (phba->fc_topology &&
3078 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3079 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3080 "3314 Toplogy changed was 0x%x is 0x%x\n",
3081 phba->fc_topology,
3082 bf_get(lpfc_mbx_read_top_topology, la));
3083 phba->fc_topology_changed = 1;
3084 }
3085
James Smart76a95d72010-11-20 23:11:48 -05003086 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003087 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003088
James Smartdf9e1b52011-12-13 13:22:17 -05003089 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003090 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003091 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003092
James Smart6a9c52c2009-10-02 15:16:51 -04003093 /* if npiv is enabled and this adapter supports npiv log
3094 * a message that npiv is not supported in this topology
3095 */
3096 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003097 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3098 "1309 Link Up Event npiv not supported in loop "
3099 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003100 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003101 if (bf_get(lpfc_mbx_read_top_il, la))
3102 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003103
James Smart76a95d72010-11-20 23:11:48 -05003104 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3105 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003106
3107 if (i == 0) {
3108 phba->alpa_map[0] = 0;
3109 } else {
James Smarte8b62012007-08-02 11:10:09 -04003110 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003111 int numalpa, j, k;
3112 union {
3113 uint8_t pamap[16];
3114 struct {
3115 uint32_t wd1;
3116 uint32_t wd2;
3117 uint32_t wd3;
3118 uint32_t wd4;
3119 } pa;
3120 } un;
3121 numalpa = phba->alpa_map[0];
3122 j = 0;
3123 while (j < numalpa) {
3124 memset(un.pamap, 0, 16);
3125 for (k = 1; j < numalpa; k++) {
3126 un.pamap[k - 1] =
3127 phba->alpa_map[j + 1];
3128 j++;
3129 if (k == 16)
3130 break;
3131 }
3132 /* Link Up Event ALPA map */
3133 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003134 KERN_WARNING,
3135 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003136 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003137 "ALPA map Data: x%x "
3138 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003139 un.pa.wd1, un.pa.wd2,
3140 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003141 }
3142 }
3143 }
3144 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003145 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003146 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003147 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003148 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3149 }
James Smart2e0fef82007-06-17 19:56:36 -05003150 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003151 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003152 }
James Smart92d7f7b2007-06-17 19:56:38 -05003153 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003154
James Smart38c20672013-03-01 16:37:44 -05003155 if (fc_flags) {
3156 spin_lock_irq(shost->host_lock);
3157 vport->fc_flag |= fc_flags;
3158 spin_unlock_irq(shost->host_lock);
3159 }
3160
dea31012005-04-17 16:05:31 -05003161 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003162 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3163 if (!sparam_mbox)
3164 goto out;
3165
3166 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3167 if (rc) {
3168 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3169 goto out;
3170 }
3171 sparam_mbox->vport = vport;
3172 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3173 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3174 if (rc == MBX_NOT_FINISHED) {
3175 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3176 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3177 kfree(mp);
3178 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3179 goto out;
dea31012005-04-17 16:05:31 -05003180 }
3181
James Smart76a95d72010-11-20 23:11:48 -05003182 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003183 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3184 if (!cfglink_mbox)
3185 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003186 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003187 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003188 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003189 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003190 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003191 if (rc == MBX_NOT_FINISHED) {
3192 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3193 goto out;
3194 }
3195 } else {
James Smart32b97932009-07-19 10:01:21 -04003196 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003197 /*
3198 * Add the driver's default FCF record at FCF index 0 now. This
3199 * is phase 1 implementation that support FCF index 0 and driver
3200 * defaults.
3201 */
James Smart45ed1192009-10-02 15:17:02 -04003202 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003203 fcf_record = kzalloc(sizeof(struct fcf_record),
3204 GFP_KERNEL);
3205 if (unlikely(!fcf_record)) {
3206 lpfc_printf_log(phba, KERN_ERR,
3207 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003208 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003209 "fcf record\n");
3210 rc = -ENODEV;
3211 goto out;
3212 }
3213
3214 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3215 LPFC_FCOE_FCF_DEF_INDEX);
3216 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3217 if (unlikely(rc)) {
3218 lpfc_printf_log(phba, KERN_ERR,
3219 LOG_MBOX | LOG_SLI,
3220 "2013 Could not manually add FCF "
3221 "record 0, status %d\n", rc);
3222 rc = -ENODEV;
3223 kfree(fcf_record);
3224 goto out;
3225 }
3226 kfree(fcf_record);
3227 }
3228 /*
3229 * The driver is expected to do FIP/FCF. Call the port
3230 * and get the FCF Table.
3231 */
James Smart32b97932009-07-19 10:01:21 -04003232 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003233 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003234 spin_unlock_irq(&phba->hbalock);
3235 return;
3236 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003237 /* This is the initial FCF discovery scan */
3238 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003239 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003240 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3241 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003242 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3243 LPFC_FCOE_FCF_GET_FIRST);
3244 if (rc) {
3245 spin_lock_irq(&phba->hbalock);
3246 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3247 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003248 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003249 }
James Smart38b92ef2010-08-04 16:11:39 -04003250 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003251 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003252 }
James Smart6fb120a2009-05-22 14:52:59 -04003253
3254 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003255out:
3256 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003257 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3258 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3259 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003260 lpfc_issue_clear_la(phba, vport);
3261 return;
dea31012005-04-17 16:05:31 -05003262}
3263
3264static void
James Smart84774a42008-08-24 21:50:06 -04003265lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003266{
dea31012005-04-17 16:05:31 -05003267 uint32_t control;
3268 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003269 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003270 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003271 if (phba->sli_rev <= LPFC_SLI_REV3) {
3272 control = readl(phba->HCregaddr);
3273 control |= HC_LAINT_ENA;
3274 writel(control, phba->HCregaddr);
3275 readl(phba->HCregaddr); /* flush */
3276 }
James Smart2e0fef82007-06-17 19:56:36 -05003277 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003278}
3279
James Smart84774a42008-08-24 21:50:06 -04003280static void
3281lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3282{
3283 lpfc_linkdown(phba);
3284 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003285 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003286 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3287}
3288
3289
dea31012005-04-17 16:05:31 -05003290/*
James Smart76a95d72010-11-20 23:11:48 -05003291 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003292 * command upon completion. It is setup in the LPFC_MBOXQ
3293 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003294 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003295 */
3296void
James Smart76a95d72010-11-20 23:11:48 -05003297lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003298{
James Smart2e0fef82007-06-17 19:56:36 -05003299 struct lpfc_vport *vport = pmb->vport;
3300 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003301 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003302 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003303 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003304 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3305
James Smart0d2b6b82008-06-14 22:52:47 -04003306 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003307 pring = lpfc_phba_elsring(phba);
3308 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3309
dea31012005-04-17 16:05:31 -05003310 /* Check for error */
3311 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003312 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003313 "1307 READ_LA mbox error x%x state x%x\n",
3314 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003315 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003316 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003317 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003318 }
3319
James Smart76a95d72010-11-20 23:11:48 -05003320 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003321
3322 memcpy(&phba->alpa_map[0], mp->virt, 128);
3323
James Smart2e0fef82007-06-17 19:56:36 -05003324 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003325 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003326 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003327 else
James Smart2e0fef82007-06-17 19:56:36 -05003328 vport->fc_flag &= ~FC_BYPASSED_MODE;
3329 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003330
James Smartbe0c0082013-03-01 16:38:14 -05003331 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003332 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003333 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003334 if (phba->fc_eventTag != 0)
3335 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003336 }
dea31012005-04-17 16:05:31 -05003337
3338 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003339 if (phba->sli_rev < LPFC_SLI_REV4) {
3340 spin_lock_irq(&phba->hbalock);
3341 if (bf_get(lpfc_mbx_read_top_mm, la))
3342 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3343 else
3344 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3345 spin_unlock_irq(&phba->hbalock);
3346 }
dea31012005-04-17 16:05:31 -05003347
James Smart4d9ab992009-10-02 15:16:39 -04003348 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003349 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003350 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003351 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003352 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003353 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003354 "1306 Link Up Event in loop back mode "
3355 "x%x received Data: x%x x%x x%x x%x\n",
3356 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003357 bf_get(lpfc_mbx_read_top_alpa_granted,
3358 la),
3359 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003360 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003361 } else {
3362 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003363 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003364 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003365 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003366 bf_get(lpfc_mbx_read_top_alpa_granted,
3367 la),
3368 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003369 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003370 bf_get(lpfc_mbx_read_top_mm, la),
3371 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003372 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003373 }
James Smart92d7f7b2007-06-17 19:56:38 -05003374 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003375 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3376 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003377 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003378 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003379 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3380 "1308 Link Down Event in loop back mode "
3381 "x%x received "
3382 "Data: x%x x%x x%x\n",
3383 la->eventTag, phba->fc_eventTag,
3384 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003385 else
James Smart3163f722008-02-08 18:50:25 -05003386 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003387 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003388 "Data: x%x x%x x%x x%x x%x\n",
3389 la->eventTag, phba->fc_eventTag,
3390 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003391 bf_get(lpfc_mbx_read_top_mm, la),
3392 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003393 lpfc_mbx_issue_link_down(phba);
3394 }
James Smartbe0c0082013-03-01 16:38:14 -05003395 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3396 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003397 if (phba->link_state != LPFC_LINK_DOWN) {
3398 phba->fc_stat.LinkDown++;
3399 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3400 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003401 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003402 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003403 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003404 lpfc_mbx_issue_link_down(phba);
3405 } else
3406 lpfc_enable_la(phba);
3407
3408 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3409 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3410 "Data: x%x x%x x%x\n",
3411 la->eventTag, phba->fc_eventTag,
3412 phba->pport->port_state, vport->fc_flag);
3413 /*
3414 * The cmnd that triggered this will be waiting for this
3415 * signal.
3416 */
3417 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3418 if (phba->wait_4_mlo_maint_flg) {
3419 phba->wait_4_mlo_maint_flg = 0;
3420 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003421 }
James Smart84774a42008-08-24 21:50:06 -04003422 }
3423
James Smartbe0c0082013-03-01 16:38:14 -05003424 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3425 bf_get(lpfc_mbx_read_top_fa, la)) {
3426 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003427 lpfc_issue_clear_la(phba, vport);
3428 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003429 "1311 fa %d\n",
3430 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003431 }
3432
James Smart76a95d72010-11-20 23:11:48 -05003433lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003434 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3435 kfree(mp);
3436 mempool_free(pmb, phba->mbox_mem_pool);
3437 return;
3438}
3439
3440/*
3441 * This routine handles processing a REG_LOGIN mailbox
3442 * command upon completion. It is setup in the LPFC_MBOXQ
3443 * as the completion routine when the command is
3444 * handed off to the SLI layer.
3445 */
3446void
James Smart2e0fef82007-06-17 19:56:36 -05003447lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003448{
James Smart2e0fef82007-06-17 19:56:36 -05003449 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003450 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003451 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003452 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003453
dea31012005-04-17 16:05:31 -05003454 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003455 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003456
James Smartbe6bb942015-04-07 15:07:22 -04003457 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3458 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3459 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003460 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003461 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003462 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3463 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3464
James Smart40426292010-12-15 17:58:10 -05003465 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3466 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003467 /* We rcvd a rscn after issuing this
3468 * mbox reg login, we may have cycled
3469 * back through the state and be
3470 * back at reg login state so this
3471 * mbox needs to be ignored becase
3472 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003473 * process.
James Smartffc95492010-06-07 15:23:17 -04003474 */
3475 spin_lock_irq(shost->host_lock);
3476 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3477 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003478
3479 /*
3480 * We cannot leave the RPI registered because
3481 * if we go thru discovery again for this ndlp
3482 * a subsequent REG_RPI will fail.
3483 */
3484 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3485 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003486 }
3487
3488 /* Call state machine */
3489 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003490
dea31012005-04-17 16:05:31 -05003491 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3492 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003493 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003494 /* decrement the node reference count held for this callback
3495 * function.
3496 */
James Smart329f9bc2007-04-25 09:53:01 -04003497 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003498
3499 return;
3500}
3501
James Smart92d7f7b2007-06-17 19:56:38 -05003502static void
3503lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3504{
James Smart04c68492009-05-22 14:52:52 -04003505 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003506 struct lpfc_vport *vport = pmb->vport;
3507 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3508
3509 switch (mb->mbxStatus) {
3510 case 0x0011:
3511 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003512 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3513 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3514 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003515 break;
James Smart78730cf2010-04-06 15:06:30 -04003516 /* If VPI is busy, reset the HBA */
3517 case 0x9700:
3518 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3519 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3520 vport->vpi, mb->mbxStatus);
3521 if (!(phba->pport->load_flag & FC_UNLOADING))
3522 lpfc_workq_post_event(phba, NULL, NULL,
3523 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003524 }
James Smart72100cc2010-02-12 14:43:01 -05003525 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003526 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003527 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003528 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003529 vport->unreg_vpi_cmpl = VPORT_OK;
3530 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003531 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003532 /*
3533 * This shost reference might have been taken at the beginning of
3534 * lpfc_vport_delete()
3535 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003536 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003537 scsi_host_put(shost);
3538}
3539
James Smartd7c255b2008-08-24 21:50:00 -04003540int
James Smart92d7f7b2007-06-17 19:56:38 -05003541lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3542{
3543 struct lpfc_hba *phba = vport->phba;
3544 LPFC_MBOXQ_t *mbox;
3545 int rc;
3546
3547 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3548 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003549 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003550
3551 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3552 mbox->vport = vport;
3553 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003554 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003555 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003556 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3557 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003558 mempool_free(mbox, phba->mbox_mem_pool);
3559 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003560 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003561 }
James Smartd7c255b2008-08-24 21:50:00 -04003562 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003563}
3564
3565static void
3566lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3567{
3568 struct lpfc_vport *vport = pmb->vport;
3569 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003570 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003571
3572 switch (mb->mbxStatus) {
3573 case 0x0011:
3574 case 0x9601:
3575 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003576 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3577 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3578 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003579 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3580 spin_lock_irq(shost->host_lock);
3581 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3582 spin_unlock_irq(shost->host_lock);
3583 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003584
James Smart01649562017-02-12 13:52:32 -08003585 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3586 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
3587 lpfc_nvme_update_localport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003588 goto out;
3589 }
James Smart92d7f7b2007-06-17 19:56:38 -05003590
James Smart72100cc2010-02-12 14:43:01 -05003591 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003592 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003593 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003594 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003595 vport->num_disc_nodes = 0;
3596 /* go thru NPR list and issue ELS PLOGIs */
3597 if (vport->fc_npr_cnt)
3598 lpfc_els_disc_plogi(vport);
3599
3600 if (!vport->num_disc_nodes) {
3601 spin_lock_irq(shost->host_lock);
3602 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3603 spin_unlock_irq(shost->host_lock);
3604 lpfc_can_disctmo(vport);
3605 }
3606 vport->port_state = LPFC_VPORT_READY;
3607
3608out:
3609 mempool_free(pmb, phba->mbox_mem_pool);
3610 return;
3611}
3612
James Smart21e9a0a2009-05-22 14:53:21 -04003613/**
3614 * lpfc_create_static_vport - Read HBA config region to create static vports.
3615 * @phba: pointer to lpfc hba data structure.
3616 *
3617 * This routine issue a DUMP mailbox command for config region 22 to get
3618 * the list of static vports to be created. The function create vports
3619 * based on the information returned from the HBA.
3620 **/
3621void
3622lpfc_create_static_vport(struct lpfc_hba *phba)
3623{
3624 LPFC_MBOXQ_t *pmb = NULL;
3625 MAILBOX_t *mb;
3626 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003627 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003628 struct fc_vport_identifiers vport_id;
3629 struct fc_vport *new_fc_vport;
3630 struct Scsi_Host *shost;
3631 struct lpfc_vport *vport;
3632 uint16_t offset = 0;
3633 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003634 struct lpfc_dmabuf *mp;
3635 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003636
3637 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3638 if (!pmb) {
3639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3640 "0542 lpfc_create_static_vport failed to"
3641 " allocate mailbox memory\n");
3642 return;
3643 }
James Smartcdcc2342012-08-14 14:26:35 -04003644 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003645 mb = &pmb->u.mb;
3646
3647 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3648 if (!vport_info) {
3649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3650 "0543 lpfc_create_static_vport failed to"
3651 " allocate vport_info\n");
3652 mempool_free(pmb, phba->mbox_mem_pool);
3653 return;
3654 }
3655
3656 vport_buff = (uint8_t *) vport_info;
3657 do {
James Smartcdcc2342012-08-14 14:26:35 -04003658 /* free dma buffer from previous round */
3659 if (pmb->context1) {
3660 mp = (struct lpfc_dmabuf *)pmb->context1;
3661 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3662 kfree(mp);
3663 }
James Smart1c6834a2009-07-19 10:01:26 -04003664 if (lpfc_dump_static_vport(phba, pmb, offset))
3665 goto out;
3666
James Smart21e9a0a2009-05-22 14:53:21 -04003667 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003668 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3669 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003670
James Smartcdcc2342012-08-14 14:26:35 -04003671 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003672 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3673 "0544 lpfc_create_static_vport failed to"
3674 " issue dump mailbox command ret 0x%x "
3675 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003676 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003677 goto out;
3678 }
3679
James Smart1c6834a2009-07-19 10:01:26 -04003680 if (phba->sli_rev == LPFC_SLI_REV4) {
3681 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003682 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003683 if (byte_count > sizeof(struct static_vport_info) -
3684 offset)
3685 byte_count = sizeof(struct static_vport_info)
3686 - offset;
3687 memcpy(vport_buff + offset, mp->virt, byte_count);
3688 offset += byte_count;
3689 } else {
3690 if (mb->un.varDmp.word_cnt >
3691 sizeof(struct static_vport_info) - offset)
3692 mb->un.varDmp.word_cnt =
3693 sizeof(struct static_vport_info)
3694 - offset;
3695 byte_count = mb->un.varDmp.word_cnt;
3696 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3697 vport_buff + offset,
3698 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003699
James Smart1c6834a2009-07-19 10:01:26 -04003700 offset += byte_count;
3701 }
James Smart21e9a0a2009-05-22 14:53:21 -04003702
James Smart1c6834a2009-07-19 10:01:26 -04003703 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003704 offset < sizeof(struct static_vport_info));
3705
3706
3707 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3708 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3709 != VPORT_INFO_REV)) {
3710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3711 "0545 lpfc_create_static_vport bad"
3712 " information header 0x%x 0x%x\n",
3713 le32_to_cpu(vport_info->signature),
3714 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3715
3716 goto out;
3717 }
3718
3719 shost = lpfc_shost_from_vport(phba->pport);
3720
3721 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3722 memset(&vport_id, 0, sizeof(vport_id));
3723 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3724 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3725 if (!vport_id.port_name || !vport_id.node_name)
3726 continue;
3727
3728 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3729 vport_id.vport_type = FC_PORTTYPE_NPIV;
3730 vport_id.disable = false;
3731 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3732
3733 if (!new_fc_vport) {
3734 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3735 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003736 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003737 continue;
3738 }
3739
3740 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3741 vport->vport_flag |= STATIC_VPORT;
3742 }
3743
3744out:
James Smart21e9a0a2009-05-22 14:53:21 -04003745 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003746 if (mbx_wait_rc != MBX_TIMEOUT) {
3747 if (pmb->context1) {
3748 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003749 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3750 kfree(mp);
3751 }
James Smart21e9a0a2009-05-22 14:53:21 -04003752 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003753 }
James Smart21e9a0a2009-05-22 14:53:21 -04003754
3755 return;
3756}
3757
dea31012005-04-17 16:05:31 -05003758/*
3759 * This routine handles processing a Fabric REG_LOGIN mailbox
3760 * command upon completion. It is setup in the LPFC_MBOXQ
3761 * as the completion routine when the command is
3762 * handed off to the SLI layer.
3763 */
3764void
James Smart2e0fef82007-06-17 19:56:36 -05003765lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003766{
James Smart92d7f7b2007-06-17 19:56:38 -05003767 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003768 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003769 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003770 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003771 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003772
James Smart549e55c2007-08-02 11:09:51 -04003773 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003774 pmb->context1 = NULL;
3775 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003776
dea31012005-04-17 16:05:31 -05003777 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003778 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3779 "0258 Register Fabric login error: 0x%x\n",
3780 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003781 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3782 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003783 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003784
James Smart76a95d72010-11-20 23:11:48 -05003785 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003786 /* FLOGI failed, use loop map to make discovery list */
3787 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003788
James Smart92d7f7b2007-06-17 19:56:38 -05003789 /* Start discovery */
3790 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003791 /* Decrement the reference count to ndlp after the
3792 * reference to the ndlp are done.
3793 */
3794 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003795 return;
3796 }
3797
3798 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003799 /* Decrement the reference count to ndlp after the reference
3800 * to the ndlp are done.
3801 */
3802 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003803 return;
3804 }
3805
James Smart6d368e52011-05-24 11:44:12 -04003806 if (phba->sli_rev < LPFC_SLI_REV4)
3807 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003808 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003809 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003810 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003811
James Smart2e0fef82007-06-17 19:56:36 -05003812 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003813 /* when physical port receive logo donot start
3814 * vport discovery */
3815 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3816 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003817 else {
3818 shost = lpfc_shost_from_vport(vport);
3819 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003820 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003821 spin_unlock_irq(shost->host_lock);
3822 }
James Smart92d7f7b2007-06-17 19:56:38 -05003823 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003824 }
3825
3826 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3827 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003828 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003829
3830 /* Drop the reference count from the mbox at the end after
3831 * all the current reference to the ndlp have been done.
3832 */
3833 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003834 return;
3835}
3836
James Smarta0f2d3e2017-02-12 13:52:31 -08003837 /*
3838 * This routine will issue a GID_FT for each FC4 Type supported
3839 * by the driver. ALL GID_FTs must complete before discovery is started.
3840 */
3841int
3842lpfc_issue_gidft(struct lpfc_vport *vport)
3843{
3844 struct lpfc_hba *phba = vport->phba;
3845
3846 /* Good status, issue CT Request to NameServer */
3847 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3848 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3849 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3850 /* Cannot issue NameServer FCP Query, so finish up
3851 * discovery
3852 */
3853 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3854 "0604 %s FC TYPE %x %s\n",
3855 "Failed to issue GID_FT to ",
3856 FC_TYPE_FCP,
3857 "Finishing discovery.");
3858 return 0;
3859 }
3860 vport->gidft_inp++;
3861 }
3862
3863 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3864 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3865 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3866 /* Cannot issue NameServer NVME Query, so finish up
3867 * discovery
3868 */
3869 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3870 "0605 %s FC_TYPE %x %s %d\n",
3871 "Failed to issue GID_FT to ",
3872 FC_TYPE_NVME,
3873 "Finishing discovery: gidftinp ",
3874 vport->gidft_inp);
3875 if (vport->gidft_inp == 0)
3876 return 0;
3877 } else
3878 vport->gidft_inp++;
3879 }
3880 return vport->gidft_inp;
3881}
3882
dea31012005-04-17 16:05:31 -05003883/*
3884 * This routine handles processing a NameServer REG_LOGIN mailbox
3885 * command upon completion. It is setup in the LPFC_MBOXQ
3886 * as the completion routine when the command is
3887 * handed off to the SLI layer.
3888 */
3889void
James Smart2e0fef82007-06-17 19:56:36 -05003890lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003891{
James Smart21e9a0a2009-05-22 14:53:21 -04003892 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003893 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3894 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3895 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003896
James Smartd439d282010-09-29 11:18:45 -04003897 pmb->context1 = NULL;
3898 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003899 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003900
dea31012005-04-17 16:05:31 -05003901 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003902 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3903 "0260 Register NameServer error: 0x%x\n",
3904 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003905
3906out:
James Smartfa4066b2008-01-11 01:53:27 -05003907 /* decrement the node reference count held for this
3908 * callback function.
3909 */
James Smart329f9bc2007-04-25 09:53:01 -04003910 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003911 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3912 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003913 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003914
3915 /* If no other thread is using the ndlp, free it */
3916 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003917
James Smart76a95d72010-11-20 23:11:48 -05003918 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003919 /*
3920 * RegLogin failed, use loop map to make discovery
3921 * list
3922 */
3923 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003924
James Smart92d7f7b2007-06-17 19:56:38 -05003925 /* Start discovery */
3926 lpfc_disc_start(vport);
3927 return;
3928 }
3929 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003930 return;
3931 }
3932
James Smart6d368e52011-05-24 11:44:12 -04003933 if (phba->sli_rev < LPFC_SLI_REV4)
3934 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003935 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003936 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003937 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003938 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3939 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3940 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003941 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003942 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003943
James Smart2e0fef82007-06-17 19:56:36 -05003944 if (vport->port_state < LPFC_VPORT_READY) {
3945 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003946 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3947 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3948 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3949 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003950
3951 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3952 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3953 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3954
3955 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3956 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08003957 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3958 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003959
3960 /* Issue SCR just before NameServer GID_FT Query */
3961 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003962 }
3963
James Smart2e0fef82007-06-17 19:56:36 -05003964 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003965 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003966 goto out;
dea31012005-04-17 16:05:31 -05003967
James Smarta0f2d3e2017-02-12 13:52:31 -08003968 /*
3969 * At this point in time we may need to wait for multiple
3970 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
3971 *
3972 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05003973 * callback function.
3974 */
James Smart329f9bc2007-04-25 09:53:01 -04003975 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003976 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3977 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003978 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003979
3980 return;
3981}
3982
3983static void
James Smart2e0fef82007-06-17 19:56:36 -05003984lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003985{
James Smart2e0fef82007-06-17 19:56:36 -05003986 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3987 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003988 struct lpfc_rport_data *rdata;
3989 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003990 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003991
James Smart895427b2017-02-12 13:52:30 -08003992 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
3993 return;
3994
dea31012005-04-17 16:05:31 -05003995 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003996 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3997 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003998 rport_ids.port_id = ndlp->nlp_DID;
3999 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004000
James Smart329f9bc2007-04-25 09:53:01 -04004001 /*
4002 * We leave our node pointer in rport->dd_data when we unregister a
4003 * FCP target port. But fc_remote_port_add zeros the space to which
4004 * rport->dd_data points. So, if we're reusing a previously
4005 * registered port, drop the reference that we took the last time we
4006 * registered the port.
4007 */
James Smart466e8402015-05-21 13:55:28 -04004008 rport = ndlp->rport;
4009 if (rport) {
4010 rdata = rport->dd_data;
4011 /* break the link before dropping the ref */
4012 ndlp->rport = NULL;
4013 if (rdata && rdata->pnode == ndlp)
4014 lpfc_nlp_put(ndlp);
4015 rdata->pnode = NULL;
4016 /* drop reference for earlier registeration */
4017 put_device(&rport->dev);
4018 }
James Smart858c9f62007-06-17 19:56:39 -05004019
4020 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4021 "rport add: did:x%x flg:x%x type x%x",
4022 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4023
James Smart9589b0622011-04-16 11:03:17 -04004024 /* Don't add the remote port if unloading. */
4025 if (vport->load_flag & FC_UNLOADING)
4026 return;
4027
James Smart2e0fef82007-06-17 19:56:36 -05004028 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004029 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004030 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4031 "Warning: fc_remote_port_add failed\n");
4032 return;
4033 }
4034
4035 /* initialize static port data */
4036 rport->maxframe_size = ndlp->nlp_maxframe;
4037 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004038 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004039 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004040
4041 if (ndlp->nlp_type & NLP_FCP_TARGET)
4042 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4043 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4044 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4045
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004046 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4047 fc_remote_port_rolechg(rport, rport_ids.roles);
4048
James Smart34f5ad82012-08-03 12:35:03 -04004049 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4050 "3183 rport register x%06x, rport %p role x%x\n",
4051 ndlp->nlp_DID, rport, rport_ids.roles);
4052
James Smart071fbd3d2006-04-15 11:53:20 -04004053 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004054 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004055 ndlp->nlp_sid = rport->scsi_target_id;
4056 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004057 return;
4058}
4059
4060static void
James Smart2e0fef82007-06-17 19:56:36 -05004061lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004062{
4063 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004064 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004065 struct lpfc_hba *phba = vport->phba;
4066
4067 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4068 return;
James Smartc01f3202006-08-18 17:47:08 -04004069
James Smart2ea259e2017-02-12 13:52:27 -08004070 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004071 "rport delete: did:x%x flg:x%x type x%x",
4072 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4073
James Smart2ea259e2017-02-12 13:52:27 -08004074 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004075 "3184 rport unregister x%06x, rport %p\n",
4076 ndlp->nlp_DID, rport);
4077
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004078 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004079
4080 return;
4081}
4082
James Smartde0c5b32007-04-25 09:52:27 -04004083static void
James Smart2e0fef82007-06-17 19:56:36 -05004084lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004085{
James Smart2e0fef82007-06-17 19:56:36 -05004086 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4087
4088 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004089 switch (state) {
4090 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004091 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004092 break;
4093 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004094 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004095 break;
4096 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004097 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004098 break;
4099 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004100 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004101 break;
4102 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004103 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004104 break;
4105 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004106 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004107 break;
4108 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004109 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004110 break;
4111 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004112 if (vport->fc_npr_cnt == 0 && count == -1)
4113 vport->fc_npr_cnt = 0;
4114 else
4115 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004116 break;
4117 }
James Smart2e0fef82007-06-17 19:56:36 -05004118 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004119}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004120
James Smartde0c5b32007-04-25 09:52:27 -04004121static void
James Smart2e0fef82007-06-17 19:56:36 -05004122lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004123 int old_state, int new_state)
4124{
James Smart2e0fef82007-06-17 19:56:36 -05004125 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smarta0f2d3e2017-02-12 13:52:31 -08004126 struct lpfc_hba *phba = vport->phba;
James Smart2e0fef82007-06-17 19:56:36 -05004127
James Smartde0c5b32007-04-25 09:52:27 -04004128 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004129 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4130 ndlp->nlp_type |= NLP_FC_NODE;
4131 }
4132 if (new_state == NLP_STE_MAPPED_NODE)
4133 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4134 if (new_state == NLP_STE_NPR_NODE)
4135 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004136
James Smarta0f2d3e2017-02-12 13:52:31 -08004137 /* FCP and NVME Transport interface */
4138 if ((old_state == NLP_STE_MAPPED_NODE ||
4139 old_state == NLP_STE_UNMAPPED_NODE)) {
4140 if (ndlp->rport) {
4141 vport->phba->nport_event_cnt++;
4142 lpfc_unregister_remote_port(ndlp);
4143 }
4144
4145 /* Notify the NVME transport of this rport's loss */
4146 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4147 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
4148 (vport->phba->nvmet_support == 0) &&
4149 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) ||
4150 (ndlp->nlp_DID == Fabric_DID))) {
4151 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004152 lpfc_nvme_unregister_port(vport, ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004153 }
James Smartde0c5b32007-04-25 09:52:27 -04004154 }
dea31012005-04-17 16:05:31 -05004155
James Smarta0f2d3e2017-02-12 13:52:31 -08004156 /* FCP and NVME Transport interfaces */
4157
James Smartde0c5b32007-04-25 09:52:27 -04004158 if (new_state == NLP_STE_MAPPED_NODE ||
4159 new_state == NLP_STE_UNMAPPED_NODE) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004160 if ((ndlp->nlp_fc4_type & NLP_FC4_FCP) ||
4161 (ndlp->nlp_DID == Fabric_DID)) {
4162 vport->phba->nport_event_cnt++;
4163 /*
4164 * Tell the fc transport about the port, if we haven't
4165 * already. If we have, and it's a scsi entity, be
4166 */
4167 lpfc_register_remote_port(vport, ndlp);
4168 }
4169 /* Notify the NVME transport of this new rport. */
4170 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4171 if (vport->phba->nvmet_support == 0) {
4172 /* Register this rport with the transport.
4173 * Initiators take the NDLP ref count in
4174 * the register.
4175 */
4176 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004177 lpfc_nvme_register_port(vport, ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004178 }
4179 }
James Smartde0c5b32007-04-25 09:52:27 -04004180 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004181
James Smartea2151b2008-09-07 11:52:10 -04004182 if ((new_state == NLP_STE_MAPPED_NODE) &&
4183 (vport->stat_data_enabled)) {
4184 /*
4185 * A new target is discovered, if there is no buffer for
4186 * statistical data collection allocate buffer.
4187 */
4188 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4189 sizeof(struct lpfc_scsicmd_bkt),
4190 GFP_KERNEL);
4191
4192 if (!ndlp->lat_data)
4193 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4194 "0286 lpfc_nlp_state_cleanup failed to "
4195 "allocate statistical data buffer DID "
4196 "0x%x\n", ndlp->nlp_DID);
4197 }
James Smart858c9f62007-06-17 19:56:39 -05004198 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004199 * If the node just added to Mapped list was an FCP target,
4200 * but the remote port registration failed or assigned a target
4201 * id outside the presentable range - move the node to the
4202 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004203 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004204 if ((new_state == NLP_STE_MAPPED_NODE) &&
4205 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004206 (!ndlp->rport ||
4207 ndlp->rport->scsi_target_id == -1 ||
4208 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004209 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004210 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004211 spin_unlock_irq(shost->host_lock);
4212 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004213 }
James Smartde0c5b32007-04-25 09:52:27 -04004214}
4215
James Smart685f0bf2007-04-25 09:53:08 -04004216static char *
4217lpfc_nlp_state_name(char *buffer, size_t size, int state)
4218{
4219 static char *states[] = {
4220 [NLP_STE_UNUSED_NODE] = "UNUSED",
4221 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4222 [NLP_STE_ADISC_ISSUE] = "ADISC",
4223 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4224 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004225 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004226 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4227 [NLP_STE_MAPPED_NODE] = "MAPPED",
4228 [NLP_STE_NPR_NODE] = "NPR",
4229 };
4230
James Smart311464e2007-08-02 11:10:37 -04004231 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004232 strlcpy(buffer, states[state], size);
4233 else
4234 snprintf(buffer, size, "unknown (%d)", state);
4235 return buffer;
4236}
4237
James Smartde0c5b32007-04-25 09:52:27 -04004238void
James Smart2e0fef82007-06-17 19:56:36 -05004239lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4240 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004241{
James Smart2e0fef82007-06-17 19:56:36 -05004242 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004243 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004244 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004245
James Smarte8b62012007-08-02 11:10:09 -04004246 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4247 "0904 NPort state transition x%06x, %s -> %s\n",
4248 ndlp->nlp_DID,
4249 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4250 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004251
4252 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4253 "node statechg did:x%x old:%d ste:%d",
4254 ndlp->nlp_DID, old_state, state);
4255
James Smartde0c5b32007-04-25 09:52:27 -04004256 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004257 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004258 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004259 if (old_state == NLP_STE_UNMAPPED_NODE) {
4260 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4261 ndlp->nlp_type &= ~NLP_FC_NODE;
4262 }
4263
James Smart685f0bf2007-04-25 09:53:08 -04004264 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004265 spin_lock_irq(shost->host_lock);
4266 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4267 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004268 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004269 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004270
4271 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004272 lpfc_nlp_counters(vport, state, 1);
4273 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004274}
4275
4276void
James Smarte47c9092008-02-08 18:49:26 -05004277lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4278{
4279 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4280
4281 if (list_empty(&ndlp->nlp_listp)) {
4282 spin_lock_irq(shost->host_lock);
4283 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4284 spin_unlock_irq(shost->host_lock);
4285 }
4286}
4287
4288void
James Smart2e0fef82007-06-17 19:56:36 -05004289lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004290{
James Smart2e0fef82007-06-17 19:56:36 -05004291 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4292
James Smart0d2b6b82008-06-14 22:52:47 -04004293 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004294 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004295 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4296 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004297 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004298 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004299 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004300 NLP_STE_UNUSED_NODE);
4301}
4302
Adrian Bunk4d9db012008-02-14 23:24:02 +02004303static void
James Smarte47c9092008-02-08 18:49:26 -05004304lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4305{
James Smart0d2b6b82008-06-14 22:52:47 -04004306 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004307 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4308 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4309 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4310 NLP_STE_UNUSED_NODE);
4311}
James Smart109f6ed2008-12-04 22:39:08 -05004312/**
James Smart3621a712009-04-06 18:47:14 -04004313 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004314 * @vport: Pointer to Virtual Port object.
4315 * @ndlp: Pointer to FC node object.
4316 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004317 *
4318 * This function is always called when node object need to be initialized.
4319 * It initializes all the fields of the node object. Although the reference
4320 * to phba from @ndlp can be obtained indirectly through it's reference to
4321 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4322 * to the life-span of the @ndlp might go beyond the existence of @vport as
4323 * the final release of ndlp is determined by its reference count. And, the
4324 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004325 **/
4326static inline void
4327lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4328 uint32_t did)
4329{
4330 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4331 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4332 init_timer(&ndlp->nlp_delayfunc);
4333 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4334 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4335 ndlp->nlp_DID = did;
4336 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004337 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004338 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004339 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004340 kref_init(&ndlp->kref);
4341 NLP_INT_NODE_ACT(ndlp);
4342 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004343 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004344}
James Smarte47c9092008-02-08 18:49:26 -05004345
4346struct lpfc_nodelist *
4347lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4348 int state)
4349{
4350 struct lpfc_hba *phba = vport->phba;
4351 uint32_t did;
4352 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004353 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004354
4355 if (!ndlp)
4356 return NULL;
4357
4358 spin_lock_irqsave(&phba->ndlp_lock, flags);
4359 /* The ndlp should not be in memory free mode */
4360 if (NLP_CHK_FREE_REQ(ndlp)) {
4361 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4362 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4363 "0277 lpfc_enable_node: ndlp:x%p "
4364 "usgmap:x%x refcnt:%d\n",
4365 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004366 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004367 return NULL;
4368 }
4369 /* The ndlp should not already be in active mode */
4370 if (NLP_CHK_NODE_ACT(ndlp)) {
4371 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4372 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4373 "0278 lpfc_enable_node: ndlp:x%p "
4374 "usgmap:x%x refcnt:%d\n",
4375 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004376 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004377 return NULL;
4378 }
4379
4380 /* Keep the original DID */
4381 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004382 if (phba->sli_rev == LPFC_SLI_REV4)
4383 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004384
4385 /* re-initialize ndlp except of ndlp linked list pointer */
4386 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4387 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004388 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004389
James Smartcff261f2013-12-17 20:29:47 -05004390 if (phba->sli_rev == LPFC_SLI_REV4)
4391 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4392
James Smarte47c9092008-02-08 18:49:26 -05004393 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004394 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004395 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004396 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4397 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4398 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4399 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004400 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004401 ndlp->nlp_usg_map, ndlp);
4402 }
James Smart725dd392013-10-10 12:23:30 -04004403
James Smarte47c9092008-02-08 18:49:26 -05004404
4405 if (state != NLP_STE_UNUSED_NODE)
4406 lpfc_nlp_set_state(vport, ndlp, state);
4407
4408 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4409 "node enable: did:x%x",
4410 ndlp->nlp_DID, 0, 0);
4411 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004412}
4413
4414void
James Smart2e0fef82007-06-17 19:56:36 -05004415lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004416{
James Smart87af33f2007-10-27 13:37:43 -04004417 /*
James Smartfa4066b2008-01-11 01:53:27 -05004418 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004419 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004420 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4421 * until ALL other outstanding threads have completed. We check
4422 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004423 */
James Smartfa4066b2008-01-11 01:53:27 -05004424 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4425 return;
James Smart51ef4c22007-08-02 11:10:31 -04004426 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004427 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004428 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004429 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004430 }
James Smart02902172015-05-21 13:55:17 -04004431
4432 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004433 return;
dea31012005-04-17 16:05:31 -05004434}
4435
4436/*
4437 * Start / ReStart rescue timer for Discovery / RSCN handling
4438 */
4439void
James Smart2e0fef82007-06-17 19:56:36 -05004440lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004441{
James Smart2e0fef82007-06-17 19:56:36 -05004442 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4443 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004444 uint32_t tmo;
4445
James Smart2e0fef82007-06-17 19:56:36 -05004446 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004447 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004448 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4449 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004450 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004451 * FC spec states we need 3 * ratov for CT requests
4452 */
4453 tmo = ((phba->fc_ratov * 3) + 3);
4454 }
dea31012005-04-17 16:05:31 -05004455
James Smart858c9f62007-06-17 19:56:39 -05004456
4457 if (!timer_pending(&vport->fc_disctmo)) {
4458 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4459 "set disc timer: tmo:x%x state:x%x flg:x%x",
4460 tmo, vport->port_state, vport->fc_flag);
4461 }
4462
James Smart256ec0d2013-04-17 20:14:58 -04004463 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004464 spin_lock_irq(shost->host_lock);
4465 vport->fc_flag |= FC_DISC_TMO;
4466 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004467
4468 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004469 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4470 "0247 Start Discovery Timer state x%x "
4471 "Data: x%x x%lx x%x x%x\n",
4472 vport->port_state, tmo,
4473 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4474 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004475
4476 return;
4477}
4478
4479/*
4480 * Cancel rescue timer for Discovery / RSCN handling
4481 */
4482int
James Smart2e0fef82007-06-17 19:56:36 -05004483lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004484{
James Smart2e0fef82007-06-17 19:56:36 -05004485 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004486 unsigned long iflags;
4487
James Smart858c9f62007-06-17 19:56:39 -05004488 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4489 "can disc timer: state:x%x rtry:x%x flg:x%x",
4490 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4491
dea31012005-04-17 16:05:31 -05004492 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004493 if (vport->fc_flag & FC_DISC_TMO) {
4494 spin_lock_irqsave(shost->host_lock, iflags);
4495 vport->fc_flag &= ~FC_DISC_TMO;
4496 spin_unlock_irqrestore(shost->host_lock, iflags);
4497 del_timer_sync(&vport->fc_disctmo);
4498 spin_lock_irqsave(&vport->work_port_lock, iflags);
4499 vport->work_port_events &= ~WORKER_DISC_TMO;
4500 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004501 }
4502
4503 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004504 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4505 "0248 Cancel Discovery Timer state x%x "
4506 "Data: x%x x%x x%x\n",
4507 vport->port_state, vport->fc_flag,
4508 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004509 return 0;
dea31012005-04-17 16:05:31 -05004510}
4511
4512/*
4513 * Check specified ring for outstanding IOCB on the SLI queue
4514 * Return true if iocb matches the specified nport
4515 */
4516int
James Smart2e0fef82007-06-17 19:56:36 -05004517lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4518 struct lpfc_sli_ring *pring,
4519 struct lpfc_iocbq *iocb,
4520 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004521{
James Smart2e0fef82007-06-17 19:56:36 -05004522 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004523 struct lpfc_vport *vport = ndlp->vport;
4524
4525 if (iocb->vport != vport)
4526 return 0;
4527
dea31012005-04-17 16:05:31 -05004528 if (pring->ringno == LPFC_ELS_RING) {
4529 switch (icmd->ulpCommand) {
4530 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004531 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004532 return 1;
dea31012005-04-17 16:05:31 -05004533 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004534 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4535 return 1;
dea31012005-04-17 16:05:31 -05004536 case CMD_XMIT_ELS_RSP64_CX:
4537 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004538 return 1;
dea31012005-04-17 16:05:31 -05004539 }
James Smart895427b2017-02-12 13:52:30 -08004540 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004541 /* Skip match check if waiting to relogin to FCP target */
4542 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004543 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004544 return 0;
dea31012005-04-17 16:05:31 -05004545 }
4546 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004547 return 1;
dea31012005-04-17 16:05:31 -05004548 }
dea31012005-04-17 16:05:31 -05004549 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004550 return 0;
dea31012005-04-17 16:05:31 -05004551}
4552
James Smart895427b2017-02-12 13:52:30 -08004553static void
4554__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4555 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4556 struct list_head *dequeue_list)
4557{
4558 struct lpfc_iocbq *iocb, *next_iocb;
4559
4560 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4561 /* Check to see if iocb matches the nport */
4562 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4563 /* match, dequeue */
4564 list_move_tail(&iocb->list, dequeue_list);
4565 }
4566}
4567
4568static void
4569lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4570 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4571{
4572 struct lpfc_sli *psli = &phba->sli;
4573 uint32_t i;
4574
4575 spin_lock_irq(&phba->hbalock);
4576 for (i = 0; i < psli->num_rings; i++)
4577 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4578 dequeue_list);
4579 spin_unlock_irq(&phba->hbalock);
4580}
4581
4582static void
4583lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4584 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4585{
4586 struct lpfc_sli_ring *pring;
4587 struct lpfc_queue *qp = NULL;
4588
4589 spin_lock_irq(&phba->hbalock);
4590 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4591 pring = qp->pring;
4592 if (!pring)
4593 continue;
4594 spin_lock_irq(&pring->ring_lock);
4595 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4596 spin_unlock_irq(&pring->ring_lock);
4597 }
4598 spin_unlock_irq(&phba->hbalock);
4599}
4600
dea31012005-04-17 16:05:31 -05004601/*
4602 * Free resources / clean up outstanding I/Os
4603 * associated with nlp_rpi in the LPFC_NODELIST entry.
4604 */
4605static int
James Smart2e0fef82007-06-17 19:56:36 -05004606lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004607{
James Smart2534ba72007-04-25 09:52:20 -04004608 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004609
James Smart92d7f7b2007-06-17 19:56:38 -05004610 lpfc_fabric_abort_nport(ndlp);
4611
dea31012005-04-17 16:05:31 -05004612 /*
4613 * Everything that matches on txcmplq will be returned
4614 * by firmware with a no rpi error.
4615 */
James Smart40426292010-12-15 17:58:10 -05004616 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004617 if (phba->sli_rev != LPFC_SLI_REV4)
4618 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4619 else
4620 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004621 }
James Smart2534ba72007-04-25 09:52:20 -04004622
James Smarta257bf92009-04-06 18:48:10 -04004623 /* Cancel all the IOCBs from the completions list */
4624 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4625 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004626
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004627 return 0;
dea31012005-04-17 16:05:31 -05004628}
4629
James Smart086a3452012-08-14 14:25:21 -04004630/**
4631 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4632 * @phba: Pointer to HBA context object.
4633 * @pmb: Pointer to mailbox object.
4634 *
4635 * This function will issue an ELS LOGO command after completing
4636 * the UNREG_RPI.
4637 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004638static void
James Smart086a3452012-08-14 14:25:21 -04004639lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4640{
4641 struct lpfc_vport *vport = pmb->vport;
4642 struct lpfc_nodelist *ndlp;
4643
4644 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4645 if (!ndlp)
4646 return;
4647 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004648 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004649}
4650
dea31012005-04-17 16:05:31 -05004651/*
4652 * Free rpi associated with LPFC_NODELIST entry.
4653 * This routine is called from lpfc_freenode(), when we are removing
4654 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4655 * LOGO that completes successfully, and we are waiting to PLOGI back
4656 * to the remote NPort. In addition, it is called after we receive
4657 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4658 * we are waiting to PLOGI back to the remote NPort.
4659 */
4660int
James Smart2e0fef82007-06-17 19:56:36 -05004661lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004662{
James Smart2e0fef82007-06-17 19:56:36 -05004663 struct lpfc_hba *phba = vport->phba;
4664 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004665 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004666 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004667
James Smartf454a9a2013-09-06 12:20:07 -04004668 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4669 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4670 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4671 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4672 "3366 RPI x%x needs to be "
4673 "unregistered nlp_flag x%x "
4674 "did x%x\n",
4675 ndlp->nlp_rpi, ndlp->nlp_flag,
4676 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004677 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4678 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004679 /* SLI4 ports require the physical rpi value. */
4680 rpi = ndlp->nlp_rpi;
4681 if (phba->sli_rev == LPFC_SLI_REV4)
4682 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004683
James Smart6d368e52011-05-24 11:44:12 -04004684 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004685 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004686 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4687 mbox->context1 = ndlp;
4688 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4689 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004690 if (phba->sli_rev == LPFC_SLI_REV4 &&
4691 (!(vport->load_flag & FC_UNLOADING)) &&
4692 (bf_get(lpfc_sli_intf_if_type,
4693 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004694 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004695 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004696 mbox->context1 = lpfc_nlp_get(ndlp);
4697 mbox->mbox_cmpl =
4698 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004699 /*
4700 * accept PLOGIs after unreg_rpi_cmpl
4701 */
4702 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004703 } else
4704 mbox->mbox_cmpl =
4705 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004706 }
4707
James Smart0b727fe2007-10-27 13:37:25 -04004708 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004709 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004710 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004711 acc_plogi = 1;
4712 }
dea31012005-04-17 16:05:31 -05004713 }
dea31012005-04-17 16:05:31 -05004714 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004715
James Smart40426292010-12-15 17:58:10 -05004716 if (phba->sli_rev != LPFC_SLI_REV4)
4717 ndlp->nlp_rpi = 0;
4718 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004719 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004720 if (acc_plogi)
4721 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004722 return 1;
4723 }
James Smart7c5e5182015-05-22 10:42:43 -04004724 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004725 return 0;
4726}
4727
James Smartecfd03c2010-02-12 14:41:27 -05004728/**
4729 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4730 * @phba: pointer to lpfc hba data structure.
4731 *
4732 * This routine is invoked to unregister all the currently registered RPIs
4733 * to the HBA.
4734 **/
4735void
4736lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4737{
4738 struct lpfc_vport **vports;
4739 struct lpfc_nodelist *ndlp;
4740 struct Scsi_Host *shost;
4741 int i;
4742
4743 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004744 if (!vports) {
4745 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4746 "2884 Vport array allocation failed \n");
4747 return;
4748 }
James Smartecfd03c2010-02-12 14:41:27 -05004749 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4750 shost = lpfc_shost_from_vport(vports[i]);
4751 spin_lock_irq(shost->host_lock);
4752 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004753 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004754 /* The mempool_alloc might sleep */
4755 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004756 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004757 spin_lock_irq(shost->host_lock);
4758 }
James Smartecfd03c2010-02-12 14:41:27 -05004759 }
4760 spin_unlock_irq(shost->host_lock);
4761 }
4762 lpfc_destroy_vport_work_array(phba, vports);
4763}
4764
James Smart92d7f7b2007-06-17 19:56:38 -05004765void
4766lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4767{
4768 struct lpfc_hba *phba = vport->phba;
4769 LPFC_MBOXQ_t *mbox;
4770 int rc;
4771
James Smart5af5eee2010-10-22 11:06:38 -04004772 if (phba->sli_rev == LPFC_SLI_REV4) {
4773 lpfc_sli4_unreg_all_rpis(vport);
4774 return;
4775 }
4776
James Smart92d7f7b2007-06-17 19:56:38 -05004777 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4778 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004779 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4780 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004781 mbox->vport = vport;
4782 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004783 mbox->context1 = NULL;
4784 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004785 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004786 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004787
4788 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4789 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4790 "1836 Could not issue "
4791 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004792 }
4793}
4794
4795void
4796lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4797{
4798 struct lpfc_hba *phba = vport->phba;
4799 LPFC_MBOXQ_t *mbox;
4800 int rc;
4801
4802 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4803 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004804 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4805 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004806 mbox->vport = vport;
4807 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004808 mbox->context1 = NULL;
4809 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004810 if (rc != MBX_TIMEOUT)
4811 mempool_free(mbox, phba->mbox_mem_pool);
4812
4813 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004814 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4815 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004816 "unreg_did (default rpis) status %d\n",
4817 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004818 }
4819}
4820
dea31012005-04-17 16:05:31 -05004821/*
4822 * Free resources associated with LPFC_NODELIST entry
4823 * so it can be freed.
4824 */
4825static int
James Smart2e0fef82007-06-17 19:56:36 -05004826lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004827{
James Smart2e0fef82007-06-17 19:56:36 -05004828 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4829 struct lpfc_hba *phba = vport->phba;
4830 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004831 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004832
4833 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004834 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4835 "0900 Cleanup node for NPort x%x "
4836 "Data: x%x x%x x%x\n",
4837 ndlp->nlp_DID, ndlp->nlp_flag,
4838 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004839 if (NLP_CHK_FREE_REQ(ndlp)) {
4840 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4841 "0280 lpfc_cleanup_node: ndlp:x%p "
4842 "usgmap:x%x refcnt:%d\n",
4843 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004844 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004845 lpfc_dequeue_node(vport, ndlp);
4846 } else {
4847 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4848 "0281 lpfc_cleanup_node: ndlp:x%p "
4849 "usgmap:x%x refcnt:%d\n",
4850 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004851 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004852 lpfc_disable_node(vport, ndlp);
4853 }
dea31012005-04-17 16:05:31 -05004854
James Smart086a3452012-08-14 14:25:21 -04004855
4856 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4857
dea31012005-04-17 16:05:31 -05004858 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4859 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004860 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004861 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004862 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4863 mb->context2 = NULL;
4864 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4865 }
4866 }
James Smart33ccf8d2006-08-17 11:57:58 -04004867
James Smart2e0fef82007-06-17 19:56:36 -05004868 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004869 /* Cleanup REG_LOGIN completions which are not yet processed */
4870 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4871 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004872 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004873 (ndlp != (struct lpfc_nodelist *) mb->context2))
4874 continue;
4875
4876 mb->context2 = NULL;
4877 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4878 }
4879
dea31012005-04-17 16:05:31 -05004880 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004881 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004882 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004883 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004884 mp = (struct lpfc_dmabuf *) (mb->context1);
4885 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004886 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004887 kfree(mp);
4888 }
4889 list_del(&mb->list);
4890 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004891 /* We shall not invoke the lpfc_nlp_put to decrement
4892 * the ndlp reference count as we are in the process
4893 * of lpfc_nlp_release.
4894 */
dea31012005-04-17 16:05:31 -05004895 }
4896 }
James Smart2e0fef82007-06-17 19:56:36 -05004897 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004898
James Smarte47c9092008-02-08 18:49:26 -05004899 lpfc_els_abort(phba, ndlp);
4900
James Smart2e0fef82007-06-17 19:56:36 -05004901 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004902 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004903 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004904
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004905 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004906 del_timer_sync(&ndlp->nlp_delayfunc);
4907
James Smart0d2b6b82008-06-14 22:52:47 -04004908 list_del_init(&ndlp->els_retry_evt.evt_listp);
4909 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004910 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004911 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004912
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004913 return 0;
dea31012005-04-17 16:05:31 -05004914}
4915
4916/*
4917 * Check to see if we can free the nlp back to the freelist.
4918 * If we are in the middle of using the nlp in the discovery state
4919 * machine, defer the free till we reach the end of the state machine.
4920 */
James Smart329f9bc2007-04-25 09:53:01 -04004921static void
James Smart2e0fef82007-06-17 19:56:36 -05004922lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004923{
James Smarta8adb832007-10-27 13:37:53 -04004924 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004925 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004926 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004927 LPFC_MBOXQ_t *mbox;
4928 int rc;
dea31012005-04-17 16:05:31 -05004929
James Smart0d2b6b82008-06-14 22:52:47 -04004930 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004931 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004932 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4933 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004934 /* For this case we need to cleanup the default rpi
4935 * allocated by the firmware.
4936 */
James Smartbe6bb942015-04-07 15:07:22 -04004937 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4938 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4939 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004940 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004941 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004942 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4943 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004944 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004945 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004946 if (rc) {
4947 mempool_free(mbox, phba->mbox_mem_pool);
4948 }
4949 else {
4950 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4951 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4952 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004953 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004954 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4955 if (rc == MBX_NOT_FINISHED) {
4956 mempool_free(mbox, phba->mbox_mem_pool);
4957 }
4958 }
4959 }
4960 }
James Smart2e0fef82007-06-17 19:56:36 -05004961 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004962
James Smart2e0fef82007-06-17 19:56:36 -05004963 /*
James Smart466e8402015-05-21 13:55:28 -04004964 * ndlp->rport must be set to NULL before it reaches here
4965 * i.e. break rport/node link before doing lpfc_nlp_put for
4966 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05004967 */
James Smart92d7f7b2007-06-17 19:56:38 -05004968 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04004969 /*
4970 * extra lpfc_nlp_put dropped the reference of ndlp
4971 * for registered rport so need to cleanup rport
4972 */
4973 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4974 "0940 removed node x%p DID x%x "
4975 " rport not null %p\n",
4976 ndlp, ndlp->nlp_DID, ndlp->rport);
4977 rport = ndlp->rport;
4978 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004979 rdata->pnode = NULL;
4980 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04004981 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05004982 }
dea31012005-04-17 16:05:31 -05004983}
4984
4985static int
James Smart2e0fef82007-06-17 19:56:36 -05004986lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4987 uint32_t did)
dea31012005-04-17 16:05:31 -05004988{
James Smart2e0fef82007-06-17 19:56:36 -05004989 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004990
4991 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004992 return 0;
dea31012005-04-17 16:05:31 -05004993
dea31012005-04-17 16:05:31 -05004994 /* First check for Direct match */
4995 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004996 return 1;
dea31012005-04-17 16:05:31 -05004997
4998 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004999 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005000 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005001 return 0;
dea31012005-04-17 16:05:31 -05005002 }
5003
5004 matchdid.un.word = did;
5005 ndlpdid.un.word = ndlp->nlp_DID;
5006 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5007 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5008 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005009 /* This code is supposed to match the ID
5010 * for a private loop device that is
5011 * connect to fl_port. But we need to
5012 * check that the port did not just go
5013 * from pt2pt to fabric or we could end
5014 * up matching ndlp->nlp_DID 000001 to
5015 * fabric DID 0x20101
5016 */
dea31012005-04-17 16:05:31 -05005017 if ((ndlpdid.un.b.domain == 0) &&
5018 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005019 if (ndlpdid.un.b.id &&
5020 vport->phba->fc_topology ==
5021 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005022 return 1;
dea31012005-04-17 16:05:31 -05005023 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005024 return 0;
dea31012005-04-17 16:05:31 -05005025 }
5026
5027 matchdid.un.word = ndlp->nlp_DID;
5028 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5029 (mydid.un.b.area == ndlpdid.un.b.area)) {
5030 if ((matchdid.un.b.domain == 0) &&
5031 (matchdid.un.b.area == 0)) {
5032 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005033 return 1;
dea31012005-04-17 16:05:31 -05005034 }
5035 }
5036 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005037 return 0;
dea31012005-04-17 16:05:31 -05005038}
5039
James Smart685f0bf2007-04-25 09:53:08 -04005040/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005041static struct lpfc_nodelist *
5042__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005043{
James Smart2fb9bd82006-12-02 13:33:57 -05005044 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005045 uint32_t data1;
5046
James Smart2e0fef82007-06-17 19:56:36 -05005047 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5048 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005049 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5050 ((uint32_t) ndlp->nlp_xri << 16) |
5051 ((uint32_t) ndlp->nlp_type << 8) |
5052 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005053 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5054 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005055 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005056 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005057 ndlp->nlp_flag, data1,
5058 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005059 return ndlp;
dea31012005-04-17 16:05:31 -05005060 }
5061 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005062
dea31012005-04-17 16:05:31 -05005063 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005064 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5065 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005066 return NULL;
5067}
5068
5069struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005070lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005071{
James Smart2e0fef82007-06-17 19:56:36 -05005072 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005073 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005074 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005075
James Smartfedd3b72011-02-16 12:39:24 -05005076 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005077 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005078 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005079 return ndlp;
5080}
5081
5082struct lpfc_nodelist *
5083lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5084{
5085 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5086 struct lpfc_nodelist *ndlp;
5087
5088 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005089 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05005090 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5091 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005092 return NULL;
5093 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05005094 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005095 if (!ndlp)
5096 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005097 lpfc_nlp_init(vport, ndlp, did);
5098 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5099 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005100 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005101 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005102 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005103 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5104 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5105 if (!ndlp)
5106 return NULL;
5107 spin_lock_irq(shost->host_lock);
5108 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5109 spin_unlock_irq(shost->host_lock);
5110 return ndlp;
dea31012005-04-17 16:05:31 -05005111 }
James Smarte47c9092008-02-08 18:49:26 -05005112
James Smart58da1ff2008-04-07 10:15:56 -04005113 if ((vport->fc_flag & FC_RSCN_MODE) &&
5114 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005115 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005116 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005117 * we don't need to try to discover it again.
5118 */
5119 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5120 return NULL;
5121
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005122 /* Since this node is marked for discovery,
5123 * delay timeout is not needed.
5124 */
James Smart0d2b6b82008-06-14 22:52:47 -04005125 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04005126 spin_lock_irq(shost->host_lock);
5127 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5128 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005129 } else
dea31012005-04-17 16:05:31 -05005130 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005131 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005132 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04005133 * or we are already in the process of discovery on it,
5134 * we don't need to try to discover it again.
5135 */
James Smart685f0bf2007-04-25 09:53:08 -04005136 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005137 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5138 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05005139 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005140 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5141 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005142 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005143 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005144 }
5145 return ndlp;
5146}
5147
5148/* Build a list of nodes to discover based on the loopmap */
5149void
James Smart2e0fef82007-06-17 19:56:36 -05005150lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005151{
James Smart2e0fef82007-06-17 19:56:36 -05005152 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005153 int j;
5154 uint32_t alpa, index;
5155
James Smart2e0fef82007-06-17 19:56:36 -05005156 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005157 return;
James Smart2e0fef82007-06-17 19:56:36 -05005158
James Smart76a95d72010-11-20 23:11:48 -05005159 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005160 return;
dea31012005-04-17 16:05:31 -05005161
5162 /* Check for loop map present or not */
5163 if (phba->alpa_map[0]) {
5164 for (j = 1; j <= phba->alpa_map[0]; j++) {
5165 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005166 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005167 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005168 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005169 }
5170 } else {
5171 /* No alpamap, so try all alpa's */
5172 for (j = 0; j < FC_MAXLOOP; j++) {
5173 /* If cfg_scan_down is set, start from highest
5174 * ALPA (0xef) to lowest (0x1).
5175 */
James Smart3de2a652007-08-02 11:09:59 -04005176 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005177 index = j;
5178 else
5179 index = FC_MAXLOOP - j - 1;
5180 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005181 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005182 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005183 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005184 }
5185 }
5186 return;
5187}
5188
James Smart895427b2017-02-12 13:52:30 -08005189/* SLI3 only */
dea31012005-04-17 16:05:31 -05005190void
James Smart2e0fef82007-06-17 19:56:36 -05005191lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005192{
dea31012005-04-17 16:05:31 -05005193 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005194 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005195 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5196 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005197 int rc;
5198
James Smart92d7f7b2007-06-17 19:56:38 -05005199 /*
5200 * if it's not a physical port or if we already send
5201 * clear_la then don't send it.
5202 */
5203 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005204 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5205 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005206 return;
5207
James Smart2e0fef82007-06-17 19:56:36 -05005208 /* Link up discovery */
5209 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5210 phba->link_state = LPFC_CLEAR_LA;
5211 lpfc_clear_la(phba, mbox);
5212 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5213 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005214 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005215 if (rc == MBX_NOT_FINISHED) {
5216 mempool_free(mbox, phba->mbox_mem_pool);
5217 lpfc_disc_flush_list(vport);
5218 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5219 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005220 phba->link_state = LPFC_HBA_ERROR;
5221 }
5222 }
5223}
5224
5225/* Reg_vpi to tell firmware to resume normal operations */
5226void
5227lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5228{
5229 LPFC_MBOXQ_t *regvpimbox;
5230
5231 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5232 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005233 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005234 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5235 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005236 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005237 == MBX_NOT_FINISHED) {
5238 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005239 }
5240 }
5241}
5242
5243/* Start Link up / RSCN discovery on NPR nodes */
5244void
5245lpfc_disc_start(struct lpfc_vport *vport)
5246{
5247 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5248 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005249 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005250 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005251
James Smarte74c03c2013-04-17 20:15:19 -04005252 if (!lpfc_is_link_up(phba)) {
5253 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5254 "3315 Link is not up %x\n",
5255 phba->link_state);
dea31012005-04-17 16:05:31 -05005256 return;
James Smarte74c03c2013-04-17 20:15:19 -04005257 }
James Smart2e0fef82007-06-17 19:56:36 -05005258
5259 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005260 clear_la_pending = 1;
5261 else
5262 clear_la_pending = 0;
5263
James Smart2e0fef82007-06-17 19:56:36 -05005264 if (vport->port_state < LPFC_VPORT_READY)
5265 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005266
James Smart2e0fef82007-06-17 19:56:36 -05005267 lpfc_set_disctmo(vport);
5268
James Smart2e0fef82007-06-17 19:56:36 -05005269 vport->fc_prevDID = vport->fc_myDID;
5270 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005271
5272 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005273 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5274 "0202 Start Discovery hba state x%x "
5275 "Data: x%x x%x x%x\n",
5276 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5277 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005278
5279 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005280 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005281
5282 if (num_sent)
5283 return;
5284
James Smart6fa139f2013-04-17 20:20:00 -04005285 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005286 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005287 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005288 !(vport->fc_flag & FC_RSCN_MODE) &&
5289 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005290 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005291 lpfc_issue_reg_vpi(phba, vport);
5292 return;
5293 }
James Smart2e0fef82007-06-17 19:56:36 -05005294
James Smart92d7f7b2007-06-17 19:56:38 -05005295 /*
5296 * For SLI2, we need to set port_state to READY and continue
5297 * discovery.
5298 */
5299 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5300 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005301 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005302
5303 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005304 vport->num_disc_nodes = 0;
5305 /* go thru NPR nodes and issue ELS PLOGIs */
5306 if (vport->fc_npr_cnt)
5307 lpfc_els_disc_plogi(vport);
5308
5309 if (!vport->num_disc_nodes) {
5310 spin_lock_irq(shost->host_lock);
5311 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5312 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005313 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005314 }
5315 }
James Smart92d7f7b2007-06-17 19:56:38 -05005316 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005317 } else {
5318 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005319 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005320
5321 if (num_sent)
5322 return;
5323
James Smart2e0fef82007-06-17 19:56:36 -05005324 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005325 /* Check to see if more RSCNs came in while we
5326 * were processing this one.
5327 */
James Smart2e0fef82007-06-17 19:56:36 -05005328 if ((vport->fc_rscn_id_cnt == 0) &&
5329 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5330 spin_lock_irq(shost->host_lock);
5331 vport->fc_flag &= ~FC_RSCN_MODE;
5332 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005333 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005334 } else
James Smart2e0fef82007-06-17 19:56:36 -05005335 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005336 }
5337 }
5338 return;
5339}
5340
5341/*
5342 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5343 * ring the match the sppecified nodelist.
5344 */
5345static void
James Smart2e0fef82007-06-17 19:56:36 -05005346lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005347{
James Smart2534ba72007-04-25 09:52:20 -04005348 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005349 struct lpfc_sli *psli;
5350 IOCB_t *icmd;
5351 struct lpfc_iocbq *iocb, *next_iocb;
5352 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005353
5354 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005355 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005356
5357 /* Error matching iocb on txq or txcmplq
5358 * First check the txq.
5359 */
James Smart2e0fef82007-06-17 19:56:36 -05005360 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005361 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5362 if (iocb->context1 != ndlp) {
5363 continue;
5364 }
5365 icmd = &iocb->iocb;
5366 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5367 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5368
James Smart2534ba72007-04-25 09:52:20 -04005369 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005370 }
5371 }
5372
5373 /* Next check the txcmplq */
5374 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5375 if (iocb->context1 != ndlp) {
5376 continue;
5377 }
5378 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005379 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5380 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005381 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005382 }
5383 }
James Smart2e0fef82007-06-17 19:56:36 -05005384 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005385
James Smarta257bf92009-04-06 18:48:10 -04005386 /* Cancel all the IOCBs from the completions list */
5387 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5388 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005389}
5390
Adrian Bunka6ababd2007-11-05 18:07:33 +01005391static void
James Smart2e0fef82007-06-17 19:56:36 -05005392lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005393{
5394 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005395 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005396
James Smart2e0fef82007-06-17 19:56:36 -05005397 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5398 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005399 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005400 if (!NLP_CHK_NODE_ACT(ndlp))
5401 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005402 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5403 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5404 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005405 }
dea31012005-04-17 16:05:31 -05005406 }
5407 }
dea31012005-04-17 16:05:31 -05005408}
5409
James Smart92d7f7b2007-06-17 19:56:38 -05005410void
5411lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5412{
5413 lpfc_els_flush_rscn(vport);
5414 lpfc_els_flush_cmd(vport);
5415 lpfc_disc_flush_list(vport);
5416}
5417
dea31012005-04-17 16:05:31 -05005418/*****************************************************************************/
5419/*
5420 * NAME: lpfc_disc_timeout
5421 *
5422 * FUNCTION: Fibre Channel driver discovery timeout routine.
5423 *
5424 * EXECUTION ENVIRONMENT: interrupt only
5425 *
5426 * CALLED FROM:
5427 * Timer function
5428 *
5429 * RETURNS:
5430 * none
5431 */
5432/*****************************************************************************/
5433void
5434lpfc_disc_timeout(unsigned long ptr)
5435{
James Smart2e0fef82007-06-17 19:56:36 -05005436 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5437 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005438 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005439 unsigned long flags = 0;
5440
5441 if (unlikely(!phba))
5442 return;
5443
James Smart5e9d9b82008-06-14 22:52:53 -04005444 spin_lock_irqsave(&vport->work_port_lock, flags);
5445 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5446 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005447 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005448 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005449
James Smart5e9d9b82008-06-14 22:52:53 -04005450 if (!tmo_posted)
5451 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005452 return;
5453}
5454
5455static void
James Smart2e0fef82007-06-17 19:56:36 -05005456lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005457{
James Smart2e0fef82007-06-17 19:56:36 -05005458 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5459 struct lpfc_hba *phba = vport->phba;
5460 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005461 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005462 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005463 int rc, clrlaerr = 0;
5464
James Smart2e0fef82007-06-17 19:56:36 -05005465 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005466 return;
5467
James Smart2e0fef82007-06-17 19:56:36 -05005468 spin_lock_irq(shost->host_lock);
5469 vport->fc_flag &= ~FC_DISC_TMO;
5470 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005471
James Smart858c9f62007-06-17 19:56:39 -05005472 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5473 "disc timeout: state:x%x rtry:x%x flg:x%x",
5474 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5475
James Smart2e0fef82007-06-17 19:56:36 -05005476 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005477
5478 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005479 /*
5480 * port_state is identically LPFC_LOCAL_CFG_LINK while
5481 * waiting for FAN timeout
5482 */
James Smarte8b62012007-08-02 11:10:09 -04005483 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5484 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005485
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005486 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005487 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005488 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005489 if (!NLP_CHK_NODE_ACT(ndlp))
5490 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005491 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5492 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005493 if (ndlp->nlp_type & NLP_FABRIC) {
5494 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005495 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005496
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005497 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005498 /* Fail outstanding IO now since device
5499 * is marked for PLOGI.
5500 */
James Smart2e0fef82007-06-17 19:56:36 -05005501 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005502 }
5503 }
James Smart92d7f7b2007-06-17 19:56:38 -05005504 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005505 if (phba->sli_rev <= LPFC_SLI_REV3)
5506 lpfc_initial_flogi(vport);
5507 else
5508 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005509 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005510 }
dea31012005-04-17 16:05:31 -05005511 break;
5512
James Smart92d7f7b2007-06-17 19:56:38 -05005513 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005514 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005515 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005516 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005517 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5518 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005519 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005520
5521 /* Assume no Fabric and go on with discovery.
5522 * Check for outstanding ELS FLOGI to abort.
5523 */
5524
5525 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005526 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005527
5528 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005529 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005530 break;
5531
5532 case LPFC_FABRIC_CFG_LINK:
5533 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5534 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005535 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5536 "0223 Timeout while waiting for "
5537 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005538 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005539 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005540 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005541 lpfc_els_abort(phba, ndlp);
5542
5543 /* ReStart discovery */
5544 goto restart_disc;
dea31012005-04-17 16:05:31 -05005545
5546 case LPFC_NS_QRY:
5547 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005548 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5549 "0224 NameServer Query timeout "
5550 "Data: x%x x%x\n",
5551 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005552
James Smart92d7f7b2007-06-17 19:56:38 -05005553 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5554 /* Try it one more time */
5555 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005556 vport->gidft_inp = 0;
5557 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005558 if (rc == 0)
5559 break;
dea31012005-04-17 16:05:31 -05005560 }
James Smart92d7f7b2007-06-17 19:56:38 -05005561 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005562
James Smart87af33f2007-10-27 13:37:43 -04005563restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005564 /*
5565 * Discovery is over.
5566 * set port_state to PORT_READY if SLI2.
5567 * cmpl_reg_vpi will set port_state to READY for SLI3.
5568 */
James Smart3772a992009-05-22 14:50:54 -04005569 if (phba->sli_rev < LPFC_SLI_REV4) {
5570 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5571 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005572 else {
James Smart3772a992009-05-22 14:50:54 -04005573 lpfc_issue_clear_la(phba, vport);
5574 vport->port_state = LPFC_VPORT_READY;
5575 }
dea31012005-04-17 16:05:31 -05005576 }
5577
5578 /* Setup and issue mailbox INITIALIZE LINK command */
5579 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5580 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005581 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5582 "0206 Device Discovery "
5583 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005584 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005585 break;
5586 }
5587
5588 lpfc_linkdown(phba);
5589 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5590 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005591 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005592 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005593 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005594 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005595 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005596 if (rc == MBX_NOT_FINISHED)
5597 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5598
5599 break;
5600
5601 case LPFC_DISC_AUTH:
5602 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005603 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5604 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005605 lpfc_disc_flush_list(vport);
5606
James Smart92d7f7b2007-06-17 19:56:38 -05005607 /*
5608 * set port_state to PORT_READY if SLI2.
5609 * cmpl_reg_vpi will set port_state to READY for SLI3.
5610 */
James Smart3772a992009-05-22 14:50:54 -04005611 if (phba->sli_rev < LPFC_SLI_REV4) {
5612 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5613 lpfc_issue_reg_vpi(phba, vport);
5614 else { /* NPIV Not enabled */
5615 lpfc_issue_clear_la(phba, vport);
5616 vport->port_state = LPFC_VPORT_READY;
5617 }
dea31012005-04-17 16:05:31 -05005618 }
5619 break;
5620
James Smart2e0fef82007-06-17 19:56:36 -05005621 case LPFC_VPORT_READY:
5622 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005623 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5624 "0231 RSCN timeout Data: x%x "
5625 "x%x\n",
5626 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005627
5628 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005629 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005630
James Smart2e0fef82007-06-17 19:56:36 -05005631 lpfc_els_flush_rscn(vport);
5632 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005633 }
5634 break;
James Smart2e0fef82007-06-17 19:56:36 -05005635
James Smart92d7f7b2007-06-17 19:56:38 -05005636 default:
James Smarte8b62012007-08-02 11:10:09 -04005637 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005638 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005639 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005640 break;
5641 }
5642
5643 switch (phba->link_state) {
5644 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005645 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005646 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5647 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005648 clrlaerr = 1;
5649 break;
5650
James Smart09372822008-01-11 01:52:54 -05005651 case LPFC_LINK_UP:
5652 lpfc_issue_clear_la(phba, vport);
5653 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005654 case LPFC_LINK_UNKNOWN:
5655 case LPFC_WARM_START:
5656 case LPFC_INIT_START:
5657 case LPFC_INIT_MBX_CMDS:
5658 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005659 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005660 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5661 "0230 Unexpected timeout, hba link "
5662 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005663 clrlaerr = 1;
5664 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005665
5666 case LPFC_HBA_READY:
5667 break;
dea31012005-04-17 16:05:31 -05005668 }
5669
5670 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005671 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005672 if (phba->sli_rev != LPFC_SLI_REV4) {
5673 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5674 ~LPFC_STOP_IOCB_EVENT;
5675 psli->sli3_ring[LPFC_FCP_RING].flag &=
5676 ~LPFC_STOP_IOCB_EVENT;
5677 }
James Smart2e0fef82007-06-17 19:56:36 -05005678 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005679 }
dea31012005-04-17 16:05:31 -05005680 return;
5681}
5682
dea31012005-04-17 16:05:31 -05005683/*
5684 * This routine handles processing a NameServer REG_LOGIN mailbox
5685 * command upon completion. It is setup in the LPFC_MBOXQ
5686 * as the completion routine when the command is
5687 * handed off to the SLI layer.
5688 */
5689void
James Smart2e0fef82007-06-17 19:56:36 -05005690lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005691{
James Smart04c68492009-05-22 14:52:52 -04005692 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005693 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5694 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5695 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005696
5697 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005698 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005699
James Smart6d368e52011-05-24 11:44:12 -04005700 if (phba->sli_rev < LPFC_SLI_REV4)
5701 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005702 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005703 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005704 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005705 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5706 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5707 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005708 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005709 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005710 /*
5711 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005712 * 0xfffffa (FDMI well known port).
5713 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5714 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005715 */
James Smart4258e982015-12-16 18:11:58 -05005716 if (vport->port_type == LPFC_PHYSICAL_PORT)
5717 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005718 else
James Smart4258e982015-12-16 18:11:58 -05005719 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5720
dea31012005-04-17 16:05:31 -05005721
James Smartfa4066b2008-01-11 01:53:27 -05005722 /* decrement the node reference count held for this callback
5723 * function.
5724 */
James Smart329f9bc2007-04-25 09:53:01 -04005725 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005726 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5727 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005728 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005729
5730 return;
5731}
5732
James Smart685f0bf2007-04-25 09:53:08 -04005733static int
5734lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5735{
5736 uint16_t *rpi = param;
5737
James Smarteff4a012012-01-18 16:25:25 -05005738 /* check for active node */
5739 if (!NLP_CHK_NODE_ACT(ndlp))
5740 return 0;
5741
James Smart685f0bf2007-04-25 09:53:08 -04005742 return ndlp->nlp_rpi == *rpi;
5743}
5744
5745static int
5746lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5747{
5748 return memcmp(&ndlp->nlp_portname, param,
5749 sizeof(ndlp->nlp_portname)) == 0;
5750}
5751
Adrian Bunka6ababd2007-11-05 18:07:33 +01005752static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005753__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005754{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005755 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005756
James Smart2e0fef82007-06-17 19:56:36 -05005757 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005758 if (filter(ndlp, param)) {
5759 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5760 "3185 FIND node filter %p DID "
5761 "Data: x%p x%x x%x\n",
5762 filter, ndlp, ndlp->nlp_DID,
5763 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005764 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005765 }
James Smart685f0bf2007-04-25 09:53:08 -04005766 }
James Smart34f5ad82012-08-03 12:35:03 -04005767 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5768 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005769 return NULL;
dea31012005-04-17 16:05:31 -05005770}
5771
James Smart685f0bf2007-04-25 09:53:08 -04005772/*
5773 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005774 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005775 */
5776struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005777__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005778{
James Smart2e0fef82007-06-17 19:56:36 -05005779 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005780}
5781
James Smart488d1462006-03-07 15:02:37 -05005782/*
James Smart685f0bf2007-04-25 09:53:08 -04005783 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005784 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005785 */
5786struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005787lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005788{
James Smart2e0fef82007-06-17 19:56:36 -05005789 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005790 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005791
James Smart2e0fef82007-06-17 19:56:36 -05005792 spin_lock_irq(shost->host_lock);
5793 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5794 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005795 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005796}
5797
James Smartcb69f7d2011-12-13 13:21:57 -05005798/*
5799 * This routine looks up the ndlp lists for the given RPI. If the rpi
5800 * is found, the routine returns the node element list pointer else
5801 * return NULL.
5802 */
5803struct lpfc_nodelist *
5804lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5805{
5806 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5807 struct lpfc_nodelist *ndlp;
5808
5809 spin_lock_irq(shost->host_lock);
5810 ndlp = __lpfc_findnode_rpi(vport, rpi);
5811 spin_unlock_irq(shost->host_lock);
5812 return ndlp;
5813}
5814
5815/**
5816 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5817 * @phba: pointer to lpfc hba data structure.
5818 * @vpi: the physical host virtual N_Port identifier.
5819 *
5820 * This routine finds a vport on a HBA (referred by @phba) through a
5821 * @vpi. The function walks the HBA's vport list and returns the address
5822 * of the vport with the matching @vpi.
5823 *
5824 * Return code
5825 * NULL - No vport with the matching @vpi found
5826 * Otherwise - Address to the vport with the matching @vpi.
5827 **/
5828struct lpfc_vport *
5829lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5830{
5831 struct lpfc_vport *vport;
5832 unsigned long flags;
5833 int i = 0;
5834
5835 /* The physical ports are always vpi 0 - translate is unnecessary. */
5836 if (vpi > 0) {
5837 /*
5838 * Translate the physical vpi to the logical vpi. The
5839 * vport stores the logical vpi.
5840 */
5841 for (i = 0; i < phba->max_vpi; i++) {
5842 if (vpi == phba->vpi_ids[i])
5843 break;
5844 }
5845
5846 if (i >= phba->max_vpi) {
5847 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5848 "2936 Could not find Vport mapped "
5849 "to vpi %d\n", vpi);
5850 return NULL;
5851 }
5852 }
5853
5854 spin_lock_irqsave(&phba->hbalock, flags);
5855 list_for_each_entry(vport, &phba->port_list, listentry) {
5856 if (vport->vpi == i) {
5857 spin_unlock_irqrestore(&phba->hbalock, flags);
5858 return vport;
5859 }
5860 }
5861 spin_unlock_irqrestore(&phba->hbalock, flags);
5862 return NULL;
5863}
5864
dea31012005-04-17 16:05:31 -05005865void
James Smart2e0fef82007-06-17 19:56:36 -05005866lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5867 uint32_t did)
dea31012005-04-17 16:05:31 -05005868{
5869 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005870
5871 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005872 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005873 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005874 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005875 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5876 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5877 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5878 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005879 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005880 ndlp->nlp_usg_map, ndlp);
5881
James Smartcff261f2013-12-17 20:29:47 -05005882 ndlp->active_rrqs_xri_bitmap =
5883 mempool_alloc(vport->phba->active_rrq_pool,
5884 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005885 if (ndlp->active_rrqs_xri_bitmap)
5886 memset(ndlp->active_rrqs_xri_bitmap, 0,
5887 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005888 }
5889
James Smart725dd392013-10-10 12:23:30 -04005890
James Smart858c9f62007-06-17 19:56:39 -05005891
5892 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5893 "node init: did:x%x",
5894 ndlp->nlp_DID, 0, 0);
5895
dea31012005-04-17 16:05:31 -05005896 return;
5897}
James Smart329f9bc2007-04-25 09:53:01 -04005898
James Smart98c9ea52007-10-27 13:37:33 -04005899/* This routine releases all resources associated with a specifc NPort's ndlp
5900 * and mempool_free's the nodelist.
5901 */
James Smart311464e2007-08-02 11:10:37 -04005902static void
James Smart329f9bc2007-04-25 09:53:01 -04005903lpfc_nlp_release(struct kref *kref)
5904{
James Smarte47c9092008-02-08 18:49:26 -05005905 struct lpfc_hba *phba;
5906 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005907 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5908 kref);
James Smart858c9f62007-06-17 19:56:39 -05005909
5910 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5911 "node release: did:x%x flg:x%x type:x%x",
5912 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5913
James Smarte47c9092008-02-08 18:49:26 -05005914 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005915 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005916 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005917 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005918 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005919
5920 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005921 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005922
5923 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005924 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005925 spin_lock_irqsave(&phba->ndlp_lock, flags);
5926 NLP_CLR_NODE_ACT(ndlp);
5927 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005928 if (phba->sli_rev == LPFC_SLI_REV4)
5929 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005930
5931 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005932 if (NLP_CHK_FREE_REQ(ndlp)) {
5933 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005934 if (phba->sli_rev == LPFC_SLI_REV4)
5935 mempool_free(ndlp->active_rrqs_xri_bitmap,
5936 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005937 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005938 }
James Smart329f9bc2007-04-25 09:53:01 -04005939}
5940
James Smart98c9ea52007-10-27 13:37:33 -04005941/* This routine bumps the reference count for a ndlp structure to ensure
5942 * that one discovery thread won't free a ndlp while another discovery thread
5943 * is using it.
5944 */
James Smart329f9bc2007-04-25 09:53:01 -04005945struct lpfc_nodelist *
5946lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5947{
James Smarte47c9092008-02-08 18:49:26 -05005948 struct lpfc_hba *phba;
5949 unsigned long flags;
5950
James Smart98c9ea52007-10-27 13:37:33 -04005951 if (ndlp) {
5952 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5953 "node get: did:x%x flg:x%x refcnt:x%x",
5954 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005955 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005956 /* The check of ndlp usage to prevent incrementing the
5957 * ndlp reference count that is in the process of being
5958 * released.
5959 */
James Smarta257bf92009-04-06 18:48:10 -04005960 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005961 spin_lock_irqsave(&phba->ndlp_lock, flags);
5962 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5963 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5964 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5965 "0276 lpfc_nlp_get: ndlp:x%p "
5966 "usgmap:x%x refcnt:%d\n",
5967 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005968 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005969 return NULL;
5970 } else
5971 kref_get(&ndlp->kref);
5972 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005973 }
James Smart329f9bc2007-04-25 09:53:01 -04005974 return ndlp;
5975}
5976
James Smart98c9ea52007-10-27 13:37:33 -04005977/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005978 * count goes to 0, this indicates the the associated nodelist should be
5979 * freed. Returning 1 indicates the ndlp resource has been released; on the
5980 * other hand, returning 0 indicates the ndlp resource has not been released
5981 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005982 */
James Smart329f9bc2007-04-25 09:53:01 -04005983int
5984lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5985{
James Smarte47c9092008-02-08 18:49:26 -05005986 struct lpfc_hba *phba;
5987 unsigned long flags;
5988
5989 if (!ndlp)
5990 return 1;
5991
5992 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5993 "node put: did:x%x flg:x%x refcnt:x%x",
5994 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005995 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04005996 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005997 spin_lock_irqsave(&phba->ndlp_lock, flags);
5998 /* Check the ndlp memory free acknowledge flag to avoid the
5999 * possible race condition that kref_put got invoked again
6000 * after previous one has done ndlp memory free.
6001 */
6002 if (NLP_CHK_FREE_ACK(ndlp)) {
6003 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6004 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6005 "0274 lpfc_nlp_put: ndlp:x%p "
6006 "usgmap:x%x refcnt:%d\n",
6007 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006008 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006009 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006010 }
James Smarte47c9092008-02-08 18:49:26 -05006011 /* Check the ndlp inactivate log flag to avoid the possible
6012 * race condition that kref_put got invoked again after ndlp
6013 * is already in inactivating state.
6014 */
6015 if (NLP_CHK_IACT_REQ(ndlp)) {
6016 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6017 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6018 "0275 lpfc_nlp_put: ndlp:x%p "
6019 "usgmap:x%x refcnt:%d\n",
6020 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006021 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006022 return 1;
6023 }
6024 /* For last put, mark the ndlp usage flags to make sure no
6025 * other kref_get and kref_put on the same ndlp shall get
6026 * in between the process when the final kref_put has been
6027 * invoked on this ndlp.
6028 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006029 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006030 /* Indicate ndlp is put to inactive state. */
6031 NLP_SET_IACT_REQ(ndlp);
6032 /* Acknowledge ndlp memory free has been seen. */
6033 if (NLP_CHK_FREE_REQ(ndlp))
6034 NLP_SET_FREE_ACK(ndlp);
6035 }
6036 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6037 /* Note, the kref_put returns 1 when decrementing a reference
6038 * count that was 1, it invokes the release callback function,
6039 * but it still left the reference count as 1 (not actually
6040 * performs the last decrementation). Otherwise, it actually
6041 * decrements the reference count and returns 0.
6042 */
6043 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006044}
James Smart98c9ea52007-10-27 13:37:33 -04006045
6046/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006047 * by any other discovery thread. This routine returns 1 if the
6048 * ndlp has been freed. A return value of 0 indicates the ndlp is
6049 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006050 */
6051int
6052lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6053{
6054 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6055 "node not used: did:x%x flg:x%x refcnt:x%x",
6056 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006057 kref_read(&ndlp->kref));
6058 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006059 if (lpfc_nlp_put(ndlp))
6060 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006061 return 0;
6062}
James Smart6fb120a2009-05-22 14:52:59 -04006063
6064/**
6065 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6066 * @phba: Pointer to hba context object.
6067 *
6068 * This function iterate through all FC nodes associated
6069 * will all vports to check if there is any node with
6070 * fc_rports associated with it. If there is an fc_rport
6071 * associated with the node, then the node is either in
6072 * discovered state or its devloss_timer is pending.
6073 */
6074static int
6075lpfc_fcf_inuse(struct lpfc_hba *phba)
6076{
6077 struct lpfc_vport **vports;
6078 int i, ret = 0;
6079 struct lpfc_nodelist *ndlp;
6080 struct Scsi_Host *shost;
6081
6082 vports = lpfc_create_vport_work_array(phba);
6083
James Smart63e801c2010-11-20 23:14:19 -05006084 /* If driver cannot allocate memory, indicate fcf is in use */
6085 if (!vports)
6086 return 1;
6087
James Smart6fb120a2009-05-22 14:52:59 -04006088 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6089 shost = lpfc_shost_from_vport(vports[i]);
6090 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006091 /*
6092 * IF the CVL_RCVD bit is not set then we have sent the
6093 * flogi.
6094 * If dev_loss fires while we are waiting we do not want to
6095 * unreg the fcf.
6096 */
6097 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6098 spin_unlock_irq(shost->host_lock);
6099 ret = 1;
6100 goto out;
6101 }
James Smart6fb120a2009-05-22 14:52:59 -04006102 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6103 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6104 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6105 ret = 1;
6106 spin_unlock_irq(shost->host_lock);
6107 goto out;
James Smart80c17842012-03-01 22:35:45 -05006108 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6109 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006110 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006111 "2624 RPI %x DID %x flag %x "
6112 "still logged in\n",
6113 ndlp->nlp_rpi, ndlp->nlp_DID,
6114 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006115 }
6116 }
6117 spin_unlock_irq(shost->host_lock);
6118 }
6119out:
6120 lpfc_destroy_vport_work_array(phba, vports);
6121 return ret;
6122}
6123
6124/**
6125 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6126 * @phba: Pointer to hba context object.
6127 * @mboxq: Pointer to mailbox object.
6128 *
6129 * This function frees memory associated with the mailbox command.
6130 */
James Smart1b511972011-12-13 13:23:09 -05006131void
James Smart6fb120a2009-05-22 14:52:59 -04006132lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6133{
6134 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006135 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006136
6137 if (mboxq->u.mb.mbxStatus) {
6138 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6139 "2555 UNREG_VFI mbxStatus error x%x "
6140 "HBA state x%x\n",
6141 mboxq->u.mb.mbxStatus, vport->port_state);
6142 }
James Smart38b92ef2010-08-04 16:11:39 -04006143 spin_lock_irq(shost->host_lock);
6144 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6145 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006146 mempool_free(mboxq, phba->mbox_mem_pool);
6147 return;
6148}
6149
6150/**
6151 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6152 * @phba: Pointer to hba context object.
6153 * @mboxq: Pointer to mailbox object.
6154 *
6155 * This function frees memory associated with the mailbox command.
6156 */
6157static void
6158lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6159{
6160 struct lpfc_vport *vport = mboxq->vport;
6161
6162 if (mboxq->u.mb.mbxStatus) {
6163 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6164 "2550 UNREG_FCFI mbxStatus error x%x "
6165 "HBA state x%x\n",
6166 mboxq->u.mb.mbxStatus, vport->port_state);
6167 }
6168 mempool_free(mboxq, phba->mbox_mem_pool);
6169 return;
6170}
6171
6172/**
James Smartecfd03c2010-02-12 14:41:27 -05006173 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006174 * @phba: Pointer to hba context object.
6175 *
James Smartecfd03c2010-02-12 14:41:27 -05006176 * This function prepare the HBA for unregistering the currently registered
6177 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6178 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006179 */
James Smartecfd03c2010-02-12 14:41:27 -05006180int
6181lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006182{
James Smart6fb120a2009-05-22 14:52:59 -04006183 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006184 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006185 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006186 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006187
James Smartecfd03c2010-02-12 14:41:27 -05006188 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006189 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006190 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006191
James Smart4d9ab992009-10-02 15:16:39 -04006192 /* At this point, all discovery is aborted */
6193 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006194
6195 /* Unregister VPIs */
6196 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006197 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006198 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006199 /* Stop FLOGI/FDISC retries */
6200 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6201 if (ndlp)
6202 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006203 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006204 if (phba->sli_rev == LPFC_SLI_REV4)
6205 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006206 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006207 shost = lpfc_shost_from_vport(vports[i]);
6208 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006209 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006210 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006211 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006212 }
6213 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006214 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6215 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6216 if (ndlp)
6217 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6218 lpfc_cleanup_pending_mbox(phba->pport);
6219 if (phba->sli_rev == LPFC_SLI_REV4)
6220 lpfc_sli4_unreg_all_rpis(phba->pport);
6221 lpfc_mbx_unreg_vpi(phba->pport);
6222 shost = lpfc_shost_from_vport(phba->pport);
6223 spin_lock_irq(shost->host_lock);
6224 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6225 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6226 spin_unlock_irq(shost->host_lock);
6227 }
James Smart6fb120a2009-05-22 14:52:59 -04006228
James Smart695a8142010-01-26 23:08:03 -05006229 /* Cleanup any outstanding ELS commands */
6230 lpfc_els_flush_all_cmd(phba);
6231
James Smart1b511972011-12-13 13:23:09 -05006232 /* Unregister the physical port VFI */
6233 rc = lpfc_issue_unreg_vfi(phba->pport);
6234 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006235}
6236
6237/**
6238 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6239 * @phba: Pointer to hba context object.
6240 *
6241 * This function issues synchronous unregister FCF mailbox command to HBA to
6242 * unregister the currently registered FCF record. The driver does not reset
6243 * the driver FCF usage state flags.
6244 *
6245 * Return 0 if successfully issued, none-zero otherwise.
6246 */
6247int
6248lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6249{
6250 LPFC_MBOXQ_t *mbox;
6251 int rc;
6252
James Smart6fb120a2009-05-22 14:52:59 -04006253 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6254 if (!mbox) {
6255 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006256 "2551 UNREG_FCFI mbox allocation failed"
6257 "HBA state x%x\n", phba->pport->port_state);
6258 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006259 }
James Smart6fb120a2009-05-22 14:52:59 -04006260 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6261 mbox->vport = phba->pport;
6262 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6263 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6264
6265 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006266 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6267 "2552 Unregister FCFI command failed rc x%x "
6268 "HBA state x%x\n",
6269 rc, phba->pport->port_state);
6270 return -EINVAL;
6271 }
6272 return 0;
6273}
6274
6275/**
6276 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6277 * @phba: Pointer to hba context object.
6278 *
6279 * This function unregisters the currently reigstered FCF. This function
6280 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6281 */
6282void
6283lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6284{
6285 int rc;
6286
6287 /* Preparation for unregistering fcf */
6288 rc = lpfc_unregister_fcf_prep(phba);
6289 if (rc) {
6290 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6291 "2748 Failed to prepare for unregistering "
6292 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006293 return;
6294 }
6295
James Smartecfd03c2010-02-12 14:41:27 -05006296 /* Now, unregister FCF record and reset HBA FCF state */
6297 rc = lpfc_sli4_unregister_fcf(phba);
6298 if (rc)
6299 return;
6300 /* Reset HBA FCF states after successful unregister FCF */
6301 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006302 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006303
6304 /*
6305 * If driver is not unloading, check if there is any other
6306 * FCF record that can be used for discovery.
6307 */
6308 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006309 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006310 return;
6311
James Smart0c9ab6f2010-02-26 14:15:57 -05006312 /* This is considered as the initial FCF discovery scan */
6313 spin_lock_irq(&phba->hbalock);
6314 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6315 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006316
6317 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006318 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006319
James Smart0c9ab6f2010-02-26 14:15:57 -05006320 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006321
James Smart0c9ab6f2010-02-26 14:15:57 -05006322 if (rc) {
6323 spin_lock_irq(&phba->hbalock);
6324 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6325 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006326 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006327 "2553 lpfc_unregister_unused_fcf failed "
6328 "to read FCF record HBA state x%x\n",
6329 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006330 }
James Smartecfd03c2010-02-12 14:41:27 -05006331}
6332
6333/**
6334 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6335 * @phba: Pointer to hba context object.
6336 *
6337 * This function just unregisters the currently reigstered FCF. It does not
6338 * try to find another FCF for discovery.
6339 */
6340void
6341lpfc_unregister_fcf(struct lpfc_hba *phba)
6342{
6343 int rc;
6344
6345 /* Preparation for unregistering fcf */
6346 rc = lpfc_unregister_fcf_prep(phba);
6347 if (rc) {
6348 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6349 "2749 Failed to prepare for unregistering "
6350 "HBA's FCF record: rc=%d\n", rc);
6351 return;
6352 }
6353
6354 /* Now, unregister FCF record and reset HBA FCF state */
6355 rc = lpfc_sli4_unregister_fcf(phba);
6356 if (rc)
6357 return;
6358 /* Set proper HBA FCF states after successful unregister FCF */
6359 spin_lock_irq(&phba->hbalock);
6360 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6361 spin_unlock_irq(&phba->hbalock);
6362}
6363
6364/**
6365 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6366 * @phba: Pointer to hba context object.
6367 *
6368 * This function check if there are any connected remote port for the FCF and
6369 * if all the devices are disconnected, this function unregister FCFI.
6370 * This function also tries to use another FCF for discovery.
6371 */
6372void
6373lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6374{
6375 /*
James Smart3804dc82010-07-14 15:31:37 -04006376 * If HBA is not running in FIP mode, if HBA does not support
6377 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6378 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006379 */
6380 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006381 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006382 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006383 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006384 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006385 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006386 spin_unlock_irq(&phba->hbalock);
6387 return;
6388 }
6389 spin_unlock_irq(&phba->hbalock);
6390
6391 if (lpfc_fcf_inuse(phba))
6392 return;
6393
6394 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006395}
6396
6397/**
6398 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6399 * @phba: Pointer to hba context object.
6400 * @buff: Buffer containing the FCF connection table as in the config
6401 * region.
6402 * This function create driver data structure for the FCF connection
6403 * record table read from config region 23.
6404 */
6405static void
6406lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6407 uint8_t *buff)
6408{
6409 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6410 struct lpfc_fcf_conn_hdr *conn_hdr;
6411 struct lpfc_fcf_conn_rec *conn_rec;
6412 uint32_t record_count;
6413 int i;
6414
6415 /* Free the current connect table */
6416 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006417 &phba->fcf_conn_rec_list, list) {
6418 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006419 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006420 }
James Smart6fb120a2009-05-22 14:52:59 -04006421
6422 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6423 record_count = conn_hdr->length * sizeof(uint32_t)/
6424 sizeof(struct lpfc_fcf_conn_rec);
6425
6426 conn_rec = (struct lpfc_fcf_conn_rec *)
6427 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6428
6429 for (i = 0; i < record_count; i++) {
6430 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6431 continue;
6432 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6433 GFP_KERNEL);
6434 if (!conn_entry) {
6435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6436 "2566 Failed to allocate connection"
6437 " table entry\n");
6438 return;
6439 }
6440
6441 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6442 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006443 list_add_tail(&conn_entry->list,
6444 &phba->fcf_conn_rec_list);
6445 }
James Smartdf0d0852013-05-31 17:05:08 -04006446
6447 if (!list_empty(&phba->fcf_conn_rec_list)) {
6448 i = 0;
6449 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6450 list) {
6451 conn_rec = &conn_entry->conn_rec;
6452 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6453 "3345 FCF connection list rec[%02d]: "
6454 "flags:x%04x, vtag:x%04x, "
6455 "fabric_name:x%02x:%02x:%02x:%02x:"
6456 "%02x:%02x:%02x:%02x, "
6457 "switch_name:x%02x:%02x:%02x:%02x:"
6458 "%02x:%02x:%02x:%02x\n", i++,
6459 conn_rec->flags, conn_rec->vlan_tag,
6460 conn_rec->fabric_name[0],
6461 conn_rec->fabric_name[1],
6462 conn_rec->fabric_name[2],
6463 conn_rec->fabric_name[3],
6464 conn_rec->fabric_name[4],
6465 conn_rec->fabric_name[5],
6466 conn_rec->fabric_name[6],
6467 conn_rec->fabric_name[7],
6468 conn_rec->switch_name[0],
6469 conn_rec->switch_name[1],
6470 conn_rec->switch_name[2],
6471 conn_rec->switch_name[3],
6472 conn_rec->switch_name[4],
6473 conn_rec->switch_name[5],
6474 conn_rec->switch_name[6],
6475 conn_rec->switch_name[7]);
6476 }
6477 }
James Smart6fb120a2009-05-22 14:52:59 -04006478}
6479
6480/**
6481 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6482 * @phba: Pointer to hba context object.
6483 * @buff: Buffer containing the FCoE parameter data structure.
6484 *
6485 * This function update driver data structure with config
6486 * parameters read from config region 23.
6487 */
6488static void
6489lpfc_read_fcoe_param(struct lpfc_hba *phba,
6490 uint8_t *buff)
6491{
6492 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6493 struct lpfc_fcoe_params *fcoe_param;
6494
6495 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6496 buff;
6497 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006498 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006499
6500 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6501 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6502 return;
6503
James Smart6fb120a2009-05-22 14:52:59 -04006504 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6505 phba->valid_vlan = 1;
6506 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6507 0xFFF;
6508 }
6509
6510 phba->fc_map[0] = fcoe_param->fc_map[0];
6511 phba->fc_map[1] = fcoe_param->fc_map[1];
6512 phba->fc_map[2] = fcoe_param->fc_map[2];
6513 return;
6514}
6515
6516/**
6517 * lpfc_get_rec_conf23 - Get a record type in config region data.
6518 * @buff: Buffer containing config region 23 data.
6519 * @size: Size of the data buffer.
6520 * @rec_type: Record type to be searched.
6521 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006522 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006523 * of the record specified by record_type. If record found, this
6524 * function return pointer to the record else return NULL.
6525 */
6526static uint8_t *
6527lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6528{
6529 uint32_t offset = 0, rec_length;
6530
6531 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6532 (size < sizeof(uint32_t)))
6533 return NULL;
6534
6535 rec_length = buff[offset + 1];
6536
6537 /*
6538 * One TLV record has one word header and number of data words
6539 * specified in the rec_length field of the record header.
6540 */
6541 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6542 <= size) {
6543 if (buff[offset] == rec_type)
6544 return &buff[offset];
6545
6546 if (buff[offset] == LPFC_REGION23_LAST_REC)
6547 return NULL;
6548
6549 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6550 rec_length = buff[offset + 1];
6551 }
6552 return NULL;
6553}
6554
6555/**
6556 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6557 * @phba: Pointer to lpfc_hba data structure.
6558 * @buff: Buffer containing config region 23 data.
6559 * @size: Size of the data buffer.
6560 *
Stefan Weileef35c22010-08-06 21:11:15 +02006561 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006562 * populate driver data structure with the parameters.
6563 */
6564void
6565lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6566 uint8_t *buff,
6567 uint32_t size)
6568{
James Smarteb016562014-09-03 12:58:06 -04006569 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006570 uint8_t *rec_ptr;
6571
6572 /*
6573 * If data size is less than 2 words signature and version cannot be
6574 * verified.
6575 */
6576 if (size < 2*sizeof(uint32_t))
6577 return;
6578
6579 /* Check the region signature first */
6580 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6582 "2567 Config region 23 has bad signature\n");
6583 return;
6584 }
6585
6586 offset += 4;
6587
6588 /* Check the data structure version */
6589 if (buff[offset] != LPFC_REGION23_VERSION) {
6590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6591 "2568 Config region 23 has bad version\n");
6592 return;
6593 }
6594 offset += 4;
6595
James Smart6fb120a2009-05-22 14:52:59 -04006596 /* Read FCoE param record */
6597 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6598 size - offset, FCOE_PARAM_TYPE);
6599 if (rec_ptr)
6600 lpfc_read_fcoe_param(phba, rec_ptr);
6601
6602 /* Read FCF connection table */
6603 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6604 size - offset, FCOE_CONN_TBL_TYPE);
6605 if (rec_ptr)
6606 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6607
6608}