blob: ed223937798a9d9653dda07daeb9f10c916a1b79 [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>
34
James Smartda0436e2009-05-22 14:51:39 -040035#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040037#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_disc.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050045#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050046#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050047
48/* AlpaArray for assignment of scsid for scan-down and bind_method */
49static uint8_t lpfcAlpaArray[] = {
50 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
51 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
52 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
53 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
54 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
55 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
56 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
57 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
58 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
59 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
60 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
61 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
62 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
63};
64
James Smart2e0fef82007-06-17 19:56:36 -050065static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010066static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040067static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
James Smarta93ff372010-10-22 11:06:08 -040068static int lpfc_fcf_inuse(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050069
James Smartc01f3202006-08-18 17:47:08 -040070void
71lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050072{
James Smartc01f3202006-08-18 17:47:08 -040073 struct lpfc_rport_data *rdata;
74 struct lpfc_nodelist * ndlp;
75 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050076
James Smartc01f3202006-08-18 17:47:08 -040077 rdata = rport->dd_data;
78 ndlp = rdata->pnode;
79
James Smart58da1ff2008-04-07 10:15:56 -040080 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040081 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
82 printk(KERN_ERR "Cannot find remote node"
83 " to terminate I/O Data x%x\n",
84 rport->port_id);
dea31012005-04-17 16:05:31 -050085 return;
86 }
87
James Smarta257bf92009-04-06 18:48:10 -040088 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050089
James Smart858c9f62007-06-17 19:56:39 -050090 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
91 "rport terminate: sid:x%x did:x%x flg:x%x",
92 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
93
James Smartc01f3202006-08-18 17:47:08 -040094 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040095 lpfc_sli_abort_iocb(ndlp->vport,
96 &phba->sli.ring[phba->sli.fcp_ring],
97 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040098 }
James Smartc01f3202006-08-18 17:47:08 -040099}
100
101/*
102 * This function will be called when dev_loss_tmo fire.
103 */
104void
105lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
106{
107 struct lpfc_rport_data *rdata;
108 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500109 struct lpfc_vport *vport;
James Smart466e8402015-05-21 13:55:28 -0400110 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500111 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500112 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400113 int put_node;
114 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400115
116 rdata = rport->dd_data;
117 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400118 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400119 return;
James Smartc01f3202006-08-18 17:47:08 -0400120
James Smart858c9f62007-06-17 19:56:39 -0500121 vport = ndlp->vport;
122 phba = vport->phba;
123
124 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
125 "rport devlosscb: sid:x%x did:x%x flg:x%x",
126 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
127
James Smart34f5ad82012-08-03 12:35:03 -0400128 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
129 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
130 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
131
James Smarta8adb832007-10-27 13:37:53 -0400132 /* Don't defer this if we are in the process of deleting the vport
133 * or unloading the driver. The unload will cleanup the node
134 * appropriately we just need to cleanup the ndlp rport info here.
135 */
136 if (vport->load_flag & FC_UNLOADING) {
137 put_node = rdata->pnode != NULL;
138 put_rport = ndlp->rport != NULL;
139 rdata->pnode = NULL;
140 ndlp->rport = NULL;
141 if (put_node)
142 lpfc_nlp_put(ndlp);
143 if (put_rport)
144 put_device(&rport->dev);
145 return;
146 }
147
148 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
149 return;
150
James Smart466e8402015-05-21 13:55:28 -0400151 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
152 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
153 "6789 rport name %llx != node port name %llx",
154 rport->port_name,
155 wwn_to_u64(ndlp->nlp_portname.u.wwn));
James Smart34f5ad82012-08-03 12:35:03 -0400156
James Smart858c9f62007-06-17 19:56:39 -0500157 evtp = &ndlp->dev_loss_evt;
158
James Smart466e8402015-05-21 13:55:28 -0400159 if (!list_empty(&evtp->evt_listp)) {
160 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
161 "6790 rport name %llx dev_loss_evt pending",
162 rport->port_name);
James Smart858c9f62007-06-17 19:56:39 -0500163 return;
James Smart466e8402015-05-21 13:55:28 -0400164 }
James Smart858c9f62007-06-17 19:56:39 -0500165
James Smart466e8402015-05-21 13:55:28 -0400166 shost = lpfc_shost_from_vport(vport);
167 spin_lock_irq(shost->host_lock);
168 ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
169 spin_unlock_irq(shost->host_lock);
James Smarta62a4352013-04-17 20:18:19 -0400170
James Smartfa4066b2008-01-11 01:53:27 -0500171 /* We need to hold the node by incrementing the reference
172 * count until this queued work is done
173 */
James Smart466e8402015-05-21 13:55:28 -0400174 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
175
176 spin_lock_irq(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -0400177 if (evtp->evt_arg1) {
178 evtp->evt = LPFC_EVT_DEV_LOSS;
179 list_add_tail(&evtp->evt_listp, &phba->work_list);
180 lpfc_worker_wake_up(phba);
181 }
James Smart858c9f62007-06-17 19:56:39 -0500182 spin_unlock_irq(&phba->hbalock);
183
James Smart858c9f62007-06-17 19:56:39 -0500184 return;
185}
186
James Smarta93ff372010-10-22 11:06:08 -0400187/**
188 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
189 * @ndlp: Pointer to remote node object.
190 *
191 * This function is called from the worker thread when devloss timeout timer
192 * expires. For SLI4 host, this routine shall return 1 when at lease one
193 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
194 * routine shall return 0 when there is no remote node is still in use of FCF
195 * when devloss timeout happened to this @ndlp.
196 **/
197static int
James Smart858c9f62007-06-17 19:56:39 -0500198lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
199{
200 struct lpfc_rport_data *rdata;
201 struct fc_rport *rport;
202 struct lpfc_vport *vport;
203 struct lpfc_hba *phba;
James Smart466e8402015-05-21 13:55:28 -0400204 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500205 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400206 int put_node;
James Smart858c9f62007-06-17 19:56:39 -0500207 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400208 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500209
210 rport = ndlp->rport;
James Smart858c9f62007-06-17 19:56:39 -0500211 vport = ndlp->vport;
James Smart466e8402015-05-21 13:55:28 -0400212 shost = lpfc_shost_from_vport(vport);
213
214 spin_lock_irq(shost->host_lock);
215 ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
216 spin_unlock_irq(shost->host_lock);
217
218 if (!rport)
219 return fcf_inuse;
220
221 name = (uint8_t *) &ndlp->nlp_portname;
James Smart858c9f62007-06-17 19:56:39 -0500222 phba = vport->phba;
223
James Smarta93ff372010-10-22 11:06:08 -0400224 if (phba->sli_rev == LPFC_SLI_REV4)
225 fcf_inuse = lpfc_fcf_inuse(phba);
226
James Smart858c9f62007-06-17 19:56:39 -0500227 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
228 "rport devlosstmo:did:x%x type:x%x id:x%x",
229 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
230
James Smart34f5ad82012-08-03 12:35:03 -0400231 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
232 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
233 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
234
James Smart466e8402015-05-21 13:55:28 -0400235 /*
236 * lpfc_nlp_remove if reached with dangling rport drops the
237 * reference. To make sure that does not happen clear rport
238 * pointer in ndlp before lpfc_nlp_put.
239 */
240 rdata = rport->dd_data;
241
James Smarta8adb832007-10-27 13:37:53 -0400242 /* Don't defer this if we are in the process of deleting the vport
243 * or unloading the driver. The unload will cleanup the node
244 * appropriately we just need to cleanup the ndlp rport info here.
245 */
246 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500247 if (ndlp->nlp_sid != NLP_NO_SID) {
248 /* flush the target */
249 lpfc_sli_abort_iocb(vport,
250 &phba->sli.ring[phba->sli.fcp_ring],
251 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
252 }
James Smarta8adb832007-10-27 13:37:53 -0400253 put_node = rdata->pnode != NULL;
James Smarta8adb832007-10-27 13:37:53 -0400254 rdata->pnode = NULL;
255 ndlp->rport = NULL;
256 if (put_node)
257 lpfc_nlp_put(ndlp);
James Smart466e8402015-05-21 13:55:28 -0400258 put_device(&rport->dev);
259
James Smarta93ff372010-10-22 11:06:08 -0400260 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400261 }
262
James Smartd7c255b2008-08-24 21:50:00 -0400263 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
264 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
265 "0284 Devloss timeout Ignored on "
266 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
267 "NPort x%x\n",
268 *name, *(name+1), *(name+2), *(name+3),
269 *(name+4), *(name+5), *(name+6), *(name+7),
270 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400271 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400272 }
James Smart858c9f62007-06-17 19:56:39 -0500273
James Smart466e8402015-05-21 13:55:28 -0400274 put_node = rdata->pnode != NULL;
275 rdata->pnode = NULL;
276 ndlp->rport = NULL;
277 if (put_node)
278 lpfc_nlp_put(ndlp);
279 put_device(&rport->dev);
280
281 if (ndlp->nlp_type & NLP_FABRIC)
James Smarta93ff372010-10-22 11:06:08 -0400282 return fcf_inuse;
James Smart82085712007-04-25 09:52:41 -0400283
dea31012005-04-17 16:05:31 -0500284 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400285 warn_on = 1;
James Smart51ef4c22007-08-02 11:10:31 -0400286 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
287 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500288 }
James Smartc01f3202006-08-18 17:47:08 -0400289
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400290 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400291 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
292 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400293 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
294 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400295 *name, *(name+1), *(name+2), *(name+3),
296 *(name+4), *(name+5), *(name+6), *(name+7),
297 ndlp->nlp_DID, ndlp->nlp_flag,
298 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400299 } else {
James Smarte8b62012007-08-02 11:10:09 -0400300 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
301 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400302 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
303 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400304 *name, *(name+1), *(name+2), *(name+3),
305 *(name+4), *(name+5), *(name+6), *(name+7),
306 ndlp->nlp_DID, ndlp->nlp_flag,
307 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400308 }
309
James Smart2e0fef82007-06-17 19:56:36 -0500310 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400311 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400312 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400313 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400314 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
315 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500316 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400317
James Smarta93ff372010-10-22 11:06:08 -0400318 return fcf_inuse;
319}
320
321/**
322 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
323 * @phba: Pointer to hba context object.
324 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
325 * @nlp_did: remote node identifer with devloss timeout.
326 *
327 * This function is called from the worker thread after invoking devloss
328 * timeout handler and releasing the reference count for the ndlp with
329 * which the devloss timeout was handled for SLI4 host. For the devloss
330 * timeout of the last remote node which had been in use of FCF, when this
331 * routine is invoked, it shall be guaranteed that none of the remote are
332 * in-use of FCF. When devloss timeout to the last remote using the FCF,
333 * if the FIP engine is neither in FCF table scan process nor roundrobin
334 * failover process, the in-use FCF shall be unregistered. If the FIP
335 * engine is in FCF discovery process, the devloss timeout state shall
336 * be set for either the FCF table scan process or roundrobin failover
337 * process to unregister the in-use FCF.
338 **/
339static void
340lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
341 uint32_t nlp_did)
342{
343 /* If devloss timeout happened to a remote node when FCF had no
344 * longer been in-use, do nothing.
345 */
346 if (!fcf_inuse)
347 return;
348
349 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
350 spin_lock_irq(&phba->hbalock);
351 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
352 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
353 spin_unlock_irq(&phba->hbalock);
354 return;
355 }
356 phba->hba_flag |= HBA_DEVLOSS_TMO;
357 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
358 "2847 Last remote node (x%x) using "
359 "FCF devloss tmo\n", nlp_did);
360 }
361 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
362 spin_unlock_irq(&phba->hbalock);
363 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
364 "2868 Devloss tmo to FCF rediscovery "
365 "in progress\n");
366 return;
367 }
368 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
369 spin_unlock_irq(&phba->hbalock);
370 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
371 "2869 Devloss tmo to idle FIP engine, "
372 "unreg in-use FCF and rescan.\n");
373 /* Unregister in-use FCF and rescan */
374 lpfc_unregister_fcf_rescan(phba);
375 return;
376 }
377 spin_unlock_irq(&phba->hbalock);
378 if (phba->hba_flag & FCF_TS_INPROG)
379 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
380 "2870 FCF table scan in progress\n");
381 if (phba->hba_flag & FCF_RR_INPROG)
382 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
383 "2871 FLOGI roundrobin FCF failover "
384 "in progress\n");
385 }
James Smart6fb120a2009-05-22 14:52:59 -0400386 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500387}
James Smartc01f3202006-08-18 17:47:08 -0400388
James Smartea2151b2008-09-07 11:52:10 -0400389/**
James Smart3621a712009-04-06 18:47:14 -0400390 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400391 * @phba: Pointer to hba context object.
392 *
393 * This function is called from the functions which need to post
394 * events from interrupt context. This function allocates data
395 * structure required for posting event. It also keeps track of
396 * number of events pending and prevent event storm when there are
397 * too many events.
398 **/
399struct lpfc_fast_path_event *
400lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
401 struct lpfc_fast_path_event *ret;
402
403 /* If there are lot of fast event do not exhaust memory due to this */
404 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
405 return NULL;
406
407 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
408 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400409 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400410 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400411 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
412 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
413 }
James Smartea2151b2008-09-07 11:52:10 -0400414 return ret;
415}
416
417/**
James Smart3621a712009-04-06 18:47:14 -0400418 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400419 * @phba: Pointer to hba context object.
420 * @evt: Event object which need to be freed.
421 *
422 * This function frees the data structure required for posting
423 * events.
424 **/
425void
426lpfc_free_fast_evt(struct lpfc_hba *phba,
427 struct lpfc_fast_path_event *evt) {
428
429 atomic_dec(&phba->fast_event_count);
430 kfree(evt);
431}
432
433/**
James Smart3621a712009-04-06 18:47:14 -0400434 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400435 * @phba: Pointer to hba context object.
436 * @evtp: Event data structure.
437 *
438 * This function is called from worker thread, when the interrupt
439 * context need to post an event. This function posts the event
440 * to fc transport netlink interface.
441 **/
442static void
443lpfc_send_fastpath_evt(struct lpfc_hba *phba,
444 struct lpfc_work_evt *evtp)
445{
446 unsigned long evt_category, evt_sub_category;
447 struct lpfc_fast_path_event *fast_evt_data;
448 char *evt_data;
449 uint32_t evt_data_size;
450 struct Scsi_Host *shost;
451
452 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
453 work_evt);
454
455 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
456 evt_sub_category = (unsigned long) fast_evt_data->un.
457 fabric_evt.subcategory;
458 shost = lpfc_shost_from_vport(fast_evt_data->vport);
459 if (evt_category == FC_REG_FABRIC_EVENT) {
460 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
461 evt_data = (char *) &fast_evt_data->un.read_check_error;
462 evt_data_size = sizeof(fast_evt_data->un.
463 read_check_error);
464 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500465 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400466 evt_data = (char *) &fast_evt_data->un.fabric_evt;
467 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
468 } else {
469 lpfc_free_fast_evt(phba, fast_evt_data);
470 return;
471 }
472 } else if (evt_category == FC_REG_SCSI_EVENT) {
473 switch (evt_sub_category) {
474 case LPFC_EVENT_QFULL:
475 case LPFC_EVENT_DEVBSY:
476 evt_data = (char *) &fast_evt_data->un.scsi_evt;
477 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
478 break;
479 case LPFC_EVENT_CHECK_COND:
480 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
481 evt_data_size = sizeof(fast_evt_data->un.
482 check_cond_evt);
483 break;
484 case LPFC_EVENT_VARQUEDEPTH:
485 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
486 evt_data_size = sizeof(fast_evt_data->un.
487 queue_depth_evt);
488 break;
489 default:
490 lpfc_free_fast_evt(phba, fast_evt_data);
491 return;
492 }
493 } else {
494 lpfc_free_fast_evt(phba, fast_evt_data);
495 return;
496 }
497
498 fc_host_post_vendor_event(shost,
499 fc_get_event_number(),
500 evt_data_size,
501 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500502 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400503
504 lpfc_free_fast_evt(phba, fast_evt_data);
505 return;
506}
507
dea31012005-04-17 16:05:31 -0500508static void
James Smart2e0fef82007-06-17 19:56:36 -0500509lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500510{
511 struct lpfc_work_evt *evtp = NULL;
512 struct lpfc_nodelist *ndlp;
513 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400514 int fcf_inuse;
515 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500516
James Smart2e0fef82007-06-17 19:56:36 -0500517 spin_lock_irq(&phba->hbalock);
518 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500519 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
520 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500521 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500522 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500523 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500524 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500525 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500526 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500527 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500528 /* decrement the node reference count held
529 * for this queued work
530 */
531 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500532 break;
James Smart858c9f62007-06-17 19:56:39 -0500533 case LPFC_EVT_DEV_LOSS:
534 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400535 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500536 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500537 /* decrement the node reference count held for
538 * this queued work
539 */
James Smarta93ff372010-10-22 11:06:08 -0400540 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500541 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400542 if (phba->sli_rev == LPFC_SLI_REV4)
543 lpfc_sli4_post_dev_loss_tmo_handler(phba,
544 fcf_inuse,
545 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500546 break;
dea31012005-04-17 16:05:31 -0500547 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500548 if (phba->link_state < LPFC_LINK_DOWN)
549 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500550 else
James Smart2e0fef82007-06-17 19:56:36 -0500551 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500552 complete((struct completion *)(evtp->evt_arg2));
553 break;
James Smart46fa3112007-04-25 09:51:45 -0400554 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500555 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400556 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400557 *(int *)(evtp->evt_arg1) = 0;
558 complete((struct completion *)(evtp->evt_arg2));
559 break;
560 case LPFC_EVT_OFFLINE:
561 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500562 lpfc_sli_brdrestart(phba);
563 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400564 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
565 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500566 complete((struct completion *)(evtp->evt_arg2));
567 break;
568 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400569 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500570 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500571 lpfc_sli_brdreset(phba);
572 lpfc_hba_down_post(phba);
573 *(int *)(evtp->evt_arg1) =
574 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400575 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500576 complete((struct completion *)(evtp->evt_arg2));
577 break;
578 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400579 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500580 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500581 = (phba->pport->stopped)
582 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400583 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500584 complete((struct completion *)(evtp->evt_arg2));
585 break;
James Smartea2151b2008-09-07 11:52:10 -0400586 case LPFC_EVT_FASTPATH_MGMT_EVT:
587 lpfc_send_fastpath_evt(phba, evtp);
588 free_evt = 0;
589 break;
James Smart78730cf2010-04-06 15:06:30 -0400590 case LPFC_EVT_RESET_HBA:
591 if (!(phba->pport->load_flag & FC_UNLOADING))
592 lpfc_reset_hba(phba);
593 break;
dea31012005-04-17 16:05:31 -0500594 }
595 if (free_evt)
596 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500597 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500598 }
James Smart2e0fef82007-06-17 19:56:36 -0500599 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500600
601}
602
James Smart311464e2007-08-02 11:10:37 -0400603static void
James Smart2e0fef82007-06-17 19:56:36 -0500604lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500605{
606 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500607 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400608 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400609 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400610 int i;
dea31012005-04-17 16:05:31 -0500611
James Smart2e0fef82007-06-17 19:56:36 -0500612 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500613 ha_copy = phba->work_ha;
614 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500615 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500616
James Smartda0436e2009-05-22 14:51:39 -0400617 /* First, try to post the next mailbox command to SLI4 device */
618 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
619 lpfc_sli4_post_async_mbox(phba);
620
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500621 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400622 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500623 lpfc_handle_eratt(phba);
624
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500625 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500626 lpfc_sli_handle_mb_event(phba);
627
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500628 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500629 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400630
James Smartda0436e2009-05-22 14:51:39 -0400631 /* Process SLI4 events */
632 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500633 if (phba->hba_flag & HBA_RRQ_ACTIVE)
634 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400635 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
636 lpfc_sli4_fcp_xri_abort_event_proc(phba);
637 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
638 lpfc_sli4_els_xri_abort_event_proc(phba);
639 if (phba->hba_flag & ASYNC_EVENT)
640 lpfc_sli4_async_event_proc(phba);
641 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
642 spin_lock_irq(&phba->hbalock);
643 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
644 spin_unlock_irq(&phba->hbalock);
645 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
646 }
James Smartecfd03c2010-02-12 14:41:27 -0500647 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
648 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400649 }
650
James Smart549e55c2007-08-02 11:09:51 -0400651 vports = lpfc_create_vport_work_array(phba);
652 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400653 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400654 /*
655 * We could have no vports in array if unloading, so if
656 * this happens then just use the pport
657 */
658 if (vports[i] == NULL && i == 0)
659 vport = phba->pport;
660 else
661 vport = vports[i];
662 if (vport == NULL)
663 break;
James Smart58da1ff2008-04-07 10:15:56 -0400664 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400665 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400666 vport->work_port_events &= ~work_port_events;
667 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400668 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400669 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400670 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400671 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400672 if (work_port_events & WORKER_HB_TMO)
673 lpfc_hb_timeout_handler(phba);
674 if (work_port_events & WORKER_MBOX_TMO)
675 lpfc_mbox_timeout_handler(phba);
676 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
677 lpfc_unblock_fabric_iocbs(phba);
James Smart549e55c2007-08-02 11:09:51 -0400678 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
679 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500680 if (work_port_events & WORKER_DELAYED_DISC_TMO)
681 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500682 }
James Smart09372822008-01-11 01:52:54 -0500683 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500684
James Smart858c9f62007-06-17 19:56:39 -0500685 pring = &phba->sli.ring[LPFC_ELS_RING];
686 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
687 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400688 if ((status & HA_RXMASK) ||
689 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400690 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400691 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500692 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400693 /* Set the lpfc data pending flag */
694 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500695 } else {
James Smart94661502013-03-01 16:37:29 -0500696 if (phba->link_state >= LPFC_LINK_UP) {
697 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
698 lpfc_sli_handle_slow_ring_event(phba, pring,
699 (status &
700 HA_RXMASK));
701 }
James Smart858c9f62007-06-17 19:56:39 -0500702 }
Sebastian Herbsztb1039182015-07-22 10:53:22 +0200703 if ((phba->sli_rev == LPFC_SLI_REV4) &&
James Smart0e9bb8d2013-03-01 16:35:12 -0500704 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400705 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500706 /*
707 * Turn on Ring interrupts
708 */
James Smart3772a992009-05-22 14:50:54 -0400709 if (phba->sli_rev <= LPFC_SLI_REV3) {
710 spin_lock_irq(&phba->hbalock);
711 control = readl(phba->HCregaddr);
712 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
713 lpfc_debugfs_slow_ring_trc(phba,
714 "WRK Enable ring: cntl:x%x hacopy:x%x",
715 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400716
James Smart3772a992009-05-22 14:50:54 -0400717 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
718 writel(control, phba->HCregaddr);
719 readl(phba->HCregaddr); /* flush */
720 } else {
721 lpfc_debugfs_slow_ring_trc(phba,
722 "WRK Ring ok: cntl:x%x hacopy:x%x",
723 control, ha_copy, 0);
724 }
725 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500726 }
727 }
James Smart2e0fef82007-06-17 19:56:36 -0500728 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500729}
730
dea31012005-04-17 16:05:31 -0500731int
732lpfc_do_work(void *p)
733{
734 struct lpfc_hba *phba = p;
735 int rc;
dea31012005-04-17 16:05:31 -0500736
Dongsheng Yang8698a742014-03-11 18:09:12 +0800737 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400738 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400739 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500740
James Smart3a55b532008-12-04 22:38:54 -0500741 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400742 /* wait and check worker queue activities */
743 rc = wait_event_interruptible(phba->work_waitq,
744 (test_and_clear_bit(LPFC_DATA_READY,
745 &phba->data_flags)
746 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500747 /* Signal wakeup shall terminate the worker thread */
748 if (rc) {
749 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
750 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500751 break;
James Smart3a55b532008-12-04 22:38:54 -0500752 }
dea31012005-04-17 16:05:31 -0500753
James Smart5e9d9b82008-06-14 22:52:53 -0400754 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500755 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500756 }
James Smart3a55b532008-12-04 22:38:54 -0500757 phba->worker_thread = NULL;
758 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
759 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500760 return 0;
761}
762
763/*
764 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300765 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500766 * embedding it in the IOCB.
767 */
768int
James Smart2e0fef82007-06-17 19:56:36 -0500769lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500770 uint32_t evt)
771{
772 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500773 unsigned long flags;
dea31012005-04-17 16:05:31 -0500774
775 /*
776 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
777 * be queued to worker thread for processing
778 */
James Smart92d7f7b2007-06-17 19:56:38 -0500779 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500780 if (!evtp)
781 return 0;
782
783 evtp->evt_arg1 = arg1;
784 evtp->evt_arg2 = arg2;
785 evtp->evt = evt;
786
James Smarted957682007-06-17 19:56:37 -0500787 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400788 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500789 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500790
James Smart5e9d9b82008-06-14 22:52:53 -0400791 lpfc_worker_wake_up(phba);
792
dea31012005-04-17 16:05:31 -0500793 return 1;
794}
795
James Smart92d7f7b2007-06-17 19:56:38 -0500796void
797lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
798{
James Smart09372822008-01-11 01:52:54 -0500799 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500800 struct lpfc_hba *phba = vport->phba;
801 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500802
803 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500804 if (!NLP_CHK_NODE_ACT(ndlp))
805 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500806 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
807 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400808 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
809 ((vport->port_type == LPFC_NPIV_PORT) &&
810 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500811 lpfc_unreg_rpi(vport, ndlp);
812
813 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400814 if ((phba->sli_rev < LPFC_SLI_REV4) &&
815 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500816 continue;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -0400817 lpfc_disc_state_machine(vport, ndlp, NULL,
818 remove
819 ? NLP_EVT_DEVICE_RM
820 : NLP_EVT_DEVICE_RECOVERY);
James Smart92d7f7b2007-06-17 19:56:38 -0500821 }
822 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400823 if (phba->sli_rev == LPFC_SLI_REV4)
824 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500825 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500826 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500827 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500828 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500829 }
830}
831
James Smart87af33f2007-10-27 13:37:43 -0400832void
James Smart98c9ea52007-10-27 13:37:33 -0400833lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500834{
James Smart695a8142010-01-26 23:08:03 -0500835 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
836
James Smart45ed1192009-10-02 15:17:02 -0400837 /* Cleanup any outstanding received buffers */
838 lpfc_cleanup_rcv_buffers(vport);
839
James Smart92d7f7b2007-06-17 19:56:38 -0500840 /* Cleanup any outstanding RSCN activity */
841 lpfc_els_flush_rscn(vport);
842
843 /* Cleanup any outstanding ELS commands */
844 lpfc_els_flush_cmd(vport);
845
846 lpfc_cleanup_rpis(vport, 0);
847
James Smart92d7f7b2007-06-17 19:56:38 -0500848 /* Turn off discovery timer if its running */
849 lpfc_can_disctmo(vport);
850}
851
James Smart3772a992009-05-22 14:50:54 -0400852void
James Smart98c9ea52007-10-27 13:37:33 -0400853lpfc_linkdown_port(struct lpfc_vport *vport)
854{
855 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
856
857 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
858
859 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
860 "Link Down: state:x%x rtry:x%x flg:x%x",
861 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
862
863 lpfc_port_link_failure(vport);
864
James Smart92494142011-02-16 12:39:44 -0500865 /* Stop delayed Nport discovery */
866 spin_lock_irq(shost->host_lock);
867 vport->fc_flag &= ~FC_DISC_DELAYED;
868 spin_unlock_irq(shost->host_lock);
869 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400870}
871
dea31012005-04-17 16:05:31 -0500872int
James Smart685f0bf2007-04-25 09:53:08 -0400873lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500874{
James Smart2e0fef82007-06-17 19:56:36 -0500875 struct lpfc_vport *vport = phba->pport;
876 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400877 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400878 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400879 int i;
dea31012005-04-17 16:05:31 -0500880
James Smart3163f722008-02-08 18:50:25 -0500881 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500882 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500883
884 /* Block all SCSI stack I/Os */
885 lpfc_scsi_dev_block(phba);
886
James Smart2e0fef82007-06-17 19:56:36 -0500887 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500888 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500889 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500890 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500891 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500892 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500893 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500894 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500895 }
James Smart549e55c2007-08-02 11:09:51 -0400896 vports = lpfc_create_vport_work_array(phba);
897 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400898 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400899 /* Issue a LINK DOWN event to all nodes */
900 lpfc_linkdown_port(vports[i]);
901 }
James Smart09372822008-01-11 01:52:54 -0500902 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500903 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500904 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
905 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400906 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500907 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500908 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400909 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500910 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500911 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500912 }
913 }
914
dea31012005-04-17 16:05:31 -0500915 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500916 if (phba->pport->fc_flag & FC_PT2PT) {
917 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500918 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
919 if (mb) {
dea31012005-04-17 16:05:31 -0500920 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500921 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500922 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400923 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500924 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500925 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500926 }
927 }
James Smart2e0fef82007-06-17 19:56:36 -0500928 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500929 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500930 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500931 }
James Smart2e0fef82007-06-17 19:56:36 -0500932
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500933 return 0;
dea31012005-04-17 16:05:31 -0500934}
935
James Smart92d7f7b2007-06-17 19:56:38 -0500936static void
937lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500938{
James Smart92d7f7b2007-06-17 19:56:38 -0500939 struct lpfc_nodelist *ndlp;
940
941 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500942 if (!NLP_CHK_NODE_ACT(ndlp))
943 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500944 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
945 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500946 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500947 /* On Linkup its safe to clean up the ndlp
948 * from Fabric connections.
949 */
James Smart92d7f7b2007-06-17 19:56:38 -0500950 if (ndlp->nlp_DID != Fabric_DID)
951 lpfc_unreg_rpi(vport, ndlp);
952 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
953 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500954 /* Fail outstanding IO now since device is
955 * marked for PLOGI.
956 */
James Smart92d7f7b2007-06-17 19:56:38 -0500957 lpfc_unreg_rpi(vport, ndlp);
958 }
959 }
960}
961
962static void
963lpfc_linkup_port(struct lpfc_vport *vport)
964{
965 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500966 struct lpfc_hba *phba = vport->phba;
967
968 if ((vport->load_flag & FC_UNLOADING) != 0)
969 return;
970
James Smart858c9f62007-06-17 19:56:39 -0500971 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
972 "Link Up: top:x%x speed:x%x flg:x%x",
973 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
974
James Smart92d7f7b2007-06-17 19:56:38 -0500975 /* If NPIV is not enabled, only bring the physical port up */
976 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
977 (vport != phba->pport))
978 return;
dea31012005-04-17 16:05:31 -0500979
James Smart2e0fef82007-06-17 19:56:36 -0500980 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400981
James Smart2e0fef82007-06-17 19:56:36 -0500982 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500983 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
984 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
985 vport->fc_flag |= FC_NDISC_ACTIVE;
986 vport->fc_ns_retry = 0;
987 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500988
James Smart92d7f7b2007-06-17 19:56:38 -0500989 if (vport->fc_flag & FC_LBIT)
990 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500991
James Smart92d7f7b2007-06-17 19:56:38 -0500992}
993
994static int
995lpfc_linkup(struct lpfc_hba *phba)
996{
James Smart549e55c2007-08-02 11:09:51 -0400997 struct lpfc_vport **vports;
998 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500999
1000 phba->link_state = LPFC_LINK_UP;
1001
1002 /* Unblock fabric iocbs if they are blocked */
1003 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1004 del_timer_sync(&phba->fabric_block_timer);
1005
James Smart549e55c2007-08-02 11:09:51 -04001006 vports = lpfc_create_vport_work_array(phba);
1007 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001008 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001009 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001010 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001011
1012 return 0;
1013}
1014
1015/*
1016 * This routine handles processing a CLEAR_LA mailbox
1017 * command upon completion. It is setup in the LPFC_MBOXQ
1018 * as the completion routine when the command is
1019 * handed off to the SLI layer.
1020 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001021static void
James Smart2e0fef82007-06-17 19:56:36 -05001022lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001023{
James Smart2e0fef82007-06-17 19:56:36 -05001024 struct lpfc_vport *vport = pmb->vport;
1025 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1026 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001027 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001028 uint32_t control;
1029
dea31012005-04-17 16:05:31 -05001030 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001031 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001032 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1033 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1034
1035 /* Check for error */
1036 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001037 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001038 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1039 "0320 CLEAR_LA mbxStatus error x%x hba "
1040 "state x%x\n",
1041 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001042 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001043 goto out;
1044 }
1045
James Smart92d7f7b2007-06-17 19:56:38 -05001046 if (vport->port_type == LPFC_PHYSICAL_PORT)
1047 phba->link_state = LPFC_HBA_READY;
1048
1049 spin_lock_irq(&phba->hbalock);
1050 psli->sli_flag |= LPFC_PROCESS_LA;
1051 control = readl(phba->HCregaddr);
1052 control |= HC_LAINT_ENA;
1053 writel(control, phba->HCregaddr);
1054 readl(phba->HCregaddr); /* flush */
1055 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001056 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001057 return;
dea31012005-04-17 16:05:31 -05001058
dea31012005-04-17 16:05:31 -05001059out:
1060 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001061 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1062 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001063 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001064
James Smart2e0fef82007-06-17 19:56:36 -05001065 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001066 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001067 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001068
James Smart2e0fef82007-06-17 19:56:36 -05001069 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001070
1071 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001072
1073 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001074 psli->sli_flag |= LPFC_PROCESS_LA;
1075 control = readl(phba->HCregaddr);
1076 control |= HC_LAINT_ENA;
1077 writel(control, phba->HCregaddr);
1078 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001079 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001080
1081 return;
1082}
1083
James Smart2e0fef82007-06-17 19:56:36 -05001084
James Smartd6de08c2015-12-16 18:11:53 -05001085void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001086lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001087{
James Smart2e0fef82007-06-17 19:56:36 -05001088 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001089
James Smart04c68492009-05-22 14:52:52 -04001090 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001091 goto out;
dea31012005-04-17 16:05:31 -05001092
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001093 mempool_free(pmb, phba->mbox_mem_pool);
1094
James Smart1b511972011-12-13 13:23:09 -05001095 /* don't perform discovery for SLI4 loopback diagnostic test */
1096 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1097 !(phba->hba_flag & HBA_FCOE_MODE) &&
1098 (phba->link_flag & LS_LOOPBACK_MODE))
1099 return;
1100
James Smart76a95d72010-11-20 23:11:48 -05001101 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001102 vport->fc_flag & FC_PUBLIC_LOOP &&
1103 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001104 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001105 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001106 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1107 */
James Smart2e0fef82007-06-17 19:56:36 -05001108 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001109 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001110 }
dea31012005-04-17 16:05:31 -05001111
James Smart2e0fef82007-06-17 19:56:36 -05001112 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001113 * LPFC_FLOGI while waiting for FLOGI cmpl
1114 */
James Smartd6de08c2015-12-16 18:11:53 -05001115 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001116 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001117 else if (vport->fc_flag & FC_PT2PT)
1118 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001119 return;
dea31012005-04-17 16:05:31 -05001120
1121out:
James Smarte8b62012007-08-02 11:10:09 -04001122 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1123 "0306 CONFIG_LINK mbxStatus error x%x "
1124 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001125 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001126 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001127
1128 lpfc_linkdown(phba);
1129
James Smarte8b62012007-08-02 11:10:09 -04001130 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1131 "0200 CONFIG_LINK bad hba state x%x\n",
1132 vport->port_state);
dea31012005-04-17 16:05:31 -05001133
James Smart92d7f7b2007-06-17 19:56:38 -05001134 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001135 return;
1136}
1137
James Smart7d791df2011-07-22 18:37:52 -04001138/**
1139 * lpfc_sli4_clear_fcf_rr_bmask
1140 * @phba pointer to the struct lpfc_hba for this port.
1141 * This fucnction resets the round robin bit mask and clears the
1142 * fcf priority list. The list deletions are done while holding the
1143 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1144 * from the lpfc_fcf_pri record.
1145 **/
1146void
1147lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1148{
1149 struct lpfc_fcf_pri *fcf_pri;
1150 struct lpfc_fcf_pri *next_fcf_pri;
1151 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1152 spin_lock_irq(&phba->hbalock);
1153 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1154 &phba->fcf.fcf_pri_list, list) {
1155 list_del_init(&fcf_pri->list);
1156 fcf_pri->fcf_rec.flag = 0;
1157 }
1158 spin_unlock_irq(&phba->hbalock);
1159}
dea31012005-04-17 16:05:31 -05001160static void
James Smart6fb120a2009-05-22 14:52:59 -04001161lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1162{
1163 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001164
1165 if (mboxq->u.mb.mbxStatus) {
1166 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1167 "2017 REG_FCFI mbxStatus error x%x "
1168 "HBA state x%x\n",
1169 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001170 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001171 }
1172
1173 /* Start FCoE discovery by sending a FLOGI. */
1174 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1175 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001176 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001177 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001178 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001179
James Smart32b97932009-07-19 10:01:21 -04001180 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001181 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1182 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001183 goto fail_out;
1184
1185 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001186 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001187 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001188 phba->hba_flag &= ~FCF_TS_INPROG;
1189 if (vport->port_state != LPFC_FLOGI) {
1190 phba->hba_flag |= FCF_RR_INPROG;
1191 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001192 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001193 goto out;
1194 }
1195 spin_unlock_irq(&phba->hbalock);
1196 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001197
James Smarta93ff372010-10-22 11:06:08 -04001198fail_out:
1199 spin_lock_irq(&phba->hbalock);
1200 phba->hba_flag &= ~FCF_RR_INPROG;
1201 spin_unlock_irq(&phba->hbalock);
1202out:
James Smart6fb120a2009-05-22 14:52:59 -04001203 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001204}
1205
1206/**
1207 * lpfc_fab_name_match - Check if the fcf fabric name match.
1208 * @fab_name: pointer to fabric name.
1209 * @new_fcf_record: pointer to fcf record.
1210 *
1211 * This routine compare the fcf record's fabric name with provided
1212 * fabric name. If the fabric name are identical this function
1213 * returns 1 else return 0.
1214 **/
1215static uint32_t
1216lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1217{
James Smartecfd03c2010-02-12 14:41:27 -05001218 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001219 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001220 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1221 return 0;
1222 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1223 return 0;
1224 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1225 return 0;
1226 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1227 return 0;
1228 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1229 return 0;
1230 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1231 return 0;
1232 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1233 return 0;
1234 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001235}
1236
1237/**
James Smart8fa38512009-07-19 10:01:03 -04001238 * lpfc_sw_name_match - Check if the fcf switch name match.
1239 * @fab_name: pointer to fabric name.
1240 * @new_fcf_record: pointer to fcf record.
1241 *
1242 * This routine compare the fcf record's switch name with provided
1243 * switch name. If the switch name are identical this function
1244 * returns 1 else return 0.
1245 **/
1246static uint32_t
1247lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1248{
James Smartecfd03c2010-02-12 14:41:27 -05001249 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001250 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001251 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1252 return 0;
1253 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1254 return 0;
1255 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1256 return 0;
1257 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1258 return 0;
1259 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1260 return 0;
1261 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1262 return 0;
1263 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1264 return 0;
1265 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001266}
1267
1268/**
James Smart6fb120a2009-05-22 14:52:59 -04001269 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001270 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001271 * @new_fcf_record: pointer to fcf record.
1272 *
1273 * This routine compare the fcf record's mac address with HBA's
1274 * FCF mac address. If the mac addresses are identical this function
1275 * returns 1 else return 0.
1276 **/
1277static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001278lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001279{
James Smartecfd03c2010-02-12 14:41:27 -05001280 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001281 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001282 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1283 return 0;
1284 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1285 return 0;
1286 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1287 return 0;
1288 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1289 return 0;
1290 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1291 return 0;
1292 return 1;
1293}
1294
1295static bool
1296lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1297{
1298 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001299}
1300
1301/**
James Smart7d791df2011-07-22 18:37:52 -04001302 * lpfc_update_fcf_record - Update driver fcf record
1303 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1304 * @phba: pointer to lpfc hba data structure.
1305 * @fcf_index: Index for the lpfc_fcf_record.
1306 * @new_fcf_record: pointer to hba fcf record.
1307 *
1308 * This routine updates the driver FCF priority record from the new HBA FCF
1309 * record. This routine is called with the host lock held.
1310 **/
1311static void
1312__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1313 struct fcf_record *new_fcf_record
1314 )
1315{
1316 struct lpfc_fcf_pri *fcf_pri;
1317
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001318 lockdep_assert_held(&phba->hbalock);
1319
James Smart7d791df2011-07-22 18:37:52 -04001320 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1321 fcf_pri->fcf_rec.fcf_index = fcf_index;
1322 /* FCF record priority */
1323 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1324
1325}
1326
1327/**
James Smart6fb120a2009-05-22 14:52:59 -04001328 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001329 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001330 * @new_fcf_record: pointer to fcf record.
1331 *
1332 * This routine copies the FCF information from the FCF
1333 * record to lpfc_hba data structure.
1334 **/
1335static void
James Smartecfd03c2010-02-12 14:41:27 -05001336lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1337 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001338{
James Smartecfd03c2010-02-12 14:41:27 -05001339 /* Fabric name */
1340 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001341 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001342 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001343 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001344 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001345 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001346 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001347 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001348 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001349 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001350 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001351 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001352 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001353 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001354 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001355 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001356 /* Mac address */
1357 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1358 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1359 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1360 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1361 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1362 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1363 /* FCF record index */
1364 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1365 /* FCF record priority */
1366 fcf_rec->priority = new_fcf_record->fip_priority;
1367 /* Switch name */
1368 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001369 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001370 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001371 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001372 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001373 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001374 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001375 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001376 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001377 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001378 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001379 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001380 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001381 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001382 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001383 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001384}
1385
1386/**
James Smartecfd03c2010-02-12 14:41:27 -05001387 * lpfc_update_fcf_record - Update driver fcf record
1388 * @phba: pointer to lpfc hba data structure.
1389 * @fcf_rec: pointer to driver fcf record.
1390 * @new_fcf_record: pointer to hba fcf record.
1391 * @addr_mode: address mode to be set to the driver fcf record.
1392 * @vlan_id: vlan tag to be set to the driver fcf record.
1393 * @flag: flag bits to be set to the driver fcf record.
1394 *
1395 * This routine updates the driver FCF record from the new HBA FCF record
1396 * together with the address mode, vlan_id, and other informations. This
1397 * routine is called with the host lock held.
1398 **/
1399static void
1400__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1401 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1402 uint16_t vlan_id, uint32_t flag)
1403{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001404 lockdep_assert_held(&phba->hbalock);
1405
James Smartecfd03c2010-02-12 14:41:27 -05001406 /* Copy the fields from the HBA's FCF record */
1407 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1408 /* Update other fields of driver FCF record */
1409 fcf_rec->addr_mode = addr_mode;
1410 fcf_rec->vlan_id = vlan_id;
1411 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001412 __lpfc_update_fcf_record_pri(phba,
1413 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1414 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001415}
1416
1417/**
James Smart6fb120a2009-05-22 14:52:59 -04001418 * lpfc_register_fcf - Register the FCF with hba.
1419 * @phba: pointer to lpfc hba data structure.
1420 *
1421 * This routine issues a register fcfi mailbox command to register
1422 * the fcf with HBA.
1423 **/
1424static void
1425lpfc_register_fcf(struct lpfc_hba *phba)
1426{
1427 LPFC_MBOXQ_t *fcf_mbxq;
1428 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001429
James Smartd439d282010-09-29 11:18:45 -04001430 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001431 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001432 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001433 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001434 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001435 return;
1436 }
1437
1438 /* The FCF is already registered, start discovery */
1439 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001440 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001441 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001442 if (phba->pport->port_state != LPFC_FLOGI &&
1443 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001444 phba->hba_flag |= FCF_RR_INPROG;
1445 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001446 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001447 return;
1448 }
1449 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001450 return;
1451 }
James Smartd439d282010-09-29 11:18:45 -04001452 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001453
James Smarta93ff372010-10-22 11:06:08 -04001454 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001455 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001456 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001457 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001458 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001459 return;
James Smart4d9ab992009-10-02 15:16:39 -04001460 }
James Smart6fb120a2009-05-22 14:52:59 -04001461
1462 lpfc_reg_fcfi(phba, fcf_mbxq);
1463 fcf_mbxq->vport = phba->pport;
1464 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1465 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001466 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001467 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001468 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001469 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001470 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001471 }
James Smart6fb120a2009-05-22 14:52:59 -04001472
1473 return;
1474}
1475
1476/**
1477 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1478 * @phba: pointer to lpfc hba data structure.
1479 * @new_fcf_record: pointer to fcf record.
1480 * @boot_flag: Indicates if this record used by boot bios.
1481 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001482 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001483 *
1484 * This routine compare the fcf record with connect list obtained from the
1485 * config region to decide if this FCF can be used for SAN discovery. It returns
1486 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1487 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1488 * is used by boot bios and addr_mode will indicate the addressing mode to be
1489 * used for this FCF when the function returns.
1490 * If the FCF record need to be used with a particular vlan id, the vlan is
1491 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001492 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001493 **/
1494static int
1495lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1496 struct fcf_record *new_fcf_record,
1497 uint32_t *boot_flag, uint32_t *addr_mode,
1498 uint16_t *vlan_id)
1499{
1500 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001501 int i, j, fcf_vlan_id = 0;
1502
1503 /* Find the lowest VLAN id in the FCF record */
1504 for (i = 0; i < 512; i++) {
1505 if (new_fcf_record->vlan_bitmap[i]) {
1506 fcf_vlan_id = i * 8;
1507 j = 0;
1508 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1509 j++;
1510 fcf_vlan_id++;
1511 }
1512 break;
1513 }
1514 }
James Smart6fb120a2009-05-22 14:52:59 -04001515
James Smart26979ce2012-09-29 11:31:55 -04001516 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001517 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001518 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1519 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001520 return 0;
1521
James Smart45ed1192009-10-02 15:17:02 -04001522 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001523 *boot_flag = 0;
1524 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1525 new_fcf_record);
1526 if (phba->valid_vlan)
1527 *vlan_id = phba->vlan_id;
1528 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001529 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001530 return 1;
1531 }
1532
1533 /*
1534 * If there are no FCF connection table entry, driver connect to all
1535 * FCFs.
1536 */
1537 if (list_empty(&phba->fcf_conn_rec_list)) {
1538 *boot_flag = 0;
1539 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1540 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001541
1542 /*
1543 * When there are no FCF connect entries, use driver's default
1544 * addressing mode - FPMA.
1545 */
1546 if (*addr_mode & LPFC_FCF_FPMA)
1547 *addr_mode = LPFC_FCF_FPMA;
1548
James Smart4d9ab992009-10-02 15:16:39 -04001549 /* If FCF record report a vlan id use that vlan id */
1550 if (fcf_vlan_id)
1551 *vlan_id = fcf_vlan_id;
1552 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001553 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001554 return 1;
1555 }
1556
James Smartecfd03c2010-02-12 14:41:27 -05001557 list_for_each_entry(conn_entry,
1558 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001559 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1560 continue;
1561
1562 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1563 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001564 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001565 continue;
James Smart8fa38512009-07-19 10:01:03 -04001566 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1567 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1568 new_fcf_record))
1569 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001570 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1571 /*
1572 * If the vlan bit map does not have the bit set for the
1573 * vlan id to be used, then it is not a match.
1574 */
1575 if (!(new_fcf_record->vlan_bitmap
1576 [conn_entry->conn_rec.vlan_tag / 8] &
1577 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1578 continue;
1579 }
1580
1581 /*
James Smart0c287582009-06-10 17:22:56 -04001582 * If connection record does not support any addressing mode,
1583 * skip the FCF record.
1584 */
1585 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1586 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1587 continue;
1588
1589 /*
James Smart6fb120a2009-05-22 14:52:59 -04001590 * Check if the connection record specifies a required
1591 * addressing mode.
1592 */
1593 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1594 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1595
1596 /*
1597 * If SPMA required but FCF not support this continue.
1598 */
1599 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1600 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1601 new_fcf_record) & LPFC_FCF_SPMA))
1602 continue;
1603
1604 /*
1605 * If FPMA required but FCF not support this continue.
1606 */
1607 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1608 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1609 new_fcf_record) & LPFC_FCF_FPMA))
1610 continue;
1611 }
1612
1613 /*
1614 * This fcf record matches filtering criteria.
1615 */
1616 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1617 *boot_flag = 1;
1618 else
1619 *boot_flag = 0;
1620
James Smart0c287582009-06-10 17:22:56 -04001621 /*
1622 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001623 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001624 * by FCF, allow fabric to pick the addressing mode.
1625 */
James Smart6fb120a2009-05-22 14:52:59 -04001626 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1627 new_fcf_record);
1628 /*
1629 * If the user specified a required address mode, assign that
1630 * address mode
1631 */
1632 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1633 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1634 *addr_mode = (conn_entry->conn_rec.flags &
1635 FCFCNCT_AM_SPMA) ?
1636 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1637 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001638 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001639 * addr mode only if FCF support the addr_mode.
1640 */
1641 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1642 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1643 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1644 (*addr_mode & LPFC_FCF_SPMA))
1645 *addr_mode = LPFC_FCF_SPMA;
1646 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1647 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1648 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1649 (*addr_mode & LPFC_FCF_FPMA))
1650 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001651
James Smart4d9ab992009-10-02 15:16:39 -04001652 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001653 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1654 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001655 /*
1656 * If no vlan id is specified in connect list, use the vlan id
1657 * in the FCF record
1658 */
1659 else if (fcf_vlan_id)
1660 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001661 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001662 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001663
1664 return 1;
1665 }
1666
1667 return 0;
1668}
1669
1670/**
James Smart32b97932009-07-19 10:01:21 -04001671 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1672 * @phba: pointer to lpfc hba data structure.
1673 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1674 *
1675 * This function check if there is any fcoe event pending while driver
1676 * scan FCF entries. If there is any pending event, it will restart the
1677 * FCF saning and return 1 else return 0.
1678 */
1679int
1680lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1681{
James Smart32b97932009-07-19 10:01:21 -04001682 /*
1683 * If the Link is up and no FCoE events while in the
1684 * FCF discovery, no need to restart FCF discovery.
1685 */
1686 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001687 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001688 return 0;
1689
James Smart0c9ab6f2010-02-26 14:15:57 -05001690 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1691 "2768 Pending link or FCF event during current "
1692 "handling of the previous event: link_state:x%x, "
1693 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1694 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1695 phba->fcoe_eventtag);
1696
James Smart32b97932009-07-19 10:01:21 -04001697 spin_lock_irq(&phba->hbalock);
1698 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1699 spin_unlock_irq(&phba->hbalock);
1700
James Smart0c9ab6f2010-02-26 14:15:57 -05001701 if (phba->link_state >= LPFC_LINK_UP) {
1702 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1703 "2780 Restart FCF table scan due to "
1704 "pending FCF event:evt_tag_at_scan:x%x, "
1705 "evt_tag_current:x%x\n",
1706 phba->fcoe_eventtag_at_fcf_scan,
1707 phba->fcoe_eventtag);
1708 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1709 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001710 /*
James Smarta93ff372010-10-22 11:06:08 -04001711 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001712 * flag
1713 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001714 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1715 "2833 Stop FCF discovery process due to link "
1716 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001717 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001718 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001719 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001720 spin_unlock_irq(&phba->hbalock);
1721 }
James Smart32b97932009-07-19 10:01:21 -04001722
James Smart0c9ab6f2010-02-26 14:15:57 -05001723 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001724 if (unreg_fcf) {
1725 spin_lock_irq(&phba->hbalock);
1726 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1727 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001728 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001729 }
James Smart32b97932009-07-19 10:01:21 -04001730 return 1;
1731}
1732
1733/**
James Smart999d8132010-03-15 11:24:56 -04001734 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1735 * @phba: pointer to lpfc hba data structure.
1736 * @fcf_cnt: number of eligible fcf record seen so far.
1737 *
1738 * This function makes an running random selection decision on FCF record to
1739 * use through a sequence of @fcf_cnt eligible FCF records with equal
1740 * probability. To perform integer manunipulation of random numbers with
1741 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001742 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001743 *
1744 * Returns true when outcome is for the newly read FCF record should be
1745 * chosen; otherwise, return false when outcome is for keeping the previously
1746 * chosen FCF record.
1747 **/
1748static bool
1749lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1750{
1751 uint32_t rand_num;
1752
1753 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001754 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001755
1756 /* Decision with probability 1/fcf_cnt */
1757 if ((fcf_cnt * rand_num) < 0xFFFF)
1758 return true;
1759 else
1760 return false;
1761}
1762
1763/**
James Smart3804dc82010-07-14 15:31:37 -04001764 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001765 * @phba: pointer to lpfc hba data structure.
1766 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001767 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001768 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001769 * This routine parses the non-embedded fcf mailbox command by performing the
1770 * necessarily error checking, non-embedded read FCF record mailbox command
1771 * SGE parsing, and endianness swapping.
1772 *
1773 * Returns the pointer to the new FCF record in the non-embedded mailbox
1774 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001775 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001776static struct fcf_record *
1777lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1778 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001779{
1780 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001781 struct lpfc_mbx_sge sge;
1782 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001783 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001784 union lpfc_sli4_cfg_shdr *shdr;
1785 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001786
James Smart6fb120a2009-05-22 14:52:59 -04001787 /* Get the first SGE entry from the non-embedded DMA memory. This
1788 * routine only uses a single SGE.
1789 */
1790 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001791 if (unlikely(!mboxq->sge_array)) {
1792 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1793 "2524 Failed to get the non-embedded SGE "
1794 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001795 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001796 }
1797 virt_addr = mboxq->sge_array->addr[0];
1798
1799 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001800 lpfc_sli_pcimem_bcopy(shdr, shdr,
1801 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001803 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001804 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001805 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001806 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1807 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001808 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001809 "2726 READ_FCF_RECORD Indicates empty "
1810 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001811 else
1812 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001813 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001814 "with status x%x add_status x%x, "
1815 "mbx\n", shdr_status, shdr_add_status);
1816 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001817 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001818
1819 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001820 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1821 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1822 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001823 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001824 new_fcf_record = (struct fcf_record *)(virt_addr +
1825 sizeof(struct lpfc_mbx_read_fcf_tbl));
1826 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001827 offsetof(struct fcf_record, vlan_bitmap));
1828 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1829 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001830
James Smart0c9ab6f2010-02-26 14:15:57 -05001831 return new_fcf_record;
1832}
1833
1834/**
1835 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1836 * @phba: pointer to lpfc hba data structure.
1837 * @fcf_record: pointer to the fcf record.
1838 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1839 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1840 *
1841 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1842 * enabled.
1843 **/
1844static void
1845lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1846 struct fcf_record *fcf_record,
1847 uint16_t vlan_id,
1848 uint16_t next_fcf_index)
1849{
1850 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1851 "2764 READ_FCF_RECORD:\n"
1852 "\tFCF_Index : x%x\n"
1853 "\tFCF_Avail : x%x\n"
1854 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001855 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001856 "\tFIP_Priority : x%x\n"
1857 "\tMAC_Provider : x%x\n"
1858 "\tLowest VLANID : x%x\n"
1859 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1860 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1861 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1862 "\tNext_FCF_Index: x%x\n",
1863 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1864 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1865 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001866 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001867 fcf_record->fip_priority,
1868 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1869 vlan_id,
1870 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1871 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1872 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1873 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1874 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1875 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1876 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1877 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1878 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1879 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1880 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1881 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1882 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1883 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1884 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1885 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1886 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1887 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1888 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1889 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1890 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1891 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1892 next_fcf_index);
1893}
1894
1895/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001896 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1897 * @phba: pointer to lpfc hba data structure.
1898 * @fcf_rec: pointer to an existing FCF record.
1899 * @new_fcf_record: pointer to a new FCF record.
1900 * @new_vlan_id: vlan id from the new FCF record.
1901 *
1902 * This function performs matching test of a new FCF record against an existing
1903 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1904 * will not be used as part of the FCF record matching criteria.
1905 *
1906 * Returns true if all the fields matching, otherwise returns false.
1907 */
1908static bool
1909lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1910 struct lpfc_fcf_rec *fcf_rec,
1911 struct fcf_record *new_fcf_record,
1912 uint16_t new_vlan_id)
1913{
1914 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1915 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1916 return false;
1917 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1918 return false;
1919 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1920 return false;
1921 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1922 return false;
James Smart7d791df2011-07-22 18:37:52 -04001923 if (fcf_rec->priority != new_fcf_record->fip_priority)
1924 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001925 return true;
1926}
1927
1928/**
James Smarta93ff372010-10-22 11:06:08 -04001929 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1930 * @vport: Pointer to vport object.
1931 * @fcf_index: index to next fcf.
1932 *
1933 * This function processing the roundrobin fcf failover to next fcf index.
1934 * When this function is invoked, there will be a current fcf registered
1935 * for flogi.
1936 * Return: 0 for continue retrying flogi on currently registered fcf;
1937 * 1 for stop flogi on currently registered fcf;
1938 */
1939int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1940{
1941 struct lpfc_hba *phba = vport->phba;
1942 int rc;
1943
1944 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1945 spin_lock_irq(&phba->hbalock);
1946 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1947 spin_unlock_irq(&phba->hbalock);
1948 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1949 "2872 Devloss tmo with no eligible "
1950 "FCF, unregister in-use FCF (x%x) "
1951 "and rescan FCF table\n",
1952 phba->fcf.current_rec.fcf_indx);
1953 lpfc_unregister_fcf_rescan(phba);
1954 goto stop_flogi_current_fcf;
1955 }
1956 /* Mark the end to FLOGI roundrobin failover */
1957 phba->hba_flag &= ~FCF_RR_INPROG;
1958 /* Allow action to new fcf asynchronous event */
1959 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1960 spin_unlock_irq(&phba->hbalock);
1961 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1962 "2865 No FCF available, stop roundrobin FCF "
1963 "failover and change port state:x%x/x%x\n",
1964 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1965 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1966 goto stop_flogi_current_fcf;
1967 } else {
1968 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1969 "2794 Try FLOGI roundrobin FCF failover to "
1970 "(x%x)\n", fcf_index);
1971 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1972 if (rc)
1973 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1974 "2761 FLOGI roundrobin FCF failover "
1975 "failed (rc:x%x) to read FCF (x%x)\n",
1976 rc, phba->fcf.current_rec.fcf_indx);
1977 else
1978 goto stop_flogi_current_fcf;
1979 }
1980 return 0;
1981
1982stop_flogi_current_fcf:
1983 lpfc_can_disctmo(vport);
1984 return 1;
1985}
1986
1987/**
James Smart7d791df2011-07-22 18:37:52 -04001988 * lpfc_sli4_fcf_pri_list_del
1989 * @phba: pointer to lpfc hba data structure.
1990 * @fcf_index the index of the fcf record to delete
1991 * This routine checks the on list flag of the fcf_index to be deleted.
1992 * If it is one the list then it is removed from the list, and the flag
1993 * is cleared. This routine grab the hbalock before removing the fcf
1994 * record from the list.
1995 **/
1996static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1997 uint16_t fcf_index)
1998{
1999 struct lpfc_fcf_pri *new_fcf_pri;
2000
2001 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2002 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2003 "3058 deleting idx x%x pri x%x flg x%x\n",
2004 fcf_index, new_fcf_pri->fcf_rec.priority,
2005 new_fcf_pri->fcf_rec.flag);
2006 spin_lock_irq(&phba->hbalock);
2007 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2008 if (phba->fcf.current_rec.priority ==
2009 new_fcf_pri->fcf_rec.priority)
2010 phba->fcf.eligible_fcf_cnt--;
2011 list_del_init(&new_fcf_pri->list);
2012 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2013 }
2014 spin_unlock_irq(&phba->hbalock);
2015}
2016
2017/**
2018 * lpfc_sli4_set_fcf_flogi_fail
2019 * @phba: pointer to lpfc hba data structure.
2020 * @fcf_index the index of the fcf record to update
2021 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2022 * flag so the the round robin slection for the particular priority level
2023 * will try a different fcf record that does not have this bit set.
2024 * If the fcf record is re-read for any reason this flag is cleared brfore
2025 * adding it to the priority list.
2026 **/
2027void
2028lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2029{
2030 struct lpfc_fcf_pri *new_fcf_pri;
2031 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2032 spin_lock_irq(&phba->hbalock);
2033 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2034 spin_unlock_irq(&phba->hbalock);
2035}
2036
2037/**
2038 * lpfc_sli4_fcf_pri_list_add
2039 * @phba: pointer to lpfc hba data structure.
2040 * @fcf_index the index of the fcf record to add
2041 * This routine checks the priority of the fcf_index to be added.
2042 * If it is a lower priority than the current head of the fcf_pri list
2043 * then it is added to the list in the right order.
2044 * If it is the same priority as the current head of the list then it
2045 * is added to the head of the list and its bit in the rr_bmask is set.
2046 * If the fcf_index to be added is of a higher priority than the current
2047 * head of the list then the rr_bmask is cleared, its bit is set in the
2048 * rr_bmask and it is added to the head of the list.
2049 * returns:
2050 * 0=success 1=failure
2051 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002052static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2053 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002054 struct fcf_record *new_fcf_record)
2055{
2056 uint16_t current_fcf_pri;
2057 uint16_t last_index;
2058 struct lpfc_fcf_pri *fcf_pri;
2059 struct lpfc_fcf_pri *next_fcf_pri;
2060 struct lpfc_fcf_pri *new_fcf_pri;
2061 int ret;
2062
2063 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2064 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2065 "3059 adding idx x%x pri x%x flg x%x\n",
2066 fcf_index, new_fcf_record->fip_priority,
2067 new_fcf_pri->fcf_rec.flag);
2068 spin_lock_irq(&phba->hbalock);
2069 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2070 list_del_init(&new_fcf_pri->list);
2071 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2072 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2073 if (list_empty(&phba->fcf.fcf_pri_list)) {
2074 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2075 ret = lpfc_sli4_fcf_rr_index_set(phba,
2076 new_fcf_pri->fcf_rec.fcf_index);
2077 goto out;
2078 }
2079
2080 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2081 LPFC_SLI4_FCF_TBL_INDX_MAX);
2082 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2083 ret = 0; /* Empty rr list */
2084 goto out;
2085 }
2086 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2087 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2088 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2089 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2090 memset(phba->fcf.fcf_rr_bmask, 0,
2091 sizeof(*phba->fcf.fcf_rr_bmask));
2092 /* fcfs_at_this_priority_level = 1; */
2093 phba->fcf.eligible_fcf_cnt = 1;
2094 } else
2095 /* fcfs_at_this_priority_level++; */
2096 phba->fcf.eligible_fcf_cnt++;
2097 ret = lpfc_sli4_fcf_rr_index_set(phba,
2098 new_fcf_pri->fcf_rec.fcf_index);
2099 goto out;
2100 }
2101
2102 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2103 &phba->fcf.fcf_pri_list, list) {
2104 if (new_fcf_pri->fcf_rec.priority <=
2105 fcf_pri->fcf_rec.priority) {
2106 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2107 list_add(&new_fcf_pri->list,
2108 &phba->fcf.fcf_pri_list);
2109 else
2110 list_add(&new_fcf_pri->list,
2111 &((struct lpfc_fcf_pri *)
2112 fcf_pri->list.prev)->list);
2113 ret = 0;
2114 goto out;
2115 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2116 || new_fcf_pri->fcf_rec.priority <
2117 next_fcf_pri->fcf_rec.priority) {
2118 list_add(&new_fcf_pri->list, &fcf_pri->list);
2119 ret = 0;
2120 goto out;
2121 }
2122 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2123 continue;
2124
2125 }
2126 ret = 1;
2127out:
2128 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2129 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2130 spin_unlock_irq(&phba->hbalock);
2131 return ret;
2132}
2133
2134/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002135 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2136 * @phba: pointer to lpfc hba data structure.
2137 * @mboxq: pointer to mailbox object.
2138 *
2139 * This function iterates through all the fcf records available in
2140 * HBA and chooses the optimal FCF record for discovery. After finding
2141 * the FCF for discovery it registers the FCF record and kicks start
2142 * discovery.
2143 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2144 * use an FCF record which matches fabric name and mac address of the
2145 * currently used FCF record.
2146 * If the driver supports only one FCF, it will try to use the FCF record
2147 * used by BOOT_BIOS.
2148 */
2149void
2150lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2151{
2152 struct fcf_record *new_fcf_record;
2153 uint32_t boot_flag, addr_mode;
2154 uint16_t fcf_index, next_fcf_index;
2155 struct lpfc_fcf_rec *fcf_rec = NULL;
2156 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002157 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002158 int rc;
2159
2160 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002161 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002162 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2163 return;
2164 }
2165
2166 /* Parse the FCF record from the non-embedded mailbox command */
2167 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2168 &next_fcf_index);
2169 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002170 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002171 "2765 Mailbox command READ_FCF_RECORD "
2172 "failed to retrieve a FCF record.\n");
2173 /* Let next new FCF event trigger fast failover */
2174 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002175 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002176 spin_unlock_irq(&phba->hbalock);
2177 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2178 return;
2179 }
2180
2181 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002182 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2183 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002184
2185 /* Log the FCF record information if turned on */
2186 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2187 next_fcf_index);
2188
James Smart6fb120a2009-05-22 14:52:59 -04002189 /*
2190 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002191 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002192 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002193 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002194 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002195 lpfc_sli4_fcf_pri_list_del(phba,
2196 bf_get(lpfc_fcf_record_fcf_index,
2197 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002198 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002199 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002200 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002201 bf_get(lpfc_fcf_record_fcf_index,
2202 new_fcf_record),
2203 bf_get(lpfc_fcf_record_fcf_avail,
2204 new_fcf_record),
2205 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002206 new_fcf_record),
2207 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002208 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002209 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2210 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2211 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002212 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2213 phba->fcf.current_rec.fcf_indx) {
2214 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2215 "2862 FCF (x%x) matches property "
2216 "of in-use FCF (x%x)\n",
2217 bf_get(lpfc_fcf_record_fcf_index,
2218 new_fcf_record),
2219 phba->fcf.current_rec.fcf_indx);
2220 goto read_next_fcf;
2221 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002222 /*
2223 * In case the current in-use FCF record becomes
2224 * invalid/unavailable during FCF discovery that
2225 * was not triggered by fast FCF failover process,
2226 * treat it as fast FCF failover.
2227 */
2228 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2229 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2230 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2231 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002232 "(x%x), enter FCF failover "
2233 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002234 phba->fcf.current_rec.fcf_indx);
2235 spin_lock_irq(&phba->hbalock);
2236 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2237 spin_unlock_irq(&phba->hbalock);
2238 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2239 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2240 LPFC_FCOE_FCF_GET_FIRST);
2241 return;
2242 }
2243 }
James Smart6fb120a2009-05-22 14:52:59 -04002244 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002245 } else {
2246 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002247 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2248 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002249 if (rc)
2250 goto read_next_fcf;
2251 }
2252
James Smart6fb120a2009-05-22 14:52:59 -04002253 /*
2254 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002255 * FCF record for the discovery. The condition that a rescan
2256 * matches the in-use FCF record: fabric name, switch name, mac
2257 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002258 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002259 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002260 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002261 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2262 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002263 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002264 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2265 phba->fcf.current_rec.fcf_indx) {
2266 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2267 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2268 /* Stop FCF redisc wait timer */
2269 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2270 phba);
2271 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2272 /* Fast failover, mark completed */
2273 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2274 spin_unlock_irq(&phba->hbalock);
2275 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2276 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002277 "FCF (x%x), port_state:x%x, "
2278 "fc_flag:x%x\n",
2279 phba->fcf.current_rec.fcf_indx,
2280 phba->pport->port_state,
2281 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002282 goto out;
2283 } else
2284 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2285 "2863 New FCF (x%x) matches "
2286 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002287 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002288 new_fcf_record),
2289 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002290 }
James Smartecfd03c2010-02-12 14:41:27 -05002291 /*
2292 * Read next FCF record from HBA searching for the matching
2293 * with in-use record only if not during the fast failover
2294 * period. In case of fast failover period, it shall try to
2295 * determine whether the FCF record just read should be the
2296 * next candidate.
2297 */
2298 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002299 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002300 goto read_next_fcf;
2301 }
James Smart6fb120a2009-05-22 14:52:59 -04002302 }
James Smartecfd03c2010-02-12 14:41:27 -05002303 /*
2304 * Update on failover FCF record only if it's in FCF fast-failover
2305 * period; otherwise, update on current FCF record.
2306 */
James Smartfc2b9892010-02-26 14:15:29 -05002307 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2308 fcf_rec = &phba->fcf.failover_rec;
2309 else
James Smartecfd03c2010-02-12 14:41:27 -05002310 fcf_rec = &phba->fcf.current_rec;
2311
James Smart6fb120a2009-05-22 14:52:59 -04002312 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2313 /*
James Smartecfd03c2010-02-12 14:41:27 -05002314 * If the driver FCF record does not have boot flag
2315 * set and new hba fcf record has boot flag set, use
2316 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002317 */
James Smartecfd03c2010-02-12 14:41:27 -05002318 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2319 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002320 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2321 "2837 Update current FCF record "
2322 "(x%x) with new FCF record (x%x)\n",
2323 fcf_rec->fcf_indx,
2324 bf_get(lpfc_fcf_record_fcf_index,
2325 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002326 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2327 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002328 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002329 goto read_next_fcf;
2330 }
2331 /*
James Smartecfd03c2010-02-12 14:41:27 -05002332 * If the driver FCF record has boot flag set and the
2333 * new hba FCF record does not have boot flag, read
2334 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002335 */
James Smartecfd03c2010-02-12 14:41:27 -05002336 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002337 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002338 goto read_next_fcf;
2339 }
2340 /*
James Smartecfd03c2010-02-12 14:41:27 -05002341 * If the new hba FCF record has lower priority value
2342 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002343 */
James Smartfc2b9892010-02-26 14:15:29 -05002344 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002345 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002346 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2347 "2838 Update current FCF record "
2348 "(x%x) with new FCF record (x%x)\n",
2349 fcf_rec->fcf_indx,
2350 bf_get(lpfc_fcf_record_fcf_index,
2351 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002352 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2353 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002354 /* Reset running random FCF selection count */
2355 phba->fcf.eligible_fcf_cnt = 1;
2356 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2357 /* Update running random FCF selection count */
2358 phba->fcf.eligible_fcf_cnt++;
2359 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2360 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002361 if (select_new_fcf) {
2362 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2363 "2839 Update current FCF record "
2364 "(x%x) with new FCF record (x%x)\n",
2365 fcf_rec->fcf_indx,
2366 bf_get(lpfc_fcf_record_fcf_index,
2367 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002368 /* Choose the new FCF by random selection */
2369 __lpfc_update_fcf_record(phba, fcf_rec,
2370 new_fcf_record,
2371 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002372 }
James Smart6fb120a2009-05-22 14:52:59 -04002373 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002374 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002375 goto read_next_fcf;
2376 }
2377 /*
James Smartecfd03c2010-02-12 14:41:27 -05002378 * This is the first suitable FCF record, choose this record for
2379 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002380 */
James Smartecfd03c2010-02-12 14:41:27 -05002381 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002382 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002383 "2840 Update initial FCF candidate "
2384 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002385 bf_get(lpfc_fcf_record_fcf_index,
2386 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002387 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2388 addr_mode, vlan_id, (boot_flag ?
2389 BOOT_ENABLE : 0));
2390 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002391 /* Setup initial running random FCF selection count */
2392 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002393 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002394 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002395 goto read_next_fcf;
2396
2397read_next_fcf:
2398 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002399 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2400 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2401 /*
2402 * Case of FCF fast failover scan
2403 */
2404
2405 /*
2406 * It has not found any suitable FCF record, cancel
2407 * FCF scan inprogress, and do nothing
2408 */
2409 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002410 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002411 "2782 No suitable FCF found: "
2412 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002413 phba->fcoe_eventtag_at_fcf_scan,
2414 bf_get(lpfc_fcf_record_fcf_index,
2415 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002416 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002417 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2418 phba->hba_flag &= ~FCF_TS_INPROG;
2419 spin_unlock_irq(&phba->hbalock);
2420 /* Unregister in-use FCF and rescan */
2421 lpfc_printf_log(phba, KERN_INFO,
2422 LOG_FIP,
2423 "2864 On devloss tmo "
2424 "unreg in-use FCF and "
2425 "rescan FCF table\n");
2426 lpfc_unregister_fcf_rescan(phba);
2427 return;
2428 }
2429 /*
2430 * Let next new FCF event trigger fast failover
2431 */
2432 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002433 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002434 return;
2435 }
2436 /*
2437 * It has found a suitable FCF record that is not
2438 * the same as in-use FCF record, unregister the
2439 * in-use FCF record, replace the in-use FCF record
2440 * with the new FCF record, mark FCF fast failover
2441 * completed, and then start register the new FCF
2442 * record.
2443 */
2444
James Smart0c9ab6f2010-02-26 14:15:57 -05002445 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002446 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002447
2448 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002449 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002450 "2842 Replace in-use FCF (x%x) "
2451 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002452 phba->fcf.current_rec.fcf_indx,
2453 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002454 memcpy(&phba->fcf.current_rec,
2455 &phba->fcf.failover_rec,
2456 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002457 /*
2458 * Mark the fast FCF failover rediscovery completed
2459 * and the start of the first round of the roundrobin
2460 * FCF failover.
2461 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002462 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002463 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002464 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002465 /* Register to the new FCF record */
2466 lpfc_register_fcf(phba);
2467 } else {
2468 /*
2469 * In case of transaction period to fast FCF failover,
2470 * do nothing when search to the end of the FCF table.
2471 */
2472 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2473 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2474 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002475
James Smart7d791df2011-07-22 18:37:52 -04002476 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2477 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002478 /*
2479 * In case the current in-use FCF record no
2480 * longer existed during FCF discovery that
2481 * was not triggered by fast FCF failover
2482 * process, treat it as fast FCF failover.
2483 */
2484 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2485 "2841 In-use FCF record (x%x) "
2486 "not reported, entering fast "
2487 "FCF failover mode scanning.\n",
2488 phba->fcf.current_rec.fcf_indx);
2489 spin_lock_irq(&phba->hbalock);
2490 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2491 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002492 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2493 LPFC_FCOE_FCF_GET_FIRST);
2494 return;
2495 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002496 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002497 lpfc_register_fcf(phba);
2498 }
2499 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002500 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002501 return;
2502
2503out:
2504 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2505 lpfc_register_fcf(phba);
2506
2507 return;
2508}
2509
2510/**
James Smarta93ff372010-10-22 11:06:08 -04002511 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002512 * @phba: pointer to lpfc hba data structure.
2513 * @mboxq: pointer to mailbox object.
2514 *
James Smarta93ff372010-10-22 11:06:08 -04002515 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002516 * read FCF record mailbox command from the eligible FCF record bmask for
2517 * performing the failover. If the FCF read back is not valid/available, it
2518 * fails through to retrying FLOGI to the currently registered FCF again.
2519 * Otherwise, if the FCF read back is valid and available, it will set the
2520 * newly read FCF record to the failover FCF record, unregister currently
2521 * registered FCF record, copy the failover FCF record to the current
2522 * FCF record, and then register the current FCF record before proceeding
2523 * to trying FLOGI on the new failover FCF.
2524 */
2525void
2526lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2527{
2528 struct fcf_record *new_fcf_record;
2529 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002530 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002531 uint16_t current_fcf_index;
2532 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002533 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002534
James Smarta93ff372010-10-22 11:06:08 -04002535 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002536 if (phba->link_state < LPFC_LINK_UP) {
2537 spin_lock_irq(&phba->hbalock);
2538 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002539 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002540 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002541 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002542 }
2543
2544 /* Parse the FCF record from the non-embedded mailbox command */
2545 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2546 &next_fcf_index);
2547 if (!new_fcf_record) {
2548 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2549 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002550 "failed to retrieve a FCF record. "
2551 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2552 phba->fcf.fcf_flag);
2553 lpfc_unregister_fcf_rescan(phba);
2554 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002555 }
2556
2557 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002558 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2559 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002560
2561 /* Log the FCF record information if turned on */
2562 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2563 next_fcf_index);
2564
James Smarta93ff372010-10-22 11:06:08 -04002565 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2566 if (!rc) {
2567 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2568 "2848 Remove ineligible FCF (x%x) from "
2569 "from roundrobin bmask\n", fcf_index);
2570 /* Clear roundrobin bmask bit for ineligible FCF */
2571 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2572 /* Perform next round of roundrobin FCF failover */
2573 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2574 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2575 if (rc)
2576 goto out;
2577 goto error_out;
2578 }
2579
2580 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2581 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2582 "2760 Perform FLOGI roundrobin FCF failover: "
2583 "FCF (x%x) back to FCF (x%x)\n",
2584 phba->fcf.current_rec.fcf_indx, fcf_index);
2585 /* Wait 500 ms before retrying FLOGI to current FCF */
2586 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002587 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002588 goto out;
2589 }
2590
James Smart0c9ab6f2010-02-26 14:15:57 -05002591 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002592 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002593 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2594 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002595 spin_lock_irq(&phba->hbalock);
2596 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2597 new_fcf_record, addr_mode, vlan_id,
2598 (boot_flag ? BOOT_ENABLE : 0));
2599 spin_unlock_irq(&phba->hbalock);
2600
2601 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2602
2603 /* Unregister the current in-use FCF record */
2604 lpfc_unregister_fcf(phba);
2605
2606 /* Replace in-use record with the new record */
2607 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2608 sizeof(struct lpfc_fcf_rec));
2609
2610 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002611 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2612 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002613
James Smarta93ff372010-10-22 11:06:08 -04002614error_out:
2615 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002616out:
2617 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002618}
2619
2620/**
2621 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2622 * @phba: pointer to lpfc hba data structure.
2623 * @mboxq: pointer to mailbox object.
2624 *
2625 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002626 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002627 * failover when a new FCF event happened. If the FCF read back is
2628 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002629 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002630 */
2631void
2632lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2633{
2634 struct fcf_record *new_fcf_record;
2635 uint32_t boot_flag, addr_mode;
2636 uint16_t fcf_index, next_fcf_index;
2637 uint16_t vlan_id;
2638 int rc;
2639
2640 /* If link state is not up, no need to proceed */
2641 if (phba->link_state < LPFC_LINK_UP)
2642 goto out;
2643
2644 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002645 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002646 goto out;
2647
2648 /* Parse the FCF record from the non-embedded mailbox command */
2649 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2650 &next_fcf_index);
2651 if (!new_fcf_record) {
2652 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2653 "2767 Mailbox command READ_FCF_RECORD "
2654 "failed to retrieve a FCF record.\n");
2655 goto out;
2656 }
2657
2658 /* Check the connection list for eligibility */
2659 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2660 &addr_mode, &vlan_id);
2661
2662 /* Log the FCF record information if turned on */
2663 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2664 next_fcf_index);
2665
2666 if (!rc)
2667 goto out;
2668
2669 /* Update the eligible FCF record index bmask */
2670 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002671
2672 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002673
2674out:
2675 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2676}
2677
2678/**
James Smart76a95d72010-11-20 23:11:48 -05002679 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2680 * @phba: pointer to lpfc hba data structure.
2681 * @mboxq: pointer to mailbox data structure.
2682 *
2683 * This function handles completion of init vfi mailbox command.
2684 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002685static void
James Smart76a95d72010-11-20 23:11:48 -05002686lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2687{
2688 struct lpfc_vport *vport = mboxq->vport;
2689
James Smartdf9e1b52011-12-13 13:22:17 -05002690 /*
2691 * VFI not supported on interface type 0, just do the flogi
2692 * Also continue if the VFI is in use - just use the same one.
2693 */
2694 if (mboxq->u.mb.mbxStatus &&
2695 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2696 LPFC_SLI_INTF_IF_TYPE_0) &&
2697 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002698 lpfc_printf_vlog(vport, KERN_ERR,
2699 LOG_MBOX,
2700 "2891 Init VFI mailbox failed 0x%x\n",
2701 mboxq->u.mb.mbxStatus);
2702 mempool_free(mboxq, phba->mbox_mem_pool);
2703 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2704 return;
2705 }
James Smart73d91e52011-10-10 21:32:10 -04002706
James Smart76a95d72010-11-20 23:11:48 -05002707 lpfc_initial_flogi(vport);
2708 mempool_free(mboxq, phba->mbox_mem_pool);
2709 return;
2710}
2711
2712/**
2713 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2714 * @vport: pointer to lpfc_vport data structure.
2715 *
2716 * This function issue a init_vfi mailbox command to initialize the VFI and
2717 * VPI for the physical port.
2718 */
2719void
2720lpfc_issue_init_vfi(struct lpfc_vport *vport)
2721{
2722 LPFC_MBOXQ_t *mboxq;
2723 int rc;
2724 struct lpfc_hba *phba = vport->phba;
2725
2726 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2727 if (!mboxq) {
2728 lpfc_printf_vlog(vport, KERN_ERR,
2729 LOG_MBOX, "2892 Failed to allocate "
2730 "init_vfi mailbox\n");
2731 return;
2732 }
2733 lpfc_init_vfi(mboxq, vport);
2734 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2735 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2736 if (rc == MBX_NOT_FINISHED) {
2737 lpfc_printf_vlog(vport, KERN_ERR,
2738 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2739 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2740 }
2741}
2742
2743/**
James Smart1c6834a2009-07-19 10:01:26 -04002744 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2745 * @phba: pointer to lpfc hba data structure.
2746 * @mboxq: pointer to mailbox data structure.
2747 *
2748 * This function handles completion of init vpi mailbox command.
2749 */
James Smart695a8142010-01-26 23:08:03 -05002750void
James Smart1c6834a2009-07-19 10:01:26 -04002751lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2752{
2753 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002754 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002755 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2756
James Smart1c6834a2009-07-19 10:01:26 -04002757 if (mboxq->u.mb.mbxStatus) {
2758 lpfc_printf_vlog(vport, KERN_ERR,
2759 LOG_MBOX,
2760 "2609 Init VPI mailbox failed 0x%x\n",
2761 mboxq->u.mb.mbxStatus);
2762 mempool_free(mboxq, phba->mbox_mem_pool);
2763 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2764 return;
2765 }
James Smart72100cc2010-02-12 14:43:01 -05002766 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002767 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002768 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002769
James Smart695a8142010-01-26 23:08:03 -05002770 /* If this port is physical port or FDISC is done, do reg_vpi */
2771 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2772 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2773 if (!ndlp)
2774 lpfc_printf_vlog(vport, KERN_ERR,
2775 LOG_DISCOVERY,
2776 "2731 Cannot find fabric "
2777 "controller node\n");
2778 else
2779 lpfc_register_new_vport(phba, vport, ndlp);
2780 mempool_free(mboxq, phba->mbox_mem_pool);
2781 return;
2782 }
2783
James Smart1c6834a2009-07-19 10:01:26 -04002784 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2785 lpfc_initial_fdisc(vport);
2786 else {
2787 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002788 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2789 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002790 }
James Smart695a8142010-01-26 23:08:03 -05002791 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002792 return;
2793}
2794
2795/**
James Smartecfd03c2010-02-12 14:41:27 -05002796 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2797 * @vport: pointer to lpfc_vport data structure.
2798 *
2799 * This function issue a init_vpi mailbox command to initialize
2800 * VPI for the vport.
2801 */
2802void
2803lpfc_issue_init_vpi(struct lpfc_vport *vport)
2804{
2805 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002806 int rc, vpi;
2807
2808 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2809 vpi = lpfc_alloc_vpi(vport->phba);
2810 if (!vpi) {
2811 lpfc_printf_vlog(vport, KERN_ERR,
2812 LOG_MBOX,
2813 "3303 Failed to obtain vport vpi\n");
2814 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2815 return;
2816 }
2817 vport->vpi = vpi;
2818 }
James Smartecfd03c2010-02-12 14:41:27 -05002819
2820 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2821 if (!mboxq) {
2822 lpfc_printf_vlog(vport, KERN_ERR,
2823 LOG_MBOX, "2607 Failed to allocate "
2824 "init_vpi mailbox\n");
2825 return;
2826 }
2827 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2828 mboxq->vport = vport;
2829 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2830 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2831 if (rc == MBX_NOT_FINISHED) {
2832 lpfc_printf_vlog(vport, KERN_ERR,
2833 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2834 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2835 }
2836}
2837
2838/**
James Smart6fb120a2009-05-22 14:52:59 -04002839 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2840 * @phba: pointer to lpfc hba data structure.
2841 *
2842 * This function loops through the list of vports on the @phba and issues an
2843 * FDISC if possible.
2844 */
2845void
2846lpfc_start_fdiscs(struct lpfc_hba *phba)
2847{
2848 struct lpfc_vport **vports;
2849 int i;
2850
2851 vports = lpfc_create_vport_work_array(phba);
2852 if (vports != NULL) {
2853 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2854 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2855 continue;
2856 /* There are no vpi for this vport */
2857 if (vports[i]->vpi > phba->max_vpi) {
2858 lpfc_vport_set_state(vports[i],
2859 FC_VPORT_FAILED);
2860 continue;
2861 }
James Smart76a95d72010-11-20 23:11:48 -05002862 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002863 lpfc_vport_set_state(vports[i],
2864 FC_VPORT_LINKDOWN);
2865 continue;
2866 }
James Smart1c6834a2009-07-19 10:01:26 -04002867 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002868 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002869 continue;
2870 }
James Smart6fb120a2009-05-22 14:52:59 -04002871 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2872 lpfc_initial_fdisc(vports[i]);
2873 else {
2874 lpfc_vport_set_state(vports[i],
2875 FC_VPORT_NO_FABRIC_SUPP);
2876 lpfc_printf_vlog(vports[i], KERN_ERR,
2877 LOG_ELS,
2878 "0259 No NPIV "
2879 "Fabric support\n");
2880 }
2881 }
2882 }
2883 lpfc_destroy_vport_work_array(phba, vports);
2884}
2885
2886void
2887lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2888{
2889 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2890 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002891 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002892
James Smartf5eca9b2012-03-01 22:37:54 -05002893 /*
2894 * VFI not supported for interface type 0, so ignore any mailbox
2895 * error (except VFI in use) and continue with the discovery.
2896 */
2897 if (mboxq->u.mb.mbxStatus &&
2898 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2899 LPFC_SLI_INTF_IF_TYPE_0) &&
2900 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002901 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2902 "2018 REG_VFI mbxStatus error x%x "
2903 "HBA state x%x\n",
2904 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002905 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002906 /* FLOGI failed, use loop map to make discovery list */
2907 lpfc_disc_list_loopmap(vport);
2908 /* Start discovery */
2909 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002910 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002911 }
2912 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002913 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002914 }
James Smartae05ebe2013-03-01 16:35:38 -05002915
James Smarte74c03c2013-04-17 20:15:19 -04002916 /* If the VFI is already registered, there is nothing else to do
2917 * Unless this was a VFI update and we are in PT2PT mode, then
2918 * we should drop through to set the port state to ready.
2919 */
James Smartae05ebe2013-03-01 16:35:38 -05002920 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002921 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2922 vport->fc_flag & FC_PT2PT))
2923 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002924
James Smartc8685952009-11-18 15:39:16 -05002925 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002926 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002927 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002928 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002929 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002930 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002931 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002932
James Smart1b511972011-12-13 13:23:09 -05002933 /* In case SLI4 FC loopback test, we are ready */
2934 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2935 (phba->link_flag & LS_LOOPBACK_MODE)) {
2936 phba->link_state = LPFC_HBA_READY;
2937 goto out_free_mem;
2938 }
2939
James Smarte74c03c2013-04-17 20:15:19 -04002940 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2941 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2942 "alpacnt:%d LinkState:%x topology:%x\n",
2943 vport->port_state, vport->fc_flag, vport->fc_myDID,
2944 vport->phba->alpa_map[0],
2945 phba->link_state, phba->fc_topology);
2946
James Smart6fb120a2009-05-22 14:52:59 -04002947 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002948 /*
2949 * For private loop or for NPort pt2pt,
2950 * just start discovery and we are done.
2951 */
2952 if ((vport->fc_flag & FC_PT2PT) ||
2953 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2954 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2955
James Smart76a95d72010-11-20 23:11:48 -05002956 /* Use loop map to make discovery list */
2957 lpfc_disc_list_loopmap(vport);
2958 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002959 if (vport->fc_flag & FC_PT2PT)
2960 vport->port_state = LPFC_VPORT_READY;
2961 else
2962 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002963 } else {
2964 lpfc_start_fdiscs(phba);
2965 lpfc_do_scr_ns_plogi(phba, vport);
2966 }
James Smart6fb120a2009-05-22 14:52:59 -04002967 }
2968
James Smart1b511972011-12-13 13:23:09 -05002969out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002970 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002971 if (dmabuf) {
2972 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2973 kfree(dmabuf);
2974 }
James Smart6fb120a2009-05-22 14:52:59 -04002975 return;
2976}
2977
2978static void
James Smart2e0fef82007-06-17 19:56:36 -05002979lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002980{
James Smart6fb120a2009-05-22 14:52:59 -04002981 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002982 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002983 struct lpfc_vport *vport = pmb->vport;
James Smart21bf0b92015-08-31 16:48:21 -04002984 struct serv_parm *sp = &vport->fc_sparam;
2985 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05002986
2987 /* Check for error */
2988 if (mb->mbxStatus) {
2989 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002990 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2991 "0319 READ_SPARAM mbxStatus error x%x "
2992 "hba state x%x>\n",
2993 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002994 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002995 goto out;
2996 }
2997
James Smart2e0fef82007-06-17 19:56:36 -05002998 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002999 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003000
3001 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3002 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3003 ed_tov = (ed_tov + 999999) / 1000000;
3004
3005 phba->fc_edtov = ed_tov;
3006 phba->fc_ratov = (2 * ed_tov) / 1000;
3007 if (phba->fc_ratov < FF_DEF_RATOV) {
3008 /* RA_TOV should be atleast 10sec for initial flogi */
3009 phba->fc_ratov = FF_DEF_RATOV;
3010 }
3011
James Smart05580562011-05-24 11:40:48 -04003012 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003013 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3014 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3015 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3016 }
3017
dea31012005-04-17 16:05:31 -05003018 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3019 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003020 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003021 return;
3022
3023out:
3024 pmb->context1 = NULL;
3025 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3026 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003027 lpfc_issue_clear_la(phba, vport);
3028 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003029 return;
3030}
3031
3032static void
James Smart76a95d72010-11-20 23:11:48 -05003033lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003034{
James Smart92d7f7b2007-06-17 19:56:38 -05003035 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003036 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003037 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003038 int i;
James Smart14691152006-12-02 13:34:28 -05003039 struct lpfc_dmabuf *mp;
3040 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003041 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003042 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003043
James Smart92d7f7b2007-06-17 19:56:38 -05003044 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003045 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3046
3047 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3048 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3049 case LPFC_LINK_SPEED_1GHZ:
3050 case LPFC_LINK_SPEED_2GHZ:
3051 case LPFC_LINK_SPEED_4GHZ:
3052 case LPFC_LINK_SPEED_8GHZ:
3053 case LPFC_LINK_SPEED_10GHZ:
3054 case LPFC_LINK_SPEED_16GHZ:
3055 case LPFC_LINK_SPEED_32GHZ:
3056 break;
3057 default:
3058 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3059 break;
3060 }
dea31012005-04-17 16:05:31 -05003061 }
3062
James Smarte74c03c2013-04-17 20:15:19 -04003063 if (phba->fc_topology &&
3064 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3065 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3066 "3314 Toplogy changed was 0x%x is 0x%x\n",
3067 phba->fc_topology,
3068 bf_get(lpfc_mbx_read_top_topology, la));
3069 phba->fc_topology_changed = 1;
3070 }
3071
James Smart76a95d72010-11-20 23:11:48 -05003072 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003073 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003074
James Smartdf9e1b52011-12-13 13:22:17 -05003075 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003076 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003077 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003078
James Smart6a9c52c2009-10-02 15:16:51 -04003079 /* if npiv is enabled and this adapter supports npiv log
3080 * a message that npiv is not supported in this topology
3081 */
3082 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003083 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3084 "1309 Link Up Event npiv not supported in loop "
3085 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003086 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003087 if (bf_get(lpfc_mbx_read_top_il, la))
3088 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003089
James Smart76a95d72010-11-20 23:11:48 -05003090 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3091 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003092
3093 if (i == 0) {
3094 phba->alpa_map[0] = 0;
3095 } else {
James Smarte8b62012007-08-02 11:10:09 -04003096 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003097 int numalpa, j, k;
3098 union {
3099 uint8_t pamap[16];
3100 struct {
3101 uint32_t wd1;
3102 uint32_t wd2;
3103 uint32_t wd3;
3104 uint32_t wd4;
3105 } pa;
3106 } un;
3107 numalpa = phba->alpa_map[0];
3108 j = 0;
3109 while (j < numalpa) {
3110 memset(un.pamap, 0, 16);
3111 for (k = 1; j < numalpa; k++) {
3112 un.pamap[k - 1] =
3113 phba->alpa_map[j + 1];
3114 j++;
3115 if (k == 16)
3116 break;
3117 }
3118 /* Link Up Event ALPA map */
3119 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003120 KERN_WARNING,
3121 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003122 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003123 "ALPA map Data: x%x "
3124 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003125 un.pa.wd1, un.pa.wd2,
3126 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003127 }
3128 }
3129 }
3130 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003131 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003132 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003133 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003134 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3135 }
James Smart2e0fef82007-06-17 19:56:36 -05003136 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003137 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003138 }
James Smart92d7f7b2007-06-17 19:56:38 -05003139 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003140
James Smart38c20672013-03-01 16:37:44 -05003141 if (fc_flags) {
3142 spin_lock_irq(shost->host_lock);
3143 vport->fc_flag |= fc_flags;
3144 spin_unlock_irq(shost->host_lock);
3145 }
3146
dea31012005-04-17 16:05:31 -05003147 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003148 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3149 if (!sparam_mbox)
3150 goto out;
3151
3152 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3153 if (rc) {
3154 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3155 goto out;
3156 }
3157 sparam_mbox->vport = vport;
3158 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3159 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3160 if (rc == MBX_NOT_FINISHED) {
3161 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3162 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3163 kfree(mp);
3164 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3165 goto out;
dea31012005-04-17 16:05:31 -05003166 }
3167
James Smart76a95d72010-11-20 23:11:48 -05003168 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003169 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3170 if (!cfglink_mbox)
3171 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003172 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003173 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003174 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003175 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003176 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003177 if (rc == MBX_NOT_FINISHED) {
3178 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3179 goto out;
3180 }
3181 } else {
James Smart32b97932009-07-19 10:01:21 -04003182 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003183 /*
3184 * Add the driver's default FCF record at FCF index 0 now. This
3185 * is phase 1 implementation that support FCF index 0 and driver
3186 * defaults.
3187 */
James Smart45ed1192009-10-02 15:17:02 -04003188 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003189 fcf_record = kzalloc(sizeof(struct fcf_record),
3190 GFP_KERNEL);
3191 if (unlikely(!fcf_record)) {
3192 lpfc_printf_log(phba, KERN_ERR,
3193 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003194 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003195 "fcf record\n");
3196 rc = -ENODEV;
3197 goto out;
3198 }
3199
3200 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3201 LPFC_FCOE_FCF_DEF_INDEX);
3202 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3203 if (unlikely(rc)) {
3204 lpfc_printf_log(phba, KERN_ERR,
3205 LOG_MBOX | LOG_SLI,
3206 "2013 Could not manually add FCF "
3207 "record 0, status %d\n", rc);
3208 rc = -ENODEV;
3209 kfree(fcf_record);
3210 goto out;
3211 }
3212 kfree(fcf_record);
3213 }
3214 /*
3215 * The driver is expected to do FIP/FCF. Call the port
3216 * and get the FCF Table.
3217 */
James Smart32b97932009-07-19 10:01:21 -04003218 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003219 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003220 spin_unlock_irq(&phba->hbalock);
3221 return;
3222 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003223 /* This is the initial FCF discovery scan */
3224 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003225 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003226 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3227 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003228 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3229 LPFC_FCOE_FCF_GET_FIRST);
3230 if (rc) {
3231 spin_lock_irq(&phba->hbalock);
3232 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3233 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003234 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003235 }
James Smart38b92ef2010-08-04 16:11:39 -04003236 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003237 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003238 }
James Smart6fb120a2009-05-22 14:52:59 -04003239
3240 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003241out:
3242 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003243 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3244 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3245 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003246 lpfc_issue_clear_la(phba, vport);
3247 return;
dea31012005-04-17 16:05:31 -05003248}
3249
3250static void
James Smart84774a42008-08-24 21:50:06 -04003251lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003252{
dea31012005-04-17 16:05:31 -05003253 uint32_t control;
3254 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003255 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003256 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003257 if (phba->sli_rev <= LPFC_SLI_REV3) {
3258 control = readl(phba->HCregaddr);
3259 control |= HC_LAINT_ENA;
3260 writel(control, phba->HCregaddr);
3261 readl(phba->HCregaddr); /* flush */
3262 }
James Smart2e0fef82007-06-17 19:56:36 -05003263 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003264}
3265
James Smart84774a42008-08-24 21:50:06 -04003266static void
3267lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3268{
3269 lpfc_linkdown(phba);
3270 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003271 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003272 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3273}
3274
3275
dea31012005-04-17 16:05:31 -05003276/*
James Smart76a95d72010-11-20 23:11:48 -05003277 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003278 * command upon completion. It is setup in the LPFC_MBOXQ
3279 * as the completion routine when the command is
3280 * handed off to the SLI layer.
3281 */
3282void
James Smart76a95d72010-11-20 23:11:48 -05003283lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003284{
James Smart2e0fef82007-06-17 19:56:36 -05003285 struct lpfc_vport *vport = pmb->vport;
3286 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003287 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003288 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003289 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3290
James Smart0d2b6b82008-06-14 22:52:47 -04003291 /* Unblock ELS traffic */
3292 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003293 /* Check for error */
3294 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003295 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003296 "1307 READ_LA mbox error x%x state x%x\n",
3297 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003298 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003299 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003300 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003301 }
3302
James Smart76a95d72010-11-20 23:11:48 -05003303 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003304
3305 memcpy(&phba->alpa_map[0], mp->virt, 128);
3306
James Smart2e0fef82007-06-17 19:56:36 -05003307 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003308 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003309 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003310 else
James Smart2e0fef82007-06-17 19:56:36 -05003311 vport->fc_flag &= ~FC_BYPASSED_MODE;
3312 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003313
James Smartbe0c0082013-03-01 16:38:14 -05003314 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003315 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003316 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003317 if (phba->fc_eventTag != 0)
3318 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003319 }
dea31012005-04-17 16:05:31 -05003320
3321 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003322 if (phba->sli_rev < LPFC_SLI_REV4) {
3323 spin_lock_irq(&phba->hbalock);
3324 if (bf_get(lpfc_mbx_read_top_mm, la))
3325 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3326 else
3327 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3328 spin_unlock_irq(&phba->hbalock);
3329 }
dea31012005-04-17 16:05:31 -05003330
James Smart4d9ab992009-10-02 15:16:39 -04003331 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003332 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003333 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003334 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003335 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003336 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003337 "1306 Link Up Event in loop back mode "
3338 "x%x received Data: x%x x%x x%x x%x\n",
3339 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003340 bf_get(lpfc_mbx_read_top_alpa_granted,
3341 la),
3342 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003343 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003344 } else {
3345 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003346 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003347 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003348 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003349 bf_get(lpfc_mbx_read_top_alpa_granted,
3350 la),
3351 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003352 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003353 bf_get(lpfc_mbx_read_top_mm, la),
3354 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003355 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003356 }
James Smart92d7f7b2007-06-17 19:56:38 -05003357 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003358 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3359 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003360 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003361 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003362 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3363 "1308 Link Down Event in loop back mode "
3364 "x%x received "
3365 "Data: x%x x%x x%x\n",
3366 la->eventTag, phba->fc_eventTag,
3367 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003368 else
James Smart3163f722008-02-08 18:50:25 -05003369 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003370 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003371 "Data: x%x x%x x%x x%x x%x\n",
3372 la->eventTag, phba->fc_eventTag,
3373 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003374 bf_get(lpfc_mbx_read_top_mm, la),
3375 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003376 lpfc_mbx_issue_link_down(phba);
3377 }
James Smartbe0c0082013-03-01 16:38:14 -05003378 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3379 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003380 if (phba->link_state != LPFC_LINK_DOWN) {
3381 phba->fc_stat.LinkDown++;
3382 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3383 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003384 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003385 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003386 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003387 lpfc_mbx_issue_link_down(phba);
3388 } else
3389 lpfc_enable_la(phba);
3390
3391 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3392 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3393 "Data: x%x x%x x%x\n",
3394 la->eventTag, phba->fc_eventTag,
3395 phba->pport->port_state, vport->fc_flag);
3396 /*
3397 * The cmnd that triggered this will be waiting for this
3398 * signal.
3399 */
3400 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3401 if (phba->wait_4_mlo_maint_flg) {
3402 phba->wait_4_mlo_maint_flg = 0;
3403 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003404 }
James Smart84774a42008-08-24 21:50:06 -04003405 }
3406
James Smartbe0c0082013-03-01 16:38:14 -05003407 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3408 bf_get(lpfc_mbx_read_top_fa, la)) {
3409 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003410 lpfc_issue_clear_la(phba, vport);
3411 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003412 "1311 fa %d\n",
3413 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003414 }
3415
James Smart76a95d72010-11-20 23:11:48 -05003416lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003417 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3418 kfree(mp);
3419 mempool_free(pmb, phba->mbox_mem_pool);
3420 return;
3421}
3422
3423/*
3424 * This routine handles processing a REG_LOGIN mailbox
3425 * command upon completion. It is setup in the LPFC_MBOXQ
3426 * as the completion routine when the command is
3427 * handed off to the SLI layer.
3428 */
3429void
James Smart2e0fef82007-06-17 19:56:36 -05003430lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003431{
James Smart2e0fef82007-06-17 19:56:36 -05003432 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003433 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003434 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003435 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003436
dea31012005-04-17 16:05:31 -05003437 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003438 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003439
James Smartbe6bb942015-04-07 15:07:22 -04003440 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3441 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3442 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3443 atomic_read(&ndlp->kref.refcount),
3444 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003445 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3446 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3447
James Smart40426292010-12-15 17:58:10 -05003448 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3449 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003450 /* We rcvd a rscn after issuing this
3451 * mbox reg login, we may have cycled
3452 * back through the state and be
3453 * back at reg login state so this
3454 * mbox needs to be ignored becase
3455 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003456 * process.
James Smartffc95492010-06-07 15:23:17 -04003457 */
3458 spin_lock_irq(shost->host_lock);
3459 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3460 spin_unlock_irq(shost->host_lock);
James Smart4b7789b2015-12-16 18:11:55 -05003461 }
3462
3463 /* Call state machine */
3464 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003465
dea31012005-04-17 16:05:31 -05003466 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3467 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003468 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003469 /* decrement the node reference count held for this callback
3470 * function.
3471 */
James Smart329f9bc2007-04-25 09:53:01 -04003472 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003473
3474 return;
3475}
3476
James Smart92d7f7b2007-06-17 19:56:38 -05003477static void
3478lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3479{
James Smart04c68492009-05-22 14:52:52 -04003480 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003481 struct lpfc_vport *vport = pmb->vport;
3482 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3483
3484 switch (mb->mbxStatus) {
3485 case 0x0011:
3486 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003487 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3488 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3489 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003490 break;
James Smart78730cf2010-04-06 15:06:30 -04003491 /* If VPI is busy, reset the HBA */
3492 case 0x9700:
3493 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3494 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3495 vport->vpi, mb->mbxStatus);
3496 if (!(phba->pport->load_flag & FC_UNLOADING))
3497 lpfc_workq_post_event(phba, NULL, NULL,
3498 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003499 }
James Smart72100cc2010-02-12 14:43:01 -05003500 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003501 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003502 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003503 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003504 vport->unreg_vpi_cmpl = VPORT_OK;
3505 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003506 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003507 /*
3508 * This shost reference might have been taken at the beginning of
3509 * lpfc_vport_delete()
3510 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003511 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003512 scsi_host_put(shost);
3513}
3514
James Smartd7c255b2008-08-24 21:50:00 -04003515int
James Smart92d7f7b2007-06-17 19:56:38 -05003516lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3517{
3518 struct lpfc_hba *phba = vport->phba;
3519 LPFC_MBOXQ_t *mbox;
3520 int rc;
3521
3522 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3523 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003524 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003525
3526 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3527 mbox->vport = vport;
3528 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003529 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003530 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003531 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3532 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003533 mempool_free(mbox, phba->mbox_mem_pool);
3534 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003535 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003536 }
James Smartd7c255b2008-08-24 21:50:00 -04003537 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003538}
3539
3540static void
3541lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3542{
3543 struct lpfc_vport *vport = pmb->vport;
3544 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003545 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003546
3547 switch (mb->mbxStatus) {
3548 case 0x0011:
3549 case 0x9601:
3550 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003551 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3552 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3553 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003554 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3555 spin_lock_irq(shost->host_lock);
3556 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3557 spin_unlock_irq(shost->host_lock);
3558 vport->fc_myDID = 0;
3559 goto out;
3560 }
James Smart92d7f7b2007-06-17 19:56:38 -05003561
James Smart72100cc2010-02-12 14:43:01 -05003562 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003563 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003564 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003565 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003566 vport->num_disc_nodes = 0;
3567 /* go thru NPR list and issue ELS PLOGIs */
3568 if (vport->fc_npr_cnt)
3569 lpfc_els_disc_plogi(vport);
3570
3571 if (!vport->num_disc_nodes) {
3572 spin_lock_irq(shost->host_lock);
3573 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3574 spin_unlock_irq(shost->host_lock);
3575 lpfc_can_disctmo(vport);
3576 }
3577 vport->port_state = LPFC_VPORT_READY;
3578
3579out:
3580 mempool_free(pmb, phba->mbox_mem_pool);
3581 return;
3582}
3583
James Smart21e9a0a2009-05-22 14:53:21 -04003584/**
3585 * lpfc_create_static_vport - Read HBA config region to create static vports.
3586 * @phba: pointer to lpfc hba data structure.
3587 *
3588 * This routine issue a DUMP mailbox command for config region 22 to get
3589 * the list of static vports to be created. The function create vports
3590 * based on the information returned from the HBA.
3591 **/
3592void
3593lpfc_create_static_vport(struct lpfc_hba *phba)
3594{
3595 LPFC_MBOXQ_t *pmb = NULL;
3596 MAILBOX_t *mb;
3597 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003598 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003599 struct fc_vport_identifiers vport_id;
3600 struct fc_vport *new_fc_vport;
3601 struct Scsi_Host *shost;
3602 struct lpfc_vport *vport;
3603 uint16_t offset = 0;
3604 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003605 struct lpfc_dmabuf *mp;
3606 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003607
3608 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3609 if (!pmb) {
3610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3611 "0542 lpfc_create_static_vport failed to"
3612 " allocate mailbox memory\n");
3613 return;
3614 }
James Smartcdcc2342012-08-14 14:26:35 -04003615 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003616 mb = &pmb->u.mb;
3617
3618 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3619 if (!vport_info) {
3620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3621 "0543 lpfc_create_static_vport failed to"
3622 " allocate vport_info\n");
3623 mempool_free(pmb, phba->mbox_mem_pool);
3624 return;
3625 }
3626
3627 vport_buff = (uint8_t *) vport_info;
3628 do {
James Smartcdcc2342012-08-14 14:26:35 -04003629 /* free dma buffer from previous round */
3630 if (pmb->context1) {
3631 mp = (struct lpfc_dmabuf *)pmb->context1;
3632 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3633 kfree(mp);
3634 }
James Smart1c6834a2009-07-19 10:01:26 -04003635 if (lpfc_dump_static_vport(phba, pmb, offset))
3636 goto out;
3637
James Smart21e9a0a2009-05-22 14:53:21 -04003638 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003639 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3640 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003641
James Smartcdcc2342012-08-14 14:26:35 -04003642 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003643 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3644 "0544 lpfc_create_static_vport failed to"
3645 " issue dump mailbox command ret 0x%x "
3646 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003647 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003648 goto out;
3649 }
3650
James Smart1c6834a2009-07-19 10:01:26 -04003651 if (phba->sli_rev == LPFC_SLI_REV4) {
3652 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003653 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003654 if (byte_count > sizeof(struct static_vport_info) -
3655 offset)
3656 byte_count = sizeof(struct static_vport_info)
3657 - offset;
3658 memcpy(vport_buff + offset, mp->virt, byte_count);
3659 offset += byte_count;
3660 } else {
3661 if (mb->un.varDmp.word_cnt >
3662 sizeof(struct static_vport_info) - offset)
3663 mb->un.varDmp.word_cnt =
3664 sizeof(struct static_vport_info)
3665 - offset;
3666 byte_count = mb->un.varDmp.word_cnt;
3667 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3668 vport_buff + offset,
3669 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003670
James Smart1c6834a2009-07-19 10:01:26 -04003671 offset += byte_count;
3672 }
James Smart21e9a0a2009-05-22 14:53:21 -04003673
James Smart1c6834a2009-07-19 10:01:26 -04003674 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003675 offset < sizeof(struct static_vport_info));
3676
3677
3678 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3679 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3680 != VPORT_INFO_REV)) {
3681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3682 "0545 lpfc_create_static_vport bad"
3683 " information header 0x%x 0x%x\n",
3684 le32_to_cpu(vport_info->signature),
3685 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3686
3687 goto out;
3688 }
3689
3690 shost = lpfc_shost_from_vport(phba->pport);
3691
3692 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3693 memset(&vport_id, 0, sizeof(vport_id));
3694 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3695 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3696 if (!vport_id.port_name || !vport_id.node_name)
3697 continue;
3698
3699 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3700 vport_id.vport_type = FC_PORTTYPE_NPIV;
3701 vport_id.disable = false;
3702 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3703
3704 if (!new_fc_vport) {
3705 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3706 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003707 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003708 continue;
3709 }
3710
3711 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3712 vport->vport_flag |= STATIC_VPORT;
3713 }
3714
3715out:
James Smart21e9a0a2009-05-22 14:53:21 -04003716 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003717 if (mbx_wait_rc != MBX_TIMEOUT) {
3718 if (pmb->context1) {
3719 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003720 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3721 kfree(mp);
3722 }
James Smart21e9a0a2009-05-22 14:53:21 -04003723 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003724 }
James Smart21e9a0a2009-05-22 14:53:21 -04003725
3726 return;
3727}
3728
dea31012005-04-17 16:05:31 -05003729/*
3730 * This routine handles processing a Fabric REG_LOGIN mailbox
3731 * command upon completion. It is setup in the LPFC_MBOXQ
3732 * as the completion routine when the command is
3733 * handed off to the SLI layer.
3734 */
3735void
James Smart2e0fef82007-06-17 19:56:36 -05003736lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003737{
James Smart92d7f7b2007-06-17 19:56:38 -05003738 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003739 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003740 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003741 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003742 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003743
James Smart549e55c2007-08-02 11:09:51 -04003744 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003745 pmb->context1 = NULL;
3746 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003747
dea31012005-04-17 16:05:31 -05003748 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003749 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3750 "0258 Register Fabric login error: 0x%x\n",
3751 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003752 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3753 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003754 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003755
James Smart76a95d72010-11-20 23:11:48 -05003756 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003757 /* FLOGI failed, use loop map to make discovery list */
3758 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003759
James Smart92d7f7b2007-06-17 19:56:38 -05003760 /* Start discovery */
3761 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003762 /* Decrement the reference count to ndlp after the
3763 * reference to the ndlp are done.
3764 */
3765 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003766 return;
3767 }
3768
3769 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003770 /* Decrement the reference count to ndlp after the reference
3771 * to the ndlp are done.
3772 */
3773 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003774 return;
3775 }
3776
James Smart6d368e52011-05-24 11:44:12 -04003777 if (phba->sli_rev < LPFC_SLI_REV4)
3778 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003779 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003780 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003781 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003782
James Smart2e0fef82007-06-17 19:56:36 -05003783 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003784 /* when physical port receive logo donot start
3785 * vport discovery */
3786 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3787 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003788 else {
3789 shost = lpfc_shost_from_vport(vport);
3790 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003791 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003792 spin_unlock_irq(shost->host_lock);
3793 }
James Smart92d7f7b2007-06-17 19:56:38 -05003794 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003795 }
3796
3797 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3798 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003799 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003800
3801 /* Drop the reference count from the mbox at the end after
3802 * all the current reference to the ndlp have been done.
3803 */
3804 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003805 return;
3806}
3807
3808/*
3809 * This routine handles processing a NameServer REG_LOGIN mailbox
3810 * command upon completion. It is setup in the LPFC_MBOXQ
3811 * as the completion routine when the command is
3812 * handed off to the SLI layer.
3813 */
3814void
James Smart2e0fef82007-06-17 19:56:36 -05003815lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003816{
James Smart21e9a0a2009-05-22 14:53:21 -04003817 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003818 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3819 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3820 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003821
James Smartd439d282010-09-29 11:18:45 -04003822 pmb->context1 = NULL;
3823 pmb->context2 = NULL;
3824
dea31012005-04-17 16:05:31 -05003825 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003826out:
James Smart21e9a0a2009-05-22 14:53:21 -04003827 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3828 "0260 Register NameServer error: 0x%x\n",
3829 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003830 /* decrement the node reference count held for this
3831 * callback function.
3832 */
James Smart329f9bc2007-04-25 09:53:01 -04003833 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003834 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3835 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003836 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003837
3838 /* If no other thread is using the ndlp, free it */
3839 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003840
James Smart76a95d72010-11-20 23:11:48 -05003841 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003842 /*
3843 * RegLogin failed, use loop map to make discovery
3844 * list
3845 */
3846 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003847
James Smart92d7f7b2007-06-17 19:56:38 -05003848 /* Start discovery */
3849 lpfc_disc_start(vport);
3850 return;
3851 }
3852 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003853 return;
3854 }
3855
James Smart6d368e52011-05-24 11:44:12 -04003856 if (phba->sli_rev < LPFC_SLI_REV4)
3857 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003858 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003859 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003860 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003861 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3862 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3863 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3864 atomic_read(&ndlp->kref.refcount),
3865 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003866
James Smart2e0fef82007-06-17 19:56:36 -05003867 if (vport->port_state < LPFC_VPORT_READY) {
3868 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003869 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3870 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3871 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3872 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarte26422f2015-05-21 13:55:16 -04003873 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0);
James Smart92d7f7b2007-06-17 19:56:38 -05003874
3875 /* Issue SCR just before NameServer GID_FT Query */
3876 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003877 }
3878
James Smart2e0fef82007-06-17 19:56:36 -05003879 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003880 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003881 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003882 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003883 goto out;
dea31012005-04-17 16:05:31 -05003884 }
3885
James Smartfa4066b2008-01-11 01:53:27 -05003886 /* decrement the node reference count held for this
3887 * callback function.
3888 */
James Smart329f9bc2007-04-25 09:53:01 -04003889 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003890 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3891 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003892 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003893
3894 return;
3895}
3896
3897static void
James Smart2e0fef82007-06-17 19:56:36 -05003898lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003899{
James Smart2e0fef82007-06-17 19:56:36 -05003900 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3901 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003902 struct lpfc_rport_data *rdata;
3903 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003904 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003905
3906 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003907 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3908 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003909 rport_ids.port_id = ndlp->nlp_DID;
3910 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003911
James Smart329f9bc2007-04-25 09:53:01 -04003912 /*
3913 * We leave our node pointer in rport->dd_data when we unregister a
3914 * FCP target port. But fc_remote_port_add zeros the space to which
3915 * rport->dd_data points. So, if we're reusing a previously
3916 * registered port, drop the reference that we took the last time we
3917 * registered the port.
3918 */
James Smart466e8402015-05-21 13:55:28 -04003919 rport = ndlp->rport;
3920 if (rport) {
3921 rdata = rport->dd_data;
3922 /* break the link before dropping the ref */
3923 ndlp->rport = NULL;
3924 if (rdata && rdata->pnode == ndlp)
3925 lpfc_nlp_put(ndlp);
3926 rdata->pnode = NULL;
3927 /* drop reference for earlier registeration */
3928 put_device(&rport->dev);
3929 }
James Smart858c9f62007-06-17 19:56:39 -05003930
3931 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3932 "rport add: did:x%x flg:x%x type x%x",
3933 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3934
James Smart9589b0622011-04-16 11:03:17 -04003935 /* Don't add the remote port if unloading. */
3936 if (vport->load_flag & FC_UNLOADING)
3937 return;
3938
James Smart2e0fef82007-06-17 19:56:36 -05003939 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003940 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003941 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3942 "Warning: fc_remote_port_add failed\n");
3943 return;
3944 }
3945
3946 /* initialize static port data */
3947 rport->maxframe_size = ndlp->nlp_maxframe;
3948 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003949 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003950 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003951
3952 if (ndlp->nlp_type & NLP_FCP_TARGET)
3953 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3954 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3955 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3956
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003957 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3958 fc_remote_port_rolechg(rport, rport_ids.roles);
3959
James Smart34f5ad82012-08-03 12:35:03 -04003960 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3961 "3183 rport register x%06x, rport %p role x%x\n",
3962 ndlp->nlp_DID, rport, rport_ids.roles);
3963
James Smart071fbd3d2006-04-15 11:53:20 -04003964 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003965 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003966 ndlp->nlp_sid = rport->scsi_target_id;
3967 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003968 return;
3969}
3970
3971static void
James Smart2e0fef82007-06-17 19:56:36 -05003972lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003973{
3974 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003975
James Smart858c9f62007-06-17 19:56:39 -05003976 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3977 "rport delete: did:x%x flg:x%x type x%x",
3978 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3979
James Smart34f5ad82012-08-03 12:35:03 -04003980 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3981 "3184 rport unregister x%06x, rport %p\n",
3982 ndlp->nlp_DID, rport);
3983
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003984 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003985
3986 return;
3987}
3988
James Smartde0c5b32007-04-25 09:52:27 -04003989static void
James Smart2e0fef82007-06-17 19:56:36 -05003990lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003991{
James Smart2e0fef82007-06-17 19:56:36 -05003992 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3993
3994 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003995 switch (state) {
3996 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003997 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003998 break;
3999 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004000 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004001 break;
4002 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004003 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004004 break;
4005 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004006 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004007 break;
4008 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004009 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004010 break;
4011 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004012 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004013 break;
4014 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004015 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004016 break;
4017 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004018 if (vport->fc_npr_cnt == 0 && count == -1)
4019 vport->fc_npr_cnt = 0;
4020 else
4021 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004022 break;
4023 }
James Smart2e0fef82007-06-17 19:56:36 -05004024 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004025}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004026
James Smartde0c5b32007-04-25 09:52:27 -04004027static void
James Smart2e0fef82007-06-17 19:56:36 -05004028lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004029 int old_state, int new_state)
4030{
James Smart2e0fef82007-06-17 19:56:36 -05004031 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4032
James Smartde0c5b32007-04-25 09:52:27 -04004033 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004034 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4035 ndlp->nlp_type |= NLP_FC_NODE;
4036 }
4037 if (new_state == NLP_STE_MAPPED_NODE)
4038 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4039 if (new_state == NLP_STE_NPR_NODE)
4040 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004041
James Smartde0c5b32007-04-25 09:52:27 -04004042 /* Transport interface */
4043 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
4044 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004045 vport->phba->nport_event_cnt++;
4046 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004047 }
dea31012005-04-17 16:05:31 -05004048
James Smartde0c5b32007-04-25 09:52:27 -04004049 if (new_state == NLP_STE_MAPPED_NODE ||
4050 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05004051 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05004052 /*
4053 * Tell the fc transport about the port, if we haven't
4054 * already. If we have, and it's a scsi entity, be
4055 * sure to unblock any attached scsi devices
4056 */
4057 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004058 }
James Smartea2151b2008-09-07 11:52:10 -04004059 if ((new_state == NLP_STE_MAPPED_NODE) &&
4060 (vport->stat_data_enabled)) {
4061 /*
4062 * A new target is discovered, if there is no buffer for
4063 * statistical data collection allocate buffer.
4064 */
4065 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4066 sizeof(struct lpfc_scsicmd_bkt),
4067 GFP_KERNEL);
4068
4069 if (!ndlp->lat_data)
4070 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4071 "0286 lpfc_nlp_state_cleanup failed to "
4072 "allocate statistical data buffer DID "
4073 "0x%x\n", ndlp->nlp_DID);
4074 }
James Smart858c9f62007-06-17 19:56:39 -05004075 /*
4076 * if we added to Mapped list, but the remote port
4077 * registration failed or assigned a target id outside
4078 * our presentable range - move the node to the
4079 * Unmapped List
4080 */
James Smartde0c5b32007-04-25 09:52:27 -04004081 if (new_state == NLP_STE_MAPPED_NODE &&
4082 (!ndlp->rport ||
4083 ndlp->rport->scsi_target_id == -1 ||
4084 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004085 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004086 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004087 spin_unlock_irq(shost->host_lock);
4088 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004089 }
James Smartde0c5b32007-04-25 09:52:27 -04004090}
4091
James Smart685f0bf2007-04-25 09:53:08 -04004092static char *
4093lpfc_nlp_state_name(char *buffer, size_t size, int state)
4094{
4095 static char *states[] = {
4096 [NLP_STE_UNUSED_NODE] = "UNUSED",
4097 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4098 [NLP_STE_ADISC_ISSUE] = "ADISC",
4099 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4100 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004101 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004102 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4103 [NLP_STE_MAPPED_NODE] = "MAPPED",
4104 [NLP_STE_NPR_NODE] = "NPR",
4105 };
4106
James Smart311464e2007-08-02 11:10:37 -04004107 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004108 strlcpy(buffer, states[state], size);
4109 else
4110 snprintf(buffer, size, "unknown (%d)", state);
4111 return buffer;
4112}
4113
James Smartde0c5b32007-04-25 09:52:27 -04004114void
James Smart2e0fef82007-06-17 19:56:36 -05004115lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4116 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004117{
James Smart2e0fef82007-06-17 19:56:36 -05004118 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004119 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004120 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004121
James Smarte8b62012007-08-02 11:10:09 -04004122 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4123 "0904 NPort state transition x%06x, %s -> %s\n",
4124 ndlp->nlp_DID,
4125 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4126 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004127
4128 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4129 "node statechg did:x%x old:%d ste:%d",
4130 ndlp->nlp_DID, old_state, state);
4131
James Smartde0c5b32007-04-25 09:52:27 -04004132 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004133 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004134 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004135 if (old_state == NLP_STE_UNMAPPED_NODE) {
4136 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4137 ndlp->nlp_type &= ~NLP_FC_NODE;
4138 }
4139
James Smart685f0bf2007-04-25 09:53:08 -04004140 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004141 spin_lock_irq(shost->host_lock);
4142 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4143 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004144 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004145 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004146
4147 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004148 lpfc_nlp_counters(vport, state, 1);
4149 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004150}
4151
4152void
James Smarte47c9092008-02-08 18:49:26 -05004153lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4154{
4155 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4156
4157 if (list_empty(&ndlp->nlp_listp)) {
4158 spin_lock_irq(shost->host_lock);
4159 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4160 spin_unlock_irq(shost->host_lock);
4161 }
4162}
4163
4164void
James Smart2e0fef82007-06-17 19:56:36 -05004165lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004166{
James Smart2e0fef82007-06-17 19:56:36 -05004167 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4168
James Smart0d2b6b82008-06-14 22:52:47 -04004169 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004170 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004171 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4172 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004173 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004174 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004175 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004176 NLP_STE_UNUSED_NODE);
4177}
4178
Adrian Bunk4d9db012008-02-14 23:24:02 +02004179static void
James Smarte47c9092008-02-08 18:49:26 -05004180lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4181{
James Smart0d2b6b82008-06-14 22:52:47 -04004182 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004183 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4184 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4185 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4186 NLP_STE_UNUSED_NODE);
4187}
James Smart109f6ed2008-12-04 22:39:08 -05004188/**
James Smart3621a712009-04-06 18:47:14 -04004189 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004190 * @vport: Pointer to Virtual Port object.
4191 * @ndlp: Pointer to FC node object.
4192 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004193 *
4194 * This function is always called when node object need to be initialized.
4195 * It initializes all the fields of the node object. Although the reference
4196 * to phba from @ndlp can be obtained indirectly through it's reference to
4197 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4198 * to the life-span of the @ndlp might go beyond the existence of @vport as
4199 * the final release of ndlp is determined by its reference count. And, the
4200 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004201 **/
4202static inline void
4203lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4204 uint32_t did)
4205{
4206 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4207 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4208 init_timer(&ndlp->nlp_delayfunc);
4209 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4210 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4211 ndlp->nlp_DID = did;
4212 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004213 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004214 ndlp->nlp_sid = NLP_NO_SID;
4215 kref_init(&ndlp->kref);
4216 NLP_INT_NODE_ACT(ndlp);
4217 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004218 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004219}
James Smarte47c9092008-02-08 18:49:26 -05004220
4221struct lpfc_nodelist *
4222lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4223 int state)
4224{
4225 struct lpfc_hba *phba = vport->phba;
4226 uint32_t did;
4227 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004228 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004229
4230 if (!ndlp)
4231 return NULL;
4232
4233 spin_lock_irqsave(&phba->ndlp_lock, flags);
4234 /* The ndlp should not be in memory free mode */
4235 if (NLP_CHK_FREE_REQ(ndlp)) {
4236 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4237 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4238 "0277 lpfc_enable_node: ndlp:x%p "
4239 "usgmap:x%x refcnt:%d\n",
4240 (void *)ndlp, ndlp->nlp_usg_map,
4241 atomic_read(&ndlp->kref.refcount));
4242 return NULL;
4243 }
4244 /* The ndlp should not already be in active mode */
4245 if (NLP_CHK_NODE_ACT(ndlp)) {
4246 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4247 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4248 "0278 lpfc_enable_node: ndlp:x%p "
4249 "usgmap:x%x refcnt:%d\n",
4250 (void *)ndlp, ndlp->nlp_usg_map,
4251 atomic_read(&ndlp->kref.refcount));
4252 return NULL;
4253 }
4254
4255 /* Keep the original DID */
4256 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004257 if (phba->sli_rev == LPFC_SLI_REV4)
4258 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004259
4260 /* re-initialize ndlp except of ndlp linked list pointer */
4261 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4262 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004263 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004264
James Smartcff261f2013-12-17 20:29:47 -05004265 if (phba->sli_rev == LPFC_SLI_REV4)
4266 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4267
James Smarte47c9092008-02-08 18:49:26 -05004268 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004269 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004270 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004271 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4272 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4273 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4274 ndlp->nlp_flag,
4275 atomic_read(&ndlp->kref.refcount),
4276 ndlp->nlp_usg_map, ndlp);
4277 }
James Smart725dd392013-10-10 12:23:30 -04004278
James Smarte47c9092008-02-08 18:49:26 -05004279
4280 if (state != NLP_STE_UNUSED_NODE)
4281 lpfc_nlp_set_state(vport, ndlp, state);
4282
4283 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4284 "node enable: did:x%x",
4285 ndlp->nlp_DID, 0, 0);
4286 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004287}
4288
4289void
James Smart2e0fef82007-06-17 19:56:36 -05004290lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004291{
James Smart87af33f2007-10-27 13:37:43 -04004292 /*
James Smartfa4066b2008-01-11 01:53:27 -05004293 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004294 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004295 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4296 * until ALL other outstanding threads have completed. We check
4297 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004298 */
James Smartfa4066b2008-01-11 01:53:27 -05004299 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4300 return;
James Smart51ef4c22007-08-02 11:10:31 -04004301 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004302 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004303 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004304 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004305 }
James Smart02902172015-05-21 13:55:17 -04004306
4307 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004308 return;
dea31012005-04-17 16:05:31 -05004309}
4310
4311/*
4312 * Start / ReStart rescue timer for Discovery / RSCN handling
4313 */
4314void
James Smart2e0fef82007-06-17 19:56:36 -05004315lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004316{
James Smart2e0fef82007-06-17 19:56:36 -05004317 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4318 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004319 uint32_t tmo;
4320
James Smart2e0fef82007-06-17 19:56:36 -05004321 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004322 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004323 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4324 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004325 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004326 * FC spec states we need 3 * ratov for CT requests
4327 */
4328 tmo = ((phba->fc_ratov * 3) + 3);
4329 }
dea31012005-04-17 16:05:31 -05004330
James Smart858c9f62007-06-17 19:56:39 -05004331
4332 if (!timer_pending(&vport->fc_disctmo)) {
4333 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4334 "set disc timer: tmo:x%x state:x%x flg:x%x",
4335 tmo, vport->port_state, vport->fc_flag);
4336 }
4337
James Smart256ec0d2013-04-17 20:14:58 -04004338 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004339 spin_lock_irq(shost->host_lock);
4340 vport->fc_flag |= FC_DISC_TMO;
4341 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004342
4343 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004344 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4345 "0247 Start Discovery Timer state x%x "
4346 "Data: x%x x%lx x%x x%x\n",
4347 vport->port_state, tmo,
4348 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4349 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004350
4351 return;
4352}
4353
4354/*
4355 * Cancel rescue timer for Discovery / RSCN handling
4356 */
4357int
James Smart2e0fef82007-06-17 19:56:36 -05004358lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004359{
James Smart2e0fef82007-06-17 19:56:36 -05004360 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004361 unsigned long iflags;
4362
James Smart858c9f62007-06-17 19:56:39 -05004363 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4364 "can disc timer: state:x%x rtry:x%x flg:x%x",
4365 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4366
dea31012005-04-17 16:05:31 -05004367 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004368 if (vport->fc_flag & FC_DISC_TMO) {
4369 spin_lock_irqsave(shost->host_lock, iflags);
4370 vport->fc_flag &= ~FC_DISC_TMO;
4371 spin_unlock_irqrestore(shost->host_lock, iflags);
4372 del_timer_sync(&vport->fc_disctmo);
4373 spin_lock_irqsave(&vport->work_port_lock, iflags);
4374 vport->work_port_events &= ~WORKER_DISC_TMO;
4375 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004376 }
4377
4378 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004379 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4380 "0248 Cancel Discovery Timer state x%x "
4381 "Data: x%x x%x x%x\n",
4382 vport->port_state, vport->fc_flag,
4383 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004384 return 0;
dea31012005-04-17 16:05:31 -05004385}
4386
4387/*
4388 * Check specified ring for outstanding IOCB on the SLI queue
4389 * Return true if iocb matches the specified nport
4390 */
4391int
James Smart2e0fef82007-06-17 19:56:36 -05004392lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4393 struct lpfc_sli_ring *pring,
4394 struct lpfc_iocbq *iocb,
4395 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004396{
James Smart2e0fef82007-06-17 19:56:36 -05004397 struct lpfc_sli *psli = &phba->sli;
4398 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004399 struct lpfc_vport *vport = ndlp->vport;
4400
4401 if (iocb->vport != vport)
4402 return 0;
4403
dea31012005-04-17 16:05:31 -05004404 if (pring->ringno == LPFC_ELS_RING) {
4405 switch (icmd->ulpCommand) {
4406 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004407 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004408 return 1;
dea31012005-04-17 16:05:31 -05004409 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004410 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4411 return 1;
dea31012005-04-17 16:05:31 -05004412 case CMD_XMIT_ELS_RSP64_CX:
4413 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004414 return 1;
dea31012005-04-17 16:05:31 -05004415 }
James Smarta4bc3372006-12-02 13:34:16 -05004416 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004417
4418 } else if (pring->ringno == psli->fcp_ring) {
4419 /* Skip match check if waiting to relogin to FCP target */
4420 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004421 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004422 return 0;
dea31012005-04-17 16:05:31 -05004423 }
4424 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004425 return 1;
dea31012005-04-17 16:05:31 -05004426 }
4427 } else if (pring->ringno == psli->next_ring) {
4428
4429 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004430 return 0;
dea31012005-04-17 16:05:31 -05004431}
4432
4433/*
4434 * Free resources / clean up outstanding I/Os
4435 * associated with nlp_rpi in the LPFC_NODELIST entry.
4436 */
4437static int
James Smart2e0fef82007-06-17 19:56:36 -05004438lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004439{
James Smart2534ba72007-04-25 09:52:20 -04004440 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004441 struct lpfc_sli *psli;
4442 struct lpfc_sli_ring *pring;
4443 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004444 uint32_t i;
dea31012005-04-17 16:05:31 -05004445
James Smart92d7f7b2007-06-17 19:56:38 -05004446 lpfc_fabric_abort_nport(ndlp);
4447
dea31012005-04-17 16:05:31 -05004448 /*
4449 * Everything that matches on txcmplq will be returned
4450 * by firmware with a no rpi error.
4451 */
4452 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004453 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004454 /* Now process each ring */
4455 for (i = 0; i < psli->num_rings; i++) {
4456 pring = &psli->ring[i];
4457
James Smart2e0fef82007-06-17 19:56:36 -05004458 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004459 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004460 list) {
dea31012005-04-17 16:05:31 -05004461 /*
4462 * Check to see if iocb matches the nport we are
4463 * looking for
4464 */
James Smart92d7f7b2007-06-17 19:56:38 -05004465 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4466 ndlp))) {
dea31012005-04-17 16:05:31 -05004467 /* It matches, so deque and call compl
4468 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004469 list_move_tail(&iocb->list,
4470 &completions);
dea31012005-04-17 16:05:31 -05004471 }
4472 }
James Smart2e0fef82007-06-17 19:56:36 -05004473 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004474 }
4475 }
James Smart2534ba72007-04-25 09:52:20 -04004476
James Smarta257bf92009-04-06 18:48:10 -04004477 /* Cancel all the IOCBs from the completions list */
4478 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4479 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004480
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004481 return 0;
dea31012005-04-17 16:05:31 -05004482}
4483
James Smart086a3452012-08-14 14:25:21 -04004484/**
4485 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4486 * @phba: Pointer to HBA context object.
4487 * @pmb: Pointer to mailbox object.
4488 *
4489 * This function will issue an ELS LOGO command after completing
4490 * the UNREG_RPI.
4491 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004492static void
James Smart086a3452012-08-14 14:25:21 -04004493lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4494{
4495 struct lpfc_vport *vport = pmb->vport;
4496 struct lpfc_nodelist *ndlp;
4497
4498 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4499 if (!ndlp)
4500 return;
4501 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004502 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004503}
4504
dea31012005-04-17 16:05:31 -05004505/*
4506 * Free rpi associated with LPFC_NODELIST entry.
4507 * This routine is called from lpfc_freenode(), when we are removing
4508 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4509 * LOGO that completes successfully, and we are waiting to PLOGI back
4510 * to the remote NPort. In addition, it is called after we receive
4511 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4512 * we are waiting to PLOGI back to the remote NPort.
4513 */
4514int
James Smart2e0fef82007-06-17 19:56:36 -05004515lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004516{
James Smart2e0fef82007-06-17 19:56:36 -05004517 struct lpfc_hba *phba = vport->phba;
4518 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004519 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004520 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004521
James Smartf454a9a2013-09-06 12:20:07 -04004522 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4523 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4524 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4525 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4526 "3366 RPI x%x needs to be "
4527 "unregistered nlp_flag x%x "
4528 "did x%x\n",
4529 ndlp->nlp_rpi, ndlp->nlp_flag,
4530 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004531 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4532 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004533 /* SLI4 ports require the physical rpi value. */
4534 rpi = ndlp->nlp_rpi;
4535 if (phba->sli_rev == LPFC_SLI_REV4)
4536 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004537
James Smart6d368e52011-05-24 11:44:12 -04004538 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004539 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004540 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4541 mbox->context1 = ndlp;
4542 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4543 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004544 if (phba->sli_rev == LPFC_SLI_REV4 &&
4545 (!(vport->load_flag & FC_UNLOADING)) &&
4546 (bf_get(lpfc_sli_intf_if_type,
4547 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004548 LPFC_SLI_INTF_IF_TYPE_2) &&
4549 (atomic_read(&ndlp->kref.refcount) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004550 mbox->context1 = lpfc_nlp_get(ndlp);
4551 mbox->mbox_cmpl =
4552 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004553 /*
4554 * accept PLOGIs after unreg_rpi_cmpl
4555 */
4556 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004557 } else
4558 mbox->mbox_cmpl =
4559 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004560 }
4561
James Smart0b727fe2007-10-27 13:37:25 -04004562 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004563 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004564 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004565 acc_plogi = 1;
4566 }
dea31012005-04-17 16:05:31 -05004567 }
dea31012005-04-17 16:05:31 -05004568 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004569
James Smart40426292010-12-15 17:58:10 -05004570 if (phba->sli_rev != LPFC_SLI_REV4)
4571 ndlp->nlp_rpi = 0;
4572 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004573 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004574 if (acc_plogi)
4575 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004576 return 1;
4577 }
James Smart7c5e5182015-05-22 10:42:43 -04004578 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004579 return 0;
4580}
4581
James Smartecfd03c2010-02-12 14:41:27 -05004582/**
4583 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4584 * @phba: pointer to lpfc hba data structure.
4585 *
4586 * This routine is invoked to unregister all the currently registered RPIs
4587 * to the HBA.
4588 **/
4589void
4590lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4591{
4592 struct lpfc_vport **vports;
4593 struct lpfc_nodelist *ndlp;
4594 struct Scsi_Host *shost;
4595 int i;
4596
4597 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004598 if (!vports) {
4599 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4600 "2884 Vport array allocation failed \n");
4601 return;
4602 }
James Smartecfd03c2010-02-12 14:41:27 -05004603 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4604 shost = lpfc_shost_from_vport(vports[i]);
4605 spin_lock_irq(shost->host_lock);
4606 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004607 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004608 /* The mempool_alloc might sleep */
4609 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004610 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004611 spin_lock_irq(shost->host_lock);
4612 }
James Smartecfd03c2010-02-12 14:41:27 -05004613 }
4614 spin_unlock_irq(shost->host_lock);
4615 }
4616 lpfc_destroy_vport_work_array(phba, vports);
4617}
4618
James Smart92d7f7b2007-06-17 19:56:38 -05004619void
4620lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4621{
4622 struct lpfc_hba *phba = vport->phba;
4623 LPFC_MBOXQ_t *mbox;
4624 int rc;
4625
James Smart5af5eee2010-10-22 11:06:38 -04004626 if (phba->sli_rev == LPFC_SLI_REV4) {
4627 lpfc_sli4_unreg_all_rpis(vport);
4628 return;
4629 }
4630
James Smart92d7f7b2007-06-17 19:56:38 -05004631 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4632 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004633 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4634 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004635 mbox->vport = vport;
4636 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004637 mbox->context1 = NULL;
4638 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004639 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004640 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004641
4642 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4643 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4644 "1836 Could not issue "
4645 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004646 }
4647}
4648
4649void
4650lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4651{
4652 struct lpfc_hba *phba = vport->phba;
4653 LPFC_MBOXQ_t *mbox;
4654 int rc;
4655
4656 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4657 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004658 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4659 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004660 mbox->vport = vport;
4661 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004662 mbox->context1 = NULL;
4663 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004664 if (rc != MBX_TIMEOUT)
4665 mempool_free(mbox, phba->mbox_mem_pool);
4666
4667 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004668 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4669 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004670 "unreg_did (default rpis) status %d\n",
4671 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004672 }
4673}
4674
dea31012005-04-17 16:05:31 -05004675/*
4676 * Free resources associated with LPFC_NODELIST entry
4677 * so it can be freed.
4678 */
4679static int
James Smart2e0fef82007-06-17 19:56:36 -05004680lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004681{
James Smart2e0fef82007-06-17 19:56:36 -05004682 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4683 struct lpfc_hba *phba = vport->phba;
4684 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004685 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004686
4687 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004688 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4689 "0900 Cleanup node for NPort x%x "
4690 "Data: x%x x%x x%x\n",
4691 ndlp->nlp_DID, ndlp->nlp_flag,
4692 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004693 if (NLP_CHK_FREE_REQ(ndlp)) {
4694 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4695 "0280 lpfc_cleanup_node: ndlp:x%p "
4696 "usgmap:x%x refcnt:%d\n",
4697 (void *)ndlp, ndlp->nlp_usg_map,
4698 atomic_read(&ndlp->kref.refcount));
4699 lpfc_dequeue_node(vport, ndlp);
4700 } else {
4701 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4702 "0281 lpfc_cleanup_node: ndlp:x%p "
4703 "usgmap:x%x refcnt:%d\n",
4704 (void *)ndlp, ndlp->nlp_usg_map,
4705 atomic_read(&ndlp->kref.refcount));
4706 lpfc_disable_node(vport, ndlp);
4707 }
dea31012005-04-17 16:05:31 -05004708
James Smart086a3452012-08-14 14:25:21 -04004709
4710 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4711
dea31012005-04-17 16:05:31 -05004712 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4713 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004714 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004715 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004716 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4717 mb->context2 = NULL;
4718 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4719 }
4720 }
James Smart33ccf8d2006-08-17 11:57:58 -04004721
James Smart2e0fef82007-06-17 19:56:36 -05004722 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004723 /* Cleanup REG_LOGIN completions which are not yet processed */
4724 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4725 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004726 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004727 (ndlp != (struct lpfc_nodelist *) mb->context2))
4728 continue;
4729
4730 mb->context2 = NULL;
4731 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4732 }
4733
dea31012005-04-17 16:05:31 -05004734 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004735 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004736 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004737 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004738 mp = (struct lpfc_dmabuf *) (mb->context1);
4739 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004740 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004741 kfree(mp);
4742 }
4743 list_del(&mb->list);
4744 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004745 /* We shall not invoke the lpfc_nlp_put to decrement
4746 * the ndlp reference count as we are in the process
4747 * of lpfc_nlp_release.
4748 */
dea31012005-04-17 16:05:31 -05004749 }
4750 }
James Smart2e0fef82007-06-17 19:56:36 -05004751 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004752
James Smarte47c9092008-02-08 18:49:26 -05004753 lpfc_els_abort(phba, ndlp);
4754
James Smart2e0fef82007-06-17 19:56:36 -05004755 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004756 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004757 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004758
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004759 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004760 del_timer_sync(&ndlp->nlp_delayfunc);
4761
James Smart0d2b6b82008-06-14 22:52:47 -04004762 list_del_init(&ndlp->els_retry_evt.evt_listp);
4763 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004764 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004765 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004766
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004767 return 0;
dea31012005-04-17 16:05:31 -05004768}
4769
4770/*
4771 * Check to see if we can free the nlp back to the freelist.
4772 * If we are in the middle of using the nlp in the discovery state
4773 * machine, defer the free till we reach the end of the state machine.
4774 */
James Smart329f9bc2007-04-25 09:53:01 -04004775static void
James Smart2e0fef82007-06-17 19:56:36 -05004776lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004777{
James Smarta8adb832007-10-27 13:37:53 -04004778 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004779 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004780 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004781 LPFC_MBOXQ_t *mbox;
4782 int rc;
dea31012005-04-17 16:05:31 -05004783
James Smart0d2b6b82008-06-14 22:52:47 -04004784 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004785 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004786 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4787 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004788 /* For this case we need to cleanup the default rpi
4789 * allocated by the firmware.
4790 */
James Smartbe6bb942015-04-07 15:07:22 -04004791 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4792 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4793 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4794 atomic_read(&ndlp->kref.refcount),
4795 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004796 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4797 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004798 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004799 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004800 if (rc) {
4801 mempool_free(mbox, phba->mbox_mem_pool);
4802 }
4803 else {
4804 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4805 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4806 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004807 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004808 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4809 if (rc == MBX_NOT_FINISHED) {
4810 mempool_free(mbox, phba->mbox_mem_pool);
4811 }
4812 }
4813 }
4814 }
James Smart2e0fef82007-06-17 19:56:36 -05004815 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004816
James Smart2e0fef82007-06-17 19:56:36 -05004817 /*
James Smart466e8402015-05-21 13:55:28 -04004818 * ndlp->rport must be set to NULL before it reaches here
4819 * i.e. break rport/node link before doing lpfc_nlp_put for
4820 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05004821 */
James Smart92d7f7b2007-06-17 19:56:38 -05004822 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04004823 /*
4824 * extra lpfc_nlp_put dropped the reference of ndlp
4825 * for registered rport so need to cleanup rport
4826 */
4827 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4828 "0940 removed node x%p DID x%x "
4829 " rport not null %p\n",
4830 ndlp, ndlp->nlp_DID, ndlp->rport);
4831 rport = ndlp->rport;
4832 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004833 rdata->pnode = NULL;
4834 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04004835 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05004836 }
dea31012005-04-17 16:05:31 -05004837}
4838
4839static int
James Smart2e0fef82007-06-17 19:56:36 -05004840lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4841 uint32_t did)
dea31012005-04-17 16:05:31 -05004842{
James Smart2e0fef82007-06-17 19:56:36 -05004843 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004844
4845 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004846 return 0;
dea31012005-04-17 16:05:31 -05004847
dea31012005-04-17 16:05:31 -05004848 /* First check for Direct match */
4849 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004850 return 1;
dea31012005-04-17 16:05:31 -05004851
4852 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004853 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004854 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004855 return 0;
dea31012005-04-17 16:05:31 -05004856 }
4857
4858 matchdid.un.word = did;
4859 ndlpdid.un.word = ndlp->nlp_DID;
4860 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4861 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4862 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04004863 /* This code is supposed to match the ID
4864 * for a private loop device that is
4865 * connect to fl_port. But we need to
4866 * check that the port did not just go
4867 * from pt2pt to fabric or we could end
4868 * up matching ndlp->nlp_DID 000001 to
4869 * fabric DID 0x20101
4870 */
dea31012005-04-17 16:05:31 -05004871 if ((ndlpdid.un.b.domain == 0) &&
4872 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04004873 if (ndlpdid.un.b.id &&
4874 vport->phba->fc_topology ==
4875 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004876 return 1;
dea31012005-04-17 16:05:31 -05004877 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004878 return 0;
dea31012005-04-17 16:05:31 -05004879 }
4880
4881 matchdid.un.word = ndlp->nlp_DID;
4882 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4883 (mydid.un.b.area == ndlpdid.un.b.area)) {
4884 if ((matchdid.un.b.domain == 0) &&
4885 (matchdid.un.b.area == 0)) {
4886 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004887 return 1;
dea31012005-04-17 16:05:31 -05004888 }
4889 }
4890 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004891 return 0;
dea31012005-04-17 16:05:31 -05004892}
4893
James Smart685f0bf2007-04-25 09:53:08 -04004894/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004895static struct lpfc_nodelist *
4896__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004897{
James Smart2fb9bd82006-12-02 13:33:57 -05004898 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004899 uint32_t data1;
4900
James Smart2e0fef82007-06-17 19:56:36 -05004901 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4902 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004903 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4904 ((uint32_t) ndlp->nlp_xri << 16) |
4905 ((uint32_t) ndlp->nlp_type << 8) |
4906 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004907 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4908 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05004909 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04004910 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05004911 ndlp->nlp_flag, data1,
4912 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04004913 return ndlp;
dea31012005-04-17 16:05:31 -05004914 }
4915 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004916
dea31012005-04-17 16:05:31 -05004917 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004918 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4919 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004920 return NULL;
4921}
4922
4923struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004924lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004925{
James Smart2e0fef82007-06-17 19:56:36 -05004926 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004927 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004928 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004929
James Smartfedd3b72011-02-16 12:39:24 -05004930 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004931 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004932 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004933 return ndlp;
4934}
4935
4936struct lpfc_nodelist *
4937lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4938{
4939 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4940 struct lpfc_nodelist *ndlp;
4941
4942 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004943 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004944 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4945 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004946 return NULL;
4947 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004948 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004949 if (!ndlp)
4950 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004951 lpfc_nlp_init(vport, ndlp, did);
4952 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4953 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004954 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004955 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004956 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004957 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4958 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4959 if (!ndlp)
4960 return NULL;
4961 spin_lock_irq(shost->host_lock);
4962 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4963 spin_unlock_irq(shost->host_lock);
4964 return ndlp;
dea31012005-04-17 16:05:31 -05004965 }
James Smarte47c9092008-02-08 18:49:26 -05004966
James Smart58da1ff2008-04-07 10:15:56 -04004967 if ((vport->fc_flag & FC_RSCN_MODE) &&
4968 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004969 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004970 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004971 * we don't need to try to discover it again.
4972 */
4973 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4974 return NULL;
4975
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004976 /* Since this node is marked for discovery,
4977 * delay timeout is not needed.
4978 */
James Smart0d2b6b82008-06-14 22:52:47 -04004979 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004980 spin_lock_irq(shost->host_lock);
4981 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4982 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004983 } else
dea31012005-04-17 16:05:31 -05004984 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004985 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004986 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004987 * or we are already in the process of discovery on it,
4988 * we don't need to try to discover it again.
4989 */
James Smart685f0bf2007-04-25 09:53:08 -04004990 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004991 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4992 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004993 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004994 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4995 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004996 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004997 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004998 }
4999 return ndlp;
5000}
5001
5002/* Build a list of nodes to discover based on the loopmap */
5003void
James Smart2e0fef82007-06-17 19:56:36 -05005004lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005005{
James Smart2e0fef82007-06-17 19:56:36 -05005006 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005007 int j;
5008 uint32_t alpa, index;
5009
James Smart2e0fef82007-06-17 19:56:36 -05005010 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005011 return;
James Smart2e0fef82007-06-17 19:56:36 -05005012
James Smart76a95d72010-11-20 23:11:48 -05005013 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005014 return;
dea31012005-04-17 16:05:31 -05005015
5016 /* Check for loop map present or not */
5017 if (phba->alpa_map[0]) {
5018 for (j = 1; j <= phba->alpa_map[0]; j++) {
5019 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005020 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005021 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005022 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005023 }
5024 } else {
5025 /* No alpamap, so try all alpa's */
5026 for (j = 0; j < FC_MAXLOOP; j++) {
5027 /* If cfg_scan_down is set, start from highest
5028 * ALPA (0xef) to lowest (0x1).
5029 */
James Smart3de2a652007-08-02 11:09:59 -04005030 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005031 index = j;
5032 else
5033 index = FC_MAXLOOP - j - 1;
5034 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005035 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005036 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005037 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005038 }
5039 }
5040 return;
5041}
5042
dea31012005-04-17 16:05:31 -05005043void
James Smart2e0fef82007-06-17 19:56:36 -05005044lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005045{
dea31012005-04-17 16:05:31 -05005046 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005047 struct lpfc_sli *psli = &phba->sli;
5048 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
5049 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
5050 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
5051 int rc;
5052
James Smart92d7f7b2007-06-17 19:56:38 -05005053 /*
5054 * if it's not a physical port or if we already send
5055 * clear_la then don't send it.
5056 */
5057 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005058 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5059 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005060 return;
5061
James Smart2e0fef82007-06-17 19:56:36 -05005062 /* Link up discovery */
5063 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5064 phba->link_state = LPFC_CLEAR_LA;
5065 lpfc_clear_la(phba, mbox);
5066 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5067 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005068 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005069 if (rc == MBX_NOT_FINISHED) {
5070 mempool_free(mbox, phba->mbox_mem_pool);
5071 lpfc_disc_flush_list(vport);
5072 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5073 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5074 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005075 phba->link_state = LPFC_HBA_ERROR;
5076 }
5077 }
5078}
5079
5080/* Reg_vpi to tell firmware to resume normal operations */
5081void
5082lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5083{
5084 LPFC_MBOXQ_t *regvpimbox;
5085
5086 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5087 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005088 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005089 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5090 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005091 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005092 == MBX_NOT_FINISHED) {
5093 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005094 }
5095 }
5096}
5097
5098/* Start Link up / RSCN discovery on NPR nodes */
5099void
5100lpfc_disc_start(struct lpfc_vport *vport)
5101{
5102 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5103 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005104 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005105 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005106
James Smarte74c03c2013-04-17 20:15:19 -04005107 if (!lpfc_is_link_up(phba)) {
5108 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5109 "3315 Link is not up %x\n",
5110 phba->link_state);
dea31012005-04-17 16:05:31 -05005111 return;
James Smarte74c03c2013-04-17 20:15:19 -04005112 }
James Smart2e0fef82007-06-17 19:56:36 -05005113
5114 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005115 clear_la_pending = 1;
5116 else
5117 clear_la_pending = 0;
5118
James Smart2e0fef82007-06-17 19:56:36 -05005119 if (vport->port_state < LPFC_VPORT_READY)
5120 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005121
James Smart2e0fef82007-06-17 19:56:36 -05005122 lpfc_set_disctmo(vport);
5123
James Smart2e0fef82007-06-17 19:56:36 -05005124 vport->fc_prevDID = vport->fc_myDID;
5125 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005126
5127 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005128 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5129 "0202 Start Discovery hba state x%x "
5130 "Data: x%x x%x x%x\n",
5131 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5132 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005133
5134 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005135 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005136
5137 if (num_sent)
5138 return;
5139
James Smart6fa139f2013-04-17 20:20:00 -04005140 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005141 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005142 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005143 !(vport->fc_flag & FC_RSCN_MODE) &&
5144 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005145 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005146 lpfc_issue_reg_vpi(phba, vport);
5147 return;
5148 }
James Smart2e0fef82007-06-17 19:56:36 -05005149
James Smart92d7f7b2007-06-17 19:56:38 -05005150 /*
5151 * For SLI2, we need to set port_state to READY and continue
5152 * discovery.
5153 */
5154 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5155 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005156 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005157
5158 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005159 vport->num_disc_nodes = 0;
5160 /* go thru NPR nodes and issue ELS PLOGIs */
5161 if (vport->fc_npr_cnt)
5162 lpfc_els_disc_plogi(vport);
5163
5164 if (!vport->num_disc_nodes) {
5165 spin_lock_irq(shost->host_lock);
5166 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5167 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005168 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005169 }
5170 }
James Smart92d7f7b2007-06-17 19:56:38 -05005171 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005172 } else {
5173 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005174 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005175
5176 if (num_sent)
5177 return;
5178
James Smart2e0fef82007-06-17 19:56:36 -05005179 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005180 /* Check to see if more RSCNs came in while we
5181 * were processing this one.
5182 */
James Smart2e0fef82007-06-17 19:56:36 -05005183 if ((vport->fc_rscn_id_cnt == 0) &&
5184 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5185 spin_lock_irq(shost->host_lock);
5186 vport->fc_flag &= ~FC_RSCN_MODE;
5187 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005188 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005189 } else
James Smart2e0fef82007-06-17 19:56:36 -05005190 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005191 }
5192 }
5193 return;
5194}
5195
5196/*
5197 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5198 * ring the match the sppecified nodelist.
5199 */
5200static void
James Smart2e0fef82007-06-17 19:56:36 -05005201lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005202{
James Smart2534ba72007-04-25 09:52:20 -04005203 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005204 struct lpfc_sli *psli;
5205 IOCB_t *icmd;
5206 struct lpfc_iocbq *iocb, *next_iocb;
5207 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005208
5209 psli = &phba->sli;
5210 pring = &psli->ring[LPFC_ELS_RING];
5211
5212 /* Error matching iocb on txq or txcmplq
5213 * First check the txq.
5214 */
James Smart2e0fef82007-06-17 19:56:36 -05005215 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005216 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5217 if (iocb->context1 != ndlp) {
5218 continue;
5219 }
5220 icmd = &iocb->iocb;
5221 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5222 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5223
James Smart2534ba72007-04-25 09:52:20 -04005224 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005225 }
5226 }
5227
5228 /* Next check the txcmplq */
5229 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5230 if (iocb->context1 != ndlp) {
5231 continue;
5232 }
5233 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005234 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5235 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005236 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005237 }
5238 }
James Smart2e0fef82007-06-17 19:56:36 -05005239 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005240
James Smarta257bf92009-04-06 18:48:10 -04005241 /* Cancel all the IOCBs from the completions list */
5242 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5243 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005244}
5245
Adrian Bunka6ababd2007-11-05 18:07:33 +01005246static void
James Smart2e0fef82007-06-17 19:56:36 -05005247lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005248{
5249 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005250 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005251
James Smart2e0fef82007-06-17 19:56:36 -05005252 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5253 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005254 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005255 if (!NLP_CHK_NODE_ACT(ndlp))
5256 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005257 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5258 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5259 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005260 }
dea31012005-04-17 16:05:31 -05005261 }
5262 }
dea31012005-04-17 16:05:31 -05005263}
5264
James Smart92d7f7b2007-06-17 19:56:38 -05005265void
5266lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5267{
5268 lpfc_els_flush_rscn(vport);
5269 lpfc_els_flush_cmd(vport);
5270 lpfc_disc_flush_list(vport);
5271}
5272
dea31012005-04-17 16:05:31 -05005273/*****************************************************************************/
5274/*
5275 * NAME: lpfc_disc_timeout
5276 *
5277 * FUNCTION: Fibre Channel driver discovery timeout routine.
5278 *
5279 * EXECUTION ENVIRONMENT: interrupt only
5280 *
5281 * CALLED FROM:
5282 * Timer function
5283 *
5284 * RETURNS:
5285 * none
5286 */
5287/*****************************************************************************/
5288void
5289lpfc_disc_timeout(unsigned long ptr)
5290{
James Smart2e0fef82007-06-17 19:56:36 -05005291 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5292 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005293 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005294 unsigned long flags = 0;
5295
5296 if (unlikely(!phba))
5297 return;
5298
James Smart5e9d9b82008-06-14 22:52:53 -04005299 spin_lock_irqsave(&vport->work_port_lock, flags);
5300 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5301 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005302 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005303 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005304
James Smart5e9d9b82008-06-14 22:52:53 -04005305 if (!tmo_posted)
5306 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005307 return;
5308}
5309
5310static void
James Smart2e0fef82007-06-17 19:56:36 -05005311lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005312{
James Smart2e0fef82007-06-17 19:56:36 -05005313 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5314 struct lpfc_hba *phba = vport->phba;
5315 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005316 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005317 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005318 int rc, clrlaerr = 0;
5319
James Smart2e0fef82007-06-17 19:56:36 -05005320 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005321 return;
5322
James Smart2e0fef82007-06-17 19:56:36 -05005323 spin_lock_irq(shost->host_lock);
5324 vport->fc_flag &= ~FC_DISC_TMO;
5325 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005326
James Smart858c9f62007-06-17 19:56:39 -05005327 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5328 "disc timeout: state:x%x rtry:x%x flg:x%x",
5329 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5330
James Smart2e0fef82007-06-17 19:56:36 -05005331 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005332
5333 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005334 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5335 * FAN
5336 */
5337 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005338 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5339 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005340 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005341 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005342 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005343 if (!NLP_CHK_NODE_ACT(ndlp))
5344 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005345 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5346 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005347 if (ndlp->nlp_type & NLP_FABRIC) {
5348 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005349 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005350
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005351 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005352 /* Fail outstanding IO now since device
5353 * is marked for PLOGI.
5354 */
James Smart2e0fef82007-06-17 19:56:36 -05005355 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005356 }
5357 }
James Smart92d7f7b2007-06-17 19:56:38 -05005358 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005359 if (phba->sli_rev <= LPFC_SLI_REV3)
5360 lpfc_initial_flogi(vport);
5361 else
5362 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005363 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005364 }
dea31012005-04-17 16:05:31 -05005365 break;
5366
James Smart92d7f7b2007-06-17 19:56:38 -05005367 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005368 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005369 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005370 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005371 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5372 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005373 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005374
5375 /* Assume no Fabric and go on with discovery.
5376 * Check for outstanding ELS FLOGI to abort.
5377 */
5378
5379 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005380 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005381
5382 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005383 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005384 break;
5385
5386 case LPFC_FABRIC_CFG_LINK:
5387 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5388 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005389 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5390 "0223 Timeout while waiting for "
5391 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005392 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005393 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005394 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005395 lpfc_els_abort(phba, ndlp);
5396
5397 /* ReStart discovery */
5398 goto restart_disc;
dea31012005-04-17 16:05:31 -05005399
5400 case LPFC_NS_QRY:
5401 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005402 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5403 "0224 NameServer Query timeout "
5404 "Data: x%x x%x\n",
5405 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005406
James Smart92d7f7b2007-06-17 19:56:38 -05005407 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5408 /* Try it one more time */
5409 vport->fc_ns_retry++;
5410 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5411 vport->fc_ns_retry, 0);
5412 if (rc == 0)
5413 break;
dea31012005-04-17 16:05:31 -05005414 }
James Smart92d7f7b2007-06-17 19:56:38 -05005415 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005416
James Smart87af33f2007-10-27 13:37:43 -04005417restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005418 /*
5419 * Discovery is over.
5420 * set port_state to PORT_READY if SLI2.
5421 * cmpl_reg_vpi will set port_state to READY for SLI3.
5422 */
James Smart3772a992009-05-22 14:50:54 -04005423 if (phba->sli_rev < LPFC_SLI_REV4) {
5424 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5425 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005426 else {
James Smart3772a992009-05-22 14:50:54 -04005427 lpfc_issue_clear_la(phba, vport);
5428 vport->port_state = LPFC_VPORT_READY;
5429 }
dea31012005-04-17 16:05:31 -05005430 }
5431
5432 /* Setup and issue mailbox INITIALIZE LINK command */
5433 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5434 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005435 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5436 "0206 Device Discovery "
5437 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005438 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005439 break;
5440 }
5441
5442 lpfc_linkdown(phba);
5443 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5444 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005445 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005446 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005447 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005448 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005449 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005450 if (rc == MBX_NOT_FINISHED)
5451 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5452
5453 break;
5454
5455 case LPFC_DISC_AUTH:
5456 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005457 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5458 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005459 lpfc_disc_flush_list(vport);
5460
James Smart92d7f7b2007-06-17 19:56:38 -05005461 /*
5462 * set port_state to PORT_READY if SLI2.
5463 * cmpl_reg_vpi will set port_state to READY for SLI3.
5464 */
James Smart3772a992009-05-22 14:50:54 -04005465 if (phba->sli_rev < LPFC_SLI_REV4) {
5466 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5467 lpfc_issue_reg_vpi(phba, vport);
5468 else { /* NPIV Not enabled */
5469 lpfc_issue_clear_la(phba, vport);
5470 vport->port_state = LPFC_VPORT_READY;
5471 }
dea31012005-04-17 16:05:31 -05005472 }
5473 break;
5474
James Smart2e0fef82007-06-17 19:56:36 -05005475 case LPFC_VPORT_READY:
5476 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005477 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5478 "0231 RSCN timeout Data: x%x "
5479 "x%x\n",
5480 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005481
5482 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005483 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005484
James Smart2e0fef82007-06-17 19:56:36 -05005485 lpfc_els_flush_rscn(vport);
5486 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005487 }
5488 break;
James Smart2e0fef82007-06-17 19:56:36 -05005489
James Smart92d7f7b2007-06-17 19:56:38 -05005490 default:
James Smarte8b62012007-08-02 11:10:09 -04005491 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005492 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005493 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005494 break;
5495 }
5496
5497 switch (phba->link_state) {
5498 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005499 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005500 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5501 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005502 clrlaerr = 1;
5503 break;
5504
James Smart09372822008-01-11 01:52:54 -05005505 case LPFC_LINK_UP:
5506 lpfc_issue_clear_la(phba, vport);
5507 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005508 case LPFC_LINK_UNKNOWN:
5509 case LPFC_WARM_START:
5510 case LPFC_INIT_START:
5511 case LPFC_INIT_MBX_CMDS:
5512 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005513 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005514 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5515 "0230 Unexpected timeout, hba link "
5516 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005517 clrlaerr = 1;
5518 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005519
5520 case LPFC_HBA_READY:
5521 break;
dea31012005-04-17 16:05:31 -05005522 }
5523
5524 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005525 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005526 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005527 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5528 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005529 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005530 }
5531
5532 return;
5533}
5534
dea31012005-04-17 16:05:31 -05005535/*
5536 * This routine handles processing a NameServer REG_LOGIN mailbox
5537 * command upon completion. It is setup in the LPFC_MBOXQ
5538 * as the completion routine when the command is
5539 * handed off to the SLI layer.
5540 */
5541void
James Smart2e0fef82007-06-17 19:56:36 -05005542lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005543{
James Smart04c68492009-05-22 14:52:52 -04005544 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005545 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5546 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5547 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005548
5549 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005550 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005551
James Smart6d368e52011-05-24 11:44:12 -04005552 if (phba->sli_rev < LPFC_SLI_REV4)
5553 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005554 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005555 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005556 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005557 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5558 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5559 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5560 atomic_read(&ndlp->kref.refcount),
5561 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005562 /*
5563 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005564 * 0xfffffa (FDMI well known port).
5565 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5566 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005567 */
James Smart4258e982015-12-16 18:11:58 -05005568 if (vport->port_type == LPFC_PHYSICAL_PORT)
5569 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005570 else
James Smart4258e982015-12-16 18:11:58 -05005571 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5572
dea31012005-04-17 16:05:31 -05005573
James Smartfa4066b2008-01-11 01:53:27 -05005574 /* decrement the node reference count held for this callback
5575 * function.
5576 */
James Smart329f9bc2007-04-25 09:53:01 -04005577 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005578 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5579 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005580 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005581
5582 return;
5583}
5584
James Smart685f0bf2007-04-25 09:53:08 -04005585static int
5586lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5587{
5588 uint16_t *rpi = param;
5589
James Smarteff4a012012-01-18 16:25:25 -05005590 /* check for active node */
5591 if (!NLP_CHK_NODE_ACT(ndlp))
5592 return 0;
5593
James Smart685f0bf2007-04-25 09:53:08 -04005594 return ndlp->nlp_rpi == *rpi;
5595}
5596
5597static int
5598lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5599{
5600 return memcmp(&ndlp->nlp_portname, param,
5601 sizeof(ndlp->nlp_portname)) == 0;
5602}
5603
Adrian Bunka6ababd2007-11-05 18:07:33 +01005604static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005605__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005606{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005607 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005608
James Smart2e0fef82007-06-17 19:56:36 -05005609 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005610 if (filter(ndlp, param)) {
5611 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5612 "3185 FIND node filter %p DID "
5613 "Data: x%p x%x x%x\n",
5614 filter, ndlp, ndlp->nlp_DID,
5615 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005616 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005617 }
James Smart685f0bf2007-04-25 09:53:08 -04005618 }
James Smart34f5ad82012-08-03 12:35:03 -04005619 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5620 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005621 return NULL;
dea31012005-04-17 16:05:31 -05005622}
5623
James Smart685f0bf2007-04-25 09:53:08 -04005624/*
5625 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005626 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005627 */
5628struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005629__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005630{
James Smart2e0fef82007-06-17 19:56:36 -05005631 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005632}
5633
James Smart488d1462006-03-07 15:02:37 -05005634/*
James Smart685f0bf2007-04-25 09:53:08 -04005635 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005636 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005637 */
5638struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005639lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005640{
James Smart2e0fef82007-06-17 19:56:36 -05005641 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005642 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005643
James Smart2e0fef82007-06-17 19:56:36 -05005644 spin_lock_irq(shost->host_lock);
5645 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5646 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005647 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005648}
5649
James Smartcb69f7d2011-12-13 13:21:57 -05005650/*
5651 * This routine looks up the ndlp lists for the given RPI. If the rpi
5652 * is found, the routine returns the node element list pointer else
5653 * return NULL.
5654 */
5655struct lpfc_nodelist *
5656lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5657{
5658 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5659 struct lpfc_nodelist *ndlp;
5660
5661 spin_lock_irq(shost->host_lock);
5662 ndlp = __lpfc_findnode_rpi(vport, rpi);
5663 spin_unlock_irq(shost->host_lock);
5664 return ndlp;
5665}
5666
5667/**
5668 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5669 * @phba: pointer to lpfc hba data structure.
5670 * @vpi: the physical host virtual N_Port identifier.
5671 *
5672 * This routine finds a vport on a HBA (referred by @phba) through a
5673 * @vpi. The function walks the HBA's vport list and returns the address
5674 * of the vport with the matching @vpi.
5675 *
5676 * Return code
5677 * NULL - No vport with the matching @vpi found
5678 * Otherwise - Address to the vport with the matching @vpi.
5679 **/
5680struct lpfc_vport *
5681lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5682{
5683 struct lpfc_vport *vport;
5684 unsigned long flags;
5685 int i = 0;
5686
5687 /* The physical ports are always vpi 0 - translate is unnecessary. */
5688 if (vpi > 0) {
5689 /*
5690 * Translate the physical vpi to the logical vpi. The
5691 * vport stores the logical vpi.
5692 */
5693 for (i = 0; i < phba->max_vpi; i++) {
5694 if (vpi == phba->vpi_ids[i])
5695 break;
5696 }
5697
5698 if (i >= phba->max_vpi) {
5699 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5700 "2936 Could not find Vport mapped "
5701 "to vpi %d\n", vpi);
5702 return NULL;
5703 }
5704 }
5705
5706 spin_lock_irqsave(&phba->hbalock, flags);
5707 list_for_each_entry(vport, &phba->port_list, listentry) {
5708 if (vport->vpi == i) {
5709 spin_unlock_irqrestore(&phba->hbalock, flags);
5710 return vport;
5711 }
5712 }
5713 spin_unlock_irqrestore(&phba->hbalock, flags);
5714 return NULL;
5715}
5716
dea31012005-04-17 16:05:31 -05005717void
James Smart2e0fef82007-06-17 19:56:36 -05005718lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5719 uint32_t did)
dea31012005-04-17 16:05:31 -05005720{
5721 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005722
5723 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005724 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005725 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005726 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005727 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5728 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5729 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5730 ndlp->nlp_flag,
5731 atomic_read(&ndlp->kref.refcount),
5732 ndlp->nlp_usg_map, ndlp);
5733
James Smartcff261f2013-12-17 20:29:47 -05005734 ndlp->active_rrqs_xri_bitmap =
5735 mempool_alloc(vport->phba->active_rrq_pool,
5736 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005737 if (ndlp->active_rrqs_xri_bitmap)
5738 memset(ndlp->active_rrqs_xri_bitmap, 0,
5739 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005740 }
5741
James Smart725dd392013-10-10 12:23:30 -04005742
James Smart858c9f62007-06-17 19:56:39 -05005743
5744 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5745 "node init: did:x%x",
5746 ndlp->nlp_DID, 0, 0);
5747
dea31012005-04-17 16:05:31 -05005748 return;
5749}
James Smart329f9bc2007-04-25 09:53:01 -04005750
James Smart98c9ea52007-10-27 13:37:33 -04005751/* This routine releases all resources associated with a specifc NPort's ndlp
5752 * and mempool_free's the nodelist.
5753 */
James Smart311464e2007-08-02 11:10:37 -04005754static void
James Smart329f9bc2007-04-25 09:53:01 -04005755lpfc_nlp_release(struct kref *kref)
5756{
James Smarte47c9092008-02-08 18:49:26 -05005757 struct lpfc_hba *phba;
5758 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005759 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5760 kref);
James Smart858c9f62007-06-17 19:56:39 -05005761
5762 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5763 "node release: did:x%x flg:x%x type:x%x",
5764 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5765
James Smarte47c9092008-02-08 18:49:26 -05005766 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005767 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005768 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005769 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smartbe6bb942015-04-07 15:07:22 -04005770 atomic_read(&ndlp->kref.refcount), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005771
5772 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005773 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005774
5775 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005776 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005777 spin_lock_irqsave(&phba->ndlp_lock, flags);
5778 NLP_CLR_NODE_ACT(ndlp);
5779 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005780 if (phba->sli_rev == LPFC_SLI_REV4)
5781 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005782
5783 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005784 if (NLP_CHK_FREE_REQ(ndlp)) {
5785 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005786 if (phba->sli_rev == LPFC_SLI_REV4)
5787 mempool_free(ndlp->active_rrqs_xri_bitmap,
5788 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005789 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005790 }
James Smart329f9bc2007-04-25 09:53:01 -04005791}
5792
James Smart98c9ea52007-10-27 13:37:33 -04005793/* This routine bumps the reference count for a ndlp structure to ensure
5794 * that one discovery thread won't free a ndlp while another discovery thread
5795 * is using it.
5796 */
James Smart329f9bc2007-04-25 09:53:01 -04005797struct lpfc_nodelist *
5798lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5799{
James Smarte47c9092008-02-08 18:49:26 -05005800 struct lpfc_hba *phba;
5801 unsigned long flags;
5802
James Smart98c9ea52007-10-27 13:37:33 -04005803 if (ndlp) {
5804 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5805 "node get: did:x%x flg:x%x refcnt:x%x",
5806 ndlp->nlp_DID, ndlp->nlp_flag,
5807 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005808 /* The check of ndlp usage to prevent incrementing the
5809 * ndlp reference count that is in the process of being
5810 * released.
5811 */
James Smarta257bf92009-04-06 18:48:10 -04005812 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005813 spin_lock_irqsave(&phba->ndlp_lock, flags);
5814 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5815 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5816 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5817 "0276 lpfc_nlp_get: ndlp:x%p "
5818 "usgmap:x%x refcnt:%d\n",
5819 (void *)ndlp, ndlp->nlp_usg_map,
5820 atomic_read(&ndlp->kref.refcount));
5821 return NULL;
5822 } else
5823 kref_get(&ndlp->kref);
5824 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005825 }
James Smart329f9bc2007-04-25 09:53:01 -04005826 return ndlp;
5827}
5828
James Smart98c9ea52007-10-27 13:37:33 -04005829/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005830 * count goes to 0, this indicates the the associated nodelist should be
5831 * freed. Returning 1 indicates the ndlp resource has been released; on the
5832 * other hand, returning 0 indicates the ndlp resource has not been released
5833 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005834 */
James Smart329f9bc2007-04-25 09:53:01 -04005835int
5836lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5837{
James Smarte47c9092008-02-08 18:49:26 -05005838 struct lpfc_hba *phba;
5839 unsigned long flags;
5840
5841 if (!ndlp)
5842 return 1;
5843
5844 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5845 "node put: did:x%x flg:x%x refcnt:x%x",
5846 ndlp->nlp_DID, ndlp->nlp_flag,
5847 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005848 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005849 spin_lock_irqsave(&phba->ndlp_lock, flags);
5850 /* Check the ndlp memory free acknowledge flag to avoid the
5851 * possible race condition that kref_put got invoked again
5852 * after previous one has done ndlp memory free.
5853 */
5854 if (NLP_CHK_FREE_ACK(ndlp)) {
5855 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5856 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5857 "0274 lpfc_nlp_put: ndlp:x%p "
5858 "usgmap:x%x refcnt:%d\n",
5859 (void *)ndlp, ndlp->nlp_usg_map,
5860 atomic_read(&ndlp->kref.refcount));
5861 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005862 }
James Smarte47c9092008-02-08 18:49:26 -05005863 /* Check the ndlp inactivate log flag to avoid the possible
5864 * race condition that kref_put got invoked again after ndlp
5865 * is already in inactivating state.
5866 */
5867 if (NLP_CHK_IACT_REQ(ndlp)) {
5868 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5869 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5870 "0275 lpfc_nlp_put: ndlp:x%p "
5871 "usgmap:x%x refcnt:%d\n",
5872 (void *)ndlp, ndlp->nlp_usg_map,
5873 atomic_read(&ndlp->kref.refcount));
5874 return 1;
5875 }
5876 /* For last put, mark the ndlp usage flags to make sure no
5877 * other kref_get and kref_put on the same ndlp shall get
5878 * in between the process when the final kref_put has been
5879 * invoked on this ndlp.
5880 */
5881 if (atomic_read(&ndlp->kref.refcount) == 1) {
5882 /* Indicate ndlp is put to inactive state. */
5883 NLP_SET_IACT_REQ(ndlp);
5884 /* Acknowledge ndlp memory free has been seen. */
5885 if (NLP_CHK_FREE_REQ(ndlp))
5886 NLP_SET_FREE_ACK(ndlp);
5887 }
5888 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5889 /* Note, the kref_put returns 1 when decrementing a reference
5890 * count that was 1, it invokes the release callback function,
5891 * but it still left the reference count as 1 (not actually
5892 * performs the last decrementation). Otherwise, it actually
5893 * decrements the reference count and returns 0.
5894 */
5895 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005896}
James Smart98c9ea52007-10-27 13:37:33 -04005897
5898/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005899 * by any other discovery thread. This routine returns 1 if the
5900 * ndlp has been freed. A return value of 0 indicates the ndlp is
5901 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005902 */
5903int
5904lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5905{
5906 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5907 "node not used: did:x%x flg:x%x refcnt:x%x",
5908 ndlp->nlp_DID, ndlp->nlp_flag,
5909 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005910 if (atomic_read(&ndlp->kref.refcount) == 1)
5911 if (lpfc_nlp_put(ndlp))
5912 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005913 return 0;
5914}
James Smart6fb120a2009-05-22 14:52:59 -04005915
5916/**
5917 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5918 * @phba: Pointer to hba context object.
5919 *
5920 * This function iterate through all FC nodes associated
5921 * will all vports to check if there is any node with
5922 * fc_rports associated with it. If there is an fc_rport
5923 * associated with the node, then the node is either in
5924 * discovered state or its devloss_timer is pending.
5925 */
5926static int
5927lpfc_fcf_inuse(struct lpfc_hba *phba)
5928{
5929 struct lpfc_vport **vports;
5930 int i, ret = 0;
5931 struct lpfc_nodelist *ndlp;
5932 struct Scsi_Host *shost;
5933
5934 vports = lpfc_create_vport_work_array(phba);
5935
James Smart63e801c2010-11-20 23:14:19 -05005936 /* If driver cannot allocate memory, indicate fcf is in use */
5937 if (!vports)
5938 return 1;
5939
James Smart6fb120a2009-05-22 14:52:59 -04005940 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5941 shost = lpfc_shost_from_vport(vports[i]);
5942 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005943 /*
5944 * IF the CVL_RCVD bit is not set then we have sent the
5945 * flogi.
5946 * If dev_loss fires while we are waiting we do not want to
5947 * unreg the fcf.
5948 */
5949 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5950 spin_unlock_irq(shost->host_lock);
5951 ret = 1;
5952 goto out;
5953 }
James Smart6fb120a2009-05-22 14:52:59 -04005954 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5955 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5956 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5957 ret = 1;
5958 spin_unlock_irq(shost->host_lock);
5959 goto out;
James Smart80c17842012-03-01 22:35:45 -05005960 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5961 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005962 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005963 "2624 RPI %x DID %x flag %x "
5964 "still logged in\n",
5965 ndlp->nlp_rpi, ndlp->nlp_DID,
5966 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005967 }
5968 }
5969 spin_unlock_irq(shost->host_lock);
5970 }
5971out:
5972 lpfc_destroy_vport_work_array(phba, vports);
5973 return ret;
5974}
5975
5976/**
5977 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5978 * @phba: Pointer to hba context object.
5979 * @mboxq: Pointer to mailbox object.
5980 *
5981 * This function frees memory associated with the mailbox command.
5982 */
James Smart1b511972011-12-13 13:23:09 -05005983void
James Smart6fb120a2009-05-22 14:52:59 -04005984lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5985{
5986 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005987 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005988
5989 if (mboxq->u.mb.mbxStatus) {
5990 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5991 "2555 UNREG_VFI mbxStatus error x%x "
5992 "HBA state x%x\n",
5993 mboxq->u.mb.mbxStatus, vport->port_state);
5994 }
James Smart38b92ef2010-08-04 16:11:39 -04005995 spin_lock_irq(shost->host_lock);
5996 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5997 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005998 mempool_free(mboxq, phba->mbox_mem_pool);
5999 return;
6000}
6001
6002/**
6003 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6004 * @phba: Pointer to hba context object.
6005 * @mboxq: Pointer to mailbox object.
6006 *
6007 * This function frees memory associated with the mailbox command.
6008 */
6009static void
6010lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6011{
6012 struct lpfc_vport *vport = mboxq->vport;
6013
6014 if (mboxq->u.mb.mbxStatus) {
6015 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6016 "2550 UNREG_FCFI mbxStatus error x%x "
6017 "HBA state x%x\n",
6018 mboxq->u.mb.mbxStatus, vport->port_state);
6019 }
6020 mempool_free(mboxq, phba->mbox_mem_pool);
6021 return;
6022}
6023
6024/**
James Smartecfd03c2010-02-12 14:41:27 -05006025 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006026 * @phba: Pointer to hba context object.
6027 *
James Smartecfd03c2010-02-12 14:41:27 -05006028 * This function prepare the HBA for unregistering the currently registered
6029 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6030 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006031 */
James Smartecfd03c2010-02-12 14:41:27 -05006032int
6033lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006034{
James Smart6fb120a2009-05-22 14:52:59 -04006035 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006036 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006037 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006038 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006039
James Smartecfd03c2010-02-12 14:41:27 -05006040 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006041 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006042 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006043
James Smart4d9ab992009-10-02 15:16:39 -04006044 /* At this point, all discovery is aborted */
6045 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006046
6047 /* Unregister VPIs */
6048 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006049 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006050 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006051 /* Stop FLOGI/FDISC retries */
6052 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6053 if (ndlp)
6054 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006055 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006056 if (phba->sli_rev == LPFC_SLI_REV4)
6057 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006058 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006059 shost = lpfc_shost_from_vport(vports[i]);
6060 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006061 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006062 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006063 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006064 }
6065 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006066 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6067 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6068 if (ndlp)
6069 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6070 lpfc_cleanup_pending_mbox(phba->pport);
6071 if (phba->sli_rev == LPFC_SLI_REV4)
6072 lpfc_sli4_unreg_all_rpis(phba->pport);
6073 lpfc_mbx_unreg_vpi(phba->pport);
6074 shost = lpfc_shost_from_vport(phba->pport);
6075 spin_lock_irq(shost->host_lock);
6076 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6077 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6078 spin_unlock_irq(shost->host_lock);
6079 }
James Smart6fb120a2009-05-22 14:52:59 -04006080
James Smart695a8142010-01-26 23:08:03 -05006081 /* Cleanup any outstanding ELS commands */
6082 lpfc_els_flush_all_cmd(phba);
6083
James Smart1b511972011-12-13 13:23:09 -05006084 /* Unregister the physical port VFI */
6085 rc = lpfc_issue_unreg_vfi(phba->pport);
6086 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006087}
6088
6089/**
6090 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6091 * @phba: Pointer to hba context object.
6092 *
6093 * This function issues synchronous unregister FCF mailbox command to HBA to
6094 * unregister the currently registered FCF record. The driver does not reset
6095 * the driver FCF usage state flags.
6096 *
6097 * Return 0 if successfully issued, none-zero otherwise.
6098 */
6099int
6100lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6101{
6102 LPFC_MBOXQ_t *mbox;
6103 int rc;
6104
James Smart6fb120a2009-05-22 14:52:59 -04006105 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6106 if (!mbox) {
6107 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006108 "2551 UNREG_FCFI mbox allocation failed"
6109 "HBA state x%x\n", phba->pport->port_state);
6110 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006111 }
James Smart6fb120a2009-05-22 14:52:59 -04006112 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6113 mbox->vport = phba->pport;
6114 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6115 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6116
6117 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6119 "2552 Unregister FCFI command failed rc x%x "
6120 "HBA state x%x\n",
6121 rc, phba->pport->port_state);
6122 return -EINVAL;
6123 }
6124 return 0;
6125}
6126
6127/**
6128 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6129 * @phba: Pointer to hba context object.
6130 *
6131 * This function unregisters the currently reigstered FCF. This function
6132 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6133 */
6134void
6135lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6136{
6137 int rc;
6138
6139 /* Preparation for unregistering fcf */
6140 rc = lpfc_unregister_fcf_prep(phba);
6141 if (rc) {
6142 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6143 "2748 Failed to prepare for unregistering "
6144 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006145 return;
6146 }
6147
James Smartecfd03c2010-02-12 14:41:27 -05006148 /* Now, unregister FCF record and reset HBA FCF state */
6149 rc = lpfc_sli4_unregister_fcf(phba);
6150 if (rc)
6151 return;
6152 /* Reset HBA FCF states after successful unregister FCF */
6153 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006154 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006155
6156 /*
6157 * If driver is not unloading, check if there is any other
6158 * FCF record that can be used for discovery.
6159 */
6160 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006161 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006162 return;
6163
James Smart0c9ab6f2010-02-26 14:15:57 -05006164 /* This is considered as the initial FCF discovery scan */
6165 spin_lock_irq(&phba->hbalock);
6166 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6167 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006168
6169 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006170 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006171
James Smart0c9ab6f2010-02-26 14:15:57 -05006172 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006173
James Smart0c9ab6f2010-02-26 14:15:57 -05006174 if (rc) {
6175 spin_lock_irq(&phba->hbalock);
6176 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6177 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006178 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006179 "2553 lpfc_unregister_unused_fcf failed "
6180 "to read FCF record HBA state x%x\n",
6181 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006182 }
James Smartecfd03c2010-02-12 14:41:27 -05006183}
6184
6185/**
6186 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6187 * @phba: Pointer to hba context object.
6188 *
6189 * This function just unregisters the currently reigstered FCF. It does not
6190 * try to find another FCF for discovery.
6191 */
6192void
6193lpfc_unregister_fcf(struct lpfc_hba *phba)
6194{
6195 int rc;
6196
6197 /* Preparation for unregistering fcf */
6198 rc = lpfc_unregister_fcf_prep(phba);
6199 if (rc) {
6200 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6201 "2749 Failed to prepare for unregistering "
6202 "HBA's FCF record: rc=%d\n", rc);
6203 return;
6204 }
6205
6206 /* Now, unregister FCF record and reset HBA FCF state */
6207 rc = lpfc_sli4_unregister_fcf(phba);
6208 if (rc)
6209 return;
6210 /* Set proper HBA FCF states after successful unregister FCF */
6211 spin_lock_irq(&phba->hbalock);
6212 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6213 spin_unlock_irq(&phba->hbalock);
6214}
6215
6216/**
6217 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6218 * @phba: Pointer to hba context object.
6219 *
6220 * This function check if there are any connected remote port for the FCF and
6221 * if all the devices are disconnected, this function unregister FCFI.
6222 * This function also tries to use another FCF for discovery.
6223 */
6224void
6225lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6226{
6227 /*
James Smart3804dc82010-07-14 15:31:37 -04006228 * If HBA is not running in FIP mode, if HBA does not support
6229 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6230 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006231 */
6232 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006233 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006234 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006235 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006236 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006237 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006238 spin_unlock_irq(&phba->hbalock);
6239 return;
6240 }
6241 spin_unlock_irq(&phba->hbalock);
6242
6243 if (lpfc_fcf_inuse(phba))
6244 return;
6245
6246 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006247}
6248
6249/**
6250 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6251 * @phba: Pointer to hba context object.
6252 * @buff: Buffer containing the FCF connection table as in the config
6253 * region.
6254 * This function create driver data structure for the FCF connection
6255 * record table read from config region 23.
6256 */
6257static void
6258lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6259 uint8_t *buff)
6260{
6261 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6262 struct lpfc_fcf_conn_hdr *conn_hdr;
6263 struct lpfc_fcf_conn_rec *conn_rec;
6264 uint32_t record_count;
6265 int i;
6266
6267 /* Free the current connect table */
6268 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006269 &phba->fcf_conn_rec_list, list) {
6270 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006271 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006272 }
James Smart6fb120a2009-05-22 14:52:59 -04006273
6274 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6275 record_count = conn_hdr->length * sizeof(uint32_t)/
6276 sizeof(struct lpfc_fcf_conn_rec);
6277
6278 conn_rec = (struct lpfc_fcf_conn_rec *)
6279 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6280
6281 for (i = 0; i < record_count; i++) {
6282 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6283 continue;
6284 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6285 GFP_KERNEL);
6286 if (!conn_entry) {
6287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6288 "2566 Failed to allocate connection"
6289 " table entry\n");
6290 return;
6291 }
6292
6293 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6294 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006295 list_add_tail(&conn_entry->list,
6296 &phba->fcf_conn_rec_list);
6297 }
James Smartdf0d0852013-05-31 17:05:08 -04006298
6299 if (!list_empty(&phba->fcf_conn_rec_list)) {
6300 i = 0;
6301 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6302 list) {
6303 conn_rec = &conn_entry->conn_rec;
6304 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6305 "3345 FCF connection list rec[%02d]: "
6306 "flags:x%04x, vtag:x%04x, "
6307 "fabric_name:x%02x:%02x:%02x:%02x:"
6308 "%02x:%02x:%02x:%02x, "
6309 "switch_name:x%02x:%02x:%02x:%02x:"
6310 "%02x:%02x:%02x:%02x\n", i++,
6311 conn_rec->flags, conn_rec->vlan_tag,
6312 conn_rec->fabric_name[0],
6313 conn_rec->fabric_name[1],
6314 conn_rec->fabric_name[2],
6315 conn_rec->fabric_name[3],
6316 conn_rec->fabric_name[4],
6317 conn_rec->fabric_name[5],
6318 conn_rec->fabric_name[6],
6319 conn_rec->fabric_name[7],
6320 conn_rec->switch_name[0],
6321 conn_rec->switch_name[1],
6322 conn_rec->switch_name[2],
6323 conn_rec->switch_name[3],
6324 conn_rec->switch_name[4],
6325 conn_rec->switch_name[5],
6326 conn_rec->switch_name[6],
6327 conn_rec->switch_name[7]);
6328 }
6329 }
James Smart6fb120a2009-05-22 14:52:59 -04006330}
6331
6332/**
6333 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6334 * @phba: Pointer to hba context object.
6335 * @buff: Buffer containing the FCoE parameter data structure.
6336 *
6337 * This function update driver data structure with config
6338 * parameters read from config region 23.
6339 */
6340static void
6341lpfc_read_fcoe_param(struct lpfc_hba *phba,
6342 uint8_t *buff)
6343{
6344 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6345 struct lpfc_fcoe_params *fcoe_param;
6346
6347 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6348 buff;
6349 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006350 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006351
6352 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6353 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6354 return;
6355
James Smart6fb120a2009-05-22 14:52:59 -04006356 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6357 phba->valid_vlan = 1;
6358 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6359 0xFFF;
6360 }
6361
6362 phba->fc_map[0] = fcoe_param->fc_map[0];
6363 phba->fc_map[1] = fcoe_param->fc_map[1];
6364 phba->fc_map[2] = fcoe_param->fc_map[2];
6365 return;
6366}
6367
6368/**
6369 * lpfc_get_rec_conf23 - Get a record type in config region data.
6370 * @buff: Buffer containing config region 23 data.
6371 * @size: Size of the data buffer.
6372 * @rec_type: Record type to be searched.
6373 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006374 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006375 * of the record specified by record_type. If record found, this
6376 * function return pointer to the record else return NULL.
6377 */
6378static uint8_t *
6379lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6380{
6381 uint32_t offset = 0, rec_length;
6382
6383 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6384 (size < sizeof(uint32_t)))
6385 return NULL;
6386
6387 rec_length = buff[offset + 1];
6388
6389 /*
6390 * One TLV record has one word header and number of data words
6391 * specified in the rec_length field of the record header.
6392 */
6393 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6394 <= size) {
6395 if (buff[offset] == rec_type)
6396 return &buff[offset];
6397
6398 if (buff[offset] == LPFC_REGION23_LAST_REC)
6399 return NULL;
6400
6401 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6402 rec_length = buff[offset + 1];
6403 }
6404 return NULL;
6405}
6406
6407/**
6408 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6409 * @phba: Pointer to lpfc_hba data structure.
6410 * @buff: Buffer containing config region 23 data.
6411 * @size: Size of the data buffer.
6412 *
Stefan Weileef35c22010-08-06 21:11:15 +02006413 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006414 * populate driver data structure with the parameters.
6415 */
6416void
6417lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6418 uint8_t *buff,
6419 uint32_t size)
6420{
James Smarteb016562014-09-03 12:58:06 -04006421 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006422 uint8_t *rec_ptr;
6423
6424 /*
6425 * If data size is less than 2 words signature and version cannot be
6426 * verified.
6427 */
6428 if (size < 2*sizeof(uint32_t))
6429 return;
6430
6431 /* Check the region signature first */
6432 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6434 "2567 Config region 23 has bad signature\n");
6435 return;
6436 }
6437
6438 offset += 4;
6439
6440 /* Check the data structure version */
6441 if (buff[offset] != LPFC_REGION23_VERSION) {
6442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6443 "2568 Config region 23 has bad version\n");
6444 return;
6445 }
6446 offset += 4;
6447
James Smart6fb120a2009-05-22 14:52:59 -04006448 /* Read FCoE param record */
6449 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6450 size - offset, FCOE_PARAM_TYPE);
6451 if (rec_ptr)
6452 lpfc_read_fcoe_param(phba, rec_ptr);
6453
6454 /* Read FCF connection table */
6455 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6456 size - offset, FCOE_CONN_TBL_TYPE);
6457 if (rec_ptr)
6458 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6459
6460}