blob: 2a51df7bdb0c7d880c24554368d09fe74c0eacc5 [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 Smartb857ff32014-04-04 13:52:50 -04004 * Copyright (C) 2004-2014 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>
28
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport_fc.h>
33
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040036#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_disc.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050045#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050046
47/* AlpaArray for assignment of scsid for scan-down and bind_method */
48static uint8_t lpfcAlpaArray[] = {
49 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
50 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
51 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
52 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
53 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
54 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
55 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
56 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
57 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
58 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
59 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
60 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
61 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
62};
63
James Smart2e0fef82007-06-17 19:56:36 -050064static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010065static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040066static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
James Smarta93ff372010-10-22 11:06:08 -040067static int lpfc_fcf_inuse(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050068
James Smartc01f3202006-08-18 17:47:08 -040069void
70lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050071{
James Smartc01f3202006-08-18 17:47:08 -040072 struct lpfc_rport_data *rdata;
73 struct lpfc_nodelist * ndlp;
74 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050075
James Smartc01f3202006-08-18 17:47:08 -040076 rdata = rport->dd_data;
77 ndlp = rdata->pnode;
78
James Smart58da1ff2008-04-07 10:15:56 -040079 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040080 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
81 printk(KERN_ERR "Cannot find remote node"
82 " to terminate I/O Data x%x\n",
83 rport->port_id);
dea31012005-04-17 16:05:31 -050084 return;
85 }
86
James Smarta257bf92009-04-06 18:48:10 -040087 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050088
James Smart858c9f62007-06-17 19:56:39 -050089 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
90 "rport terminate: sid:x%x did:x%x flg:x%x",
91 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
92
James Smartc01f3202006-08-18 17:47:08 -040093 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040094 lpfc_sli_abort_iocb(ndlp->vport,
95 &phba->sli.ring[phba->sli.fcp_ring],
96 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040097 }
James Smartc01f3202006-08-18 17:47:08 -040098}
99
100/*
101 * This function will be called when dev_loss_tmo fire.
102 */
103void
104lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
105{
106 struct lpfc_rport_data *rdata;
107 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500108 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500109 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500110 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400111 int put_node;
112 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400113
114 rdata = rport->dd_data;
115 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400116 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400117 return;
James Smartc01f3202006-08-18 17:47:08 -0400118
James Smart858c9f62007-06-17 19:56:39 -0500119 vport = ndlp->vport;
120 phba = vport->phba;
121
122 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
123 "rport devlosscb: sid:x%x did:x%x flg:x%x",
124 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
125
James Smart34f5ad82012-08-03 12:35:03 -0400126 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
127 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
128 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
129
James Smarta8adb832007-10-27 13:37:53 -0400130 /* Don't defer this if we are in the process of deleting the vport
131 * or unloading the driver. The unload will cleanup the node
132 * appropriately we just need to cleanup the ndlp rport info here.
133 */
134 if (vport->load_flag & FC_UNLOADING) {
135 put_node = rdata->pnode != NULL;
136 put_rport = ndlp->rport != NULL;
137 rdata->pnode = NULL;
138 ndlp->rport = NULL;
139 if (put_node)
140 lpfc_nlp_put(ndlp);
141 if (put_rport)
142 put_device(&rport->dev);
143 return;
144 }
145
146 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
147 return;
148
James Smart34f5ad82012-08-03 12:35:03 -0400149 if (ndlp->nlp_type & NLP_FABRIC) {
150
151 /* If the WWPN of the rport and ndlp don't match, ignore it */
152 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn)) {
James Smart7ba36ef2014-09-03 12:57:43 -0400153 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
154 "6789 rport name %lx != node port name %lx",
155 (unsigned long)rport->port_name,
156 (unsigned long)wwn_to_u64(
157 ndlp->nlp_portname.u.wwn));
158 put_node = rdata->pnode != NULL;
159 put_rport = ndlp->rport != NULL;
160 rdata->pnode = NULL;
161 ndlp->rport = NULL;
162 if (put_node)
163 lpfc_nlp_put(ndlp);
James Smart34f5ad82012-08-03 12:35:03 -0400164 put_device(&rport->dev);
165 return;
166 }
James Smart12838e72014-09-03 12:57:19 -0400167
168 put_node = rdata->pnode != NULL;
169 put_rport = ndlp->rport != NULL;
170 rdata->pnode = NULL;
171 ndlp->rport = NULL;
172 if (put_node)
173 lpfc_nlp_put(ndlp);
174 if (put_rport)
175 put_device(&rport->dev);
176 return;
James Smart34f5ad82012-08-03 12:35:03 -0400177 }
178
James Smart858c9f62007-06-17 19:56:39 -0500179 evtp = &ndlp->dev_loss_evt;
180
181 if (!list_empty(&evtp->evt_listp))
182 return;
183
James Smarta62a4352013-04-17 20:18:19 -0400184 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart12838e72014-09-03 12:57:19 -0400185 ndlp->nlp_add_flag |= NLP_IN_DEV_LOSS;
James Smarta62a4352013-04-17 20:18:19 -0400186
James Smart858c9f62007-06-17 19:56:39 -0500187 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500188 /* We need to hold the node by incrementing the reference
189 * count until this queued work is done
190 */
James Smart5e9d9b82008-06-14 22:52:53 -0400191 if (evtp->evt_arg1) {
192 evtp->evt = LPFC_EVT_DEV_LOSS;
193 list_add_tail(&evtp->evt_listp, &phba->work_list);
194 lpfc_worker_wake_up(phba);
195 }
James Smart858c9f62007-06-17 19:56:39 -0500196 spin_unlock_irq(&phba->hbalock);
197
James Smart858c9f62007-06-17 19:56:39 -0500198 return;
199}
200
James Smarta93ff372010-10-22 11:06:08 -0400201/**
202 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
203 * @ndlp: Pointer to remote node object.
204 *
205 * This function is called from the worker thread when devloss timeout timer
206 * expires. For SLI4 host, this routine shall return 1 when at lease one
207 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
208 * routine shall return 0 when there is no remote node is still in use of FCF
209 * when devloss timeout happened to this @ndlp.
210 **/
211static int
James Smart858c9f62007-06-17 19:56:39 -0500212lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
213{
214 struct lpfc_rport_data *rdata;
215 struct fc_rport *rport;
216 struct lpfc_vport *vport;
217 struct lpfc_hba *phba;
218 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400219 int put_node;
220 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500221 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400222 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500223
224 rport = ndlp->rport;
225
James Smart12838e72014-09-03 12:57:19 -0400226 if (!rport) {
227 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smarta93ff372010-10-22 11:06:08 -0400228 return fcf_inuse;
James Smart12838e72014-09-03 12:57:19 -0400229 }
James Smart858c9f62007-06-17 19:56:39 -0500230
231 rdata = rport->dd_data;
232 name = (uint8_t *) &ndlp->nlp_portname;
233 vport = ndlp->vport;
234 phba = vport->phba;
235
James Smarta93ff372010-10-22 11:06:08 -0400236 if (phba->sli_rev == LPFC_SLI_REV4)
237 fcf_inuse = lpfc_fcf_inuse(phba);
238
James Smart858c9f62007-06-17 19:56:39 -0500239 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
240 "rport devlosstmo:did:x%x type:x%x id:x%x",
241 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
242
James Smart34f5ad82012-08-03 12:35:03 -0400243 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
244 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
245 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
246
James Smarta8adb832007-10-27 13:37:53 -0400247 /* Don't defer this if we are in the process of deleting the vport
248 * or unloading the driver. The unload will cleanup the node
249 * appropriately we just need to cleanup the ndlp rport info here.
250 */
251 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500252 if (ndlp->nlp_sid != NLP_NO_SID) {
253 /* flush the target */
254 lpfc_sli_abort_iocb(vport,
255 &phba->sli.ring[phba->sli.fcp_ring],
256 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
257 }
James Smarta8adb832007-10-27 13:37:53 -0400258 put_node = rdata->pnode != NULL;
259 put_rport = ndlp->rport != NULL;
260 rdata->pnode = NULL;
261 ndlp->rport = NULL;
James Smart12838e72014-09-03 12:57:19 -0400262 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smarta8adb832007-10-27 13:37:53 -0400263 if (put_node)
264 lpfc_nlp_put(ndlp);
265 if (put_rport)
266 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400267 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400268 }
269
James Smartd7c255b2008-08-24 21:50:00 -0400270 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
271 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
272 "0284 Devloss timeout Ignored on "
273 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
274 "NPort x%x\n",
275 *name, *(name+1), *(name+2), *(name+3),
276 *(name+4), *(name+5), *(name+6), *(name+7),
277 ndlp->nlp_DID);
James Smart12838e72014-09-03 12:57:19 -0400278 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smarta93ff372010-10-22 11:06:08 -0400279 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400280 }
James Smart858c9f62007-06-17 19:56:39 -0500281
James Smart92d7f7b2007-06-17 19:56:38 -0500282 if (ndlp->nlp_type & NLP_FABRIC) {
283 /* We will clean up these Nodes in linkup */
284 put_node = rdata->pnode != NULL;
285 put_rport = ndlp->rport != NULL;
286 rdata->pnode = NULL;
287 ndlp->rport = NULL;
James Smart12838e72014-09-03 12:57:19 -0400288 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smart92d7f7b2007-06-17 19:56:38 -0500289 if (put_node)
290 lpfc_nlp_put(ndlp);
291 if (put_rport)
292 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400293 return fcf_inuse;
James Smart92d7f7b2007-06-17 19:56:38 -0500294 }
James Smart82085712007-04-25 09:52:41 -0400295
dea31012005-04-17 16:05:31 -0500296 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400297 warn_on = 1;
dea31012005-04-17 16:05:31 -0500298 /* flush the target */
James Smart7ba36ef2014-09-03 12:57:43 -0400299 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smart51ef4c22007-08-02 11:10:31 -0400300 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
301 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500302 }
James Smartc01f3202006-08-18 17:47:08 -0400303
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400304 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400305 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
306 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400307 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
308 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400309 *name, *(name+1), *(name+2), *(name+3),
310 *(name+4), *(name+5), *(name+6), *(name+7),
311 ndlp->nlp_DID, ndlp->nlp_flag,
312 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400313 } else {
James Smarte8b62012007-08-02 11:10:09 -0400314 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
315 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400316 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
317 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400318 *name, *(name+1), *(name+2), *(name+3),
319 *(name+4), *(name+5), *(name+6), *(name+7),
320 ndlp->nlp_DID, ndlp->nlp_flag,
321 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400322 }
323
James Smart87af33f2007-10-27 13:37:43 -0400324 put_node = rdata->pnode != NULL;
325 put_rport = ndlp->rport != NULL;
326 rdata->pnode = NULL;
327 ndlp->rport = NULL;
James Smart12838e72014-09-03 12:57:19 -0400328 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smart87af33f2007-10-27 13:37:43 -0400329 if (put_node)
330 lpfc_nlp_put(ndlp);
331 if (put_rport)
332 put_device(&rport->dev);
333
James Smart2e0fef82007-06-17 19:56:36 -0500334 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400335 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400336 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400337 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400338 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
339 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500340 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400341
James Smarta93ff372010-10-22 11:06:08 -0400342 return fcf_inuse;
343}
344
345/**
346 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
347 * @phba: Pointer to hba context object.
348 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
349 * @nlp_did: remote node identifer with devloss timeout.
350 *
351 * This function is called from the worker thread after invoking devloss
352 * timeout handler and releasing the reference count for the ndlp with
353 * which the devloss timeout was handled for SLI4 host. For the devloss
354 * timeout of the last remote node which had been in use of FCF, when this
355 * routine is invoked, it shall be guaranteed that none of the remote are
356 * in-use of FCF. When devloss timeout to the last remote using the FCF,
357 * if the FIP engine is neither in FCF table scan process nor roundrobin
358 * failover process, the in-use FCF shall be unregistered. If the FIP
359 * engine is in FCF discovery process, the devloss timeout state shall
360 * be set for either the FCF table scan process or roundrobin failover
361 * process to unregister the in-use FCF.
362 **/
363static void
364lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
365 uint32_t nlp_did)
366{
367 /* If devloss timeout happened to a remote node when FCF had no
368 * longer been in-use, do nothing.
369 */
370 if (!fcf_inuse)
371 return;
372
373 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
374 spin_lock_irq(&phba->hbalock);
375 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
376 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
377 spin_unlock_irq(&phba->hbalock);
378 return;
379 }
380 phba->hba_flag |= HBA_DEVLOSS_TMO;
381 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
382 "2847 Last remote node (x%x) using "
383 "FCF devloss tmo\n", nlp_did);
384 }
385 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
386 spin_unlock_irq(&phba->hbalock);
387 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
388 "2868 Devloss tmo to FCF rediscovery "
389 "in progress\n");
390 return;
391 }
392 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
393 spin_unlock_irq(&phba->hbalock);
394 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
395 "2869 Devloss tmo to idle FIP engine, "
396 "unreg in-use FCF and rescan.\n");
397 /* Unregister in-use FCF and rescan */
398 lpfc_unregister_fcf_rescan(phba);
399 return;
400 }
401 spin_unlock_irq(&phba->hbalock);
402 if (phba->hba_flag & FCF_TS_INPROG)
403 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
404 "2870 FCF table scan in progress\n");
405 if (phba->hba_flag & FCF_RR_INPROG)
406 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
407 "2871 FLOGI roundrobin FCF failover "
408 "in progress\n");
409 }
James Smart6fb120a2009-05-22 14:52:59 -0400410 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500411}
James Smartc01f3202006-08-18 17:47:08 -0400412
James Smartea2151b2008-09-07 11:52:10 -0400413/**
James Smart3621a712009-04-06 18:47:14 -0400414 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400415 * @phba: Pointer to hba context object.
416 *
417 * This function is called from the functions which need to post
418 * events from interrupt context. This function allocates data
419 * structure required for posting event. It also keeps track of
420 * number of events pending and prevent event storm when there are
421 * too many events.
422 **/
423struct lpfc_fast_path_event *
424lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
425 struct lpfc_fast_path_event *ret;
426
427 /* If there are lot of fast event do not exhaust memory due to this */
428 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
429 return NULL;
430
431 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
432 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400433 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400434 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400435 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
436 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
437 }
James Smartea2151b2008-09-07 11:52:10 -0400438 return ret;
439}
440
441/**
James Smart3621a712009-04-06 18:47:14 -0400442 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400443 * @phba: Pointer to hba context object.
444 * @evt: Event object which need to be freed.
445 *
446 * This function frees the data structure required for posting
447 * events.
448 **/
449void
450lpfc_free_fast_evt(struct lpfc_hba *phba,
451 struct lpfc_fast_path_event *evt) {
452
453 atomic_dec(&phba->fast_event_count);
454 kfree(evt);
455}
456
457/**
James Smart3621a712009-04-06 18:47:14 -0400458 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400459 * @phba: Pointer to hba context object.
460 * @evtp: Event data structure.
461 *
462 * This function is called from worker thread, when the interrupt
463 * context need to post an event. This function posts the event
464 * to fc transport netlink interface.
465 **/
466static void
467lpfc_send_fastpath_evt(struct lpfc_hba *phba,
468 struct lpfc_work_evt *evtp)
469{
470 unsigned long evt_category, evt_sub_category;
471 struct lpfc_fast_path_event *fast_evt_data;
472 char *evt_data;
473 uint32_t evt_data_size;
474 struct Scsi_Host *shost;
475
476 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
477 work_evt);
478
479 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
480 evt_sub_category = (unsigned long) fast_evt_data->un.
481 fabric_evt.subcategory;
482 shost = lpfc_shost_from_vport(fast_evt_data->vport);
483 if (evt_category == FC_REG_FABRIC_EVENT) {
484 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
485 evt_data = (char *) &fast_evt_data->un.read_check_error;
486 evt_data_size = sizeof(fast_evt_data->un.
487 read_check_error);
488 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500489 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400490 evt_data = (char *) &fast_evt_data->un.fabric_evt;
491 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
492 } else {
493 lpfc_free_fast_evt(phba, fast_evt_data);
494 return;
495 }
496 } else if (evt_category == FC_REG_SCSI_EVENT) {
497 switch (evt_sub_category) {
498 case LPFC_EVENT_QFULL:
499 case LPFC_EVENT_DEVBSY:
500 evt_data = (char *) &fast_evt_data->un.scsi_evt;
501 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
502 break;
503 case LPFC_EVENT_CHECK_COND:
504 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
505 evt_data_size = sizeof(fast_evt_data->un.
506 check_cond_evt);
507 break;
508 case LPFC_EVENT_VARQUEDEPTH:
509 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
510 evt_data_size = sizeof(fast_evt_data->un.
511 queue_depth_evt);
512 break;
513 default:
514 lpfc_free_fast_evt(phba, fast_evt_data);
515 return;
516 }
517 } else {
518 lpfc_free_fast_evt(phba, fast_evt_data);
519 return;
520 }
521
522 fc_host_post_vendor_event(shost,
523 fc_get_event_number(),
524 evt_data_size,
525 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500526 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400527
528 lpfc_free_fast_evt(phba, fast_evt_data);
529 return;
530}
531
dea31012005-04-17 16:05:31 -0500532static void
James Smart2e0fef82007-06-17 19:56:36 -0500533lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500534{
535 struct lpfc_work_evt *evtp = NULL;
536 struct lpfc_nodelist *ndlp;
537 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400538 int fcf_inuse;
539 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500540
James Smart2e0fef82007-06-17 19:56:36 -0500541 spin_lock_irq(&phba->hbalock);
542 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500543 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
544 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500545 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500546 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500547 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500548 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500549 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500550 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500551 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500552 /* decrement the node reference count held
553 * for this queued work
554 */
555 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500556 break;
James Smart858c9f62007-06-17 19:56:39 -0500557 case LPFC_EVT_DEV_LOSS:
558 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400559 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500560 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500561 /* decrement the node reference count held for
562 * this queued work
563 */
James Smarta93ff372010-10-22 11:06:08 -0400564 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500565 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400566 if (phba->sli_rev == LPFC_SLI_REV4)
567 lpfc_sli4_post_dev_loss_tmo_handler(phba,
568 fcf_inuse,
569 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500570 break;
dea31012005-04-17 16:05:31 -0500571 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500572 if (phba->link_state < LPFC_LINK_DOWN)
573 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500574 else
James Smart2e0fef82007-06-17 19:56:36 -0500575 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500576 complete((struct completion *)(evtp->evt_arg2));
577 break;
James Smart46fa3112007-04-25 09:51:45 -0400578 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500579 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400580 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400581 *(int *)(evtp->evt_arg1) = 0;
582 complete((struct completion *)(evtp->evt_arg2));
583 break;
584 case LPFC_EVT_OFFLINE:
585 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500586 lpfc_sli_brdrestart(phba);
587 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400588 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
589 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500590 complete((struct completion *)(evtp->evt_arg2));
591 break;
592 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400593 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500594 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500595 lpfc_sli_brdreset(phba);
596 lpfc_hba_down_post(phba);
597 *(int *)(evtp->evt_arg1) =
598 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400599 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500600 complete((struct completion *)(evtp->evt_arg2));
601 break;
602 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400603 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500604 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500605 = (phba->pport->stopped)
606 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400607 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500608 complete((struct completion *)(evtp->evt_arg2));
609 break;
James Smartea2151b2008-09-07 11:52:10 -0400610 case LPFC_EVT_FASTPATH_MGMT_EVT:
611 lpfc_send_fastpath_evt(phba, evtp);
612 free_evt = 0;
613 break;
James Smart78730cf2010-04-06 15:06:30 -0400614 case LPFC_EVT_RESET_HBA:
615 if (!(phba->pport->load_flag & FC_UNLOADING))
616 lpfc_reset_hba(phba);
617 break;
dea31012005-04-17 16:05:31 -0500618 }
619 if (free_evt)
620 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500621 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500622 }
James Smart2e0fef82007-06-17 19:56:36 -0500623 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500624
625}
626
James Smart311464e2007-08-02 11:10:37 -0400627static void
James Smart2e0fef82007-06-17 19:56:36 -0500628lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500629{
630 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500631 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400632 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400633 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400634 int i;
dea31012005-04-17 16:05:31 -0500635
James Smart2e0fef82007-06-17 19:56:36 -0500636 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500637 ha_copy = phba->work_ha;
638 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500639 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500640
James Smartda0436e2009-05-22 14:51:39 -0400641 /* First, try to post the next mailbox command to SLI4 device */
642 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
643 lpfc_sli4_post_async_mbox(phba);
644
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500645 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400646 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500647 lpfc_handle_eratt(phba);
648
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500649 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500650 lpfc_sli_handle_mb_event(phba);
651
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500652 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500653 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400654
James Smartda0436e2009-05-22 14:51:39 -0400655 /* Process SLI4 events */
656 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500657 if (phba->hba_flag & HBA_RRQ_ACTIVE)
658 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400659 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
660 lpfc_sli4_fcp_xri_abort_event_proc(phba);
661 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
662 lpfc_sli4_els_xri_abort_event_proc(phba);
663 if (phba->hba_flag & ASYNC_EVENT)
664 lpfc_sli4_async_event_proc(phba);
665 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
666 spin_lock_irq(&phba->hbalock);
667 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
668 spin_unlock_irq(&phba->hbalock);
669 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
670 }
James Smartecfd03c2010-02-12 14:41:27 -0500671 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
672 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400673 }
674
James Smart549e55c2007-08-02 11:09:51 -0400675 vports = lpfc_create_vport_work_array(phba);
676 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400677 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400678 /*
679 * We could have no vports in array if unloading, so if
680 * this happens then just use the pport
681 */
682 if (vports[i] == NULL && i == 0)
683 vport = phba->pport;
684 else
685 vport = vports[i];
686 if (vport == NULL)
687 break;
James Smart58da1ff2008-04-07 10:15:56 -0400688 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400689 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400690 vport->work_port_events &= ~work_port_events;
691 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400692 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400693 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400694 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400695 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400696 if (work_port_events & WORKER_HB_TMO)
697 lpfc_hb_timeout_handler(phba);
698 if (work_port_events & WORKER_MBOX_TMO)
699 lpfc_mbox_timeout_handler(phba);
700 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
701 lpfc_unblock_fabric_iocbs(phba);
702 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400703 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400704 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
705 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500706 if (work_port_events & WORKER_DELAYED_DISC_TMO)
707 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500708 }
James Smart09372822008-01-11 01:52:54 -0500709 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500710
James Smart858c9f62007-06-17 19:56:39 -0500711 pring = &phba->sli.ring[LPFC_ELS_RING];
712 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
713 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400714 if ((status & HA_RXMASK) ||
715 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400716 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400717 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500718 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400719 /* Set the lpfc data pending flag */
720 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500721 } else {
James Smart94661502013-03-01 16:37:29 -0500722 if (phba->link_state >= LPFC_LINK_UP) {
723 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
724 lpfc_sli_handle_slow_ring_event(phba, pring,
725 (status &
726 HA_RXMASK));
727 }
James Smart858c9f62007-06-17 19:56:39 -0500728 }
James Smart0e9bb8d2013-03-01 16:35:12 -0500729 if ((phba->sli_rev == LPFC_SLI_REV4) &
730 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400731 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500732 /*
733 * Turn on Ring interrupts
734 */
James Smart3772a992009-05-22 14:50:54 -0400735 if (phba->sli_rev <= LPFC_SLI_REV3) {
736 spin_lock_irq(&phba->hbalock);
737 control = readl(phba->HCregaddr);
738 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
739 lpfc_debugfs_slow_ring_trc(phba,
740 "WRK Enable ring: cntl:x%x hacopy:x%x",
741 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400742
James Smart3772a992009-05-22 14:50:54 -0400743 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
744 writel(control, phba->HCregaddr);
745 readl(phba->HCregaddr); /* flush */
746 } else {
747 lpfc_debugfs_slow_ring_trc(phba,
748 "WRK Ring ok: cntl:x%x hacopy:x%x",
749 control, ha_copy, 0);
750 }
751 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500752 }
753 }
James Smart2e0fef82007-06-17 19:56:36 -0500754 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500755}
756
dea31012005-04-17 16:05:31 -0500757int
758lpfc_do_work(void *p)
759{
760 struct lpfc_hba *phba = p;
761 int rc;
dea31012005-04-17 16:05:31 -0500762
Dongsheng Yang8698a742014-03-11 18:09:12 +0800763 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400764 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400765 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500766
James Smart3a55b532008-12-04 22:38:54 -0500767 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400768 /* wait and check worker queue activities */
769 rc = wait_event_interruptible(phba->work_waitq,
770 (test_and_clear_bit(LPFC_DATA_READY,
771 &phba->data_flags)
772 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500773 /* Signal wakeup shall terminate the worker thread */
774 if (rc) {
775 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
776 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500777 break;
James Smart3a55b532008-12-04 22:38:54 -0500778 }
dea31012005-04-17 16:05:31 -0500779
James Smart5e9d9b82008-06-14 22:52:53 -0400780 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500781 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500782 }
James Smart3a55b532008-12-04 22:38:54 -0500783 phba->worker_thread = NULL;
784 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
785 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500786 return 0;
787}
788
789/*
790 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300791 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500792 * embedding it in the IOCB.
793 */
794int
James Smart2e0fef82007-06-17 19:56:36 -0500795lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500796 uint32_t evt)
797{
798 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500799 unsigned long flags;
dea31012005-04-17 16:05:31 -0500800
801 /*
802 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
803 * be queued to worker thread for processing
804 */
James Smart92d7f7b2007-06-17 19:56:38 -0500805 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500806 if (!evtp)
807 return 0;
808
809 evtp->evt_arg1 = arg1;
810 evtp->evt_arg2 = arg2;
811 evtp->evt = evt;
812
James Smarted957682007-06-17 19:56:37 -0500813 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400814 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500815 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500816
James Smart5e9d9b82008-06-14 22:52:53 -0400817 lpfc_worker_wake_up(phba);
818
dea31012005-04-17 16:05:31 -0500819 return 1;
820}
821
James Smart92d7f7b2007-06-17 19:56:38 -0500822void
823lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
824{
James Smart09372822008-01-11 01:52:54 -0500825 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500826 struct lpfc_hba *phba = vport->phba;
827 struct lpfc_nodelist *ndlp, *next_ndlp;
828 int rc;
829
830 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500831 if (!NLP_CHK_NODE_ACT(ndlp))
832 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500833 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
834 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400835 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
836 ((vport->port_type == LPFC_NPIV_PORT) &&
837 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500838 lpfc_unreg_rpi(vport, ndlp);
839
840 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400841 if ((phba->sli_rev < LPFC_SLI_REV4) &&
842 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500843 continue;
844 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
845 remove
846 ? NLP_EVT_DEVICE_RM
847 : NLP_EVT_DEVICE_RECOVERY);
848 }
849 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400850 if (phba->sli_rev == LPFC_SLI_REV4)
851 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500852 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500853 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500854 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500855 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500856 }
857}
858
James Smart87af33f2007-10-27 13:37:43 -0400859void
James Smart98c9ea52007-10-27 13:37:33 -0400860lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500861{
James Smart695a8142010-01-26 23:08:03 -0500862 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
863
James Smart45ed1192009-10-02 15:17:02 -0400864 /* Cleanup any outstanding received buffers */
865 lpfc_cleanup_rcv_buffers(vport);
866
James Smart92d7f7b2007-06-17 19:56:38 -0500867 /* Cleanup any outstanding RSCN activity */
868 lpfc_els_flush_rscn(vport);
869
870 /* Cleanup any outstanding ELS commands */
871 lpfc_els_flush_cmd(vport);
872
873 lpfc_cleanup_rpis(vport, 0);
874
James Smart92d7f7b2007-06-17 19:56:38 -0500875 /* Turn off discovery timer if its running */
876 lpfc_can_disctmo(vport);
877}
878
James Smart3772a992009-05-22 14:50:54 -0400879void
James Smart98c9ea52007-10-27 13:37:33 -0400880lpfc_linkdown_port(struct lpfc_vport *vport)
881{
882 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
883
884 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
885
886 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
887 "Link Down: state:x%x rtry:x%x flg:x%x",
888 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
889
890 lpfc_port_link_failure(vport);
891
James Smart92494142011-02-16 12:39:44 -0500892 /* Stop delayed Nport discovery */
893 spin_lock_irq(shost->host_lock);
894 vport->fc_flag &= ~FC_DISC_DELAYED;
895 spin_unlock_irq(shost->host_lock);
896 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400897}
898
dea31012005-04-17 16:05:31 -0500899int
James Smart685f0bf2007-04-25 09:53:08 -0400900lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500901{
James Smart2e0fef82007-06-17 19:56:36 -0500902 struct lpfc_vport *vport = phba->pport;
903 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400904 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400905 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400906 int i;
dea31012005-04-17 16:05:31 -0500907
James Smart3163f722008-02-08 18:50:25 -0500908 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500909 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500910
911 /* Block all SCSI stack I/Os */
912 lpfc_scsi_dev_block(phba);
913
James Smart2e0fef82007-06-17 19:56:36 -0500914 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500915 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500916 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500917 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500918 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500919 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500920 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500921 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500922 }
James Smart549e55c2007-08-02 11:09:51 -0400923 vports = lpfc_create_vport_work_array(phba);
924 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400925 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400926 /* Issue a LINK DOWN event to all nodes */
927 lpfc_linkdown_port(vports[i]);
928 }
James Smart09372822008-01-11 01:52:54 -0500929 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500930 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500931 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
932 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400933 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500934 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500935 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400936 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500937 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500938 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500939 }
940 }
941
dea31012005-04-17 16:05:31 -0500942 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500943 if (phba->pport->fc_flag & FC_PT2PT) {
944 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500945 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
946 if (mb) {
dea31012005-04-17 16:05:31 -0500947 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500948 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500949 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400950 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500951 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500952 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500953 }
954 }
James Smart2e0fef82007-06-17 19:56:36 -0500955 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500956 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500957 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500958 }
James Smart2e0fef82007-06-17 19:56:36 -0500959
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500960 return 0;
dea31012005-04-17 16:05:31 -0500961}
962
James Smart92d7f7b2007-06-17 19:56:38 -0500963static void
964lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500965{
James Smart92d7f7b2007-06-17 19:56:38 -0500966 struct lpfc_nodelist *ndlp;
967
968 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500969 if (!NLP_CHK_NODE_ACT(ndlp))
970 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500971 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
972 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500973 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500974 /* On Linkup its safe to clean up the ndlp
975 * from Fabric connections.
976 */
James Smart92d7f7b2007-06-17 19:56:38 -0500977 if (ndlp->nlp_DID != Fabric_DID)
978 lpfc_unreg_rpi(vport, ndlp);
979 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
980 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500981 /* Fail outstanding IO now since device is
982 * marked for PLOGI.
983 */
James Smart92d7f7b2007-06-17 19:56:38 -0500984 lpfc_unreg_rpi(vport, ndlp);
985 }
986 }
987}
988
989static void
990lpfc_linkup_port(struct lpfc_vport *vport)
991{
992 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500993 struct lpfc_hba *phba = vport->phba;
994
995 if ((vport->load_flag & FC_UNLOADING) != 0)
996 return;
997
James Smart858c9f62007-06-17 19:56:39 -0500998 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
999 "Link Up: top:x%x speed:x%x flg:x%x",
1000 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1001
James Smart92d7f7b2007-06-17 19:56:38 -05001002 /* If NPIV is not enabled, only bring the physical port up */
1003 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1004 (vport != phba->pport))
1005 return;
dea31012005-04-17 16:05:31 -05001006
James Smart2e0fef82007-06-17 19:56:36 -05001007 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -04001008
James Smart2e0fef82007-06-17 19:56:36 -05001009 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001010 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1011 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1012 vport->fc_flag |= FC_NDISC_ACTIVE;
1013 vport->fc_ns_retry = 0;
1014 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001015
James Smart92d7f7b2007-06-17 19:56:38 -05001016 if (vport->fc_flag & FC_LBIT)
1017 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001018
James Smart92d7f7b2007-06-17 19:56:38 -05001019}
1020
1021static int
1022lpfc_linkup(struct lpfc_hba *phba)
1023{
James Smart549e55c2007-08-02 11:09:51 -04001024 struct lpfc_vport **vports;
1025 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001026
1027 phba->link_state = LPFC_LINK_UP;
1028
1029 /* Unblock fabric iocbs if they are blocked */
1030 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1031 del_timer_sync(&phba->fabric_block_timer);
1032
James Smart549e55c2007-08-02 11:09:51 -04001033 vports = lpfc_create_vport_work_array(phba);
1034 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001035 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001036 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001037 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001038
1039 return 0;
1040}
1041
1042/*
1043 * This routine handles processing a CLEAR_LA mailbox
1044 * command upon completion. It is setup in the LPFC_MBOXQ
1045 * as the completion routine when the command is
1046 * handed off to the SLI layer.
1047 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001048static void
James Smart2e0fef82007-06-17 19:56:36 -05001049lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001050{
James Smart2e0fef82007-06-17 19:56:36 -05001051 struct lpfc_vport *vport = pmb->vport;
1052 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1053 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001054 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001055 uint32_t control;
1056
dea31012005-04-17 16:05:31 -05001057 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001058 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001059 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1060 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1061
1062 /* Check for error */
1063 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001064 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001065 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1066 "0320 CLEAR_LA mbxStatus error x%x hba "
1067 "state x%x\n",
1068 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001069 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001070 goto out;
1071 }
1072
James Smart92d7f7b2007-06-17 19:56:38 -05001073 if (vport->port_type == LPFC_PHYSICAL_PORT)
1074 phba->link_state = LPFC_HBA_READY;
1075
1076 spin_lock_irq(&phba->hbalock);
1077 psli->sli_flag |= LPFC_PROCESS_LA;
1078 control = readl(phba->HCregaddr);
1079 control |= HC_LAINT_ENA;
1080 writel(control, phba->HCregaddr);
1081 readl(phba->HCregaddr); /* flush */
1082 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001083 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001084 return;
dea31012005-04-17 16:05:31 -05001085
dea31012005-04-17 16:05:31 -05001086out:
1087 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001088 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1089 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001090 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001091
James Smart2e0fef82007-06-17 19:56:36 -05001092 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001093 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001094 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001095
James Smart2e0fef82007-06-17 19:56:36 -05001096 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001097
1098 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001099
1100 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001101 psli->sli_flag |= LPFC_PROCESS_LA;
1102 control = readl(phba->HCregaddr);
1103 control |= HC_LAINT_ENA;
1104 writel(control, phba->HCregaddr);
1105 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001106 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001107
1108 return;
1109}
1110
James Smart2e0fef82007-06-17 19:56:36 -05001111
dea31012005-04-17 16:05:31 -05001112static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001113lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001114{
James Smart2e0fef82007-06-17 19:56:36 -05001115 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001116
James Smart04c68492009-05-22 14:52:52 -04001117 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001118 goto out;
dea31012005-04-17 16:05:31 -05001119
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001120 mempool_free(pmb, phba->mbox_mem_pool);
1121
James Smart1b511972011-12-13 13:23:09 -05001122 /* don't perform discovery for SLI4 loopback diagnostic test */
1123 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1124 !(phba->hba_flag & HBA_FCOE_MODE) &&
1125 (phba->link_flag & LS_LOOPBACK_MODE))
1126 return;
1127
James Smart76a95d72010-11-20 23:11:48 -05001128 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001129 vport->fc_flag & FC_PUBLIC_LOOP &&
1130 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001131 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001132 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001133 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1134 */
James Smart2e0fef82007-06-17 19:56:36 -05001135 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001136 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001137 }
dea31012005-04-17 16:05:31 -05001138
James Smart2e0fef82007-06-17 19:56:36 -05001139 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001140 * LPFC_FLOGI while waiting for FLOGI cmpl
1141 */
James Smarte6446432012-05-09 21:16:42 -04001142 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001143 lpfc_initial_flogi(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001144 return;
dea31012005-04-17 16:05:31 -05001145
1146out:
James Smarte8b62012007-08-02 11:10:09 -04001147 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1148 "0306 CONFIG_LINK mbxStatus error x%x "
1149 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001150 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001151 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001152
1153 lpfc_linkdown(phba);
1154
James Smarte8b62012007-08-02 11:10:09 -04001155 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1156 "0200 CONFIG_LINK bad hba state x%x\n",
1157 vport->port_state);
dea31012005-04-17 16:05:31 -05001158
James Smart92d7f7b2007-06-17 19:56:38 -05001159 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001160 return;
1161}
1162
James Smart7d791df2011-07-22 18:37:52 -04001163/**
1164 * lpfc_sli4_clear_fcf_rr_bmask
1165 * @phba pointer to the struct lpfc_hba for this port.
1166 * This fucnction resets the round robin bit mask and clears the
1167 * fcf priority list. The list deletions are done while holding the
1168 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1169 * from the lpfc_fcf_pri record.
1170 **/
1171void
1172lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1173{
1174 struct lpfc_fcf_pri *fcf_pri;
1175 struct lpfc_fcf_pri *next_fcf_pri;
1176 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1177 spin_lock_irq(&phba->hbalock);
1178 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1179 &phba->fcf.fcf_pri_list, list) {
1180 list_del_init(&fcf_pri->list);
1181 fcf_pri->fcf_rec.flag = 0;
1182 }
1183 spin_unlock_irq(&phba->hbalock);
1184}
dea31012005-04-17 16:05:31 -05001185static void
James Smart6fb120a2009-05-22 14:52:59 -04001186lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1187{
1188 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001189
1190 if (mboxq->u.mb.mbxStatus) {
1191 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1192 "2017 REG_FCFI mbxStatus error x%x "
1193 "HBA state x%x\n",
1194 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001195 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001196 }
1197
1198 /* Start FCoE discovery by sending a FLOGI. */
1199 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1200 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001201 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001202 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001203 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001204
James Smart32b97932009-07-19 10:01:21 -04001205 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001206 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1207 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001208 goto fail_out;
1209
1210 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001211 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001212 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001213 phba->hba_flag &= ~FCF_TS_INPROG;
1214 if (vport->port_state != LPFC_FLOGI) {
1215 phba->hba_flag |= FCF_RR_INPROG;
1216 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001217 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001218 goto out;
1219 }
1220 spin_unlock_irq(&phba->hbalock);
1221 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001222
James Smarta93ff372010-10-22 11:06:08 -04001223fail_out:
1224 spin_lock_irq(&phba->hbalock);
1225 phba->hba_flag &= ~FCF_RR_INPROG;
1226 spin_unlock_irq(&phba->hbalock);
1227out:
James Smart6fb120a2009-05-22 14:52:59 -04001228 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001229}
1230
1231/**
1232 * lpfc_fab_name_match - Check if the fcf fabric name match.
1233 * @fab_name: pointer to fabric name.
1234 * @new_fcf_record: pointer to fcf record.
1235 *
1236 * This routine compare the fcf record's fabric name with provided
1237 * fabric name. If the fabric name are identical this function
1238 * returns 1 else return 0.
1239 **/
1240static uint32_t
1241lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1242{
James Smartecfd03c2010-02-12 14:41:27 -05001243 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001244 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001245 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1246 return 0;
1247 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1248 return 0;
1249 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1250 return 0;
1251 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1252 return 0;
1253 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1254 return 0;
1255 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1256 return 0;
1257 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1258 return 0;
1259 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001260}
1261
1262/**
James Smart8fa38512009-07-19 10:01:03 -04001263 * lpfc_sw_name_match - Check if the fcf switch name match.
1264 * @fab_name: pointer to fabric name.
1265 * @new_fcf_record: pointer to fcf record.
1266 *
1267 * This routine compare the fcf record's switch name with provided
1268 * switch name. If the switch name are identical this function
1269 * returns 1 else return 0.
1270 **/
1271static uint32_t
1272lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1273{
James Smartecfd03c2010-02-12 14:41:27 -05001274 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001275 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001276 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1277 return 0;
1278 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1279 return 0;
1280 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1281 return 0;
1282 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1283 return 0;
1284 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1285 return 0;
1286 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1287 return 0;
1288 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1289 return 0;
1290 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001291}
1292
1293/**
James Smart6fb120a2009-05-22 14:52:59 -04001294 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001295 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001296 * @new_fcf_record: pointer to fcf record.
1297 *
1298 * This routine compare the fcf record's mac address with HBA's
1299 * FCF mac address. If the mac addresses are identical this function
1300 * returns 1 else return 0.
1301 **/
1302static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001303lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001304{
James Smartecfd03c2010-02-12 14:41:27 -05001305 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001306 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001307 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1308 return 0;
1309 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1310 return 0;
1311 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1312 return 0;
1313 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1314 return 0;
1315 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1316 return 0;
1317 return 1;
1318}
1319
1320static bool
1321lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1322{
1323 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001324}
1325
1326/**
James Smart7d791df2011-07-22 18:37:52 -04001327 * lpfc_update_fcf_record - Update driver fcf record
1328 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1329 * @phba: pointer to lpfc hba data structure.
1330 * @fcf_index: Index for the lpfc_fcf_record.
1331 * @new_fcf_record: pointer to hba fcf record.
1332 *
1333 * This routine updates the driver FCF priority record from the new HBA FCF
1334 * record. This routine is called with the host lock held.
1335 **/
1336static void
1337__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1338 struct fcf_record *new_fcf_record
1339 )
1340{
1341 struct lpfc_fcf_pri *fcf_pri;
1342
1343 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1344 fcf_pri->fcf_rec.fcf_index = fcf_index;
1345 /* FCF record priority */
1346 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1347
1348}
1349
1350/**
James Smart6fb120a2009-05-22 14:52:59 -04001351 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001352 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001353 * @new_fcf_record: pointer to fcf record.
1354 *
1355 * This routine copies the FCF information from the FCF
1356 * record to lpfc_hba data structure.
1357 **/
1358static void
James Smartecfd03c2010-02-12 14:41:27 -05001359lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1360 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001361{
James Smartecfd03c2010-02-12 14:41:27 -05001362 /* Fabric name */
1363 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001364 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001365 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001366 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001367 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001368 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001369 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001370 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001371 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001372 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001373 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001374 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001375 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001376 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001377 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001378 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001379 /* Mac address */
1380 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1381 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1382 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1383 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1384 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1385 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1386 /* FCF record index */
1387 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1388 /* FCF record priority */
1389 fcf_rec->priority = new_fcf_record->fip_priority;
1390 /* Switch name */
1391 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001392 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001393 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001394 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001395 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001396 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001397 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001398 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001399 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001400 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001401 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001402 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001403 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001404 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001405 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001406 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001407}
1408
1409/**
James Smartecfd03c2010-02-12 14:41:27 -05001410 * lpfc_update_fcf_record - Update driver fcf record
1411 * @phba: pointer to lpfc hba data structure.
1412 * @fcf_rec: pointer to driver fcf record.
1413 * @new_fcf_record: pointer to hba fcf record.
1414 * @addr_mode: address mode to be set to the driver fcf record.
1415 * @vlan_id: vlan tag to be set to the driver fcf record.
1416 * @flag: flag bits to be set to the driver fcf record.
1417 *
1418 * This routine updates the driver FCF record from the new HBA FCF record
1419 * together with the address mode, vlan_id, and other informations. This
1420 * routine is called with the host lock held.
1421 **/
1422static void
1423__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1424 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1425 uint16_t vlan_id, uint32_t flag)
1426{
1427 /* Copy the fields from the HBA's FCF record */
1428 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1429 /* Update other fields of driver FCF record */
1430 fcf_rec->addr_mode = addr_mode;
1431 fcf_rec->vlan_id = vlan_id;
1432 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001433 __lpfc_update_fcf_record_pri(phba,
1434 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1435 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001436}
1437
1438/**
James Smart6fb120a2009-05-22 14:52:59 -04001439 * lpfc_register_fcf - Register the FCF with hba.
1440 * @phba: pointer to lpfc hba data structure.
1441 *
1442 * This routine issues a register fcfi mailbox command to register
1443 * the fcf with HBA.
1444 **/
1445static void
1446lpfc_register_fcf(struct lpfc_hba *phba)
1447{
1448 LPFC_MBOXQ_t *fcf_mbxq;
1449 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001450
James Smartd439d282010-09-29 11:18:45 -04001451 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001452 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001453 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001454 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001455 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001456 return;
1457 }
1458
1459 /* The FCF is already registered, start discovery */
1460 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001461 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001462 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001463 if (phba->pport->port_state != LPFC_FLOGI &&
1464 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001465 phba->hba_flag |= FCF_RR_INPROG;
1466 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001467 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001468 return;
1469 }
1470 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001471 return;
1472 }
James Smartd439d282010-09-29 11:18:45 -04001473 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001474
James Smarta93ff372010-10-22 11:06:08 -04001475 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001476 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001477 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001478 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001479 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001480 return;
James Smart4d9ab992009-10-02 15:16:39 -04001481 }
James Smart6fb120a2009-05-22 14:52:59 -04001482
1483 lpfc_reg_fcfi(phba, fcf_mbxq);
1484 fcf_mbxq->vport = phba->pport;
1485 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1486 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001487 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001488 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001489 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001490 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001491 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001492 }
James Smart6fb120a2009-05-22 14:52:59 -04001493
1494 return;
1495}
1496
1497/**
1498 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1499 * @phba: pointer to lpfc hba data structure.
1500 * @new_fcf_record: pointer to fcf record.
1501 * @boot_flag: Indicates if this record used by boot bios.
1502 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001503 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001504 *
1505 * This routine compare the fcf record with connect list obtained from the
1506 * config region to decide if this FCF can be used for SAN discovery. It returns
1507 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1508 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1509 * is used by boot bios and addr_mode will indicate the addressing mode to be
1510 * used for this FCF when the function returns.
1511 * If the FCF record need to be used with a particular vlan id, the vlan is
1512 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001513 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001514 **/
1515static int
1516lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1517 struct fcf_record *new_fcf_record,
1518 uint32_t *boot_flag, uint32_t *addr_mode,
1519 uint16_t *vlan_id)
1520{
1521 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001522 int i, j, fcf_vlan_id = 0;
1523
1524 /* Find the lowest VLAN id in the FCF record */
1525 for (i = 0; i < 512; i++) {
1526 if (new_fcf_record->vlan_bitmap[i]) {
1527 fcf_vlan_id = i * 8;
1528 j = 0;
1529 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1530 j++;
1531 fcf_vlan_id++;
1532 }
1533 break;
1534 }
1535 }
James Smart6fb120a2009-05-22 14:52:59 -04001536
James Smart26979ce2012-09-29 11:31:55 -04001537 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001538 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001539 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1540 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001541 return 0;
1542
James Smart45ed1192009-10-02 15:17:02 -04001543 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001544 *boot_flag = 0;
1545 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1546 new_fcf_record);
1547 if (phba->valid_vlan)
1548 *vlan_id = phba->vlan_id;
1549 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001550 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001551 return 1;
1552 }
1553
1554 /*
1555 * If there are no FCF connection table entry, driver connect to all
1556 * FCFs.
1557 */
1558 if (list_empty(&phba->fcf_conn_rec_list)) {
1559 *boot_flag = 0;
1560 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1561 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001562
1563 /*
1564 * When there are no FCF connect entries, use driver's default
1565 * addressing mode - FPMA.
1566 */
1567 if (*addr_mode & LPFC_FCF_FPMA)
1568 *addr_mode = LPFC_FCF_FPMA;
1569
James Smart4d9ab992009-10-02 15:16:39 -04001570 /* If FCF record report a vlan id use that vlan id */
1571 if (fcf_vlan_id)
1572 *vlan_id = fcf_vlan_id;
1573 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001574 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001575 return 1;
1576 }
1577
James Smartecfd03c2010-02-12 14:41:27 -05001578 list_for_each_entry(conn_entry,
1579 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001580 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1581 continue;
1582
1583 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1584 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001585 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001586 continue;
James Smart8fa38512009-07-19 10:01:03 -04001587 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1588 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1589 new_fcf_record))
1590 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001591 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1592 /*
1593 * If the vlan bit map does not have the bit set for the
1594 * vlan id to be used, then it is not a match.
1595 */
1596 if (!(new_fcf_record->vlan_bitmap
1597 [conn_entry->conn_rec.vlan_tag / 8] &
1598 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1599 continue;
1600 }
1601
1602 /*
James Smart0c287582009-06-10 17:22:56 -04001603 * If connection record does not support any addressing mode,
1604 * skip the FCF record.
1605 */
1606 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1607 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1608 continue;
1609
1610 /*
James Smart6fb120a2009-05-22 14:52:59 -04001611 * Check if the connection record specifies a required
1612 * addressing mode.
1613 */
1614 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1615 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1616
1617 /*
1618 * If SPMA required but FCF not support this continue.
1619 */
1620 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1621 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1622 new_fcf_record) & LPFC_FCF_SPMA))
1623 continue;
1624
1625 /*
1626 * If FPMA required but FCF not support this continue.
1627 */
1628 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1629 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1630 new_fcf_record) & LPFC_FCF_FPMA))
1631 continue;
1632 }
1633
1634 /*
1635 * This fcf record matches filtering criteria.
1636 */
1637 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1638 *boot_flag = 1;
1639 else
1640 *boot_flag = 0;
1641
James Smart0c287582009-06-10 17:22:56 -04001642 /*
1643 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001644 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001645 * by FCF, allow fabric to pick the addressing mode.
1646 */
James Smart6fb120a2009-05-22 14:52:59 -04001647 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1648 new_fcf_record);
1649 /*
1650 * If the user specified a required address mode, assign that
1651 * address mode
1652 */
1653 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1654 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1655 *addr_mode = (conn_entry->conn_rec.flags &
1656 FCFCNCT_AM_SPMA) ?
1657 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1658 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001659 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001660 * addr mode only if FCF support the addr_mode.
1661 */
1662 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1663 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1664 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1665 (*addr_mode & LPFC_FCF_SPMA))
1666 *addr_mode = LPFC_FCF_SPMA;
1667 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1668 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1669 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1670 (*addr_mode & LPFC_FCF_FPMA))
1671 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001672
James Smart4d9ab992009-10-02 15:16:39 -04001673 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001674 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1675 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001676 /*
1677 * If no vlan id is specified in connect list, use the vlan id
1678 * in the FCF record
1679 */
1680 else if (fcf_vlan_id)
1681 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001682 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001683 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001684
1685 return 1;
1686 }
1687
1688 return 0;
1689}
1690
1691/**
James Smart32b97932009-07-19 10:01:21 -04001692 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1693 * @phba: pointer to lpfc hba data structure.
1694 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1695 *
1696 * This function check if there is any fcoe event pending while driver
1697 * scan FCF entries. If there is any pending event, it will restart the
1698 * FCF saning and return 1 else return 0.
1699 */
1700int
1701lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1702{
James Smart32b97932009-07-19 10:01:21 -04001703 /*
1704 * If the Link is up and no FCoE events while in the
1705 * FCF discovery, no need to restart FCF discovery.
1706 */
1707 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001708 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001709 return 0;
1710
James Smart0c9ab6f2010-02-26 14:15:57 -05001711 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1712 "2768 Pending link or FCF event during current "
1713 "handling of the previous event: link_state:x%x, "
1714 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1715 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1716 phba->fcoe_eventtag);
1717
James Smart32b97932009-07-19 10:01:21 -04001718 spin_lock_irq(&phba->hbalock);
1719 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1720 spin_unlock_irq(&phba->hbalock);
1721
James Smart0c9ab6f2010-02-26 14:15:57 -05001722 if (phba->link_state >= LPFC_LINK_UP) {
1723 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1724 "2780 Restart FCF table scan due to "
1725 "pending FCF event:evt_tag_at_scan:x%x, "
1726 "evt_tag_current:x%x\n",
1727 phba->fcoe_eventtag_at_fcf_scan,
1728 phba->fcoe_eventtag);
1729 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1730 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001731 /*
James Smarta93ff372010-10-22 11:06:08 -04001732 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001733 * flag
1734 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001735 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1736 "2833 Stop FCF discovery process due to link "
1737 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001738 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001739 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001740 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001741 spin_unlock_irq(&phba->hbalock);
1742 }
James Smart32b97932009-07-19 10:01:21 -04001743
James Smart0c9ab6f2010-02-26 14:15:57 -05001744 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001745 if (unreg_fcf) {
1746 spin_lock_irq(&phba->hbalock);
1747 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1748 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001749 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001750 }
James Smart32b97932009-07-19 10:01:21 -04001751 return 1;
1752}
1753
1754/**
James Smart999d8132010-03-15 11:24:56 -04001755 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1756 * @phba: pointer to lpfc hba data structure.
1757 * @fcf_cnt: number of eligible fcf record seen so far.
1758 *
1759 * This function makes an running random selection decision on FCF record to
1760 * use through a sequence of @fcf_cnt eligible FCF records with equal
1761 * probability. To perform integer manunipulation of random numbers with
1762 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001763 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001764 *
1765 * Returns true when outcome is for the newly read FCF record should be
1766 * chosen; otherwise, return false when outcome is for keeping the previously
1767 * chosen FCF record.
1768 **/
1769static bool
1770lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1771{
1772 uint32_t rand_num;
1773
1774 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001775 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001776
1777 /* Decision with probability 1/fcf_cnt */
1778 if ((fcf_cnt * rand_num) < 0xFFFF)
1779 return true;
1780 else
1781 return false;
1782}
1783
1784/**
James Smart3804dc82010-07-14 15:31:37 -04001785 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001786 * @phba: pointer to lpfc hba data structure.
1787 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001788 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001789 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001790 * This routine parses the non-embedded fcf mailbox command by performing the
1791 * necessarily error checking, non-embedded read FCF record mailbox command
1792 * SGE parsing, and endianness swapping.
1793 *
1794 * Returns the pointer to the new FCF record in the non-embedded mailbox
1795 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001796 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001797static struct fcf_record *
1798lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1799 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001800{
1801 void *virt_addr;
1802 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001803 struct lpfc_mbx_sge sge;
1804 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1805 uint32_t shdr_status, shdr_add_status;
1806 union lpfc_sli4_cfg_shdr *shdr;
1807 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001808
James Smart6fb120a2009-05-22 14:52:59 -04001809 /* Get the first SGE entry from the non-embedded DMA memory. This
1810 * routine only uses a single SGE.
1811 */
1812 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1813 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1814 if (unlikely(!mboxq->sge_array)) {
1815 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1816 "2524 Failed to get the non-embedded SGE "
1817 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001818 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001819 }
1820 virt_addr = mboxq->sge_array->addr[0];
1821
1822 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001823 lpfc_sli_pcimem_bcopy(shdr, shdr,
1824 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001825 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001826 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001827 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001828 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1829 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001830 "2726 READ_FCF_RECORD Indicates empty "
1831 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001832 else
1833 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001834 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001835 "with status x%x add_status x%x, "
1836 "mbx\n", shdr_status, shdr_add_status);
1837 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001838 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001839
1840 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001841 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1842 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1843 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001844 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001845 new_fcf_record = (struct fcf_record *)(virt_addr +
1846 sizeof(struct lpfc_mbx_read_fcf_tbl));
1847 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001848 offsetof(struct fcf_record, vlan_bitmap));
1849 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1850 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001851
James Smart0c9ab6f2010-02-26 14:15:57 -05001852 return new_fcf_record;
1853}
1854
1855/**
1856 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1857 * @phba: pointer to lpfc hba data structure.
1858 * @fcf_record: pointer to the fcf record.
1859 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1860 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1861 *
1862 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1863 * enabled.
1864 **/
1865static void
1866lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1867 struct fcf_record *fcf_record,
1868 uint16_t vlan_id,
1869 uint16_t next_fcf_index)
1870{
1871 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1872 "2764 READ_FCF_RECORD:\n"
1873 "\tFCF_Index : x%x\n"
1874 "\tFCF_Avail : x%x\n"
1875 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001876 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001877 "\tFIP_Priority : x%x\n"
1878 "\tMAC_Provider : x%x\n"
1879 "\tLowest VLANID : x%x\n"
1880 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1881 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1882 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1883 "\tNext_FCF_Index: x%x\n",
1884 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1885 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1886 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001887 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001888 fcf_record->fip_priority,
1889 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1890 vlan_id,
1891 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1892 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1893 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1894 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1895 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1896 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1897 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1898 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1899 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1900 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1901 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1902 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1903 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1904 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1905 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1906 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1907 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1908 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1909 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1910 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1911 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1912 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1913 next_fcf_index);
1914}
1915
1916/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001917 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1918 * @phba: pointer to lpfc hba data structure.
1919 * @fcf_rec: pointer to an existing FCF record.
1920 * @new_fcf_record: pointer to a new FCF record.
1921 * @new_vlan_id: vlan id from the new FCF record.
1922 *
1923 * This function performs matching test of a new FCF record against an existing
1924 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1925 * will not be used as part of the FCF record matching criteria.
1926 *
1927 * Returns true if all the fields matching, otherwise returns false.
1928 */
1929static bool
1930lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1931 struct lpfc_fcf_rec *fcf_rec,
1932 struct fcf_record *new_fcf_record,
1933 uint16_t new_vlan_id)
1934{
1935 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1936 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1937 return false;
1938 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1939 return false;
1940 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1941 return false;
1942 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1943 return false;
James Smart7d791df2011-07-22 18:37:52 -04001944 if (fcf_rec->priority != new_fcf_record->fip_priority)
1945 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001946 return true;
1947}
1948
1949/**
James Smarta93ff372010-10-22 11:06:08 -04001950 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1951 * @vport: Pointer to vport object.
1952 * @fcf_index: index to next fcf.
1953 *
1954 * This function processing the roundrobin fcf failover to next fcf index.
1955 * When this function is invoked, there will be a current fcf registered
1956 * for flogi.
1957 * Return: 0 for continue retrying flogi on currently registered fcf;
1958 * 1 for stop flogi on currently registered fcf;
1959 */
1960int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1961{
1962 struct lpfc_hba *phba = vport->phba;
1963 int rc;
1964
1965 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1966 spin_lock_irq(&phba->hbalock);
1967 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1968 spin_unlock_irq(&phba->hbalock);
1969 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1970 "2872 Devloss tmo with no eligible "
1971 "FCF, unregister in-use FCF (x%x) "
1972 "and rescan FCF table\n",
1973 phba->fcf.current_rec.fcf_indx);
1974 lpfc_unregister_fcf_rescan(phba);
1975 goto stop_flogi_current_fcf;
1976 }
1977 /* Mark the end to FLOGI roundrobin failover */
1978 phba->hba_flag &= ~FCF_RR_INPROG;
1979 /* Allow action to new fcf asynchronous event */
1980 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1981 spin_unlock_irq(&phba->hbalock);
1982 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1983 "2865 No FCF available, stop roundrobin FCF "
1984 "failover and change port state:x%x/x%x\n",
1985 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1986 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1987 goto stop_flogi_current_fcf;
1988 } else {
1989 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1990 "2794 Try FLOGI roundrobin FCF failover to "
1991 "(x%x)\n", fcf_index);
1992 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1993 if (rc)
1994 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1995 "2761 FLOGI roundrobin FCF failover "
1996 "failed (rc:x%x) to read FCF (x%x)\n",
1997 rc, phba->fcf.current_rec.fcf_indx);
1998 else
1999 goto stop_flogi_current_fcf;
2000 }
2001 return 0;
2002
2003stop_flogi_current_fcf:
2004 lpfc_can_disctmo(vport);
2005 return 1;
2006}
2007
2008/**
James Smart7d791df2011-07-22 18:37:52 -04002009 * lpfc_sli4_fcf_pri_list_del
2010 * @phba: pointer to lpfc hba data structure.
2011 * @fcf_index the index of the fcf record to delete
2012 * This routine checks the on list flag of the fcf_index to be deleted.
2013 * If it is one the list then it is removed from the list, and the flag
2014 * is cleared. This routine grab the hbalock before removing the fcf
2015 * record from the list.
2016 **/
2017static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2018 uint16_t fcf_index)
2019{
2020 struct lpfc_fcf_pri *new_fcf_pri;
2021
2022 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2023 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2024 "3058 deleting idx x%x pri x%x flg x%x\n",
2025 fcf_index, new_fcf_pri->fcf_rec.priority,
2026 new_fcf_pri->fcf_rec.flag);
2027 spin_lock_irq(&phba->hbalock);
2028 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2029 if (phba->fcf.current_rec.priority ==
2030 new_fcf_pri->fcf_rec.priority)
2031 phba->fcf.eligible_fcf_cnt--;
2032 list_del_init(&new_fcf_pri->list);
2033 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2034 }
2035 spin_unlock_irq(&phba->hbalock);
2036}
2037
2038/**
2039 * lpfc_sli4_set_fcf_flogi_fail
2040 * @phba: pointer to lpfc hba data structure.
2041 * @fcf_index the index of the fcf record to update
2042 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2043 * flag so the the round robin slection for the particular priority level
2044 * will try a different fcf record that does not have this bit set.
2045 * If the fcf record is re-read for any reason this flag is cleared brfore
2046 * adding it to the priority list.
2047 **/
2048void
2049lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2050{
2051 struct lpfc_fcf_pri *new_fcf_pri;
2052 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2053 spin_lock_irq(&phba->hbalock);
2054 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2055 spin_unlock_irq(&phba->hbalock);
2056}
2057
2058/**
2059 * lpfc_sli4_fcf_pri_list_add
2060 * @phba: pointer to lpfc hba data structure.
2061 * @fcf_index the index of the fcf record to add
2062 * This routine checks the priority of the fcf_index to be added.
2063 * If it is a lower priority than the current head of the fcf_pri list
2064 * then it is added to the list in the right order.
2065 * If it is the same priority as the current head of the list then it
2066 * is added to the head of the list and its bit in the rr_bmask is set.
2067 * If the fcf_index to be added is of a higher priority than the current
2068 * head of the list then the rr_bmask is cleared, its bit is set in the
2069 * rr_bmask and it is added to the head of the list.
2070 * returns:
2071 * 0=success 1=failure
2072 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002073static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2074 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002075 struct fcf_record *new_fcf_record)
2076{
2077 uint16_t current_fcf_pri;
2078 uint16_t last_index;
2079 struct lpfc_fcf_pri *fcf_pri;
2080 struct lpfc_fcf_pri *next_fcf_pri;
2081 struct lpfc_fcf_pri *new_fcf_pri;
2082 int ret;
2083
2084 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2085 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2086 "3059 adding idx x%x pri x%x flg x%x\n",
2087 fcf_index, new_fcf_record->fip_priority,
2088 new_fcf_pri->fcf_rec.flag);
2089 spin_lock_irq(&phba->hbalock);
2090 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2091 list_del_init(&new_fcf_pri->list);
2092 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2093 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2094 if (list_empty(&phba->fcf.fcf_pri_list)) {
2095 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2096 ret = lpfc_sli4_fcf_rr_index_set(phba,
2097 new_fcf_pri->fcf_rec.fcf_index);
2098 goto out;
2099 }
2100
2101 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2102 LPFC_SLI4_FCF_TBL_INDX_MAX);
2103 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2104 ret = 0; /* Empty rr list */
2105 goto out;
2106 }
2107 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2108 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2109 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2110 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2111 memset(phba->fcf.fcf_rr_bmask, 0,
2112 sizeof(*phba->fcf.fcf_rr_bmask));
2113 /* fcfs_at_this_priority_level = 1; */
2114 phba->fcf.eligible_fcf_cnt = 1;
2115 } else
2116 /* fcfs_at_this_priority_level++; */
2117 phba->fcf.eligible_fcf_cnt++;
2118 ret = lpfc_sli4_fcf_rr_index_set(phba,
2119 new_fcf_pri->fcf_rec.fcf_index);
2120 goto out;
2121 }
2122
2123 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2124 &phba->fcf.fcf_pri_list, list) {
2125 if (new_fcf_pri->fcf_rec.priority <=
2126 fcf_pri->fcf_rec.priority) {
2127 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2128 list_add(&new_fcf_pri->list,
2129 &phba->fcf.fcf_pri_list);
2130 else
2131 list_add(&new_fcf_pri->list,
2132 &((struct lpfc_fcf_pri *)
2133 fcf_pri->list.prev)->list);
2134 ret = 0;
2135 goto out;
2136 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2137 || new_fcf_pri->fcf_rec.priority <
2138 next_fcf_pri->fcf_rec.priority) {
2139 list_add(&new_fcf_pri->list, &fcf_pri->list);
2140 ret = 0;
2141 goto out;
2142 }
2143 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2144 continue;
2145
2146 }
2147 ret = 1;
2148out:
2149 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2150 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2151 spin_unlock_irq(&phba->hbalock);
2152 return ret;
2153}
2154
2155/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002156 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2157 * @phba: pointer to lpfc hba data structure.
2158 * @mboxq: pointer to mailbox object.
2159 *
2160 * This function iterates through all the fcf records available in
2161 * HBA and chooses the optimal FCF record for discovery. After finding
2162 * the FCF for discovery it registers the FCF record and kicks start
2163 * discovery.
2164 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2165 * use an FCF record which matches fabric name and mac address of the
2166 * currently used FCF record.
2167 * If the driver supports only one FCF, it will try to use the FCF record
2168 * used by BOOT_BIOS.
2169 */
2170void
2171lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2172{
2173 struct fcf_record *new_fcf_record;
2174 uint32_t boot_flag, addr_mode;
2175 uint16_t fcf_index, next_fcf_index;
2176 struct lpfc_fcf_rec *fcf_rec = NULL;
2177 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002178 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002179 int rc;
2180
2181 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002182 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002183 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2184 return;
2185 }
2186
2187 /* Parse the FCF record from the non-embedded mailbox command */
2188 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2189 &next_fcf_index);
2190 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002191 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002192 "2765 Mailbox command READ_FCF_RECORD "
2193 "failed to retrieve a FCF record.\n");
2194 /* Let next new FCF event trigger fast failover */
2195 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002196 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002197 spin_unlock_irq(&phba->hbalock);
2198 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2199 return;
2200 }
2201
2202 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002203 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2204 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002205
2206 /* Log the FCF record information if turned on */
2207 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2208 next_fcf_index);
2209
James Smart6fb120a2009-05-22 14:52:59 -04002210 /*
2211 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002213 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002214 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002215 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002216 lpfc_sli4_fcf_pri_list_del(phba,
2217 bf_get(lpfc_fcf_record_fcf_index,
2218 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002219 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002220 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002221 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002222 bf_get(lpfc_fcf_record_fcf_index,
2223 new_fcf_record),
2224 bf_get(lpfc_fcf_record_fcf_avail,
2225 new_fcf_record),
2226 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002227 new_fcf_record),
2228 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002229 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002230 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2231 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2232 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002233 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2234 phba->fcf.current_rec.fcf_indx) {
2235 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2236 "2862 FCF (x%x) matches property "
2237 "of in-use FCF (x%x)\n",
2238 bf_get(lpfc_fcf_record_fcf_index,
2239 new_fcf_record),
2240 phba->fcf.current_rec.fcf_indx);
2241 goto read_next_fcf;
2242 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002243 /*
2244 * In case the current in-use FCF record becomes
2245 * invalid/unavailable during FCF discovery that
2246 * was not triggered by fast FCF failover process,
2247 * treat it as fast FCF failover.
2248 */
2249 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2250 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2251 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2252 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002253 "(x%x), enter FCF failover "
2254 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002255 phba->fcf.current_rec.fcf_indx);
2256 spin_lock_irq(&phba->hbalock);
2257 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2258 spin_unlock_irq(&phba->hbalock);
2259 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2260 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2261 LPFC_FCOE_FCF_GET_FIRST);
2262 return;
2263 }
2264 }
James Smart6fb120a2009-05-22 14:52:59 -04002265 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002266 } else {
2267 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002268 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2269 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002270 if (rc)
2271 goto read_next_fcf;
2272 }
2273
James Smart6fb120a2009-05-22 14:52:59 -04002274 /*
2275 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002276 * FCF record for the discovery. The condition that a rescan
2277 * matches the in-use FCF record: fabric name, switch name, mac
2278 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002279 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002280 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002281 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002282 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2283 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002284 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002285 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2286 phba->fcf.current_rec.fcf_indx) {
2287 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2288 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2289 /* Stop FCF redisc wait timer */
2290 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2291 phba);
2292 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2293 /* Fast failover, mark completed */
2294 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2295 spin_unlock_irq(&phba->hbalock);
2296 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2297 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002298 "FCF (x%x), port_state:x%x, "
2299 "fc_flag:x%x\n",
2300 phba->fcf.current_rec.fcf_indx,
2301 phba->pport->port_state,
2302 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002303 goto out;
2304 } else
2305 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2306 "2863 New FCF (x%x) matches "
2307 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002308 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002309 new_fcf_record),
2310 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002311 }
James Smartecfd03c2010-02-12 14:41:27 -05002312 /*
2313 * Read next FCF record from HBA searching for the matching
2314 * with in-use record only if not during the fast failover
2315 * period. In case of fast failover period, it shall try to
2316 * determine whether the FCF record just read should be the
2317 * next candidate.
2318 */
2319 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002320 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002321 goto read_next_fcf;
2322 }
James Smart6fb120a2009-05-22 14:52:59 -04002323 }
James Smartecfd03c2010-02-12 14:41:27 -05002324 /*
2325 * Update on failover FCF record only if it's in FCF fast-failover
2326 * period; otherwise, update on current FCF record.
2327 */
James Smartfc2b9892010-02-26 14:15:29 -05002328 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2329 fcf_rec = &phba->fcf.failover_rec;
2330 else
James Smartecfd03c2010-02-12 14:41:27 -05002331 fcf_rec = &phba->fcf.current_rec;
2332
James Smart6fb120a2009-05-22 14:52:59 -04002333 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2334 /*
James Smartecfd03c2010-02-12 14:41:27 -05002335 * If the driver FCF record does not have boot flag
2336 * set and new hba fcf record has boot flag set, use
2337 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002338 */
James Smartecfd03c2010-02-12 14:41:27 -05002339 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2340 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002341 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2342 "2837 Update current FCF record "
2343 "(x%x) with new FCF record (x%x)\n",
2344 fcf_rec->fcf_indx,
2345 bf_get(lpfc_fcf_record_fcf_index,
2346 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002347 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2348 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002349 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002350 goto read_next_fcf;
2351 }
2352 /*
James Smartecfd03c2010-02-12 14:41:27 -05002353 * If the driver FCF record has boot flag set and the
2354 * new hba FCF record does not have boot flag, read
2355 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002356 */
James Smartecfd03c2010-02-12 14:41:27 -05002357 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002358 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002359 goto read_next_fcf;
2360 }
2361 /*
James Smartecfd03c2010-02-12 14:41:27 -05002362 * If the new hba FCF record has lower priority value
2363 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002364 */
James Smartfc2b9892010-02-26 14:15:29 -05002365 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002366 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002367 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2368 "2838 Update current FCF record "
2369 "(x%x) with new FCF record (x%x)\n",
2370 fcf_rec->fcf_indx,
2371 bf_get(lpfc_fcf_record_fcf_index,
2372 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002373 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2374 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002375 /* Reset running random FCF selection count */
2376 phba->fcf.eligible_fcf_cnt = 1;
2377 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2378 /* Update running random FCF selection count */
2379 phba->fcf.eligible_fcf_cnt++;
2380 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2381 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002382 if (select_new_fcf) {
2383 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2384 "2839 Update current FCF record "
2385 "(x%x) with new FCF record (x%x)\n",
2386 fcf_rec->fcf_indx,
2387 bf_get(lpfc_fcf_record_fcf_index,
2388 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002389 /* Choose the new FCF by random selection */
2390 __lpfc_update_fcf_record(phba, fcf_rec,
2391 new_fcf_record,
2392 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002393 }
James Smart6fb120a2009-05-22 14:52:59 -04002394 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002395 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002396 goto read_next_fcf;
2397 }
2398 /*
James Smartecfd03c2010-02-12 14:41:27 -05002399 * This is the first suitable FCF record, choose this record for
2400 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002401 */
James Smartecfd03c2010-02-12 14:41:27 -05002402 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002403 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002404 "2840 Update initial FCF candidate "
2405 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002406 bf_get(lpfc_fcf_record_fcf_index,
2407 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002408 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2409 addr_mode, vlan_id, (boot_flag ?
2410 BOOT_ENABLE : 0));
2411 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002412 /* Setup initial running random FCF selection count */
2413 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002414 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002415 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002416 goto read_next_fcf;
2417
2418read_next_fcf:
2419 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002420 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2421 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2422 /*
2423 * Case of FCF fast failover scan
2424 */
2425
2426 /*
2427 * It has not found any suitable FCF record, cancel
2428 * FCF scan inprogress, and do nothing
2429 */
2430 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002431 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002432 "2782 No suitable FCF found: "
2433 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002434 phba->fcoe_eventtag_at_fcf_scan,
2435 bf_get(lpfc_fcf_record_fcf_index,
2436 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002437 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002438 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2439 phba->hba_flag &= ~FCF_TS_INPROG;
2440 spin_unlock_irq(&phba->hbalock);
2441 /* Unregister in-use FCF and rescan */
2442 lpfc_printf_log(phba, KERN_INFO,
2443 LOG_FIP,
2444 "2864 On devloss tmo "
2445 "unreg in-use FCF and "
2446 "rescan FCF table\n");
2447 lpfc_unregister_fcf_rescan(phba);
2448 return;
2449 }
2450 /*
2451 * Let next new FCF event trigger fast failover
2452 */
2453 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002454 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002455 return;
2456 }
2457 /*
2458 * It has found a suitable FCF record that is not
2459 * the same as in-use FCF record, unregister the
2460 * in-use FCF record, replace the in-use FCF record
2461 * with the new FCF record, mark FCF fast failover
2462 * completed, and then start register the new FCF
2463 * record.
2464 */
2465
James Smart0c9ab6f2010-02-26 14:15:57 -05002466 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002467 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002468
2469 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002470 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002471 "2842 Replace in-use FCF (x%x) "
2472 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002473 phba->fcf.current_rec.fcf_indx,
2474 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002475 memcpy(&phba->fcf.current_rec,
2476 &phba->fcf.failover_rec,
2477 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002478 /*
2479 * Mark the fast FCF failover rediscovery completed
2480 * and the start of the first round of the roundrobin
2481 * FCF failover.
2482 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002483 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002484 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002485 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002486 /* Register to the new FCF record */
2487 lpfc_register_fcf(phba);
2488 } else {
2489 /*
2490 * In case of transaction period to fast FCF failover,
2491 * do nothing when search to the end of the FCF table.
2492 */
2493 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2494 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2495 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002496
James Smart7d791df2011-07-22 18:37:52 -04002497 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2498 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002499 /*
2500 * In case the current in-use FCF record no
2501 * longer existed during FCF discovery that
2502 * was not triggered by fast FCF failover
2503 * process, treat it as fast FCF failover.
2504 */
2505 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2506 "2841 In-use FCF record (x%x) "
2507 "not reported, entering fast "
2508 "FCF failover mode scanning.\n",
2509 phba->fcf.current_rec.fcf_indx);
2510 spin_lock_irq(&phba->hbalock);
2511 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2512 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002513 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2514 LPFC_FCOE_FCF_GET_FIRST);
2515 return;
2516 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002517 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002518 lpfc_register_fcf(phba);
2519 }
2520 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002521 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002522 return;
2523
2524out:
2525 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2526 lpfc_register_fcf(phba);
2527
2528 return;
2529}
2530
2531/**
James Smarta93ff372010-10-22 11:06:08 -04002532 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002533 * @phba: pointer to lpfc hba data structure.
2534 * @mboxq: pointer to mailbox object.
2535 *
James Smarta93ff372010-10-22 11:06:08 -04002536 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002537 * read FCF record mailbox command from the eligible FCF record bmask for
2538 * performing the failover. If the FCF read back is not valid/available, it
2539 * fails through to retrying FLOGI to the currently registered FCF again.
2540 * Otherwise, if the FCF read back is valid and available, it will set the
2541 * newly read FCF record to the failover FCF record, unregister currently
2542 * registered FCF record, copy the failover FCF record to the current
2543 * FCF record, and then register the current FCF record before proceeding
2544 * to trying FLOGI on the new failover FCF.
2545 */
2546void
2547lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2548{
2549 struct fcf_record *new_fcf_record;
2550 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002551 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002552 uint16_t current_fcf_index;
2553 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002554 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002555
James Smarta93ff372010-10-22 11:06:08 -04002556 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002557 if (phba->link_state < LPFC_LINK_UP) {
2558 spin_lock_irq(&phba->hbalock);
2559 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002560 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002561 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002562 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002563 }
2564
2565 /* Parse the FCF record from the non-embedded mailbox command */
2566 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2567 &next_fcf_index);
2568 if (!new_fcf_record) {
2569 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2570 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002571 "failed to retrieve a FCF record. "
2572 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2573 phba->fcf.fcf_flag);
2574 lpfc_unregister_fcf_rescan(phba);
2575 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002576 }
2577
2578 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002579 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2580 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002581
2582 /* Log the FCF record information if turned on */
2583 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2584 next_fcf_index);
2585
James Smarta93ff372010-10-22 11:06:08 -04002586 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2587 if (!rc) {
2588 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2589 "2848 Remove ineligible FCF (x%x) from "
2590 "from roundrobin bmask\n", fcf_index);
2591 /* Clear roundrobin bmask bit for ineligible FCF */
2592 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2593 /* Perform next round of roundrobin FCF failover */
2594 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2595 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2596 if (rc)
2597 goto out;
2598 goto error_out;
2599 }
2600
2601 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2602 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2603 "2760 Perform FLOGI roundrobin FCF failover: "
2604 "FCF (x%x) back to FCF (x%x)\n",
2605 phba->fcf.current_rec.fcf_indx, fcf_index);
2606 /* Wait 500 ms before retrying FLOGI to current FCF */
2607 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002608 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002609 goto out;
2610 }
2611
James Smart0c9ab6f2010-02-26 14:15:57 -05002612 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002613 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002614 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2615 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002616 spin_lock_irq(&phba->hbalock);
2617 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2618 new_fcf_record, addr_mode, vlan_id,
2619 (boot_flag ? BOOT_ENABLE : 0));
2620 spin_unlock_irq(&phba->hbalock);
2621
2622 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2623
2624 /* Unregister the current in-use FCF record */
2625 lpfc_unregister_fcf(phba);
2626
2627 /* Replace in-use record with the new record */
2628 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2629 sizeof(struct lpfc_fcf_rec));
2630
2631 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002632 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2633 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002634
James Smarta93ff372010-10-22 11:06:08 -04002635error_out:
2636 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002637out:
2638 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002639}
2640
2641/**
2642 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2643 * @phba: pointer to lpfc hba data structure.
2644 * @mboxq: pointer to mailbox object.
2645 *
2646 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002647 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002648 * failover when a new FCF event happened. If the FCF read back is
2649 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002650 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002651 */
2652void
2653lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2654{
2655 struct fcf_record *new_fcf_record;
2656 uint32_t boot_flag, addr_mode;
2657 uint16_t fcf_index, next_fcf_index;
2658 uint16_t vlan_id;
2659 int rc;
2660
2661 /* If link state is not up, no need to proceed */
2662 if (phba->link_state < LPFC_LINK_UP)
2663 goto out;
2664
2665 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002666 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002667 goto out;
2668
2669 /* Parse the FCF record from the non-embedded mailbox command */
2670 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2671 &next_fcf_index);
2672 if (!new_fcf_record) {
2673 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2674 "2767 Mailbox command READ_FCF_RECORD "
2675 "failed to retrieve a FCF record.\n");
2676 goto out;
2677 }
2678
2679 /* Check the connection list for eligibility */
2680 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2681 &addr_mode, &vlan_id);
2682
2683 /* Log the FCF record information if turned on */
2684 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2685 next_fcf_index);
2686
2687 if (!rc)
2688 goto out;
2689
2690 /* Update the eligible FCF record index bmask */
2691 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002692
2693 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002694
2695out:
2696 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2697}
2698
2699/**
James Smart76a95d72010-11-20 23:11:48 -05002700 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2701 * @phba: pointer to lpfc hba data structure.
2702 * @mboxq: pointer to mailbox data structure.
2703 *
2704 * This function handles completion of init vfi mailbox command.
2705 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002706static void
James Smart76a95d72010-11-20 23:11:48 -05002707lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2708{
2709 struct lpfc_vport *vport = mboxq->vport;
2710
James Smartdf9e1b52011-12-13 13:22:17 -05002711 /*
2712 * VFI not supported on interface type 0, just do the flogi
2713 * Also continue if the VFI is in use - just use the same one.
2714 */
2715 if (mboxq->u.mb.mbxStatus &&
2716 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2717 LPFC_SLI_INTF_IF_TYPE_0) &&
2718 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002719 lpfc_printf_vlog(vport, KERN_ERR,
2720 LOG_MBOX,
2721 "2891 Init VFI mailbox failed 0x%x\n",
2722 mboxq->u.mb.mbxStatus);
2723 mempool_free(mboxq, phba->mbox_mem_pool);
2724 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2725 return;
2726 }
James Smart73d91e52011-10-10 21:32:10 -04002727
James Smart76a95d72010-11-20 23:11:48 -05002728 lpfc_initial_flogi(vport);
2729 mempool_free(mboxq, phba->mbox_mem_pool);
2730 return;
2731}
2732
2733/**
2734 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2735 * @vport: pointer to lpfc_vport data structure.
2736 *
2737 * This function issue a init_vfi mailbox command to initialize the VFI and
2738 * VPI for the physical port.
2739 */
2740void
2741lpfc_issue_init_vfi(struct lpfc_vport *vport)
2742{
2743 LPFC_MBOXQ_t *mboxq;
2744 int rc;
2745 struct lpfc_hba *phba = vport->phba;
2746
2747 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2748 if (!mboxq) {
2749 lpfc_printf_vlog(vport, KERN_ERR,
2750 LOG_MBOX, "2892 Failed to allocate "
2751 "init_vfi mailbox\n");
2752 return;
2753 }
2754 lpfc_init_vfi(mboxq, vport);
2755 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2756 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2757 if (rc == MBX_NOT_FINISHED) {
2758 lpfc_printf_vlog(vport, KERN_ERR,
2759 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2760 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2761 }
2762}
2763
2764/**
James Smart1c6834a2009-07-19 10:01:26 -04002765 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2766 * @phba: pointer to lpfc hba data structure.
2767 * @mboxq: pointer to mailbox data structure.
2768 *
2769 * This function handles completion of init vpi mailbox command.
2770 */
James Smart695a8142010-01-26 23:08:03 -05002771void
James Smart1c6834a2009-07-19 10:01:26 -04002772lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2773{
2774 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002775 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002776 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2777
James Smart1c6834a2009-07-19 10:01:26 -04002778 if (mboxq->u.mb.mbxStatus) {
2779 lpfc_printf_vlog(vport, KERN_ERR,
2780 LOG_MBOX,
2781 "2609 Init VPI mailbox failed 0x%x\n",
2782 mboxq->u.mb.mbxStatus);
2783 mempool_free(mboxq, phba->mbox_mem_pool);
2784 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2785 return;
2786 }
James Smart72100cc2010-02-12 14:43:01 -05002787 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002788 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002789 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002790
James Smart695a8142010-01-26 23:08:03 -05002791 /* If this port is physical port or FDISC is done, do reg_vpi */
2792 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2793 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2794 if (!ndlp)
2795 lpfc_printf_vlog(vport, KERN_ERR,
2796 LOG_DISCOVERY,
2797 "2731 Cannot find fabric "
2798 "controller node\n");
2799 else
2800 lpfc_register_new_vport(phba, vport, ndlp);
2801 mempool_free(mboxq, phba->mbox_mem_pool);
2802 return;
2803 }
2804
James Smart1c6834a2009-07-19 10:01:26 -04002805 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2806 lpfc_initial_fdisc(vport);
2807 else {
2808 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002809 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2810 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002811 }
James Smart695a8142010-01-26 23:08:03 -05002812 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002813 return;
2814}
2815
2816/**
James Smartecfd03c2010-02-12 14:41:27 -05002817 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2818 * @vport: pointer to lpfc_vport data structure.
2819 *
2820 * This function issue a init_vpi mailbox command to initialize
2821 * VPI for the vport.
2822 */
2823void
2824lpfc_issue_init_vpi(struct lpfc_vport *vport)
2825{
2826 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002827 int rc, vpi;
2828
2829 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2830 vpi = lpfc_alloc_vpi(vport->phba);
2831 if (!vpi) {
2832 lpfc_printf_vlog(vport, KERN_ERR,
2833 LOG_MBOX,
2834 "3303 Failed to obtain vport vpi\n");
2835 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2836 return;
2837 }
2838 vport->vpi = vpi;
2839 }
James Smartecfd03c2010-02-12 14:41:27 -05002840
2841 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2842 if (!mboxq) {
2843 lpfc_printf_vlog(vport, KERN_ERR,
2844 LOG_MBOX, "2607 Failed to allocate "
2845 "init_vpi mailbox\n");
2846 return;
2847 }
2848 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2849 mboxq->vport = vport;
2850 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2851 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2852 if (rc == MBX_NOT_FINISHED) {
2853 lpfc_printf_vlog(vport, KERN_ERR,
2854 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2855 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2856 }
2857}
2858
2859/**
James Smart6fb120a2009-05-22 14:52:59 -04002860 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2861 * @phba: pointer to lpfc hba data structure.
2862 *
2863 * This function loops through the list of vports on the @phba and issues an
2864 * FDISC if possible.
2865 */
2866void
2867lpfc_start_fdiscs(struct lpfc_hba *phba)
2868{
2869 struct lpfc_vport **vports;
2870 int i;
2871
2872 vports = lpfc_create_vport_work_array(phba);
2873 if (vports != NULL) {
2874 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2875 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2876 continue;
2877 /* There are no vpi for this vport */
2878 if (vports[i]->vpi > phba->max_vpi) {
2879 lpfc_vport_set_state(vports[i],
2880 FC_VPORT_FAILED);
2881 continue;
2882 }
James Smart76a95d72010-11-20 23:11:48 -05002883 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002884 lpfc_vport_set_state(vports[i],
2885 FC_VPORT_LINKDOWN);
2886 continue;
2887 }
James Smart1c6834a2009-07-19 10:01:26 -04002888 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002889 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002890 continue;
2891 }
James Smart6fb120a2009-05-22 14:52:59 -04002892 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2893 lpfc_initial_fdisc(vports[i]);
2894 else {
2895 lpfc_vport_set_state(vports[i],
2896 FC_VPORT_NO_FABRIC_SUPP);
2897 lpfc_printf_vlog(vports[i], KERN_ERR,
2898 LOG_ELS,
2899 "0259 No NPIV "
2900 "Fabric support\n");
2901 }
2902 }
2903 }
2904 lpfc_destroy_vport_work_array(phba, vports);
2905}
2906
2907void
2908lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2909{
2910 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2911 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002912 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002913
James Smartf5eca9b2012-03-01 22:37:54 -05002914 /*
2915 * VFI not supported for interface type 0, so ignore any mailbox
2916 * error (except VFI in use) and continue with the discovery.
2917 */
2918 if (mboxq->u.mb.mbxStatus &&
2919 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2920 LPFC_SLI_INTF_IF_TYPE_0) &&
2921 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002922 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2923 "2018 REG_VFI mbxStatus error x%x "
2924 "HBA state x%x\n",
2925 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002926 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002927 /* FLOGI failed, use loop map to make discovery list */
2928 lpfc_disc_list_loopmap(vport);
2929 /* Start discovery */
2930 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002931 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002932 }
2933 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002934 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002935 }
James Smartae05ebe2013-03-01 16:35:38 -05002936
James Smarte74c03c2013-04-17 20:15:19 -04002937 /* If the VFI is already registered, there is nothing else to do
2938 * Unless this was a VFI update and we are in PT2PT mode, then
2939 * we should drop through to set the port state to ready.
2940 */
James Smartae05ebe2013-03-01 16:35:38 -05002941 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002942 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2943 vport->fc_flag & FC_PT2PT))
2944 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002945
James Smartc8685952009-11-18 15:39:16 -05002946 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002947 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002948 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002949 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002950 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002951 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002952 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002953
James Smart1b511972011-12-13 13:23:09 -05002954 /* In case SLI4 FC loopback test, we are ready */
2955 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2956 (phba->link_flag & LS_LOOPBACK_MODE)) {
2957 phba->link_state = LPFC_HBA_READY;
2958 goto out_free_mem;
2959 }
2960
James Smarte74c03c2013-04-17 20:15:19 -04002961 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2962 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2963 "alpacnt:%d LinkState:%x topology:%x\n",
2964 vport->port_state, vport->fc_flag, vport->fc_myDID,
2965 vport->phba->alpa_map[0],
2966 phba->link_state, phba->fc_topology);
2967
James Smart6fb120a2009-05-22 14:52:59 -04002968 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002969 /*
2970 * For private loop or for NPort pt2pt,
2971 * just start discovery and we are done.
2972 */
2973 if ((vport->fc_flag & FC_PT2PT) ||
2974 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2975 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2976
James Smart76a95d72010-11-20 23:11:48 -05002977 /* Use loop map to make discovery list */
2978 lpfc_disc_list_loopmap(vport);
2979 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002980 if (vport->fc_flag & FC_PT2PT)
2981 vport->port_state = LPFC_VPORT_READY;
2982 else
2983 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002984 } else {
2985 lpfc_start_fdiscs(phba);
2986 lpfc_do_scr_ns_plogi(phba, vport);
2987 }
James Smart6fb120a2009-05-22 14:52:59 -04002988 }
2989
James Smart1b511972011-12-13 13:23:09 -05002990out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002991 mempool_free(mboxq, phba->mbox_mem_pool);
2992 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2993 kfree(dmabuf);
2994 return;
2995}
2996
2997static void
James Smart2e0fef82007-06-17 19:56:36 -05002998lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002999{
James Smart6fb120a2009-05-22 14:52:59 -04003000 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003001 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003002 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003003
3004
3005 /* Check for error */
3006 if (mb->mbxStatus) {
3007 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003008 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3009 "0319 READ_SPARAM mbxStatus error x%x "
3010 "hba state x%x>\n",
3011 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003012 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003013 goto out;
3014 }
3015
James Smart2e0fef82007-06-17 19:56:36 -05003016 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003017 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04003018 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003019 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3020 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3021 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3022 }
3023
dea31012005-04-17 16:05:31 -05003024 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3025 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003026 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003027 return;
3028
3029out:
3030 pmb->context1 = NULL;
3031 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3032 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003033 lpfc_issue_clear_la(phba, vport);
3034 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003035 return;
3036}
3037
3038static void
James Smart76a95d72010-11-20 23:11:48 -05003039lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003040{
James Smart92d7f7b2007-06-17 19:56:38 -05003041 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003042 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003043 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003044 int i;
James Smart14691152006-12-02 13:34:28 -05003045 struct lpfc_dmabuf *mp;
3046 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003047 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003048 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003049
James Smart92d7f7b2007-06-17 19:56:38 -05003050 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003051 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3052 case LPFC_LINK_SPEED_1GHZ:
3053 case LPFC_LINK_SPEED_2GHZ:
3054 case LPFC_LINK_SPEED_4GHZ:
3055 case LPFC_LINK_SPEED_8GHZ:
3056 case LPFC_LINK_SPEED_10GHZ:
3057 case LPFC_LINK_SPEED_16GHZ:
3058 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04003059 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003060 default:
James Smart76a95d72010-11-20 23:11:48 -05003061 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05003062 break;
dea31012005-04-17 16:05:31 -05003063 }
3064
James Smarte74c03c2013-04-17 20:15:19 -04003065 if (phba->fc_topology &&
3066 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3067 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3068 "3314 Toplogy changed was 0x%x is 0x%x\n",
3069 phba->fc_topology,
3070 bf_get(lpfc_mbx_read_top_topology, la));
3071 phba->fc_topology_changed = 1;
3072 }
3073
James Smart76a95d72010-11-20 23:11:48 -05003074 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003075 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003076
James Smartdf9e1b52011-12-13 13:22:17 -05003077 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003078 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003079 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003080
James Smart6a9c52c2009-10-02 15:16:51 -04003081 /* if npiv is enabled and this adapter supports npiv log
3082 * a message that npiv is not supported in this topology
3083 */
3084 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003085 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3086 "1309 Link Up Event npiv not supported in loop "
3087 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003088 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003089 if (bf_get(lpfc_mbx_read_top_il, la))
3090 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003091
James Smart76a95d72010-11-20 23:11:48 -05003092 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3093 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003094
3095 if (i == 0) {
3096 phba->alpa_map[0] = 0;
3097 } else {
James Smarte8b62012007-08-02 11:10:09 -04003098 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003099 int numalpa, j, k;
3100 union {
3101 uint8_t pamap[16];
3102 struct {
3103 uint32_t wd1;
3104 uint32_t wd2;
3105 uint32_t wd3;
3106 uint32_t wd4;
3107 } pa;
3108 } un;
3109 numalpa = phba->alpa_map[0];
3110 j = 0;
3111 while (j < numalpa) {
3112 memset(un.pamap, 0, 16);
3113 for (k = 1; j < numalpa; k++) {
3114 un.pamap[k - 1] =
3115 phba->alpa_map[j + 1];
3116 j++;
3117 if (k == 16)
3118 break;
3119 }
3120 /* Link Up Event ALPA map */
3121 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003122 KERN_WARNING,
3123 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003124 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003125 "ALPA map Data: x%x "
3126 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003127 un.pa.wd1, un.pa.wd2,
3128 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003129 }
3130 }
3131 }
3132 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003133 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003134 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003135 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003136 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3137 }
James Smart2e0fef82007-06-17 19:56:36 -05003138 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003139 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003140 }
James Smart92d7f7b2007-06-17 19:56:38 -05003141 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003142
James Smart38c20672013-03-01 16:37:44 -05003143 if (fc_flags) {
3144 spin_lock_irq(shost->host_lock);
3145 vport->fc_flag |= fc_flags;
3146 spin_unlock_irq(shost->host_lock);
3147 }
3148
dea31012005-04-17 16:05:31 -05003149 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003150 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3151 if (!sparam_mbox)
3152 goto out;
3153
3154 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3155 if (rc) {
3156 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3157 goto out;
3158 }
3159 sparam_mbox->vport = vport;
3160 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3161 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3162 if (rc == MBX_NOT_FINISHED) {
3163 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3164 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3165 kfree(mp);
3166 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3167 goto out;
dea31012005-04-17 16:05:31 -05003168 }
3169
James Smart76a95d72010-11-20 23:11:48 -05003170 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003171 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3172 if (!cfglink_mbox)
3173 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003174 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003175 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003176 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003177 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003178 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003179 if (rc == MBX_NOT_FINISHED) {
3180 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3181 goto out;
3182 }
3183 } else {
James Smart32b97932009-07-19 10:01:21 -04003184 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003185 /*
3186 * Add the driver's default FCF record at FCF index 0 now. This
3187 * is phase 1 implementation that support FCF index 0 and driver
3188 * defaults.
3189 */
James Smart45ed1192009-10-02 15:17:02 -04003190 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003191 fcf_record = kzalloc(sizeof(struct fcf_record),
3192 GFP_KERNEL);
3193 if (unlikely(!fcf_record)) {
3194 lpfc_printf_log(phba, KERN_ERR,
3195 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003196 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003197 "fcf record\n");
3198 rc = -ENODEV;
3199 goto out;
3200 }
3201
3202 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3203 LPFC_FCOE_FCF_DEF_INDEX);
3204 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3205 if (unlikely(rc)) {
3206 lpfc_printf_log(phba, KERN_ERR,
3207 LOG_MBOX | LOG_SLI,
3208 "2013 Could not manually add FCF "
3209 "record 0, status %d\n", rc);
3210 rc = -ENODEV;
3211 kfree(fcf_record);
3212 goto out;
3213 }
3214 kfree(fcf_record);
3215 }
3216 /*
3217 * The driver is expected to do FIP/FCF. Call the port
3218 * and get the FCF Table.
3219 */
James Smart32b97932009-07-19 10:01:21 -04003220 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003221 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003222 spin_unlock_irq(&phba->hbalock);
3223 return;
3224 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003225 /* This is the initial FCF discovery scan */
3226 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003227 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003228 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3229 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003230 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3231 LPFC_FCOE_FCF_GET_FIRST);
3232 if (rc) {
3233 spin_lock_irq(&phba->hbalock);
3234 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3235 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003236 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003237 }
James Smart38b92ef2010-08-04 16:11:39 -04003238 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003239 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003240 }
James Smart6fb120a2009-05-22 14:52:59 -04003241
3242 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003243out:
3244 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003245 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3246 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3247 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003248 lpfc_issue_clear_la(phba, vport);
3249 return;
dea31012005-04-17 16:05:31 -05003250}
3251
3252static void
James Smart84774a42008-08-24 21:50:06 -04003253lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003254{
dea31012005-04-17 16:05:31 -05003255 uint32_t control;
3256 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003257 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003258 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003259 if (phba->sli_rev <= LPFC_SLI_REV3) {
3260 control = readl(phba->HCregaddr);
3261 control |= HC_LAINT_ENA;
3262 writel(control, phba->HCregaddr);
3263 readl(phba->HCregaddr); /* flush */
3264 }
James Smart2e0fef82007-06-17 19:56:36 -05003265 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003266}
3267
James Smart84774a42008-08-24 21:50:06 -04003268static void
3269lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3270{
3271 lpfc_linkdown(phba);
3272 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003273 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003274 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3275}
3276
3277
dea31012005-04-17 16:05:31 -05003278/*
James Smart76a95d72010-11-20 23:11:48 -05003279 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003280 * command upon completion. It is setup in the LPFC_MBOXQ
3281 * as the completion routine when the command is
3282 * handed off to the SLI layer.
3283 */
3284void
James Smart76a95d72010-11-20 23:11:48 -05003285lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003286{
James Smart2e0fef82007-06-17 19:56:36 -05003287 struct lpfc_vport *vport = pmb->vport;
3288 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003289 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003290 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003291 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3292
James Smart0d2b6b82008-06-14 22:52:47 -04003293 /* Unblock ELS traffic */
3294 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003295 /* Check for error */
3296 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003297 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003298 "1307 READ_LA mbox error x%x state x%x\n",
3299 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003300 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003301 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003302 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003303 }
3304
James Smart76a95d72010-11-20 23:11:48 -05003305 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003306
3307 memcpy(&phba->alpa_map[0], mp->virt, 128);
3308
James Smart2e0fef82007-06-17 19:56:36 -05003309 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003310 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003311 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003312 else
James Smart2e0fef82007-06-17 19:56:36 -05003313 vport->fc_flag &= ~FC_BYPASSED_MODE;
3314 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003315
James Smartbe0c0082013-03-01 16:38:14 -05003316 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003317 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003318 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003319 if (phba->fc_eventTag != 0)
3320 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003321 }
dea31012005-04-17 16:05:31 -05003322
3323 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003324 if (phba->sli_rev < LPFC_SLI_REV4) {
3325 spin_lock_irq(&phba->hbalock);
3326 if (bf_get(lpfc_mbx_read_top_mm, la))
3327 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3328 else
3329 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3330 spin_unlock_irq(&phba->hbalock);
3331 }
dea31012005-04-17 16:05:31 -05003332
James Smart4d9ab992009-10-02 15:16:39 -04003333 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003334 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003335 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003336 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003337 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003338 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003339 "1306 Link Up Event in loop back mode "
3340 "x%x received Data: x%x x%x x%x x%x\n",
3341 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003342 bf_get(lpfc_mbx_read_top_alpa_granted,
3343 la),
3344 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003345 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003346 } else {
3347 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003348 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003349 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003350 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003351 bf_get(lpfc_mbx_read_top_alpa_granted,
3352 la),
3353 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003354 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003355 bf_get(lpfc_mbx_read_top_mm, la),
3356 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003357 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003358 }
James Smart92d7f7b2007-06-17 19:56:38 -05003359 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003360 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3361 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003362 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003363 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003364 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3365 "1308 Link Down Event in loop back mode "
3366 "x%x received "
3367 "Data: x%x x%x x%x\n",
3368 la->eventTag, phba->fc_eventTag,
3369 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003370 else
James Smart3163f722008-02-08 18:50:25 -05003371 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003372 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003373 "Data: x%x x%x x%x x%x x%x\n",
3374 la->eventTag, phba->fc_eventTag,
3375 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003376 bf_get(lpfc_mbx_read_top_mm, la),
3377 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003378 lpfc_mbx_issue_link_down(phba);
3379 }
James Smartbe0c0082013-03-01 16:38:14 -05003380 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3381 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003382 if (phba->link_state != LPFC_LINK_DOWN) {
3383 phba->fc_stat.LinkDown++;
3384 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3385 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003386 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003387 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003388 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003389 lpfc_mbx_issue_link_down(phba);
3390 } else
3391 lpfc_enable_la(phba);
3392
3393 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3394 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3395 "Data: x%x x%x x%x\n",
3396 la->eventTag, phba->fc_eventTag,
3397 phba->pport->port_state, vport->fc_flag);
3398 /*
3399 * The cmnd that triggered this will be waiting for this
3400 * signal.
3401 */
3402 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3403 if (phba->wait_4_mlo_maint_flg) {
3404 phba->wait_4_mlo_maint_flg = 0;
3405 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003406 }
James Smart84774a42008-08-24 21:50:06 -04003407 }
3408
James Smartbe0c0082013-03-01 16:38:14 -05003409 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3410 bf_get(lpfc_mbx_read_top_fa, la)) {
3411 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003412 lpfc_issue_clear_la(phba, vport);
3413 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003414 "1311 fa %d\n",
3415 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003416 }
3417
James Smart76a95d72010-11-20 23:11:48 -05003418lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003419 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3420 kfree(mp);
3421 mempool_free(pmb, phba->mbox_mem_pool);
3422 return;
3423}
3424
3425/*
3426 * This routine handles processing a REG_LOGIN mailbox
3427 * command upon completion. It is setup in the LPFC_MBOXQ
3428 * as the completion routine when the command is
3429 * handed off to the SLI layer.
3430 */
3431void
James Smart2e0fef82007-06-17 19:56:36 -05003432lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003433{
James Smart2e0fef82007-06-17 19:56:36 -05003434 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003435 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003436 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003437 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003438
dea31012005-04-17 16:05:31 -05003439 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003440 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003441
James Smartbe6bb942015-04-07 15:07:22 -04003442 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3443 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3444 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3445 atomic_read(&ndlp->kref.refcount),
3446 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003447 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3448 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3449
James Smart40426292010-12-15 17:58:10 -05003450 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3451 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003452 /* We rcvd a rscn after issuing this
3453 * mbox reg login, we may have cycled
3454 * back through the state and be
3455 * back at reg login state so this
3456 * mbox needs to be ignored becase
3457 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003458 * process.
James Smartffc95492010-06-07 15:23:17 -04003459 */
3460 spin_lock_irq(shost->host_lock);
3461 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3462 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003463 } else
3464 /* Good status, call state machine */
3465 lpfc_disc_state_machine(vport, ndlp, pmb,
3466 NLP_EVT_CMPL_REG_LOGIN);
3467
dea31012005-04-17 16:05:31 -05003468 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3469 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003470 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003471 /* decrement the node reference count held for this callback
3472 * function.
3473 */
James Smart329f9bc2007-04-25 09:53:01 -04003474 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003475
3476 return;
3477}
3478
James Smart92d7f7b2007-06-17 19:56:38 -05003479static void
3480lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3481{
James Smart04c68492009-05-22 14:52:52 -04003482 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003483 struct lpfc_vport *vport = pmb->vport;
3484 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3485
3486 switch (mb->mbxStatus) {
3487 case 0x0011:
3488 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003489 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3490 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3491 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003492 break;
James Smart78730cf2010-04-06 15:06:30 -04003493 /* If VPI is busy, reset the HBA */
3494 case 0x9700:
3495 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3496 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3497 vport->vpi, mb->mbxStatus);
3498 if (!(phba->pport->load_flag & FC_UNLOADING))
3499 lpfc_workq_post_event(phba, NULL, NULL,
3500 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003501 }
James Smart72100cc2010-02-12 14:43:01 -05003502 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003503 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003504 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003505 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003506 vport->unreg_vpi_cmpl = VPORT_OK;
3507 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003508 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003509 /*
3510 * This shost reference might have been taken at the beginning of
3511 * lpfc_vport_delete()
3512 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003513 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003514 scsi_host_put(shost);
3515}
3516
James Smartd7c255b2008-08-24 21:50:00 -04003517int
James Smart92d7f7b2007-06-17 19:56:38 -05003518lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3519{
3520 struct lpfc_hba *phba = vport->phba;
3521 LPFC_MBOXQ_t *mbox;
3522 int rc;
3523
3524 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3525 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003526 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003527
3528 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3529 mbox->vport = vport;
3530 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003531 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003532 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003533 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3534 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003535 mempool_free(mbox, phba->mbox_mem_pool);
3536 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003537 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003538 }
James Smartd7c255b2008-08-24 21:50:00 -04003539 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003540}
3541
3542static void
3543lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3544{
3545 struct lpfc_vport *vport = pmb->vport;
3546 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003547 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003548
3549 switch (mb->mbxStatus) {
3550 case 0x0011:
3551 case 0x9601:
3552 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003553 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3554 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3555 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003556 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3557 spin_lock_irq(shost->host_lock);
3558 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3559 spin_unlock_irq(shost->host_lock);
3560 vport->fc_myDID = 0;
3561 goto out;
3562 }
James Smart92d7f7b2007-06-17 19:56:38 -05003563
James Smart72100cc2010-02-12 14:43:01 -05003564 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003565 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003566 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003567 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003568 vport->num_disc_nodes = 0;
3569 /* go thru NPR list and issue ELS PLOGIs */
3570 if (vport->fc_npr_cnt)
3571 lpfc_els_disc_plogi(vport);
3572
3573 if (!vport->num_disc_nodes) {
3574 spin_lock_irq(shost->host_lock);
3575 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3576 spin_unlock_irq(shost->host_lock);
3577 lpfc_can_disctmo(vport);
3578 }
3579 vport->port_state = LPFC_VPORT_READY;
3580
3581out:
3582 mempool_free(pmb, phba->mbox_mem_pool);
3583 return;
3584}
3585
James Smart21e9a0a2009-05-22 14:53:21 -04003586/**
3587 * lpfc_create_static_vport - Read HBA config region to create static vports.
3588 * @phba: pointer to lpfc hba data structure.
3589 *
3590 * This routine issue a DUMP mailbox command for config region 22 to get
3591 * the list of static vports to be created. The function create vports
3592 * based on the information returned from the HBA.
3593 **/
3594void
3595lpfc_create_static_vport(struct lpfc_hba *phba)
3596{
3597 LPFC_MBOXQ_t *pmb = NULL;
3598 MAILBOX_t *mb;
3599 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003600 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003601 struct fc_vport_identifiers vport_id;
3602 struct fc_vport *new_fc_vport;
3603 struct Scsi_Host *shost;
3604 struct lpfc_vport *vport;
3605 uint16_t offset = 0;
3606 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003607 struct lpfc_dmabuf *mp;
3608 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003609
3610 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3611 if (!pmb) {
3612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3613 "0542 lpfc_create_static_vport failed to"
3614 " allocate mailbox memory\n");
3615 return;
3616 }
James Smartcdcc2342012-08-14 14:26:35 -04003617 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003618 mb = &pmb->u.mb;
3619
3620 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3621 if (!vport_info) {
3622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3623 "0543 lpfc_create_static_vport failed to"
3624 " allocate vport_info\n");
3625 mempool_free(pmb, phba->mbox_mem_pool);
3626 return;
3627 }
3628
3629 vport_buff = (uint8_t *) vport_info;
3630 do {
James Smartcdcc2342012-08-14 14:26:35 -04003631 /* free dma buffer from previous round */
3632 if (pmb->context1) {
3633 mp = (struct lpfc_dmabuf *)pmb->context1;
3634 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3635 kfree(mp);
3636 }
James Smart1c6834a2009-07-19 10:01:26 -04003637 if (lpfc_dump_static_vport(phba, pmb, offset))
3638 goto out;
3639
James Smart21e9a0a2009-05-22 14:53:21 -04003640 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003641 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3642 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003643
James Smartcdcc2342012-08-14 14:26:35 -04003644 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003645 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3646 "0544 lpfc_create_static_vport failed to"
3647 " issue dump mailbox command ret 0x%x "
3648 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003649 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003650 goto out;
3651 }
3652
James Smart1c6834a2009-07-19 10:01:26 -04003653 if (phba->sli_rev == LPFC_SLI_REV4) {
3654 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003655 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003656 if (byte_count > sizeof(struct static_vport_info) -
3657 offset)
3658 byte_count = sizeof(struct static_vport_info)
3659 - offset;
3660 memcpy(vport_buff + offset, mp->virt, byte_count);
3661 offset += byte_count;
3662 } else {
3663 if (mb->un.varDmp.word_cnt >
3664 sizeof(struct static_vport_info) - offset)
3665 mb->un.varDmp.word_cnt =
3666 sizeof(struct static_vport_info)
3667 - offset;
3668 byte_count = mb->un.varDmp.word_cnt;
3669 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3670 vport_buff + offset,
3671 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003672
James Smart1c6834a2009-07-19 10:01:26 -04003673 offset += byte_count;
3674 }
James Smart21e9a0a2009-05-22 14:53:21 -04003675
James Smart1c6834a2009-07-19 10:01:26 -04003676 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003677 offset < sizeof(struct static_vport_info));
3678
3679
3680 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3681 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3682 != VPORT_INFO_REV)) {
3683 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3684 "0545 lpfc_create_static_vport bad"
3685 " information header 0x%x 0x%x\n",
3686 le32_to_cpu(vport_info->signature),
3687 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3688
3689 goto out;
3690 }
3691
3692 shost = lpfc_shost_from_vport(phba->pport);
3693
3694 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3695 memset(&vport_id, 0, sizeof(vport_id));
3696 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3697 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3698 if (!vport_id.port_name || !vport_id.node_name)
3699 continue;
3700
3701 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3702 vport_id.vport_type = FC_PORTTYPE_NPIV;
3703 vport_id.disable = false;
3704 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3705
3706 if (!new_fc_vport) {
3707 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3708 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003709 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003710 continue;
3711 }
3712
3713 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3714 vport->vport_flag |= STATIC_VPORT;
3715 }
3716
3717out:
James Smart21e9a0a2009-05-22 14:53:21 -04003718 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003719 if (mbx_wait_rc != MBX_TIMEOUT) {
3720 if (pmb->context1) {
3721 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003722 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3723 kfree(mp);
3724 }
James Smart21e9a0a2009-05-22 14:53:21 -04003725 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003726 }
James Smart21e9a0a2009-05-22 14:53:21 -04003727
3728 return;
3729}
3730
dea31012005-04-17 16:05:31 -05003731/*
3732 * This routine handles processing a Fabric REG_LOGIN mailbox
3733 * command upon completion. It is setup in the LPFC_MBOXQ
3734 * as the completion routine when the command is
3735 * handed off to the SLI layer.
3736 */
3737void
James Smart2e0fef82007-06-17 19:56:36 -05003738lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003739{
James Smart92d7f7b2007-06-17 19:56:38 -05003740 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003741 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003742 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003743 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003744 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003745
James Smart549e55c2007-08-02 11:09:51 -04003746 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003747 pmb->context1 = NULL;
3748 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003749
dea31012005-04-17 16:05:31 -05003750 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003751 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3752 "0258 Register Fabric login error: 0x%x\n",
3753 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003754 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3755 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003756 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003757
James Smart76a95d72010-11-20 23:11:48 -05003758 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003759 /* FLOGI failed, use loop map to make discovery list */
3760 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003761
James Smart92d7f7b2007-06-17 19:56:38 -05003762 /* Start discovery */
3763 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003764 /* Decrement the reference count to ndlp after the
3765 * reference to the ndlp are done.
3766 */
3767 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003768 return;
3769 }
3770
3771 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003772 /* Decrement the reference count to ndlp after the reference
3773 * to the ndlp are done.
3774 */
3775 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003776 return;
3777 }
3778
James Smart6d368e52011-05-24 11:44:12 -04003779 if (phba->sli_rev < LPFC_SLI_REV4)
3780 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003781 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003782 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003783 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003784
James Smart2e0fef82007-06-17 19:56:36 -05003785 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003786 /* when physical port receive logo donot start
3787 * vport discovery */
3788 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3789 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003790 else {
3791 shost = lpfc_shost_from_vport(vport);
3792 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003793 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003794 spin_unlock_irq(shost->host_lock);
3795 }
James Smart92d7f7b2007-06-17 19:56:38 -05003796 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003797 }
3798
3799 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3800 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003801 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003802
3803 /* Drop the reference count from the mbox at the end after
3804 * all the current reference to the ndlp have been done.
3805 */
3806 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003807 return;
3808}
3809
3810/*
3811 * This routine handles processing a NameServer REG_LOGIN mailbox
3812 * command upon completion. It is setup in the LPFC_MBOXQ
3813 * as the completion routine when the command is
3814 * handed off to the SLI layer.
3815 */
3816void
James Smart2e0fef82007-06-17 19:56:36 -05003817lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003818{
James Smart21e9a0a2009-05-22 14:53:21 -04003819 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003820 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3821 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3822 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003823
James Smartd439d282010-09-29 11:18:45 -04003824 pmb->context1 = NULL;
3825 pmb->context2 = NULL;
3826
dea31012005-04-17 16:05:31 -05003827 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003828out:
James Smart21e9a0a2009-05-22 14:53:21 -04003829 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3830 "0260 Register NameServer error: 0x%x\n",
3831 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003832 /* decrement the node reference count held for this
3833 * callback function.
3834 */
James Smart329f9bc2007-04-25 09:53:01 -04003835 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003836 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3837 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003838 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003839
3840 /* If no other thread is using the ndlp, free it */
3841 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003842
James Smart76a95d72010-11-20 23:11:48 -05003843 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003844 /*
3845 * RegLogin failed, use loop map to make discovery
3846 * list
3847 */
3848 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003849
James Smart92d7f7b2007-06-17 19:56:38 -05003850 /* Start discovery */
3851 lpfc_disc_start(vport);
3852 return;
3853 }
3854 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003855 return;
3856 }
3857
James Smart6d368e52011-05-24 11:44:12 -04003858 if (phba->sli_rev < LPFC_SLI_REV4)
3859 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003860 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003861 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003862 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003863 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3864 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3865 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3866 atomic_read(&ndlp->kref.refcount),
3867 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003868
James Smart2e0fef82007-06-17 19:56:36 -05003869 if (vport->port_state < LPFC_VPORT_READY) {
3870 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003871 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3872 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3873 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3874 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3875 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3876
3877 /* Issue SCR just before NameServer GID_FT Query */
3878 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003879 }
3880
James Smart2e0fef82007-06-17 19:56:36 -05003881 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003882 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003883 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003884 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003885 goto out;
dea31012005-04-17 16:05:31 -05003886 }
3887
James Smartfa4066b2008-01-11 01:53:27 -05003888 /* decrement the node reference count held for this
3889 * callback function.
3890 */
James Smart329f9bc2007-04-25 09:53:01 -04003891 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003892 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3893 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003894 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003895
3896 return;
3897}
3898
3899static void
James Smart2e0fef82007-06-17 19:56:36 -05003900lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003901{
James Smart2e0fef82007-06-17 19:56:36 -05003902 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3903 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003904 struct lpfc_rport_data *rdata;
3905 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003906 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003907
3908 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003909 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3910 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003911 rport_ids.port_id = ndlp->nlp_DID;
3912 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003913
James Smart329f9bc2007-04-25 09:53:01 -04003914 /*
3915 * We leave our node pointer in rport->dd_data when we unregister a
3916 * FCP target port. But fc_remote_port_add zeros the space to which
3917 * rport->dd_data points. So, if we're reusing a previously
3918 * registered port, drop the reference that we took the last time we
3919 * registered the port.
3920 */
3921 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003922 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003923 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003924
3925 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3926 "rport add: did:x%x flg:x%x type x%x",
3927 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3928
James Smart9589b0622011-04-16 11:03:17 -04003929 /* Don't add the remote port if unloading. */
3930 if (vport->load_flag & FC_UNLOADING)
3931 return;
3932
James Smart2e0fef82007-06-17 19:56:36 -05003933 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003934 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003935 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3936 "Warning: fc_remote_port_add failed\n");
3937 return;
3938 }
3939
3940 /* initialize static port data */
3941 rport->maxframe_size = ndlp->nlp_maxframe;
3942 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003943 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003944 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003945
3946 if (ndlp->nlp_type & NLP_FCP_TARGET)
3947 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3948 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3949 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3950
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003951 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3952 fc_remote_port_rolechg(rport, rport_ids.roles);
3953
James Smart34f5ad82012-08-03 12:35:03 -04003954 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3955 "3183 rport register x%06x, rport %p role x%x\n",
3956 ndlp->nlp_DID, rport, rport_ids.roles);
3957
James Smart071fbd3d2006-04-15 11:53:20 -04003958 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003959 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003960 ndlp->nlp_sid = rport->scsi_target_id;
3961 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003962 return;
3963}
3964
3965static void
James Smart2e0fef82007-06-17 19:56:36 -05003966lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003967{
3968 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003969
James Smart858c9f62007-06-17 19:56:39 -05003970 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3971 "rport delete: did:x%x flg:x%x type x%x",
3972 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3973
James Smart34f5ad82012-08-03 12:35:03 -04003974 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3975 "3184 rport unregister x%06x, rport %p\n",
3976 ndlp->nlp_DID, rport);
3977
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003978 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003979
3980 return;
3981}
3982
James Smartde0c5b32007-04-25 09:52:27 -04003983static void
James Smart2e0fef82007-06-17 19:56:36 -05003984lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003985{
James Smart2e0fef82007-06-17 19:56:36 -05003986 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3987
3988 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003989 switch (state) {
3990 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003991 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003992 break;
3993 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003994 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003995 break;
3996 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003997 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003998 break;
3999 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004000 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004001 break;
4002 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004003 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004004 break;
4005 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004006 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004007 break;
4008 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004009 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004010 break;
4011 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004012 if (vport->fc_npr_cnt == 0 && count == -1)
4013 vport->fc_npr_cnt = 0;
4014 else
4015 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004016 break;
4017 }
James Smart2e0fef82007-06-17 19:56:36 -05004018 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004019}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004020
James Smartde0c5b32007-04-25 09:52:27 -04004021static void
James Smart2e0fef82007-06-17 19:56:36 -05004022lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004023 int old_state, int new_state)
4024{
James Smart2e0fef82007-06-17 19:56:36 -05004025 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4026
James Smartde0c5b32007-04-25 09:52:27 -04004027 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004028 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4029 ndlp->nlp_type |= NLP_FC_NODE;
4030 }
4031 if (new_state == NLP_STE_MAPPED_NODE)
4032 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4033 if (new_state == NLP_STE_NPR_NODE)
4034 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004035
James Smartde0c5b32007-04-25 09:52:27 -04004036 /* Transport interface */
4037 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
4038 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004039 vport->phba->nport_event_cnt++;
4040 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004041 }
dea31012005-04-17 16:05:31 -05004042
James Smartde0c5b32007-04-25 09:52:27 -04004043 if (new_state == NLP_STE_MAPPED_NODE ||
4044 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05004045 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05004046 /*
4047 * Tell the fc transport about the port, if we haven't
4048 * already. If we have, and it's a scsi entity, be
4049 * sure to unblock any attached scsi devices
4050 */
4051 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004052 }
James Smartea2151b2008-09-07 11:52:10 -04004053 if ((new_state == NLP_STE_MAPPED_NODE) &&
4054 (vport->stat_data_enabled)) {
4055 /*
4056 * A new target is discovered, if there is no buffer for
4057 * statistical data collection allocate buffer.
4058 */
4059 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4060 sizeof(struct lpfc_scsicmd_bkt),
4061 GFP_KERNEL);
4062
4063 if (!ndlp->lat_data)
4064 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4065 "0286 lpfc_nlp_state_cleanup failed to "
4066 "allocate statistical data buffer DID "
4067 "0x%x\n", ndlp->nlp_DID);
4068 }
James Smart858c9f62007-06-17 19:56:39 -05004069 /*
4070 * if we added to Mapped list, but the remote port
4071 * registration failed or assigned a target id outside
4072 * our presentable range - move the node to the
4073 * Unmapped List
4074 */
James Smartde0c5b32007-04-25 09:52:27 -04004075 if (new_state == NLP_STE_MAPPED_NODE &&
4076 (!ndlp->rport ||
4077 ndlp->rport->scsi_target_id == -1 ||
4078 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004079 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004080 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004081 spin_unlock_irq(shost->host_lock);
4082 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004083 }
James Smartde0c5b32007-04-25 09:52:27 -04004084}
4085
James Smart685f0bf2007-04-25 09:53:08 -04004086static char *
4087lpfc_nlp_state_name(char *buffer, size_t size, int state)
4088{
4089 static char *states[] = {
4090 [NLP_STE_UNUSED_NODE] = "UNUSED",
4091 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4092 [NLP_STE_ADISC_ISSUE] = "ADISC",
4093 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4094 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004095 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004096 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4097 [NLP_STE_MAPPED_NODE] = "MAPPED",
4098 [NLP_STE_NPR_NODE] = "NPR",
4099 };
4100
James Smart311464e2007-08-02 11:10:37 -04004101 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004102 strlcpy(buffer, states[state], size);
4103 else
4104 snprintf(buffer, size, "unknown (%d)", state);
4105 return buffer;
4106}
4107
James Smartde0c5b32007-04-25 09:52:27 -04004108void
James Smart2e0fef82007-06-17 19:56:36 -05004109lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4110 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004111{
James Smart2e0fef82007-06-17 19:56:36 -05004112 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004113 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004114 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004115
James Smarte8b62012007-08-02 11:10:09 -04004116 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4117 "0904 NPort state transition x%06x, %s -> %s\n",
4118 ndlp->nlp_DID,
4119 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4120 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004121
4122 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4123 "node statechg did:x%x old:%d ste:%d",
4124 ndlp->nlp_DID, old_state, state);
4125
James Smartde0c5b32007-04-25 09:52:27 -04004126 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004127 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004128 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004129 if (old_state == NLP_STE_UNMAPPED_NODE) {
4130 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4131 ndlp->nlp_type &= ~NLP_FC_NODE;
4132 }
4133
James Smart685f0bf2007-04-25 09:53:08 -04004134 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004135 spin_lock_irq(shost->host_lock);
4136 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4137 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004138 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004139 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004140
4141 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004142 lpfc_nlp_counters(vport, state, 1);
4143 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004144}
4145
4146void
James Smarte47c9092008-02-08 18:49:26 -05004147lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4148{
4149 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4150
4151 if (list_empty(&ndlp->nlp_listp)) {
4152 spin_lock_irq(shost->host_lock);
4153 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4154 spin_unlock_irq(shost->host_lock);
4155 }
4156}
4157
4158void
James Smart2e0fef82007-06-17 19:56:36 -05004159lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004160{
James Smart2e0fef82007-06-17 19:56:36 -05004161 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4162
James Smart0d2b6b82008-06-14 22:52:47 -04004163 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004164 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004165 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4166 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004167 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004168 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004169 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004170 NLP_STE_UNUSED_NODE);
4171}
4172
Adrian Bunk4d9db012008-02-14 23:24:02 +02004173static void
James Smarte47c9092008-02-08 18:49:26 -05004174lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4175{
James Smart0d2b6b82008-06-14 22:52:47 -04004176 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004177 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4178 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4179 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4180 NLP_STE_UNUSED_NODE);
4181}
James Smart109f6ed2008-12-04 22:39:08 -05004182/**
James Smart3621a712009-04-06 18:47:14 -04004183 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004184 * @vport: Pointer to Virtual Port object.
4185 * @ndlp: Pointer to FC node object.
4186 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004187 *
4188 * This function is always called when node object need to be initialized.
4189 * It initializes all the fields of the node object. Although the reference
4190 * to phba from @ndlp can be obtained indirectly through it's reference to
4191 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4192 * to the life-span of the @ndlp might go beyond the existence of @vport as
4193 * the final release of ndlp is determined by its reference count. And, the
4194 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004195 **/
4196static inline void
4197lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4198 uint32_t did)
4199{
4200 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4201 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4202 init_timer(&ndlp->nlp_delayfunc);
4203 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4204 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4205 ndlp->nlp_DID = did;
4206 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004207 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004208 ndlp->nlp_sid = NLP_NO_SID;
4209 kref_init(&ndlp->kref);
4210 NLP_INT_NODE_ACT(ndlp);
4211 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004212 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004213}
James Smarte47c9092008-02-08 18:49:26 -05004214
4215struct lpfc_nodelist *
4216lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4217 int state)
4218{
4219 struct lpfc_hba *phba = vport->phba;
4220 uint32_t did;
4221 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004222 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004223
4224 if (!ndlp)
4225 return NULL;
4226
4227 spin_lock_irqsave(&phba->ndlp_lock, flags);
4228 /* The ndlp should not be in memory free mode */
4229 if (NLP_CHK_FREE_REQ(ndlp)) {
4230 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4231 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4232 "0277 lpfc_enable_node: ndlp:x%p "
4233 "usgmap:x%x refcnt:%d\n",
4234 (void *)ndlp, ndlp->nlp_usg_map,
4235 atomic_read(&ndlp->kref.refcount));
4236 return NULL;
4237 }
4238 /* The ndlp should not already be in active mode */
4239 if (NLP_CHK_NODE_ACT(ndlp)) {
4240 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4241 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4242 "0278 lpfc_enable_node: ndlp:x%p "
4243 "usgmap:x%x refcnt:%d\n",
4244 (void *)ndlp, ndlp->nlp_usg_map,
4245 atomic_read(&ndlp->kref.refcount));
4246 return NULL;
4247 }
4248
4249 /* Keep the original DID */
4250 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004251 if (phba->sli_rev == LPFC_SLI_REV4)
4252 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004253
4254 /* re-initialize ndlp except of ndlp linked list pointer */
4255 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4256 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004257 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004258
James Smartcff261f2013-12-17 20:29:47 -05004259 if (phba->sli_rev == LPFC_SLI_REV4)
4260 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4261
James Smarte47c9092008-02-08 18:49:26 -05004262 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004263 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004264 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004265 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4266 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4267 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4268 ndlp->nlp_flag,
4269 atomic_read(&ndlp->kref.refcount),
4270 ndlp->nlp_usg_map, ndlp);
4271 }
James Smart725dd392013-10-10 12:23:30 -04004272
James Smarte47c9092008-02-08 18:49:26 -05004273
4274 if (state != NLP_STE_UNUSED_NODE)
4275 lpfc_nlp_set_state(vport, ndlp, state);
4276
4277 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4278 "node enable: did:x%x",
4279 ndlp->nlp_DID, 0, 0);
4280 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004281}
4282
4283void
James Smart2e0fef82007-06-17 19:56:36 -05004284lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004285{
James Smart87af33f2007-10-27 13:37:43 -04004286 /*
James Smartfa4066b2008-01-11 01:53:27 -05004287 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004288 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004289 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4290 * until ALL other outstanding threads have completed. We check
4291 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004292 */
James Smartfa4066b2008-01-11 01:53:27 -05004293 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4294 return;
James Smart51ef4c22007-08-02 11:10:31 -04004295 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004296 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004297 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004298 lpfc_unreg_rpi(vport, ndlp);
4299 } else {
4300 lpfc_nlp_put(ndlp);
4301 }
James Smart98c9ea52007-10-27 13:37:33 -04004302 return;
dea31012005-04-17 16:05:31 -05004303}
4304
4305/*
4306 * Start / ReStart rescue timer for Discovery / RSCN handling
4307 */
4308void
James Smart2e0fef82007-06-17 19:56:36 -05004309lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004310{
James Smart2e0fef82007-06-17 19:56:36 -05004311 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4312 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004313 uint32_t tmo;
4314
James Smart2e0fef82007-06-17 19:56:36 -05004315 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004316 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004317 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4318 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004319 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004320 * FC spec states we need 3 * ratov for CT requests
4321 */
4322 tmo = ((phba->fc_ratov * 3) + 3);
4323 }
dea31012005-04-17 16:05:31 -05004324
James Smart858c9f62007-06-17 19:56:39 -05004325
4326 if (!timer_pending(&vport->fc_disctmo)) {
4327 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4328 "set disc timer: tmo:x%x state:x%x flg:x%x",
4329 tmo, vport->port_state, vport->fc_flag);
4330 }
4331
James Smart256ec0d2013-04-17 20:14:58 -04004332 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004333 spin_lock_irq(shost->host_lock);
4334 vport->fc_flag |= FC_DISC_TMO;
4335 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004336
4337 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004338 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4339 "0247 Start Discovery Timer state x%x "
4340 "Data: x%x x%lx x%x x%x\n",
4341 vport->port_state, tmo,
4342 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4343 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004344
4345 return;
4346}
4347
4348/*
4349 * Cancel rescue timer for Discovery / RSCN handling
4350 */
4351int
James Smart2e0fef82007-06-17 19:56:36 -05004352lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004353{
James Smart2e0fef82007-06-17 19:56:36 -05004354 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004355 unsigned long iflags;
4356
James Smart858c9f62007-06-17 19:56:39 -05004357 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4358 "can disc timer: state:x%x rtry:x%x flg:x%x",
4359 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4360
dea31012005-04-17 16:05:31 -05004361 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004362 if (vport->fc_flag & FC_DISC_TMO) {
4363 spin_lock_irqsave(shost->host_lock, iflags);
4364 vport->fc_flag &= ~FC_DISC_TMO;
4365 spin_unlock_irqrestore(shost->host_lock, iflags);
4366 del_timer_sync(&vport->fc_disctmo);
4367 spin_lock_irqsave(&vport->work_port_lock, iflags);
4368 vport->work_port_events &= ~WORKER_DISC_TMO;
4369 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004370 }
4371
4372 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004373 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4374 "0248 Cancel Discovery Timer state x%x "
4375 "Data: x%x x%x x%x\n",
4376 vport->port_state, vport->fc_flag,
4377 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004378 return 0;
dea31012005-04-17 16:05:31 -05004379}
4380
4381/*
4382 * Check specified ring for outstanding IOCB on the SLI queue
4383 * Return true if iocb matches the specified nport
4384 */
4385int
James Smart2e0fef82007-06-17 19:56:36 -05004386lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4387 struct lpfc_sli_ring *pring,
4388 struct lpfc_iocbq *iocb,
4389 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004390{
James Smart2e0fef82007-06-17 19:56:36 -05004391 struct lpfc_sli *psli = &phba->sli;
4392 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004393 struct lpfc_vport *vport = ndlp->vport;
4394
4395 if (iocb->vport != vport)
4396 return 0;
4397
dea31012005-04-17 16:05:31 -05004398 if (pring->ringno == LPFC_ELS_RING) {
4399 switch (icmd->ulpCommand) {
4400 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004401 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004402 return 1;
dea31012005-04-17 16:05:31 -05004403 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004404 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4405 return 1;
dea31012005-04-17 16:05:31 -05004406 case CMD_XMIT_ELS_RSP64_CX:
4407 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004408 return 1;
dea31012005-04-17 16:05:31 -05004409 }
James Smarta4bc3372006-12-02 13:34:16 -05004410 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004411
4412 } else if (pring->ringno == psli->fcp_ring) {
4413 /* Skip match check if waiting to relogin to FCP target */
4414 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004415 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004416 return 0;
dea31012005-04-17 16:05:31 -05004417 }
4418 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004419 return 1;
dea31012005-04-17 16:05:31 -05004420 }
4421 } else if (pring->ringno == psli->next_ring) {
4422
4423 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004424 return 0;
dea31012005-04-17 16:05:31 -05004425}
4426
4427/*
4428 * Free resources / clean up outstanding I/Os
4429 * associated with nlp_rpi in the LPFC_NODELIST entry.
4430 */
4431static int
James Smart2e0fef82007-06-17 19:56:36 -05004432lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004433{
James Smart2534ba72007-04-25 09:52:20 -04004434 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004435 struct lpfc_sli *psli;
4436 struct lpfc_sli_ring *pring;
4437 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004438 uint32_t i;
dea31012005-04-17 16:05:31 -05004439
James Smart92d7f7b2007-06-17 19:56:38 -05004440 lpfc_fabric_abort_nport(ndlp);
4441
dea31012005-04-17 16:05:31 -05004442 /*
4443 * Everything that matches on txcmplq will be returned
4444 * by firmware with a no rpi error.
4445 */
4446 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004447 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004448 /* Now process each ring */
4449 for (i = 0; i < psli->num_rings; i++) {
4450 pring = &psli->ring[i];
4451
James Smart2e0fef82007-06-17 19:56:36 -05004452 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004453 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004454 list) {
dea31012005-04-17 16:05:31 -05004455 /*
4456 * Check to see if iocb matches the nport we are
4457 * looking for
4458 */
James Smart92d7f7b2007-06-17 19:56:38 -05004459 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4460 ndlp))) {
dea31012005-04-17 16:05:31 -05004461 /* It matches, so deque and call compl
4462 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004463 list_move_tail(&iocb->list,
4464 &completions);
dea31012005-04-17 16:05:31 -05004465 }
4466 }
James Smart2e0fef82007-06-17 19:56:36 -05004467 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004468 }
4469 }
James Smart2534ba72007-04-25 09:52:20 -04004470
James Smarta257bf92009-04-06 18:48:10 -04004471 /* Cancel all the IOCBs from the completions list */
4472 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4473 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004474
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004475 return 0;
dea31012005-04-17 16:05:31 -05004476}
4477
James Smart086a3452012-08-14 14:25:21 -04004478/**
4479 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4480 * @phba: Pointer to HBA context object.
4481 * @pmb: Pointer to mailbox object.
4482 *
4483 * This function will issue an ELS LOGO command after completing
4484 * the UNREG_RPI.
4485 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004486static void
James Smart086a3452012-08-14 14:25:21 -04004487lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4488{
4489 struct lpfc_vport *vport = pmb->vport;
4490 struct lpfc_nodelist *ndlp;
4491
4492 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4493 if (!ndlp)
4494 return;
4495 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004496 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004497}
4498
dea31012005-04-17 16:05:31 -05004499/*
4500 * Free rpi associated with LPFC_NODELIST entry.
4501 * This routine is called from lpfc_freenode(), when we are removing
4502 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4503 * LOGO that completes successfully, and we are waiting to PLOGI back
4504 * to the remote NPort. In addition, it is called after we receive
4505 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4506 * we are waiting to PLOGI back to the remote NPort.
4507 */
4508int
James Smart2e0fef82007-06-17 19:56:36 -05004509lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004510{
James Smart2e0fef82007-06-17 19:56:36 -05004511 struct lpfc_hba *phba = vport->phba;
4512 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004513 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004514 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004515
James Smartf454a9a2013-09-06 12:20:07 -04004516 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4517 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4518 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4519 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4520 "3366 RPI x%x needs to be "
4521 "unregistered nlp_flag x%x "
4522 "did x%x\n",
4523 ndlp->nlp_rpi, ndlp->nlp_flag,
4524 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004525 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4526 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004527 /* SLI4 ports require the physical rpi value. */
4528 rpi = ndlp->nlp_rpi;
4529 if (phba->sli_rev == LPFC_SLI_REV4)
4530 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004531
James Smart6d368e52011-05-24 11:44:12 -04004532 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004533 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004534 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4535 mbox->context1 = ndlp;
4536 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4537 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004538 if (phba->sli_rev == LPFC_SLI_REV4 &&
4539 (!(vport->load_flag & FC_UNLOADING)) &&
4540 (bf_get(lpfc_sli_intf_if_type,
4541 &phba->sli4_hba.sli_intf) ==
4542 LPFC_SLI_INTF_IF_TYPE_2)) {
4543 mbox->context1 = lpfc_nlp_get(ndlp);
4544 mbox->mbox_cmpl =
4545 lpfc_sli4_unreg_rpi_cmpl_clr;
4546 } else
4547 mbox->mbox_cmpl =
4548 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004549 }
4550
James Smart0b727fe2007-10-27 13:37:25 -04004551 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004552 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004553 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004554 }
dea31012005-04-17 16:05:31 -05004555 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004556
James Smart40426292010-12-15 17:58:10 -05004557 if (phba->sli_rev != LPFC_SLI_REV4)
4558 ndlp->nlp_rpi = 0;
4559 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004560 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004561 return 1;
4562 }
4563 return 0;
4564}
4565
James Smartecfd03c2010-02-12 14:41:27 -05004566/**
4567 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4568 * @phba: pointer to lpfc hba data structure.
4569 *
4570 * This routine is invoked to unregister all the currently registered RPIs
4571 * to the HBA.
4572 **/
4573void
4574lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4575{
4576 struct lpfc_vport **vports;
4577 struct lpfc_nodelist *ndlp;
4578 struct Scsi_Host *shost;
4579 int i;
4580
4581 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004582 if (!vports) {
4583 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4584 "2884 Vport array allocation failed \n");
4585 return;
4586 }
James Smartecfd03c2010-02-12 14:41:27 -05004587 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4588 shost = lpfc_shost_from_vport(vports[i]);
4589 spin_lock_irq(shost->host_lock);
4590 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004591 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004592 /* The mempool_alloc might sleep */
4593 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004594 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004595 spin_lock_irq(shost->host_lock);
4596 }
James Smartecfd03c2010-02-12 14:41:27 -05004597 }
4598 spin_unlock_irq(shost->host_lock);
4599 }
4600 lpfc_destroy_vport_work_array(phba, vports);
4601}
4602
James Smart92d7f7b2007-06-17 19:56:38 -05004603void
4604lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4605{
4606 struct lpfc_hba *phba = vport->phba;
4607 LPFC_MBOXQ_t *mbox;
4608 int rc;
4609
James Smart5af5eee2010-10-22 11:06:38 -04004610 if (phba->sli_rev == LPFC_SLI_REV4) {
4611 lpfc_sli4_unreg_all_rpis(vport);
4612 return;
4613 }
4614
James Smart92d7f7b2007-06-17 19:56:38 -05004615 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4616 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004617 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4618 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004619 mbox->vport = vport;
4620 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004621 mbox->context1 = NULL;
4622 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004623 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004624 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004625
4626 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4627 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4628 "1836 Could not issue "
4629 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004630 }
4631}
4632
4633void
4634lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4635{
4636 struct lpfc_hba *phba = vport->phba;
4637 LPFC_MBOXQ_t *mbox;
4638 int rc;
4639
4640 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4641 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004642 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4643 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004644 mbox->vport = vport;
4645 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004646 mbox->context1 = NULL;
4647 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004648 if (rc != MBX_TIMEOUT)
4649 mempool_free(mbox, phba->mbox_mem_pool);
4650
4651 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004652 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4653 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004654 "unreg_did (default rpis) status %d\n",
4655 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004656 }
4657}
4658
dea31012005-04-17 16:05:31 -05004659/*
4660 * Free resources associated with LPFC_NODELIST entry
4661 * so it can be freed.
4662 */
4663static int
James Smart2e0fef82007-06-17 19:56:36 -05004664lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004665{
James Smart2e0fef82007-06-17 19:56:36 -05004666 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4667 struct lpfc_hba *phba = vport->phba;
4668 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004669 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004670
4671 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004672 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4673 "0900 Cleanup node for NPort x%x "
4674 "Data: x%x x%x x%x\n",
4675 ndlp->nlp_DID, ndlp->nlp_flag,
4676 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004677 if (NLP_CHK_FREE_REQ(ndlp)) {
4678 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4679 "0280 lpfc_cleanup_node: ndlp:x%p "
4680 "usgmap:x%x refcnt:%d\n",
4681 (void *)ndlp, ndlp->nlp_usg_map,
4682 atomic_read(&ndlp->kref.refcount));
4683 lpfc_dequeue_node(vport, ndlp);
4684 } else {
4685 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4686 "0281 lpfc_cleanup_node: ndlp:x%p "
4687 "usgmap:x%x refcnt:%d\n",
4688 (void *)ndlp, ndlp->nlp_usg_map,
4689 atomic_read(&ndlp->kref.refcount));
4690 lpfc_disable_node(vport, ndlp);
4691 }
dea31012005-04-17 16:05:31 -05004692
James Smart086a3452012-08-14 14:25:21 -04004693
4694 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4695
dea31012005-04-17 16:05:31 -05004696 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4697 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004698 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004699 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004700 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4701 mb->context2 = NULL;
4702 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4703 }
4704 }
James Smart33ccf8d2006-08-17 11:57:58 -04004705
James Smart2e0fef82007-06-17 19:56:36 -05004706 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004707 /* Cleanup REG_LOGIN completions which are not yet processed */
4708 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4709 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004710 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004711 (ndlp != (struct lpfc_nodelist *) mb->context2))
4712 continue;
4713
4714 mb->context2 = NULL;
4715 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4716 }
4717
dea31012005-04-17 16:05:31 -05004718 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004719 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004720 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004721 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004722 mp = (struct lpfc_dmabuf *) (mb->context1);
4723 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004724 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004725 kfree(mp);
4726 }
4727 list_del(&mb->list);
4728 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004729 /* We shall not invoke the lpfc_nlp_put to decrement
4730 * the ndlp reference count as we are in the process
4731 * of lpfc_nlp_release.
4732 */
dea31012005-04-17 16:05:31 -05004733 }
4734 }
James Smart2e0fef82007-06-17 19:56:36 -05004735 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004736
James Smarte47c9092008-02-08 18:49:26 -05004737 lpfc_els_abort(phba, ndlp);
4738
James Smart2e0fef82007-06-17 19:56:36 -05004739 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004740 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004741 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004742
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004743 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004744 del_timer_sync(&ndlp->nlp_delayfunc);
4745
James Smart0d2b6b82008-06-14 22:52:47 -04004746 list_del_init(&ndlp->els_retry_evt.evt_listp);
4747 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004748 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004749 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004750
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004751 return 0;
dea31012005-04-17 16:05:31 -05004752}
4753
4754/*
4755 * Check to see if we can free the nlp back to the freelist.
4756 * If we are in the middle of using the nlp in the discovery state
4757 * machine, defer the free till we reach the end of the state machine.
4758 */
James Smart329f9bc2007-04-25 09:53:01 -04004759static void
James Smart2e0fef82007-06-17 19:56:36 -05004760lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004761{
James Smarta8adb832007-10-27 13:37:53 -04004762 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004763 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004764 LPFC_MBOXQ_t *mbox;
4765 int rc;
dea31012005-04-17 16:05:31 -05004766
James Smart0d2b6b82008-06-14 22:52:47 -04004767 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004768 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004769 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4770 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004771 /* For this case we need to cleanup the default rpi
4772 * allocated by the firmware.
4773 */
James Smartbe6bb942015-04-07 15:07:22 -04004774 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4775 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4776 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4777 atomic_read(&ndlp->kref.refcount),
4778 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004779 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4780 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004781 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004782 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004783 if (rc) {
4784 mempool_free(mbox, phba->mbox_mem_pool);
4785 }
4786 else {
4787 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4788 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4789 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004790 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004791 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4792 if (rc == MBX_NOT_FINISHED) {
4793 mempool_free(mbox, phba->mbox_mem_pool);
4794 }
4795 }
4796 }
4797 }
James Smart2e0fef82007-06-17 19:56:36 -05004798 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004799
James Smart2e0fef82007-06-17 19:56:36 -05004800 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004801 * We can get here with a non-NULL ndlp->rport because when we
4802 * unregister a rport we don't break the rport/node linkage. So if we
4803 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004804 */
James Smart92d7f7b2007-06-17 19:56:38 -05004805 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004806 rdata = ndlp->rport->dd_data;
4807 rdata->pnode = NULL;
4808 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004809 }
dea31012005-04-17 16:05:31 -05004810}
4811
4812static int
James Smart2e0fef82007-06-17 19:56:36 -05004813lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4814 uint32_t did)
dea31012005-04-17 16:05:31 -05004815{
James Smart2e0fef82007-06-17 19:56:36 -05004816 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004817
4818 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004819 return 0;
dea31012005-04-17 16:05:31 -05004820
dea31012005-04-17 16:05:31 -05004821 /* First check for Direct match */
4822 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004823 return 1;
dea31012005-04-17 16:05:31 -05004824
4825 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004826 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004827 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004828 return 0;
dea31012005-04-17 16:05:31 -05004829 }
4830
4831 matchdid.un.word = did;
4832 ndlpdid.un.word = ndlp->nlp_DID;
4833 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4834 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4835 (mydid.un.b.area == matchdid.un.b.area)) {
4836 if ((ndlpdid.un.b.domain == 0) &&
4837 (ndlpdid.un.b.area == 0)) {
4838 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004839 return 1;
dea31012005-04-17 16:05:31 -05004840 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004841 return 0;
dea31012005-04-17 16:05:31 -05004842 }
4843
4844 matchdid.un.word = ndlp->nlp_DID;
4845 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4846 (mydid.un.b.area == ndlpdid.un.b.area)) {
4847 if ((matchdid.un.b.domain == 0) &&
4848 (matchdid.un.b.area == 0)) {
4849 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004850 return 1;
dea31012005-04-17 16:05:31 -05004851 }
4852 }
4853 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004854 return 0;
dea31012005-04-17 16:05:31 -05004855}
4856
James Smart685f0bf2007-04-25 09:53:08 -04004857/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004858static struct lpfc_nodelist *
4859__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004860{
James Smart2fb9bd82006-12-02 13:33:57 -05004861 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004862 uint32_t data1;
4863
James Smart2e0fef82007-06-17 19:56:36 -05004864 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4865 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004866 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4867 ((uint32_t) ndlp->nlp_xri << 16) |
4868 ((uint32_t) ndlp->nlp_type << 8) |
4869 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004870 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4871 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05004872 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04004873 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05004874 ndlp->nlp_flag, data1,
4875 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04004876 return ndlp;
dea31012005-04-17 16:05:31 -05004877 }
4878 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004879
dea31012005-04-17 16:05:31 -05004880 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004881 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4882 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004883 return NULL;
4884}
4885
4886struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004887lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004888{
James Smart2e0fef82007-06-17 19:56:36 -05004889 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004890 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004891 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004892
James Smartfedd3b72011-02-16 12:39:24 -05004893 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004894 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004895 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004896 return ndlp;
4897}
4898
4899struct lpfc_nodelist *
4900lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4901{
4902 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4903 struct lpfc_nodelist *ndlp;
4904
4905 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004906 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004907 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4908 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004909 return NULL;
4910 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004911 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004912 if (!ndlp)
4913 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004914 lpfc_nlp_init(vport, ndlp, did);
4915 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4916 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004917 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004918 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004919 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004920 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4921 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4922 if (!ndlp)
4923 return NULL;
4924 spin_lock_irq(shost->host_lock);
4925 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4926 spin_unlock_irq(shost->host_lock);
4927 return ndlp;
dea31012005-04-17 16:05:31 -05004928 }
James Smarte47c9092008-02-08 18:49:26 -05004929
James Smart58da1ff2008-04-07 10:15:56 -04004930 if ((vport->fc_flag & FC_RSCN_MODE) &&
4931 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004932 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004933 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004934 * we don't need to try to discover it again.
4935 */
4936 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4937 return NULL;
4938
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004939 /* Since this node is marked for discovery,
4940 * delay timeout is not needed.
4941 */
James Smart0d2b6b82008-06-14 22:52:47 -04004942 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004943 spin_lock_irq(shost->host_lock);
4944 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4945 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004946 } else
dea31012005-04-17 16:05:31 -05004947 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004948 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004949 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004950 * or we are already in the process of discovery on it,
4951 * we don't need to try to discover it again.
4952 */
James Smart685f0bf2007-04-25 09:53:08 -04004953 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004954 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4955 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004956 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004957 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4958 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004959 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004960 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004961 }
4962 return ndlp;
4963}
4964
4965/* Build a list of nodes to discover based on the loopmap */
4966void
James Smart2e0fef82007-06-17 19:56:36 -05004967lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004968{
James Smart2e0fef82007-06-17 19:56:36 -05004969 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004970 int j;
4971 uint32_t alpa, index;
4972
James Smart2e0fef82007-06-17 19:56:36 -05004973 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004974 return;
James Smart2e0fef82007-06-17 19:56:36 -05004975
James Smart76a95d72010-11-20 23:11:48 -05004976 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004977 return;
dea31012005-04-17 16:05:31 -05004978
4979 /* Check for loop map present or not */
4980 if (phba->alpa_map[0]) {
4981 for (j = 1; j <= phba->alpa_map[0]; j++) {
4982 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004983 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004984 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004985 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004986 }
4987 } else {
4988 /* No alpamap, so try all alpa's */
4989 for (j = 0; j < FC_MAXLOOP; j++) {
4990 /* If cfg_scan_down is set, start from highest
4991 * ALPA (0xef) to lowest (0x1).
4992 */
James Smart3de2a652007-08-02 11:09:59 -04004993 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004994 index = j;
4995 else
4996 index = FC_MAXLOOP - j - 1;
4997 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004998 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004999 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005000 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005001 }
5002 }
5003 return;
5004}
5005
dea31012005-04-17 16:05:31 -05005006void
James Smart2e0fef82007-06-17 19:56:36 -05005007lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005008{
dea31012005-04-17 16:05:31 -05005009 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005010 struct lpfc_sli *psli = &phba->sli;
5011 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
5012 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
5013 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
5014 int rc;
5015
James Smart92d7f7b2007-06-17 19:56:38 -05005016 /*
5017 * if it's not a physical port or if we already send
5018 * clear_la then don't send it.
5019 */
5020 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005021 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5022 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005023 return;
5024
James Smart2e0fef82007-06-17 19:56:36 -05005025 /* Link up discovery */
5026 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5027 phba->link_state = LPFC_CLEAR_LA;
5028 lpfc_clear_la(phba, mbox);
5029 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5030 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005031 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005032 if (rc == MBX_NOT_FINISHED) {
5033 mempool_free(mbox, phba->mbox_mem_pool);
5034 lpfc_disc_flush_list(vport);
5035 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5036 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5037 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005038 phba->link_state = LPFC_HBA_ERROR;
5039 }
5040 }
5041}
5042
5043/* Reg_vpi to tell firmware to resume normal operations */
5044void
5045lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5046{
5047 LPFC_MBOXQ_t *regvpimbox;
5048
5049 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5050 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005051 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005052 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5053 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005054 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005055 == MBX_NOT_FINISHED) {
5056 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005057 }
5058 }
5059}
5060
5061/* Start Link up / RSCN discovery on NPR nodes */
5062void
5063lpfc_disc_start(struct lpfc_vport *vport)
5064{
5065 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5066 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005067 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005068 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005069
James Smarte74c03c2013-04-17 20:15:19 -04005070 if (!lpfc_is_link_up(phba)) {
5071 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5072 "3315 Link is not up %x\n",
5073 phba->link_state);
dea31012005-04-17 16:05:31 -05005074 return;
James Smarte74c03c2013-04-17 20:15:19 -04005075 }
James Smart2e0fef82007-06-17 19:56:36 -05005076
5077 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005078 clear_la_pending = 1;
5079 else
5080 clear_la_pending = 0;
5081
James Smart2e0fef82007-06-17 19:56:36 -05005082 if (vport->port_state < LPFC_VPORT_READY)
5083 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005084
James Smart2e0fef82007-06-17 19:56:36 -05005085 lpfc_set_disctmo(vport);
5086
James Smart2e0fef82007-06-17 19:56:36 -05005087 vport->fc_prevDID = vport->fc_myDID;
5088 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005089
5090 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005091 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5092 "0202 Start Discovery hba state x%x "
5093 "Data: x%x x%x x%x\n",
5094 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5095 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005096
5097 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005098 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005099
5100 if (num_sent)
5101 return;
5102
James Smart6fa139f2013-04-17 20:20:00 -04005103 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005104 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005105 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005106 !(vport->fc_flag & FC_RSCN_MODE) &&
5107 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005108 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005109 lpfc_issue_reg_vpi(phba, vport);
5110 return;
5111 }
James Smart2e0fef82007-06-17 19:56:36 -05005112
James Smart92d7f7b2007-06-17 19:56:38 -05005113 /*
5114 * For SLI2, we need to set port_state to READY and continue
5115 * discovery.
5116 */
5117 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5118 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005119 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005120
5121 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005122 vport->num_disc_nodes = 0;
5123 /* go thru NPR nodes and issue ELS PLOGIs */
5124 if (vport->fc_npr_cnt)
5125 lpfc_els_disc_plogi(vport);
5126
5127 if (!vport->num_disc_nodes) {
5128 spin_lock_irq(shost->host_lock);
5129 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5130 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005131 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005132 }
5133 }
James Smart92d7f7b2007-06-17 19:56:38 -05005134 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005135 } else {
5136 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005137 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005138
5139 if (num_sent)
5140 return;
5141
James Smart2e0fef82007-06-17 19:56:36 -05005142 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005143 /* Check to see if more RSCNs came in while we
5144 * were processing this one.
5145 */
James Smart2e0fef82007-06-17 19:56:36 -05005146 if ((vport->fc_rscn_id_cnt == 0) &&
5147 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5148 spin_lock_irq(shost->host_lock);
5149 vport->fc_flag &= ~FC_RSCN_MODE;
5150 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005151 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005152 } else
James Smart2e0fef82007-06-17 19:56:36 -05005153 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005154 }
5155 }
5156 return;
5157}
5158
5159/*
5160 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5161 * ring the match the sppecified nodelist.
5162 */
5163static void
James Smart2e0fef82007-06-17 19:56:36 -05005164lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005165{
James Smart2534ba72007-04-25 09:52:20 -04005166 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005167 struct lpfc_sli *psli;
5168 IOCB_t *icmd;
5169 struct lpfc_iocbq *iocb, *next_iocb;
5170 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005171
5172 psli = &phba->sli;
5173 pring = &psli->ring[LPFC_ELS_RING];
5174
5175 /* Error matching iocb on txq or txcmplq
5176 * First check the txq.
5177 */
James Smart2e0fef82007-06-17 19:56:36 -05005178 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005179 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5180 if (iocb->context1 != ndlp) {
5181 continue;
5182 }
5183 icmd = &iocb->iocb;
5184 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5185 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5186
James Smart2534ba72007-04-25 09:52:20 -04005187 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005188 }
5189 }
5190
5191 /* Next check the txcmplq */
5192 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5193 if (iocb->context1 != ndlp) {
5194 continue;
5195 }
5196 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005197 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5198 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005199 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005200 }
5201 }
James Smart2e0fef82007-06-17 19:56:36 -05005202 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005203
James Smarta257bf92009-04-06 18:48:10 -04005204 /* Cancel all the IOCBs from the completions list */
5205 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5206 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005207}
5208
Adrian Bunka6ababd2007-11-05 18:07:33 +01005209static void
James Smart2e0fef82007-06-17 19:56:36 -05005210lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005211{
5212 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005213 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005214
James Smart2e0fef82007-06-17 19:56:36 -05005215 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5216 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005217 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005218 if (!NLP_CHK_NODE_ACT(ndlp))
5219 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005220 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5221 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5222 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005223 }
dea31012005-04-17 16:05:31 -05005224 }
5225 }
dea31012005-04-17 16:05:31 -05005226}
5227
James Smart92d7f7b2007-06-17 19:56:38 -05005228void
5229lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5230{
5231 lpfc_els_flush_rscn(vport);
5232 lpfc_els_flush_cmd(vport);
5233 lpfc_disc_flush_list(vport);
5234}
5235
dea31012005-04-17 16:05:31 -05005236/*****************************************************************************/
5237/*
5238 * NAME: lpfc_disc_timeout
5239 *
5240 * FUNCTION: Fibre Channel driver discovery timeout routine.
5241 *
5242 * EXECUTION ENVIRONMENT: interrupt only
5243 *
5244 * CALLED FROM:
5245 * Timer function
5246 *
5247 * RETURNS:
5248 * none
5249 */
5250/*****************************************************************************/
5251void
5252lpfc_disc_timeout(unsigned long ptr)
5253{
James Smart2e0fef82007-06-17 19:56:36 -05005254 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5255 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005256 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005257 unsigned long flags = 0;
5258
5259 if (unlikely(!phba))
5260 return;
5261
James Smart5e9d9b82008-06-14 22:52:53 -04005262 spin_lock_irqsave(&vport->work_port_lock, flags);
5263 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5264 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005265 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005266 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005267
James Smart5e9d9b82008-06-14 22:52:53 -04005268 if (!tmo_posted)
5269 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005270 return;
5271}
5272
5273static void
James Smart2e0fef82007-06-17 19:56:36 -05005274lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005275{
James Smart2e0fef82007-06-17 19:56:36 -05005276 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5277 struct lpfc_hba *phba = vport->phba;
5278 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005279 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005280 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005281 int rc, clrlaerr = 0;
5282
James Smart2e0fef82007-06-17 19:56:36 -05005283 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005284 return;
5285
James Smart2e0fef82007-06-17 19:56:36 -05005286 spin_lock_irq(shost->host_lock);
5287 vport->fc_flag &= ~FC_DISC_TMO;
5288 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005289
James Smart858c9f62007-06-17 19:56:39 -05005290 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5291 "disc timeout: state:x%x rtry:x%x flg:x%x",
5292 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5293
James Smart2e0fef82007-06-17 19:56:36 -05005294 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005295
5296 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005297 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5298 * FAN
5299 */
5300 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005301 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5302 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005303 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005304 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005305 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005306 if (!NLP_CHK_NODE_ACT(ndlp))
5307 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005308 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5309 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005310 if (ndlp->nlp_type & NLP_FABRIC) {
5311 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005312 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005313
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005314 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005315 /* Fail outstanding IO now since device
5316 * is marked for PLOGI.
5317 */
James Smart2e0fef82007-06-17 19:56:36 -05005318 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005319 }
5320 }
James Smart92d7f7b2007-06-17 19:56:38 -05005321 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005322 if (phba->sli_rev <= LPFC_SLI_REV3)
5323 lpfc_initial_flogi(vport);
5324 else
5325 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005326 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005327 }
dea31012005-04-17 16:05:31 -05005328 break;
5329
James Smart92d7f7b2007-06-17 19:56:38 -05005330 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005331 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005332 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005333 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005334 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5335 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005336 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005337
5338 /* Assume no Fabric and go on with discovery.
5339 * Check for outstanding ELS FLOGI to abort.
5340 */
5341
5342 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005343 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005344
5345 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005346 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005347 break;
5348
5349 case LPFC_FABRIC_CFG_LINK:
5350 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5351 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005352 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5353 "0223 Timeout while waiting for "
5354 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005355 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005356 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005357 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005358 lpfc_els_abort(phba, ndlp);
5359
5360 /* ReStart discovery */
5361 goto restart_disc;
dea31012005-04-17 16:05:31 -05005362
5363 case LPFC_NS_QRY:
5364 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005365 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5366 "0224 NameServer Query timeout "
5367 "Data: x%x x%x\n",
5368 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005369
James Smart92d7f7b2007-06-17 19:56:38 -05005370 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5371 /* Try it one more time */
5372 vport->fc_ns_retry++;
5373 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5374 vport->fc_ns_retry, 0);
5375 if (rc == 0)
5376 break;
dea31012005-04-17 16:05:31 -05005377 }
James Smart92d7f7b2007-06-17 19:56:38 -05005378 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005379
James Smart87af33f2007-10-27 13:37:43 -04005380restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005381 /*
5382 * Discovery is over.
5383 * set port_state to PORT_READY if SLI2.
5384 * cmpl_reg_vpi will set port_state to READY for SLI3.
5385 */
James Smart3772a992009-05-22 14:50:54 -04005386 if (phba->sli_rev < LPFC_SLI_REV4) {
5387 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5388 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005389 else {
James Smart3772a992009-05-22 14:50:54 -04005390 lpfc_issue_clear_la(phba, vport);
5391 vport->port_state = LPFC_VPORT_READY;
5392 }
dea31012005-04-17 16:05:31 -05005393 }
5394
5395 /* Setup and issue mailbox INITIALIZE LINK command */
5396 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5397 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005398 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5399 "0206 Device Discovery "
5400 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005401 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005402 break;
5403 }
5404
5405 lpfc_linkdown(phba);
5406 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5407 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005408 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005409 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005410 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005411 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005412 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005413 if (rc == MBX_NOT_FINISHED)
5414 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5415
5416 break;
5417
5418 case LPFC_DISC_AUTH:
5419 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005420 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5421 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005422 lpfc_disc_flush_list(vport);
5423
James Smart92d7f7b2007-06-17 19:56:38 -05005424 /*
5425 * set port_state to PORT_READY if SLI2.
5426 * cmpl_reg_vpi will set port_state to READY for SLI3.
5427 */
James Smart3772a992009-05-22 14:50:54 -04005428 if (phba->sli_rev < LPFC_SLI_REV4) {
5429 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5430 lpfc_issue_reg_vpi(phba, vport);
5431 else { /* NPIV Not enabled */
5432 lpfc_issue_clear_la(phba, vport);
5433 vport->port_state = LPFC_VPORT_READY;
5434 }
dea31012005-04-17 16:05:31 -05005435 }
5436 break;
5437
James Smart2e0fef82007-06-17 19:56:36 -05005438 case LPFC_VPORT_READY:
5439 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005440 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5441 "0231 RSCN timeout Data: x%x "
5442 "x%x\n",
5443 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005444
5445 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005446 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005447
James Smart2e0fef82007-06-17 19:56:36 -05005448 lpfc_els_flush_rscn(vport);
5449 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005450 }
5451 break;
James Smart2e0fef82007-06-17 19:56:36 -05005452
James Smart92d7f7b2007-06-17 19:56:38 -05005453 default:
James Smarte8b62012007-08-02 11:10:09 -04005454 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005455 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005456 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005457 break;
5458 }
5459
5460 switch (phba->link_state) {
5461 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005462 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005463 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5464 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005465 clrlaerr = 1;
5466 break;
5467
James Smart09372822008-01-11 01:52:54 -05005468 case LPFC_LINK_UP:
5469 lpfc_issue_clear_la(phba, vport);
5470 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005471 case LPFC_LINK_UNKNOWN:
5472 case LPFC_WARM_START:
5473 case LPFC_INIT_START:
5474 case LPFC_INIT_MBX_CMDS:
5475 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005476 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005477 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5478 "0230 Unexpected timeout, hba link "
5479 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005480 clrlaerr = 1;
5481 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005482
5483 case LPFC_HBA_READY:
5484 break;
dea31012005-04-17 16:05:31 -05005485 }
5486
5487 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005488 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005489 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005490 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5491 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005492 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005493 }
5494
5495 return;
5496}
5497
dea31012005-04-17 16:05:31 -05005498/*
5499 * This routine handles processing a NameServer REG_LOGIN mailbox
5500 * command upon completion. It is setup in the LPFC_MBOXQ
5501 * as the completion routine when the command is
5502 * handed off to the SLI layer.
5503 */
5504void
James Smart2e0fef82007-06-17 19:56:36 -05005505lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005506{
James Smart04c68492009-05-22 14:52:52 -04005507 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005508 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5509 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5510 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005511
5512 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005513 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005514
James Smart6d368e52011-05-24 11:44:12 -04005515 if (phba->sli_rev < LPFC_SLI_REV4)
5516 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005517 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005518 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005519 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005520 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5521 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5522 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5523 atomic_read(&ndlp->kref.refcount),
5524 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005525 /*
5526 * Start issuing Fabric-Device Management Interface (FDMI) command to
5527 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5528 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005529 */
James Smart2e0fef82007-06-17 19:56:36 -05005530
James Smart76b2c342015-04-07 15:07:19 -04005531 if (vport->cfg_fdmi_on & LPFC_FDMI_REG_DELAY)
James Smart256ec0d2013-04-17 20:14:58 -04005532 mod_timer(&vport->fc_fdmitmo,
5533 jiffies + msecs_to_jiffies(1000 * 60));
James Smart76b2c342015-04-07 15:07:19 -04005534 else
5535 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
dea31012005-04-17 16:05:31 -05005536
James Smartfa4066b2008-01-11 01:53:27 -05005537 /* decrement the node reference count held for this callback
5538 * function.
5539 */
James Smart329f9bc2007-04-25 09:53:01 -04005540 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005541 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5542 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005543 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005544
5545 return;
5546}
5547
James Smart685f0bf2007-04-25 09:53:08 -04005548static int
5549lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5550{
5551 uint16_t *rpi = param;
5552
James Smarteff4a012012-01-18 16:25:25 -05005553 /* check for active node */
5554 if (!NLP_CHK_NODE_ACT(ndlp))
5555 return 0;
5556
James Smart685f0bf2007-04-25 09:53:08 -04005557 return ndlp->nlp_rpi == *rpi;
5558}
5559
5560static int
5561lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5562{
5563 return memcmp(&ndlp->nlp_portname, param,
5564 sizeof(ndlp->nlp_portname)) == 0;
5565}
5566
Adrian Bunka6ababd2007-11-05 18:07:33 +01005567static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005568__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005569{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005570 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005571
James Smart2e0fef82007-06-17 19:56:36 -05005572 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005573 if (filter(ndlp, param)) {
5574 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5575 "3185 FIND node filter %p DID "
5576 "Data: x%p x%x x%x\n",
5577 filter, ndlp, ndlp->nlp_DID,
5578 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005579 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005580 }
James Smart685f0bf2007-04-25 09:53:08 -04005581 }
James Smart34f5ad82012-08-03 12:35:03 -04005582 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5583 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005584 return NULL;
dea31012005-04-17 16:05:31 -05005585}
5586
James Smart685f0bf2007-04-25 09:53:08 -04005587/*
5588 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005589 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005590 */
5591struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005592__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005593{
James Smart2e0fef82007-06-17 19:56:36 -05005594 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005595}
5596
James Smart488d1462006-03-07 15:02:37 -05005597/*
James Smart685f0bf2007-04-25 09:53:08 -04005598 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005599 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005600 */
5601struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005602lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005603{
James Smart2e0fef82007-06-17 19:56:36 -05005604 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005605 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005606
James Smart2e0fef82007-06-17 19:56:36 -05005607 spin_lock_irq(shost->host_lock);
5608 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5609 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005610 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005611}
5612
James Smartcb69f7d2011-12-13 13:21:57 -05005613/*
5614 * This routine looks up the ndlp lists for the given RPI. If the rpi
5615 * is found, the routine returns the node element list pointer else
5616 * return NULL.
5617 */
5618struct lpfc_nodelist *
5619lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5620{
5621 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5622 struct lpfc_nodelist *ndlp;
5623
5624 spin_lock_irq(shost->host_lock);
5625 ndlp = __lpfc_findnode_rpi(vport, rpi);
5626 spin_unlock_irq(shost->host_lock);
5627 return ndlp;
5628}
5629
5630/**
5631 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5632 * @phba: pointer to lpfc hba data structure.
5633 * @vpi: the physical host virtual N_Port identifier.
5634 *
5635 * This routine finds a vport on a HBA (referred by @phba) through a
5636 * @vpi. The function walks the HBA's vport list and returns the address
5637 * of the vport with the matching @vpi.
5638 *
5639 * Return code
5640 * NULL - No vport with the matching @vpi found
5641 * Otherwise - Address to the vport with the matching @vpi.
5642 **/
5643struct lpfc_vport *
5644lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5645{
5646 struct lpfc_vport *vport;
5647 unsigned long flags;
5648 int i = 0;
5649
5650 /* The physical ports are always vpi 0 - translate is unnecessary. */
5651 if (vpi > 0) {
5652 /*
5653 * Translate the physical vpi to the logical vpi. The
5654 * vport stores the logical vpi.
5655 */
5656 for (i = 0; i < phba->max_vpi; i++) {
5657 if (vpi == phba->vpi_ids[i])
5658 break;
5659 }
5660
5661 if (i >= phba->max_vpi) {
5662 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5663 "2936 Could not find Vport mapped "
5664 "to vpi %d\n", vpi);
5665 return NULL;
5666 }
5667 }
5668
5669 spin_lock_irqsave(&phba->hbalock, flags);
5670 list_for_each_entry(vport, &phba->port_list, listentry) {
5671 if (vport->vpi == i) {
5672 spin_unlock_irqrestore(&phba->hbalock, flags);
5673 return vport;
5674 }
5675 }
5676 spin_unlock_irqrestore(&phba->hbalock, flags);
5677 return NULL;
5678}
5679
dea31012005-04-17 16:05:31 -05005680void
James Smart2e0fef82007-06-17 19:56:36 -05005681lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5682 uint32_t did)
dea31012005-04-17 16:05:31 -05005683{
5684 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005685
5686 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005687 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005688 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005689 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005690 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5691 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5692 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5693 ndlp->nlp_flag,
5694 atomic_read(&ndlp->kref.refcount),
5695 ndlp->nlp_usg_map, ndlp);
5696
James Smartcff261f2013-12-17 20:29:47 -05005697 ndlp->active_rrqs_xri_bitmap =
5698 mempool_alloc(vport->phba->active_rrq_pool,
5699 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005700 if (ndlp->active_rrqs_xri_bitmap)
5701 memset(ndlp->active_rrqs_xri_bitmap, 0,
5702 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005703 }
5704
James Smart725dd392013-10-10 12:23:30 -04005705
James Smart858c9f62007-06-17 19:56:39 -05005706
5707 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5708 "node init: did:x%x",
5709 ndlp->nlp_DID, 0, 0);
5710
dea31012005-04-17 16:05:31 -05005711 return;
5712}
James Smart329f9bc2007-04-25 09:53:01 -04005713
James Smart98c9ea52007-10-27 13:37:33 -04005714/* This routine releases all resources associated with a specifc NPort's ndlp
5715 * and mempool_free's the nodelist.
5716 */
James Smart311464e2007-08-02 11:10:37 -04005717static void
James Smart329f9bc2007-04-25 09:53:01 -04005718lpfc_nlp_release(struct kref *kref)
5719{
James Smarte47c9092008-02-08 18:49:26 -05005720 struct lpfc_hba *phba;
5721 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005722 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5723 kref);
James Smart858c9f62007-06-17 19:56:39 -05005724
5725 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5726 "node release: did:x%x flg:x%x type:x%x",
5727 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5728
James Smarte47c9092008-02-08 18:49:26 -05005729 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005730 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005731 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005732 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smartbe6bb942015-04-07 15:07:22 -04005733 atomic_read(&ndlp->kref.refcount), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005734
5735 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005736 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005737
5738 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005739 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005740 spin_lock_irqsave(&phba->ndlp_lock, flags);
5741 NLP_CLR_NODE_ACT(ndlp);
5742 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005743 if (phba->sli_rev == LPFC_SLI_REV4)
5744 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005745
5746 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005747 if (NLP_CHK_FREE_REQ(ndlp)) {
5748 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005749 if (phba->sli_rev == LPFC_SLI_REV4)
5750 mempool_free(ndlp->active_rrqs_xri_bitmap,
5751 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005752 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005753 }
James Smart329f9bc2007-04-25 09:53:01 -04005754}
5755
James Smart98c9ea52007-10-27 13:37:33 -04005756/* This routine bumps the reference count for a ndlp structure to ensure
5757 * that one discovery thread won't free a ndlp while another discovery thread
5758 * is using it.
5759 */
James Smart329f9bc2007-04-25 09:53:01 -04005760struct lpfc_nodelist *
5761lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5762{
James Smarte47c9092008-02-08 18:49:26 -05005763 struct lpfc_hba *phba;
5764 unsigned long flags;
5765
James Smart98c9ea52007-10-27 13:37:33 -04005766 if (ndlp) {
5767 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5768 "node get: did:x%x flg:x%x refcnt:x%x",
5769 ndlp->nlp_DID, ndlp->nlp_flag,
5770 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005771 /* The check of ndlp usage to prevent incrementing the
5772 * ndlp reference count that is in the process of being
5773 * released.
5774 */
James Smarta257bf92009-04-06 18:48:10 -04005775 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005776 spin_lock_irqsave(&phba->ndlp_lock, flags);
5777 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5778 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5779 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5780 "0276 lpfc_nlp_get: ndlp:x%p "
5781 "usgmap:x%x refcnt:%d\n",
5782 (void *)ndlp, ndlp->nlp_usg_map,
5783 atomic_read(&ndlp->kref.refcount));
5784 return NULL;
5785 } else
5786 kref_get(&ndlp->kref);
5787 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005788 }
James Smart329f9bc2007-04-25 09:53:01 -04005789 return ndlp;
5790}
5791
James Smart98c9ea52007-10-27 13:37:33 -04005792/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005793 * count goes to 0, this indicates the the associated nodelist should be
5794 * freed. Returning 1 indicates the ndlp resource has been released; on the
5795 * other hand, returning 0 indicates the ndlp resource has not been released
5796 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005797 */
James Smart329f9bc2007-04-25 09:53:01 -04005798int
5799lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5800{
James Smarte47c9092008-02-08 18:49:26 -05005801 struct lpfc_hba *phba;
5802 unsigned long flags;
5803
5804 if (!ndlp)
5805 return 1;
5806
5807 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5808 "node put: did:x%x flg:x%x refcnt:x%x",
5809 ndlp->nlp_DID, ndlp->nlp_flag,
5810 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005811 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005812 spin_lock_irqsave(&phba->ndlp_lock, flags);
5813 /* Check the ndlp memory free acknowledge flag to avoid the
5814 * possible race condition that kref_put got invoked again
5815 * after previous one has done ndlp memory free.
5816 */
5817 if (NLP_CHK_FREE_ACK(ndlp)) {
5818 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5819 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5820 "0274 lpfc_nlp_put: ndlp:x%p "
5821 "usgmap:x%x refcnt:%d\n",
5822 (void *)ndlp, ndlp->nlp_usg_map,
5823 atomic_read(&ndlp->kref.refcount));
5824 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005825 }
James Smarte47c9092008-02-08 18:49:26 -05005826 /* Check the ndlp inactivate log flag to avoid the possible
5827 * race condition that kref_put got invoked again after ndlp
5828 * is already in inactivating state.
5829 */
5830 if (NLP_CHK_IACT_REQ(ndlp)) {
5831 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5832 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5833 "0275 lpfc_nlp_put: ndlp:x%p "
5834 "usgmap:x%x refcnt:%d\n",
5835 (void *)ndlp, ndlp->nlp_usg_map,
5836 atomic_read(&ndlp->kref.refcount));
5837 return 1;
5838 }
5839 /* For last put, mark the ndlp usage flags to make sure no
5840 * other kref_get and kref_put on the same ndlp shall get
5841 * in between the process when the final kref_put has been
5842 * invoked on this ndlp.
5843 */
5844 if (atomic_read(&ndlp->kref.refcount) == 1) {
5845 /* Indicate ndlp is put to inactive state. */
5846 NLP_SET_IACT_REQ(ndlp);
5847 /* Acknowledge ndlp memory free has been seen. */
5848 if (NLP_CHK_FREE_REQ(ndlp))
5849 NLP_SET_FREE_ACK(ndlp);
5850 }
5851 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5852 /* Note, the kref_put returns 1 when decrementing a reference
5853 * count that was 1, it invokes the release callback function,
5854 * but it still left the reference count as 1 (not actually
5855 * performs the last decrementation). Otherwise, it actually
5856 * decrements the reference count and returns 0.
5857 */
5858 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005859}
James Smart98c9ea52007-10-27 13:37:33 -04005860
5861/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005862 * by any other discovery thread. This routine returns 1 if the
5863 * ndlp has been freed. A return value of 0 indicates the ndlp is
5864 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005865 */
5866int
5867lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5868{
5869 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5870 "node not used: did:x%x flg:x%x refcnt:x%x",
5871 ndlp->nlp_DID, ndlp->nlp_flag,
5872 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005873 if (atomic_read(&ndlp->kref.refcount) == 1)
5874 if (lpfc_nlp_put(ndlp))
5875 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005876 return 0;
5877}
James Smart6fb120a2009-05-22 14:52:59 -04005878
5879/**
5880 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5881 * @phba: Pointer to hba context object.
5882 *
5883 * This function iterate through all FC nodes associated
5884 * will all vports to check if there is any node with
5885 * fc_rports associated with it. If there is an fc_rport
5886 * associated with the node, then the node is either in
5887 * discovered state or its devloss_timer is pending.
5888 */
5889static int
5890lpfc_fcf_inuse(struct lpfc_hba *phba)
5891{
5892 struct lpfc_vport **vports;
5893 int i, ret = 0;
5894 struct lpfc_nodelist *ndlp;
5895 struct Scsi_Host *shost;
5896
5897 vports = lpfc_create_vport_work_array(phba);
5898
James Smart63e801c2010-11-20 23:14:19 -05005899 /* If driver cannot allocate memory, indicate fcf is in use */
5900 if (!vports)
5901 return 1;
5902
James Smart6fb120a2009-05-22 14:52:59 -04005903 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5904 shost = lpfc_shost_from_vport(vports[i]);
5905 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005906 /*
5907 * IF the CVL_RCVD bit is not set then we have sent the
5908 * flogi.
5909 * If dev_loss fires while we are waiting we do not want to
5910 * unreg the fcf.
5911 */
5912 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5913 spin_unlock_irq(shost->host_lock);
5914 ret = 1;
5915 goto out;
5916 }
James Smart6fb120a2009-05-22 14:52:59 -04005917 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5918 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5919 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5920 ret = 1;
5921 spin_unlock_irq(shost->host_lock);
5922 goto out;
James Smart80c17842012-03-01 22:35:45 -05005923 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5924 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005925 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005926 "2624 RPI %x DID %x flag %x "
5927 "still logged in\n",
5928 ndlp->nlp_rpi, ndlp->nlp_DID,
5929 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005930 }
5931 }
5932 spin_unlock_irq(shost->host_lock);
5933 }
5934out:
5935 lpfc_destroy_vport_work_array(phba, vports);
5936 return ret;
5937}
5938
5939/**
5940 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5941 * @phba: Pointer to hba context object.
5942 * @mboxq: Pointer to mailbox object.
5943 *
5944 * This function frees memory associated with the mailbox command.
5945 */
James Smart1b511972011-12-13 13:23:09 -05005946void
James Smart6fb120a2009-05-22 14:52:59 -04005947lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5948{
5949 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005950 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005951
5952 if (mboxq->u.mb.mbxStatus) {
5953 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5954 "2555 UNREG_VFI mbxStatus error x%x "
5955 "HBA state x%x\n",
5956 mboxq->u.mb.mbxStatus, vport->port_state);
5957 }
James Smart38b92ef2010-08-04 16:11:39 -04005958 spin_lock_irq(shost->host_lock);
5959 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5960 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005961 mempool_free(mboxq, phba->mbox_mem_pool);
5962 return;
5963}
5964
5965/**
5966 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5967 * @phba: Pointer to hba context object.
5968 * @mboxq: Pointer to mailbox object.
5969 *
5970 * This function frees memory associated with the mailbox command.
5971 */
5972static void
5973lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5974{
5975 struct lpfc_vport *vport = mboxq->vport;
5976
5977 if (mboxq->u.mb.mbxStatus) {
5978 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5979 "2550 UNREG_FCFI mbxStatus error x%x "
5980 "HBA state x%x\n",
5981 mboxq->u.mb.mbxStatus, vport->port_state);
5982 }
5983 mempool_free(mboxq, phba->mbox_mem_pool);
5984 return;
5985}
5986
5987/**
James Smartecfd03c2010-02-12 14:41:27 -05005988 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005989 * @phba: Pointer to hba context object.
5990 *
James Smartecfd03c2010-02-12 14:41:27 -05005991 * This function prepare the HBA for unregistering the currently registered
5992 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5993 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005994 */
James Smartecfd03c2010-02-12 14:41:27 -05005995int
5996lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005997{
James Smart6fb120a2009-05-22 14:52:59 -04005998 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005999 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006000 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006001 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006002
James Smartecfd03c2010-02-12 14:41:27 -05006003 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006004 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006005 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006006
James Smart4d9ab992009-10-02 15:16:39 -04006007 /* At this point, all discovery is aborted */
6008 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006009
6010 /* Unregister VPIs */
6011 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006012 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006013 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006014 /* Stop FLOGI/FDISC retries */
6015 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6016 if (ndlp)
6017 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006018 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006019 if (phba->sli_rev == LPFC_SLI_REV4)
6020 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006021 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006022 shost = lpfc_shost_from_vport(vports[i]);
6023 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006024 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006025 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006026 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006027 }
6028 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006029 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6030 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6031 if (ndlp)
6032 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6033 lpfc_cleanup_pending_mbox(phba->pport);
6034 if (phba->sli_rev == LPFC_SLI_REV4)
6035 lpfc_sli4_unreg_all_rpis(phba->pport);
6036 lpfc_mbx_unreg_vpi(phba->pport);
6037 shost = lpfc_shost_from_vport(phba->pport);
6038 spin_lock_irq(shost->host_lock);
6039 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6040 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6041 spin_unlock_irq(shost->host_lock);
6042 }
James Smart6fb120a2009-05-22 14:52:59 -04006043
James Smart695a8142010-01-26 23:08:03 -05006044 /* Cleanup any outstanding ELS commands */
6045 lpfc_els_flush_all_cmd(phba);
6046
James Smart1b511972011-12-13 13:23:09 -05006047 /* Unregister the physical port VFI */
6048 rc = lpfc_issue_unreg_vfi(phba->pport);
6049 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006050}
6051
6052/**
6053 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6054 * @phba: Pointer to hba context object.
6055 *
6056 * This function issues synchronous unregister FCF mailbox command to HBA to
6057 * unregister the currently registered FCF record. The driver does not reset
6058 * the driver FCF usage state flags.
6059 *
6060 * Return 0 if successfully issued, none-zero otherwise.
6061 */
6062int
6063lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6064{
6065 LPFC_MBOXQ_t *mbox;
6066 int rc;
6067
James Smart6fb120a2009-05-22 14:52:59 -04006068 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6069 if (!mbox) {
6070 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006071 "2551 UNREG_FCFI mbox allocation failed"
6072 "HBA state x%x\n", phba->pport->port_state);
6073 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006074 }
James Smart6fb120a2009-05-22 14:52:59 -04006075 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6076 mbox->vport = phba->pport;
6077 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6078 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6079
6080 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6082 "2552 Unregister FCFI command failed rc x%x "
6083 "HBA state x%x\n",
6084 rc, phba->pport->port_state);
6085 return -EINVAL;
6086 }
6087 return 0;
6088}
6089
6090/**
6091 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6092 * @phba: Pointer to hba context object.
6093 *
6094 * This function unregisters the currently reigstered FCF. This function
6095 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6096 */
6097void
6098lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6099{
6100 int rc;
6101
6102 /* Preparation for unregistering fcf */
6103 rc = lpfc_unregister_fcf_prep(phba);
6104 if (rc) {
6105 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6106 "2748 Failed to prepare for unregistering "
6107 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006108 return;
6109 }
6110
James Smartecfd03c2010-02-12 14:41:27 -05006111 /* Now, unregister FCF record and reset HBA FCF state */
6112 rc = lpfc_sli4_unregister_fcf(phba);
6113 if (rc)
6114 return;
6115 /* Reset HBA FCF states after successful unregister FCF */
6116 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006117 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006118
6119 /*
6120 * If driver is not unloading, check if there is any other
6121 * FCF record that can be used for discovery.
6122 */
6123 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006124 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006125 return;
6126
James Smart0c9ab6f2010-02-26 14:15:57 -05006127 /* This is considered as the initial FCF discovery scan */
6128 spin_lock_irq(&phba->hbalock);
6129 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6130 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006131
6132 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006133 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006134
James Smart0c9ab6f2010-02-26 14:15:57 -05006135 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006136
James Smart0c9ab6f2010-02-26 14:15:57 -05006137 if (rc) {
6138 spin_lock_irq(&phba->hbalock);
6139 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6140 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006141 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006142 "2553 lpfc_unregister_unused_fcf failed "
6143 "to read FCF record HBA state x%x\n",
6144 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006145 }
James Smartecfd03c2010-02-12 14:41:27 -05006146}
6147
6148/**
6149 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6150 * @phba: Pointer to hba context object.
6151 *
6152 * This function just unregisters the currently reigstered FCF. It does not
6153 * try to find another FCF for discovery.
6154 */
6155void
6156lpfc_unregister_fcf(struct lpfc_hba *phba)
6157{
6158 int rc;
6159
6160 /* Preparation for unregistering fcf */
6161 rc = lpfc_unregister_fcf_prep(phba);
6162 if (rc) {
6163 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6164 "2749 Failed to prepare for unregistering "
6165 "HBA's FCF record: rc=%d\n", rc);
6166 return;
6167 }
6168
6169 /* Now, unregister FCF record and reset HBA FCF state */
6170 rc = lpfc_sli4_unregister_fcf(phba);
6171 if (rc)
6172 return;
6173 /* Set proper HBA FCF states after successful unregister FCF */
6174 spin_lock_irq(&phba->hbalock);
6175 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6176 spin_unlock_irq(&phba->hbalock);
6177}
6178
6179/**
6180 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6181 * @phba: Pointer to hba context object.
6182 *
6183 * This function check if there are any connected remote port for the FCF and
6184 * if all the devices are disconnected, this function unregister FCFI.
6185 * This function also tries to use another FCF for discovery.
6186 */
6187void
6188lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6189{
6190 /*
James Smart3804dc82010-07-14 15:31:37 -04006191 * If HBA is not running in FIP mode, if HBA does not support
6192 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6193 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006194 */
6195 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006196 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006197 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006198 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006199 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006200 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006201 spin_unlock_irq(&phba->hbalock);
6202 return;
6203 }
6204 spin_unlock_irq(&phba->hbalock);
6205
6206 if (lpfc_fcf_inuse(phba))
6207 return;
6208
6209 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006210}
6211
6212/**
6213 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6214 * @phba: Pointer to hba context object.
6215 * @buff: Buffer containing the FCF connection table as in the config
6216 * region.
6217 * This function create driver data structure for the FCF connection
6218 * record table read from config region 23.
6219 */
6220static void
6221lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6222 uint8_t *buff)
6223{
6224 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6225 struct lpfc_fcf_conn_hdr *conn_hdr;
6226 struct lpfc_fcf_conn_rec *conn_rec;
6227 uint32_t record_count;
6228 int i;
6229
6230 /* Free the current connect table */
6231 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006232 &phba->fcf_conn_rec_list, list) {
6233 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006234 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006235 }
James Smart6fb120a2009-05-22 14:52:59 -04006236
6237 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6238 record_count = conn_hdr->length * sizeof(uint32_t)/
6239 sizeof(struct lpfc_fcf_conn_rec);
6240
6241 conn_rec = (struct lpfc_fcf_conn_rec *)
6242 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6243
6244 for (i = 0; i < record_count; i++) {
6245 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6246 continue;
6247 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6248 GFP_KERNEL);
6249 if (!conn_entry) {
6250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6251 "2566 Failed to allocate connection"
6252 " table entry\n");
6253 return;
6254 }
6255
6256 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6257 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006258 list_add_tail(&conn_entry->list,
6259 &phba->fcf_conn_rec_list);
6260 }
James Smartdf0d0852013-05-31 17:05:08 -04006261
6262 if (!list_empty(&phba->fcf_conn_rec_list)) {
6263 i = 0;
6264 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6265 list) {
6266 conn_rec = &conn_entry->conn_rec;
6267 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6268 "3345 FCF connection list rec[%02d]: "
6269 "flags:x%04x, vtag:x%04x, "
6270 "fabric_name:x%02x:%02x:%02x:%02x:"
6271 "%02x:%02x:%02x:%02x, "
6272 "switch_name:x%02x:%02x:%02x:%02x:"
6273 "%02x:%02x:%02x:%02x\n", i++,
6274 conn_rec->flags, conn_rec->vlan_tag,
6275 conn_rec->fabric_name[0],
6276 conn_rec->fabric_name[1],
6277 conn_rec->fabric_name[2],
6278 conn_rec->fabric_name[3],
6279 conn_rec->fabric_name[4],
6280 conn_rec->fabric_name[5],
6281 conn_rec->fabric_name[6],
6282 conn_rec->fabric_name[7],
6283 conn_rec->switch_name[0],
6284 conn_rec->switch_name[1],
6285 conn_rec->switch_name[2],
6286 conn_rec->switch_name[3],
6287 conn_rec->switch_name[4],
6288 conn_rec->switch_name[5],
6289 conn_rec->switch_name[6],
6290 conn_rec->switch_name[7]);
6291 }
6292 }
James Smart6fb120a2009-05-22 14:52:59 -04006293}
6294
6295/**
6296 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6297 * @phba: Pointer to hba context object.
6298 * @buff: Buffer containing the FCoE parameter data structure.
6299 *
6300 * This function update driver data structure with config
6301 * parameters read from config region 23.
6302 */
6303static void
6304lpfc_read_fcoe_param(struct lpfc_hba *phba,
6305 uint8_t *buff)
6306{
6307 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6308 struct lpfc_fcoe_params *fcoe_param;
6309
6310 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6311 buff;
6312 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006313 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006314
6315 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6316 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6317 return;
6318
James Smart6fb120a2009-05-22 14:52:59 -04006319 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6320 phba->valid_vlan = 1;
6321 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6322 0xFFF;
6323 }
6324
6325 phba->fc_map[0] = fcoe_param->fc_map[0];
6326 phba->fc_map[1] = fcoe_param->fc_map[1];
6327 phba->fc_map[2] = fcoe_param->fc_map[2];
6328 return;
6329}
6330
6331/**
6332 * lpfc_get_rec_conf23 - Get a record type in config region data.
6333 * @buff: Buffer containing config region 23 data.
6334 * @size: Size of the data buffer.
6335 * @rec_type: Record type to be searched.
6336 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006337 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006338 * of the record specified by record_type. If record found, this
6339 * function return pointer to the record else return NULL.
6340 */
6341static uint8_t *
6342lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6343{
6344 uint32_t offset = 0, rec_length;
6345
6346 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6347 (size < sizeof(uint32_t)))
6348 return NULL;
6349
6350 rec_length = buff[offset + 1];
6351
6352 /*
6353 * One TLV record has one word header and number of data words
6354 * specified in the rec_length field of the record header.
6355 */
6356 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6357 <= size) {
6358 if (buff[offset] == rec_type)
6359 return &buff[offset];
6360
6361 if (buff[offset] == LPFC_REGION23_LAST_REC)
6362 return NULL;
6363
6364 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6365 rec_length = buff[offset + 1];
6366 }
6367 return NULL;
6368}
6369
6370/**
6371 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6372 * @phba: Pointer to lpfc_hba data structure.
6373 * @buff: Buffer containing config region 23 data.
6374 * @size: Size of the data buffer.
6375 *
Stefan Weileef35c22010-08-06 21:11:15 +02006376 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006377 * populate driver data structure with the parameters.
6378 */
6379void
6380lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6381 uint8_t *buff,
6382 uint32_t size)
6383{
James Smarteb016562014-09-03 12:58:06 -04006384 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006385 uint8_t *rec_ptr;
6386
6387 /*
6388 * If data size is less than 2 words signature and version cannot be
6389 * verified.
6390 */
6391 if (size < 2*sizeof(uint32_t))
6392 return;
6393
6394 /* Check the region signature first */
6395 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6397 "2567 Config region 23 has bad signature\n");
6398 return;
6399 }
6400
6401 offset += 4;
6402
6403 /* Check the data structure version */
6404 if (buff[offset] != LPFC_REGION23_VERSION) {
6405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6406 "2568 Config region 23 has bad version\n");
6407 return;
6408 }
6409 offset += 4;
6410
James Smart6fb120a2009-05-22 14:52:59 -04006411 /* Read FCoE param record */
6412 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6413 size - offset, FCOE_PARAM_TYPE);
6414 if (rec_ptr)
6415 lpfc_read_fcoe_param(phba, rec_ptr);
6416
6417 /* Read FCF connection table */
6418 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6419 size - offset, FCOE_CONN_TBL_TYPE);
6420 if (rec_ptr)
6421 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6422
6423}