blob: 0dfa56604c9123dad6531d0bbb9b91e839ff1a90 [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 Smartf25e8e72015-04-07 15:07:28 -04004 * Copyright (C) 2004-2015 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 Smart7aafb6e2015-05-21 13:55:19 -0400164 if (put_rport)
165 put_device(&rport->dev);
James Smart34f5ad82012-08-03 12:35:03 -0400166 return;
167 }
James Smart12838e72014-09-03 12:57:19 -0400168
169 put_node = rdata->pnode != NULL;
170 put_rport = ndlp->rport != NULL;
171 rdata->pnode = NULL;
172 ndlp->rport = NULL;
173 if (put_node)
174 lpfc_nlp_put(ndlp);
175 if (put_rport)
176 put_device(&rport->dev);
177 return;
James Smart34f5ad82012-08-03 12:35:03 -0400178 }
179
James Smart858c9f62007-06-17 19:56:39 -0500180 evtp = &ndlp->dev_loss_evt;
181
182 if (!list_empty(&evtp->evt_listp))
183 return;
184
James Smarta62a4352013-04-17 20:18:19 -0400185 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart12838e72014-09-03 12:57:19 -0400186 ndlp->nlp_add_flag |= NLP_IN_DEV_LOSS;
James Smarta62a4352013-04-17 20:18:19 -0400187
James Smart858c9f62007-06-17 19:56:39 -0500188 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500189 /* We need to hold the node by incrementing the reference
190 * count until this queued work is done
191 */
James Smart5e9d9b82008-06-14 22:52:53 -0400192 if (evtp->evt_arg1) {
193 evtp->evt = LPFC_EVT_DEV_LOSS;
194 list_add_tail(&evtp->evt_listp, &phba->work_list);
195 lpfc_worker_wake_up(phba);
196 }
James Smart858c9f62007-06-17 19:56:39 -0500197 spin_unlock_irq(&phba->hbalock);
198
James Smart858c9f62007-06-17 19:56:39 -0500199 return;
200}
201
James Smarta93ff372010-10-22 11:06:08 -0400202/**
203 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
204 * @ndlp: Pointer to remote node object.
205 *
206 * This function is called from the worker thread when devloss timeout timer
207 * expires. For SLI4 host, this routine shall return 1 when at lease one
208 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
209 * routine shall return 0 when there is no remote node is still in use of FCF
210 * when devloss timeout happened to this @ndlp.
211 **/
212static int
James Smart858c9f62007-06-17 19:56:39 -0500213lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
214{
215 struct lpfc_rport_data *rdata;
216 struct fc_rport *rport;
217 struct lpfc_vport *vport;
218 struct lpfc_hba *phba;
219 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400220 int put_node;
221 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500222 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400223 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500224
225 rport = ndlp->rport;
226
James Smart12838e72014-09-03 12:57:19 -0400227 if (!rport) {
228 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smarta93ff372010-10-22 11:06:08 -0400229 return fcf_inuse;
James Smart12838e72014-09-03 12:57:19 -0400230 }
James Smart858c9f62007-06-17 19:56:39 -0500231
232 rdata = rport->dd_data;
233 name = (uint8_t *) &ndlp->nlp_portname;
234 vport = ndlp->vport;
235 phba = vport->phba;
236
James Smarta93ff372010-10-22 11:06:08 -0400237 if (phba->sli_rev == LPFC_SLI_REV4)
238 fcf_inuse = lpfc_fcf_inuse(phba);
239
James Smart858c9f62007-06-17 19:56:39 -0500240 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
241 "rport devlosstmo:did:x%x type:x%x id:x%x",
242 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
243
James Smart34f5ad82012-08-03 12:35:03 -0400244 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
245 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
246 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
247
James Smarta8adb832007-10-27 13:37:53 -0400248 /* Don't defer this if we are in the process of deleting the vport
249 * or unloading the driver. The unload will cleanup the node
250 * appropriately we just need to cleanup the ndlp rport info here.
251 */
252 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500253 if (ndlp->nlp_sid != NLP_NO_SID) {
254 /* flush the target */
255 lpfc_sli_abort_iocb(vport,
256 &phba->sli.ring[phba->sli.fcp_ring],
257 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
258 }
James Smarta8adb832007-10-27 13:37:53 -0400259 put_node = rdata->pnode != NULL;
260 put_rport = ndlp->rport != NULL;
261 rdata->pnode = NULL;
262 ndlp->rport = NULL;
James Smart12838e72014-09-03 12:57:19 -0400263 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smarta8adb832007-10-27 13:37:53 -0400264 if (put_node)
265 lpfc_nlp_put(ndlp);
266 if (put_rport)
267 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400268 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400269 }
270
James Smartd7c255b2008-08-24 21:50:00 -0400271 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
272 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
273 "0284 Devloss timeout Ignored on "
274 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
275 "NPort x%x\n",
276 *name, *(name+1), *(name+2), *(name+3),
277 *(name+4), *(name+5), *(name+6), *(name+7),
278 ndlp->nlp_DID);
James Smart12838e72014-09-03 12:57:19 -0400279 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smarta93ff372010-10-22 11:06:08 -0400280 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400281 }
James Smart858c9f62007-06-17 19:56:39 -0500282
James Smart92d7f7b2007-06-17 19:56:38 -0500283 if (ndlp->nlp_type & NLP_FABRIC) {
284 /* We will clean up these Nodes in linkup */
285 put_node = rdata->pnode != NULL;
286 put_rport = ndlp->rport != NULL;
287 rdata->pnode = NULL;
288 ndlp->rport = NULL;
James Smart12838e72014-09-03 12:57:19 -0400289 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smart92d7f7b2007-06-17 19:56:38 -0500290 if (put_node)
291 lpfc_nlp_put(ndlp);
292 if (put_rport)
293 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400294 return fcf_inuse;
James Smart92d7f7b2007-06-17 19:56:38 -0500295 }
James Smart82085712007-04-25 09:52:41 -0400296
dea31012005-04-17 16:05:31 -0500297 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400298 warn_on = 1;
dea31012005-04-17 16:05:31 -0500299 /* flush the target */
James Smart7ba36ef2014-09-03 12:57:43 -0400300 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smart51ef4c22007-08-02 11:10:31 -0400301 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
302 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500303 }
James Smartc01f3202006-08-18 17:47:08 -0400304
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400305 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400306 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
307 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400308 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
309 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400310 *name, *(name+1), *(name+2), *(name+3),
311 *(name+4), *(name+5), *(name+6), *(name+7),
312 ndlp->nlp_DID, ndlp->nlp_flag,
313 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400314 } else {
James Smarte8b62012007-08-02 11:10:09 -0400315 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
316 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400317 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
318 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400319 *name, *(name+1), *(name+2), *(name+3),
320 *(name+4), *(name+5), *(name+6), *(name+7),
321 ndlp->nlp_DID, ndlp->nlp_flag,
322 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400323 }
324
James Smart87af33f2007-10-27 13:37:43 -0400325 put_node = rdata->pnode != NULL;
326 put_rport = ndlp->rport != NULL;
327 rdata->pnode = NULL;
328 ndlp->rport = NULL;
James Smart12838e72014-09-03 12:57:19 -0400329 ndlp->nlp_add_flag &= ~NLP_IN_DEV_LOSS;
James Smart87af33f2007-10-27 13:37:43 -0400330 if (put_node)
331 lpfc_nlp_put(ndlp);
332 if (put_rport)
333 put_device(&rport->dev);
334
James Smart2e0fef82007-06-17 19:56:36 -0500335 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400336 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400337 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400338 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400339 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
340 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500341 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400342
James Smarta93ff372010-10-22 11:06:08 -0400343 return fcf_inuse;
344}
345
346/**
347 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
348 * @phba: Pointer to hba context object.
349 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
350 * @nlp_did: remote node identifer with devloss timeout.
351 *
352 * This function is called from the worker thread after invoking devloss
353 * timeout handler and releasing the reference count for the ndlp with
354 * which the devloss timeout was handled for SLI4 host. For the devloss
355 * timeout of the last remote node which had been in use of FCF, when this
356 * routine is invoked, it shall be guaranteed that none of the remote are
357 * in-use of FCF. When devloss timeout to the last remote using the FCF,
358 * if the FIP engine is neither in FCF table scan process nor roundrobin
359 * failover process, the in-use FCF shall be unregistered. If the FIP
360 * engine is in FCF discovery process, the devloss timeout state shall
361 * be set for either the FCF table scan process or roundrobin failover
362 * process to unregister the in-use FCF.
363 **/
364static void
365lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
366 uint32_t nlp_did)
367{
368 /* If devloss timeout happened to a remote node when FCF had no
369 * longer been in-use, do nothing.
370 */
371 if (!fcf_inuse)
372 return;
373
374 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
375 spin_lock_irq(&phba->hbalock);
376 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
377 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
378 spin_unlock_irq(&phba->hbalock);
379 return;
380 }
381 phba->hba_flag |= HBA_DEVLOSS_TMO;
382 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
383 "2847 Last remote node (x%x) using "
384 "FCF devloss tmo\n", nlp_did);
385 }
386 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
387 spin_unlock_irq(&phba->hbalock);
388 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
389 "2868 Devloss tmo to FCF rediscovery "
390 "in progress\n");
391 return;
392 }
393 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
394 spin_unlock_irq(&phba->hbalock);
395 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
396 "2869 Devloss tmo to idle FIP engine, "
397 "unreg in-use FCF and rescan.\n");
398 /* Unregister in-use FCF and rescan */
399 lpfc_unregister_fcf_rescan(phba);
400 return;
401 }
402 spin_unlock_irq(&phba->hbalock);
403 if (phba->hba_flag & FCF_TS_INPROG)
404 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
405 "2870 FCF table scan in progress\n");
406 if (phba->hba_flag & FCF_RR_INPROG)
407 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
408 "2871 FLOGI roundrobin FCF failover "
409 "in progress\n");
410 }
James Smart6fb120a2009-05-22 14:52:59 -0400411 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500412}
James Smartc01f3202006-08-18 17:47:08 -0400413
James Smartea2151b2008-09-07 11:52:10 -0400414/**
James Smart3621a712009-04-06 18:47:14 -0400415 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400416 * @phba: Pointer to hba context object.
417 *
418 * This function is called from the functions which need to post
419 * events from interrupt context. This function allocates data
420 * structure required for posting event. It also keeps track of
421 * number of events pending and prevent event storm when there are
422 * too many events.
423 **/
424struct lpfc_fast_path_event *
425lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
426 struct lpfc_fast_path_event *ret;
427
428 /* If there are lot of fast event do not exhaust memory due to this */
429 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
430 return NULL;
431
432 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
433 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400434 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400435 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400436 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
437 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
438 }
James Smartea2151b2008-09-07 11:52:10 -0400439 return ret;
440}
441
442/**
James Smart3621a712009-04-06 18:47:14 -0400443 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400444 * @phba: Pointer to hba context object.
445 * @evt: Event object which need to be freed.
446 *
447 * This function frees the data structure required for posting
448 * events.
449 **/
450void
451lpfc_free_fast_evt(struct lpfc_hba *phba,
452 struct lpfc_fast_path_event *evt) {
453
454 atomic_dec(&phba->fast_event_count);
455 kfree(evt);
456}
457
458/**
James Smart3621a712009-04-06 18:47:14 -0400459 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400460 * @phba: Pointer to hba context object.
461 * @evtp: Event data structure.
462 *
463 * This function is called from worker thread, when the interrupt
464 * context need to post an event. This function posts the event
465 * to fc transport netlink interface.
466 **/
467static void
468lpfc_send_fastpath_evt(struct lpfc_hba *phba,
469 struct lpfc_work_evt *evtp)
470{
471 unsigned long evt_category, evt_sub_category;
472 struct lpfc_fast_path_event *fast_evt_data;
473 char *evt_data;
474 uint32_t evt_data_size;
475 struct Scsi_Host *shost;
476
477 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
478 work_evt);
479
480 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
481 evt_sub_category = (unsigned long) fast_evt_data->un.
482 fabric_evt.subcategory;
483 shost = lpfc_shost_from_vport(fast_evt_data->vport);
484 if (evt_category == FC_REG_FABRIC_EVENT) {
485 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
486 evt_data = (char *) &fast_evt_data->un.read_check_error;
487 evt_data_size = sizeof(fast_evt_data->un.
488 read_check_error);
489 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500490 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400491 evt_data = (char *) &fast_evt_data->un.fabric_evt;
492 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
493 } else {
494 lpfc_free_fast_evt(phba, fast_evt_data);
495 return;
496 }
497 } else if (evt_category == FC_REG_SCSI_EVENT) {
498 switch (evt_sub_category) {
499 case LPFC_EVENT_QFULL:
500 case LPFC_EVENT_DEVBSY:
501 evt_data = (char *) &fast_evt_data->un.scsi_evt;
502 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
503 break;
504 case LPFC_EVENT_CHECK_COND:
505 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
506 evt_data_size = sizeof(fast_evt_data->un.
507 check_cond_evt);
508 break;
509 case LPFC_EVENT_VARQUEDEPTH:
510 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
511 evt_data_size = sizeof(fast_evt_data->un.
512 queue_depth_evt);
513 break;
514 default:
515 lpfc_free_fast_evt(phba, fast_evt_data);
516 return;
517 }
518 } else {
519 lpfc_free_fast_evt(phba, fast_evt_data);
520 return;
521 }
522
523 fc_host_post_vendor_event(shost,
524 fc_get_event_number(),
525 evt_data_size,
526 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500527 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400528
529 lpfc_free_fast_evt(phba, fast_evt_data);
530 return;
531}
532
dea31012005-04-17 16:05:31 -0500533static void
James Smart2e0fef82007-06-17 19:56:36 -0500534lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500535{
536 struct lpfc_work_evt *evtp = NULL;
537 struct lpfc_nodelist *ndlp;
538 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400539 int fcf_inuse;
540 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500541
James Smart2e0fef82007-06-17 19:56:36 -0500542 spin_lock_irq(&phba->hbalock);
543 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500544 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
545 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500546 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500547 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500548 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500549 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500550 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500551 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500552 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500553 /* decrement the node reference count held
554 * for this queued work
555 */
556 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500557 break;
James Smart858c9f62007-06-17 19:56:39 -0500558 case LPFC_EVT_DEV_LOSS:
559 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400560 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500561 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500562 /* decrement the node reference count held for
563 * this queued work
564 */
James Smarta93ff372010-10-22 11:06:08 -0400565 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500566 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400567 if (phba->sli_rev == LPFC_SLI_REV4)
568 lpfc_sli4_post_dev_loss_tmo_handler(phba,
569 fcf_inuse,
570 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500571 break;
dea31012005-04-17 16:05:31 -0500572 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500573 if (phba->link_state < LPFC_LINK_DOWN)
574 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500575 else
James Smart2e0fef82007-06-17 19:56:36 -0500576 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500577 complete((struct completion *)(evtp->evt_arg2));
578 break;
James Smart46fa3112007-04-25 09:51:45 -0400579 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500580 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400581 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400582 *(int *)(evtp->evt_arg1) = 0;
583 complete((struct completion *)(evtp->evt_arg2));
584 break;
585 case LPFC_EVT_OFFLINE:
586 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500587 lpfc_sli_brdrestart(phba);
588 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400589 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
590 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500591 complete((struct completion *)(evtp->evt_arg2));
592 break;
593 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400594 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500595 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500596 lpfc_sli_brdreset(phba);
597 lpfc_hba_down_post(phba);
598 *(int *)(evtp->evt_arg1) =
599 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400600 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500601 complete((struct completion *)(evtp->evt_arg2));
602 break;
603 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400604 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500605 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500606 = (phba->pport->stopped)
607 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400608 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500609 complete((struct completion *)(evtp->evt_arg2));
610 break;
James Smartea2151b2008-09-07 11:52:10 -0400611 case LPFC_EVT_FASTPATH_MGMT_EVT:
612 lpfc_send_fastpath_evt(phba, evtp);
613 free_evt = 0;
614 break;
James Smart78730cf2010-04-06 15:06:30 -0400615 case LPFC_EVT_RESET_HBA:
616 if (!(phba->pport->load_flag & FC_UNLOADING))
617 lpfc_reset_hba(phba);
618 break;
dea31012005-04-17 16:05:31 -0500619 }
620 if (free_evt)
621 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500622 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500623 }
James Smart2e0fef82007-06-17 19:56:36 -0500624 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500625
626}
627
James Smart311464e2007-08-02 11:10:37 -0400628static void
James Smart2e0fef82007-06-17 19:56:36 -0500629lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500630{
631 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500632 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400633 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400634 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400635 int i;
dea31012005-04-17 16:05:31 -0500636
James Smart2e0fef82007-06-17 19:56:36 -0500637 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500638 ha_copy = phba->work_ha;
639 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500640 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500641
James Smartda0436e2009-05-22 14:51:39 -0400642 /* First, try to post the next mailbox command to SLI4 device */
643 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
644 lpfc_sli4_post_async_mbox(phba);
645
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500646 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400647 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500648 lpfc_handle_eratt(phba);
649
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500650 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500651 lpfc_sli_handle_mb_event(phba);
652
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500653 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500654 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400655
James Smartda0436e2009-05-22 14:51:39 -0400656 /* Process SLI4 events */
657 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500658 if (phba->hba_flag & HBA_RRQ_ACTIVE)
659 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400660 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
661 lpfc_sli4_fcp_xri_abort_event_proc(phba);
662 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
663 lpfc_sli4_els_xri_abort_event_proc(phba);
664 if (phba->hba_flag & ASYNC_EVENT)
665 lpfc_sli4_async_event_proc(phba);
666 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
667 spin_lock_irq(&phba->hbalock);
668 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
669 spin_unlock_irq(&phba->hbalock);
670 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
671 }
James Smartecfd03c2010-02-12 14:41:27 -0500672 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
673 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400674 }
675
James Smart549e55c2007-08-02 11:09:51 -0400676 vports = lpfc_create_vport_work_array(phba);
677 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400678 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400679 /*
680 * We could have no vports in array if unloading, so if
681 * this happens then just use the pport
682 */
683 if (vports[i] == NULL && i == 0)
684 vport = phba->pport;
685 else
686 vport = vports[i];
687 if (vport == NULL)
688 break;
James Smart58da1ff2008-04-07 10:15:56 -0400689 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400690 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400691 vport->work_port_events &= ~work_port_events;
692 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400693 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400694 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400695 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400696 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400697 if (work_port_events & WORKER_HB_TMO)
698 lpfc_hb_timeout_handler(phba);
699 if (work_port_events & WORKER_MBOX_TMO)
700 lpfc_mbox_timeout_handler(phba);
701 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
702 lpfc_unblock_fabric_iocbs(phba);
703 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400704 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400705 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
706 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500707 if (work_port_events & WORKER_DELAYED_DISC_TMO)
708 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500709 }
James Smart09372822008-01-11 01:52:54 -0500710 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500711
James Smart858c9f62007-06-17 19:56:39 -0500712 pring = &phba->sli.ring[LPFC_ELS_RING];
713 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
714 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400715 if ((status & HA_RXMASK) ||
716 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400717 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400718 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500719 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400720 /* Set the lpfc data pending flag */
721 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500722 } else {
James Smart94661502013-03-01 16:37:29 -0500723 if (phba->link_state >= LPFC_LINK_UP) {
724 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
725 lpfc_sli_handle_slow_ring_event(phba, pring,
726 (status &
727 HA_RXMASK));
728 }
James Smart858c9f62007-06-17 19:56:39 -0500729 }
James Smart0e9bb8d2013-03-01 16:35:12 -0500730 if ((phba->sli_rev == LPFC_SLI_REV4) &
731 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400732 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500733 /*
734 * Turn on Ring interrupts
735 */
James Smart3772a992009-05-22 14:50:54 -0400736 if (phba->sli_rev <= LPFC_SLI_REV3) {
737 spin_lock_irq(&phba->hbalock);
738 control = readl(phba->HCregaddr);
739 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
740 lpfc_debugfs_slow_ring_trc(phba,
741 "WRK Enable ring: cntl:x%x hacopy:x%x",
742 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400743
James Smart3772a992009-05-22 14:50:54 -0400744 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
745 writel(control, phba->HCregaddr);
746 readl(phba->HCregaddr); /* flush */
747 } else {
748 lpfc_debugfs_slow_ring_trc(phba,
749 "WRK Ring ok: cntl:x%x hacopy:x%x",
750 control, ha_copy, 0);
751 }
752 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500753 }
754 }
James Smart2e0fef82007-06-17 19:56:36 -0500755 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500756}
757
dea31012005-04-17 16:05:31 -0500758int
759lpfc_do_work(void *p)
760{
761 struct lpfc_hba *phba = p;
762 int rc;
dea31012005-04-17 16:05:31 -0500763
Dongsheng Yang8698a742014-03-11 18:09:12 +0800764 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400765 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400766 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500767
James Smart3a55b532008-12-04 22:38:54 -0500768 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400769 /* wait and check worker queue activities */
770 rc = wait_event_interruptible(phba->work_waitq,
771 (test_and_clear_bit(LPFC_DATA_READY,
772 &phba->data_flags)
773 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500774 /* Signal wakeup shall terminate the worker thread */
775 if (rc) {
776 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
777 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500778 break;
James Smart3a55b532008-12-04 22:38:54 -0500779 }
dea31012005-04-17 16:05:31 -0500780
James Smart5e9d9b82008-06-14 22:52:53 -0400781 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500782 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500783 }
James Smart3a55b532008-12-04 22:38:54 -0500784 phba->worker_thread = NULL;
785 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
786 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500787 return 0;
788}
789
790/*
791 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300792 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500793 * embedding it in the IOCB.
794 */
795int
James Smart2e0fef82007-06-17 19:56:36 -0500796lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500797 uint32_t evt)
798{
799 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500800 unsigned long flags;
dea31012005-04-17 16:05:31 -0500801
802 /*
803 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
804 * be queued to worker thread for processing
805 */
James Smart92d7f7b2007-06-17 19:56:38 -0500806 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500807 if (!evtp)
808 return 0;
809
810 evtp->evt_arg1 = arg1;
811 evtp->evt_arg2 = arg2;
812 evtp->evt = evt;
813
James Smarted957682007-06-17 19:56:37 -0500814 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400815 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500816 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500817
James Smart5e9d9b82008-06-14 22:52:53 -0400818 lpfc_worker_wake_up(phba);
819
dea31012005-04-17 16:05:31 -0500820 return 1;
821}
822
James Smart92d7f7b2007-06-17 19:56:38 -0500823void
824lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
825{
James Smart09372822008-01-11 01:52:54 -0500826 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500827 struct lpfc_hba *phba = vport->phba;
828 struct lpfc_nodelist *ndlp, *next_ndlp;
829 int rc;
830
831 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500832 if (!NLP_CHK_NODE_ACT(ndlp))
833 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500834 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
835 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400836 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
837 ((vport->port_type == LPFC_NPIV_PORT) &&
838 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500839 lpfc_unreg_rpi(vport, ndlp);
840
841 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400842 if ((phba->sli_rev < LPFC_SLI_REV4) &&
843 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500844 continue;
845 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
846 remove
847 ? NLP_EVT_DEVICE_RM
848 : NLP_EVT_DEVICE_RECOVERY);
849 }
850 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400851 if (phba->sli_rev == LPFC_SLI_REV4)
852 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500853 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500854 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500855 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500856 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500857 }
858}
859
James Smart87af33f2007-10-27 13:37:43 -0400860void
James Smart98c9ea52007-10-27 13:37:33 -0400861lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500862{
James Smart695a8142010-01-26 23:08:03 -0500863 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
864
James Smart45ed1192009-10-02 15:17:02 -0400865 /* Cleanup any outstanding received buffers */
866 lpfc_cleanup_rcv_buffers(vport);
867
James Smart92d7f7b2007-06-17 19:56:38 -0500868 /* Cleanup any outstanding RSCN activity */
869 lpfc_els_flush_rscn(vport);
870
871 /* Cleanup any outstanding ELS commands */
872 lpfc_els_flush_cmd(vport);
873
874 lpfc_cleanup_rpis(vport, 0);
875
James Smart92d7f7b2007-06-17 19:56:38 -0500876 /* Turn off discovery timer if its running */
877 lpfc_can_disctmo(vport);
878}
879
James Smart3772a992009-05-22 14:50:54 -0400880void
James Smart98c9ea52007-10-27 13:37:33 -0400881lpfc_linkdown_port(struct lpfc_vport *vport)
882{
883 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
884
885 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
886
887 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
888 "Link Down: state:x%x rtry:x%x flg:x%x",
889 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
890
891 lpfc_port_link_failure(vport);
892
James Smart92494142011-02-16 12:39:44 -0500893 /* Stop delayed Nport discovery */
894 spin_lock_irq(shost->host_lock);
895 vport->fc_flag &= ~FC_DISC_DELAYED;
896 spin_unlock_irq(shost->host_lock);
897 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400898}
899
dea31012005-04-17 16:05:31 -0500900int
James Smart685f0bf2007-04-25 09:53:08 -0400901lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500902{
James Smart2e0fef82007-06-17 19:56:36 -0500903 struct lpfc_vport *vport = phba->pport;
904 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400905 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400906 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400907 int i;
dea31012005-04-17 16:05:31 -0500908
James Smart3163f722008-02-08 18:50:25 -0500909 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500910 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500911
912 /* Block all SCSI stack I/Os */
913 lpfc_scsi_dev_block(phba);
914
James Smart2e0fef82007-06-17 19:56:36 -0500915 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500916 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500917 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500918 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500919 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500920 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500921 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500922 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500923 }
James Smart549e55c2007-08-02 11:09:51 -0400924 vports = lpfc_create_vport_work_array(phba);
925 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400926 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400927 /* Issue a LINK DOWN event to all nodes */
928 lpfc_linkdown_port(vports[i]);
929 }
James Smart09372822008-01-11 01:52:54 -0500930 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500931 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500932 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
933 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400934 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500935 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500936 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400937 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500938 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500939 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500940 }
941 }
942
dea31012005-04-17 16:05:31 -0500943 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500944 if (phba->pport->fc_flag & FC_PT2PT) {
945 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500946 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
947 if (mb) {
dea31012005-04-17 16:05:31 -0500948 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500949 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500950 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400951 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500952 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500953 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500954 }
955 }
James Smart2e0fef82007-06-17 19:56:36 -0500956 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500957 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500958 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500959 }
James Smart2e0fef82007-06-17 19:56:36 -0500960
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500961 return 0;
dea31012005-04-17 16:05:31 -0500962}
963
James Smart92d7f7b2007-06-17 19:56:38 -0500964static void
965lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500966{
James Smart92d7f7b2007-06-17 19:56:38 -0500967 struct lpfc_nodelist *ndlp;
968
969 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500970 if (!NLP_CHK_NODE_ACT(ndlp))
971 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500972 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
973 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500974 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500975 /* On Linkup its safe to clean up the ndlp
976 * from Fabric connections.
977 */
James Smart92d7f7b2007-06-17 19:56:38 -0500978 if (ndlp->nlp_DID != Fabric_DID)
979 lpfc_unreg_rpi(vport, ndlp);
980 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
981 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500982 /* Fail outstanding IO now since device is
983 * marked for PLOGI.
984 */
James Smart92d7f7b2007-06-17 19:56:38 -0500985 lpfc_unreg_rpi(vport, ndlp);
986 }
987 }
988}
989
990static void
991lpfc_linkup_port(struct lpfc_vport *vport)
992{
993 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500994 struct lpfc_hba *phba = vport->phba;
995
996 if ((vport->load_flag & FC_UNLOADING) != 0)
997 return;
998
James Smart858c9f62007-06-17 19:56:39 -0500999 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1000 "Link Up: top:x%x speed:x%x flg:x%x",
1001 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1002
James Smart92d7f7b2007-06-17 19:56:38 -05001003 /* If NPIV is not enabled, only bring the physical port up */
1004 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1005 (vport != phba->pport))
1006 return;
dea31012005-04-17 16:05:31 -05001007
James Smart2e0fef82007-06-17 19:56:36 -05001008 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -04001009
James Smart2e0fef82007-06-17 19:56:36 -05001010 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001011 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1012 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1013 vport->fc_flag |= FC_NDISC_ACTIVE;
1014 vport->fc_ns_retry = 0;
1015 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001016
James Smart92d7f7b2007-06-17 19:56:38 -05001017 if (vport->fc_flag & FC_LBIT)
1018 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001019
James Smart92d7f7b2007-06-17 19:56:38 -05001020}
1021
1022static int
1023lpfc_linkup(struct lpfc_hba *phba)
1024{
James Smart549e55c2007-08-02 11:09:51 -04001025 struct lpfc_vport **vports;
1026 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001027
1028 phba->link_state = LPFC_LINK_UP;
1029
1030 /* Unblock fabric iocbs if they are blocked */
1031 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1032 del_timer_sync(&phba->fabric_block_timer);
1033
James Smart549e55c2007-08-02 11:09:51 -04001034 vports = lpfc_create_vport_work_array(phba);
1035 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001036 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001037 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001038 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001039
1040 return 0;
1041}
1042
1043/*
1044 * This routine handles processing a CLEAR_LA mailbox
1045 * command upon completion. It is setup in the LPFC_MBOXQ
1046 * as the completion routine when the command is
1047 * handed off to the SLI layer.
1048 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001049static void
James Smart2e0fef82007-06-17 19:56:36 -05001050lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001051{
James Smart2e0fef82007-06-17 19:56:36 -05001052 struct lpfc_vport *vport = pmb->vport;
1053 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1054 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001055 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001056 uint32_t control;
1057
dea31012005-04-17 16:05:31 -05001058 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001059 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001060 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1061 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1062
1063 /* Check for error */
1064 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001065 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001066 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1067 "0320 CLEAR_LA mbxStatus error x%x hba "
1068 "state x%x\n",
1069 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001070 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001071 goto out;
1072 }
1073
James Smart92d7f7b2007-06-17 19:56:38 -05001074 if (vport->port_type == LPFC_PHYSICAL_PORT)
1075 phba->link_state = LPFC_HBA_READY;
1076
1077 spin_lock_irq(&phba->hbalock);
1078 psli->sli_flag |= LPFC_PROCESS_LA;
1079 control = readl(phba->HCregaddr);
1080 control |= HC_LAINT_ENA;
1081 writel(control, phba->HCregaddr);
1082 readl(phba->HCregaddr); /* flush */
1083 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001084 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001085 return;
dea31012005-04-17 16:05:31 -05001086
dea31012005-04-17 16:05:31 -05001087out:
1088 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001089 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1090 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001091 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001092
James Smart2e0fef82007-06-17 19:56:36 -05001093 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001094 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001095 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001096
James Smart2e0fef82007-06-17 19:56:36 -05001097 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001098
1099 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001100
1101 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001102 psli->sli_flag |= LPFC_PROCESS_LA;
1103 control = readl(phba->HCregaddr);
1104 control |= HC_LAINT_ENA;
1105 writel(control, phba->HCregaddr);
1106 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001107 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001108
1109 return;
1110}
1111
James Smart2e0fef82007-06-17 19:56:36 -05001112
dea31012005-04-17 16:05:31 -05001113static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001114lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001115{
James Smart2e0fef82007-06-17 19:56:36 -05001116 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001117
James Smart04c68492009-05-22 14:52:52 -04001118 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001119 goto out;
dea31012005-04-17 16:05:31 -05001120
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001121 mempool_free(pmb, phba->mbox_mem_pool);
1122
James Smart1b511972011-12-13 13:23:09 -05001123 /* don't perform discovery for SLI4 loopback diagnostic test */
1124 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1125 !(phba->hba_flag & HBA_FCOE_MODE) &&
1126 (phba->link_flag & LS_LOOPBACK_MODE))
1127 return;
1128
James Smart76a95d72010-11-20 23:11:48 -05001129 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001130 vport->fc_flag & FC_PUBLIC_LOOP &&
1131 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001132 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001133 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001134 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1135 */
James Smart2e0fef82007-06-17 19:56:36 -05001136 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001137 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001138 }
dea31012005-04-17 16:05:31 -05001139
James Smart2e0fef82007-06-17 19:56:36 -05001140 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001141 * LPFC_FLOGI while waiting for FLOGI cmpl
1142 */
James Smarte6446432012-05-09 21:16:42 -04001143 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001144 lpfc_initial_flogi(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001145 return;
dea31012005-04-17 16:05:31 -05001146
1147out:
James Smarte8b62012007-08-02 11:10:09 -04001148 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1149 "0306 CONFIG_LINK mbxStatus error x%x "
1150 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001151 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001152 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001153
1154 lpfc_linkdown(phba);
1155
James Smarte8b62012007-08-02 11:10:09 -04001156 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1157 "0200 CONFIG_LINK bad hba state x%x\n",
1158 vport->port_state);
dea31012005-04-17 16:05:31 -05001159
James Smart92d7f7b2007-06-17 19:56:38 -05001160 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001161 return;
1162}
1163
James Smart7d791df2011-07-22 18:37:52 -04001164/**
1165 * lpfc_sli4_clear_fcf_rr_bmask
1166 * @phba pointer to the struct lpfc_hba for this port.
1167 * This fucnction resets the round robin bit mask and clears the
1168 * fcf priority list. The list deletions are done while holding the
1169 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1170 * from the lpfc_fcf_pri record.
1171 **/
1172void
1173lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1174{
1175 struct lpfc_fcf_pri *fcf_pri;
1176 struct lpfc_fcf_pri *next_fcf_pri;
1177 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1178 spin_lock_irq(&phba->hbalock);
1179 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1180 &phba->fcf.fcf_pri_list, list) {
1181 list_del_init(&fcf_pri->list);
1182 fcf_pri->fcf_rec.flag = 0;
1183 }
1184 spin_unlock_irq(&phba->hbalock);
1185}
dea31012005-04-17 16:05:31 -05001186static void
James Smart6fb120a2009-05-22 14:52:59 -04001187lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1188{
1189 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001190
1191 if (mboxq->u.mb.mbxStatus) {
1192 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1193 "2017 REG_FCFI mbxStatus error x%x "
1194 "HBA state x%x\n",
1195 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001196 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001197 }
1198
1199 /* Start FCoE discovery by sending a FLOGI. */
1200 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1201 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001202 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001203 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001204 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001205
James Smart32b97932009-07-19 10:01:21 -04001206 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001207 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1208 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001209 goto fail_out;
1210
1211 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001212 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001213 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001214 phba->hba_flag &= ~FCF_TS_INPROG;
1215 if (vport->port_state != LPFC_FLOGI) {
1216 phba->hba_flag |= FCF_RR_INPROG;
1217 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001218 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001219 goto out;
1220 }
1221 spin_unlock_irq(&phba->hbalock);
1222 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001223
James Smarta93ff372010-10-22 11:06:08 -04001224fail_out:
1225 spin_lock_irq(&phba->hbalock);
1226 phba->hba_flag &= ~FCF_RR_INPROG;
1227 spin_unlock_irq(&phba->hbalock);
1228out:
James Smart6fb120a2009-05-22 14:52:59 -04001229 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001230}
1231
1232/**
1233 * lpfc_fab_name_match - Check if the fcf fabric name match.
1234 * @fab_name: pointer to fabric name.
1235 * @new_fcf_record: pointer to fcf record.
1236 *
1237 * This routine compare the fcf record's fabric name with provided
1238 * fabric name. If the fabric name are identical this function
1239 * returns 1 else return 0.
1240 **/
1241static uint32_t
1242lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1243{
James Smartecfd03c2010-02-12 14:41:27 -05001244 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001245 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001246 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1247 return 0;
1248 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1249 return 0;
1250 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1251 return 0;
1252 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1253 return 0;
1254 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1255 return 0;
1256 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1257 return 0;
1258 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1259 return 0;
1260 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001261}
1262
1263/**
James Smart8fa38512009-07-19 10:01:03 -04001264 * lpfc_sw_name_match - Check if the fcf switch name match.
1265 * @fab_name: pointer to fabric name.
1266 * @new_fcf_record: pointer to fcf record.
1267 *
1268 * This routine compare the fcf record's switch name with provided
1269 * switch name. If the switch name are identical this function
1270 * returns 1 else return 0.
1271 **/
1272static uint32_t
1273lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1274{
James Smartecfd03c2010-02-12 14:41:27 -05001275 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001276 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001277 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1278 return 0;
1279 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1280 return 0;
1281 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1282 return 0;
1283 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1284 return 0;
1285 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1286 return 0;
1287 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1288 return 0;
1289 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1290 return 0;
1291 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001292}
1293
1294/**
James Smart6fb120a2009-05-22 14:52:59 -04001295 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001296 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001297 * @new_fcf_record: pointer to fcf record.
1298 *
1299 * This routine compare the fcf record's mac address with HBA's
1300 * FCF mac address. If the mac addresses are identical this function
1301 * returns 1 else return 0.
1302 **/
1303static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001304lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001305{
James Smartecfd03c2010-02-12 14:41:27 -05001306 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001307 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001308 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1309 return 0;
1310 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1311 return 0;
1312 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1313 return 0;
1314 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1315 return 0;
1316 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1317 return 0;
1318 return 1;
1319}
1320
1321static bool
1322lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1323{
1324 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001325}
1326
1327/**
James Smart7d791df2011-07-22 18:37:52 -04001328 * lpfc_update_fcf_record - Update driver fcf record
1329 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1330 * @phba: pointer to lpfc hba data structure.
1331 * @fcf_index: Index for the lpfc_fcf_record.
1332 * @new_fcf_record: pointer to hba fcf record.
1333 *
1334 * This routine updates the driver FCF priority record from the new HBA FCF
1335 * record. This routine is called with the host lock held.
1336 **/
1337static void
1338__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1339 struct fcf_record *new_fcf_record
1340 )
1341{
1342 struct lpfc_fcf_pri *fcf_pri;
1343
1344 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1345 fcf_pri->fcf_rec.fcf_index = fcf_index;
1346 /* FCF record priority */
1347 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1348
1349}
1350
1351/**
James Smart6fb120a2009-05-22 14:52:59 -04001352 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001353 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001354 * @new_fcf_record: pointer to fcf record.
1355 *
1356 * This routine copies the FCF information from the FCF
1357 * record to lpfc_hba data structure.
1358 **/
1359static void
James Smartecfd03c2010-02-12 14:41:27 -05001360lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1361 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001362{
James Smartecfd03c2010-02-12 14:41:27 -05001363 /* Fabric name */
1364 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001365 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001366 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001367 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001368 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001369 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001370 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001371 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001372 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001373 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001374 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001375 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001376 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001377 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001378 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001379 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001380 /* Mac address */
1381 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1382 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1383 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1384 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1385 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1386 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1387 /* FCF record index */
1388 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1389 /* FCF record priority */
1390 fcf_rec->priority = new_fcf_record->fip_priority;
1391 /* Switch name */
1392 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001393 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001394 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001395 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001396 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001397 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001398 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001399 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001400 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001401 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001402 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001403 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001404 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001405 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001406 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001407 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001408}
1409
1410/**
James Smartecfd03c2010-02-12 14:41:27 -05001411 * lpfc_update_fcf_record - Update driver fcf record
1412 * @phba: pointer to lpfc hba data structure.
1413 * @fcf_rec: pointer to driver fcf record.
1414 * @new_fcf_record: pointer to hba fcf record.
1415 * @addr_mode: address mode to be set to the driver fcf record.
1416 * @vlan_id: vlan tag to be set to the driver fcf record.
1417 * @flag: flag bits to be set to the driver fcf record.
1418 *
1419 * This routine updates the driver FCF record from the new HBA FCF record
1420 * together with the address mode, vlan_id, and other informations. This
1421 * routine is called with the host lock held.
1422 **/
1423static void
1424__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1425 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1426 uint16_t vlan_id, uint32_t flag)
1427{
1428 /* Copy the fields from the HBA's FCF record */
1429 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1430 /* Update other fields of driver FCF record */
1431 fcf_rec->addr_mode = addr_mode;
1432 fcf_rec->vlan_id = vlan_id;
1433 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001434 __lpfc_update_fcf_record_pri(phba,
1435 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1436 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001437}
1438
1439/**
James Smart6fb120a2009-05-22 14:52:59 -04001440 * lpfc_register_fcf - Register the FCF with hba.
1441 * @phba: pointer to lpfc hba data structure.
1442 *
1443 * This routine issues a register fcfi mailbox command to register
1444 * the fcf with HBA.
1445 **/
1446static void
1447lpfc_register_fcf(struct lpfc_hba *phba)
1448{
1449 LPFC_MBOXQ_t *fcf_mbxq;
1450 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001451
James Smartd439d282010-09-29 11:18:45 -04001452 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001453 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001454 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001455 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001456 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001457 return;
1458 }
1459
1460 /* The FCF is already registered, start discovery */
1461 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001462 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001463 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001464 if (phba->pport->port_state != LPFC_FLOGI &&
1465 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001466 phba->hba_flag |= FCF_RR_INPROG;
1467 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001468 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001469 return;
1470 }
1471 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001472 return;
1473 }
James Smartd439d282010-09-29 11:18:45 -04001474 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001475
James Smarta93ff372010-10-22 11:06:08 -04001476 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001477 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001478 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001479 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001480 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001481 return;
James Smart4d9ab992009-10-02 15:16:39 -04001482 }
James Smart6fb120a2009-05-22 14:52:59 -04001483
1484 lpfc_reg_fcfi(phba, fcf_mbxq);
1485 fcf_mbxq->vport = phba->pport;
1486 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1487 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001488 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001489 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001490 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001491 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001492 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001493 }
James Smart6fb120a2009-05-22 14:52:59 -04001494
1495 return;
1496}
1497
1498/**
1499 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1500 * @phba: pointer to lpfc hba data structure.
1501 * @new_fcf_record: pointer to fcf record.
1502 * @boot_flag: Indicates if this record used by boot bios.
1503 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001504 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001505 *
1506 * This routine compare the fcf record with connect list obtained from the
1507 * config region to decide if this FCF can be used for SAN discovery. It returns
1508 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1509 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1510 * is used by boot bios and addr_mode will indicate the addressing mode to be
1511 * used for this FCF when the function returns.
1512 * If the FCF record need to be used with a particular vlan id, the vlan is
1513 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001514 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001515 **/
1516static int
1517lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1518 struct fcf_record *new_fcf_record,
1519 uint32_t *boot_flag, uint32_t *addr_mode,
1520 uint16_t *vlan_id)
1521{
1522 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001523 int i, j, fcf_vlan_id = 0;
1524
1525 /* Find the lowest VLAN id in the FCF record */
1526 for (i = 0; i < 512; i++) {
1527 if (new_fcf_record->vlan_bitmap[i]) {
1528 fcf_vlan_id = i * 8;
1529 j = 0;
1530 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1531 j++;
1532 fcf_vlan_id++;
1533 }
1534 break;
1535 }
1536 }
James Smart6fb120a2009-05-22 14:52:59 -04001537
James Smart26979ce2012-09-29 11:31:55 -04001538 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001539 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001540 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1541 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001542 return 0;
1543
James Smart45ed1192009-10-02 15:17:02 -04001544 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001545 *boot_flag = 0;
1546 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1547 new_fcf_record);
1548 if (phba->valid_vlan)
1549 *vlan_id = phba->vlan_id;
1550 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001551 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001552 return 1;
1553 }
1554
1555 /*
1556 * If there are no FCF connection table entry, driver connect to all
1557 * FCFs.
1558 */
1559 if (list_empty(&phba->fcf_conn_rec_list)) {
1560 *boot_flag = 0;
1561 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1562 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001563
1564 /*
1565 * When there are no FCF connect entries, use driver's default
1566 * addressing mode - FPMA.
1567 */
1568 if (*addr_mode & LPFC_FCF_FPMA)
1569 *addr_mode = LPFC_FCF_FPMA;
1570
James Smart4d9ab992009-10-02 15:16:39 -04001571 /* If FCF record report a vlan id use that vlan id */
1572 if (fcf_vlan_id)
1573 *vlan_id = fcf_vlan_id;
1574 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001575 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001576 return 1;
1577 }
1578
James Smartecfd03c2010-02-12 14:41:27 -05001579 list_for_each_entry(conn_entry,
1580 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001581 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1582 continue;
1583
1584 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1585 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001586 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001587 continue;
James Smart8fa38512009-07-19 10:01:03 -04001588 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1589 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1590 new_fcf_record))
1591 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001592 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1593 /*
1594 * If the vlan bit map does not have the bit set for the
1595 * vlan id to be used, then it is not a match.
1596 */
1597 if (!(new_fcf_record->vlan_bitmap
1598 [conn_entry->conn_rec.vlan_tag / 8] &
1599 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1600 continue;
1601 }
1602
1603 /*
James Smart0c287582009-06-10 17:22:56 -04001604 * If connection record does not support any addressing mode,
1605 * skip the FCF record.
1606 */
1607 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1608 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1609 continue;
1610
1611 /*
James Smart6fb120a2009-05-22 14:52:59 -04001612 * Check if the connection record specifies a required
1613 * addressing mode.
1614 */
1615 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1616 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1617
1618 /*
1619 * If SPMA required but FCF not support this continue.
1620 */
1621 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1622 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1623 new_fcf_record) & LPFC_FCF_SPMA))
1624 continue;
1625
1626 /*
1627 * If FPMA required but FCF not support this continue.
1628 */
1629 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1630 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1631 new_fcf_record) & LPFC_FCF_FPMA))
1632 continue;
1633 }
1634
1635 /*
1636 * This fcf record matches filtering criteria.
1637 */
1638 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1639 *boot_flag = 1;
1640 else
1641 *boot_flag = 0;
1642
James Smart0c287582009-06-10 17:22:56 -04001643 /*
1644 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001645 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001646 * by FCF, allow fabric to pick the addressing mode.
1647 */
James Smart6fb120a2009-05-22 14:52:59 -04001648 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1649 new_fcf_record);
1650 /*
1651 * If the user specified a required address mode, assign that
1652 * address mode
1653 */
1654 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1655 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1656 *addr_mode = (conn_entry->conn_rec.flags &
1657 FCFCNCT_AM_SPMA) ?
1658 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1659 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001660 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001661 * addr mode only if FCF support the addr_mode.
1662 */
1663 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1664 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1665 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1666 (*addr_mode & LPFC_FCF_SPMA))
1667 *addr_mode = LPFC_FCF_SPMA;
1668 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1669 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1670 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1671 (*addr_mode & LPFC_FCF_FPMA))
1672 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001673
James Smart4d9ab992009-10-02 15:16:39 -04001674 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001675 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1676 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001677 /*
1678 * If no vlan id is specified in connect list, use the vlan id
1679 * in the FCF record
1680 */
1681 else if (fcf_vlan_id)
1682 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001683 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001684 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001685
1686 return 1;
1687 }
1688
1689 return 0;
1690}
1691
1692/**
James Smart32b97932009-07-19 10:01:21 -04001693 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1694 * @phba: pointer to lpfc hba data structure.
1695 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1696 *
1697 * This function check if there is any fcoe event pending while driver
1698 * scan FCF entries. If there is any pending event, it will restart the
1699 * FCF saning and return 1 else return 0.
1700 */
1701int
1702lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1703{
James Smart32b97932009-07-19 10:01:21 -04001704 /*
1705 * If the Link is up and no FCoE events while in the
1706 * FCF discovery, no need to restart FCF discovery.
1707 */
1708 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001709 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001710 return 0;
1711
James Smart0c9ab6f2010-02-26 14:15:57 -05001712 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1713 "2768 Pending link or FCF event during current "
1714 "handling of the previous event: link_state:x%x, "
1715 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1716 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1717 phba->fcoe_eventtag);
1718
James Smart32b97932009-07-19 10:01:21 -04001719 spin_lock_irq(&phba->hbalock);
1720 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1721 spin_unlock_irq(&phba->hbalock);
1722
James Smart0c9ab6f2010-02-26 14:15:57 -05001723 if (phba->link_state >= LPFC_LINK_UP) {
1724 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1725 "2780 Restart FCF table scan due to "
1726 "pending FCF event:evt_tag_at_scan:x%x, "
1727 "evt_tag_current:x%x\n",
1728 phba->fcoe_eventtag_at_fcf_scan,
1729 phba->fcoe_eventtag);
1730 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1731 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001732 /*
James Smarta93ff372010-10-22 11:06:08 -04001733 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001734 * flag
1735 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001736 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1737 "2833 Stop FCF discovery process due to link "
1738 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001739 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001740 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001741 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001742 spin_unlock_irq(&phba->hbalock);
1743 }
James Smart32b97932009-07-19 10:01:21 -04001744
James Smart0c9ab6f2010-02-26 14:15:57 -05001745 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001746 if (unreg_fcf) {
1747 spin_lock_irq(&phba->hbalock);
1748 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1749 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001750 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001751 }
James Smart32b97932009-07-19 10:01:21 -04001752 return 1;
1753}
1754
1755/**
James Smart999d8132010-03-15 11:24:56 -04001756 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1757 * @phba: pointer to lpfc hba data structure.
1758 * @fcf_cnt: number of eligible fcf record seen so far.
1759 *
1760 * This function makes an running random selection decision on FCF record to
1761 * use through a sequence of @fcf_cnt eligible FCF records with equal
1762 * probability. To perform integer manunipulation of random numbers with
1763 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001764 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001765 *
1766 * Returns true when outcome is for the newly read FCF record should be
1767 * chosen; otherwise, return false when outcome is for keeping the previously
1768 * chosen FCF record.
1769 **/
1770static bool
1771lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1772{
1773 uint32_t rand_num;
1774
1775 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001776 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001777
1778 /* Decision with probability 1/fcf_cnt */
1779 if ((fcf_cnt * rand_num) < 0xFFFF)
1780 return true;
1781 else
1782 return false;
1783}
1784
1785/**
James Smart3804dc82010-07-14 15:31:37 -04001786 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001787 * @phba: pointer to lpfc hba data structure.
1788 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001789 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001790 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001791 * This routine parses the non-embedded fcf mailbox command by performing the
1792 * necessarily error checking, non-embedded read FCF record mailbox command
1793 * SGE parsing, and endianness swapping.
1794 *
1795 * Returns the pointer to the new FCF record in the non-embedded mailbox
1796 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001797 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001798static struct fcf_record *
1799lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1800 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001801{
1802 void *virt_addr;
1803 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001804 struct lpfc_mbx_sge sge;
1805 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1806 uint32_t shdr_status, shdr_add_status;
1807 union lpfc_sli4_cfg_shdr *shdr;
1808 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001809
James Smart6fb120a2009-05-22 14:52:59 -04001810 /* Get the first SGE entry from the non-embedded DMA memory. This
1811 * routine only uses a single SGE.
1812 */
1813 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1814 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1815 if (unlikely(!mboxq->sge_array)) {
1816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1817 "2524 Failed to get the non-embedded SGE "
1818 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001819 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001820 }
1821 virt_addr = mboxq->sge_array->addr[0];
1822
1823 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001824 lpfc_sli_pcimem_bcopy(shdr, shdr,
1825 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001826 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001827 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001828 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001829 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1830 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001831 "2726 READ_FCF_RECORD Indicates empty "
1832 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001833 else
1834 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001835 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001836 "with status x%x add_status x%x, "
1837 "mbx\n", shdr_status, shdr_add_status);
1838 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001839 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001840
1841 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001842 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1843 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1844 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001845 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001846 new_fcf_record = (struct fcf_record *)(virt_addr +
1847 sizeof(struct lpfc_mbx_read_fcf_tbl));
1848 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001849 offsetof(struct fcf_record, vlan_bitmap));
1850 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1851 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001852
James Smart0c9ab6f2010-02-26 14:15:57 -05001853 return new_fcf_record;
1854}
1855
1856/**
1857 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1858 * @phba: pointer to lpfc hba data structure.
1859 * @fcf_record: pointer to the fcf record.
1860 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1861 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1862 *
1863 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1864 * enabled.
1865 **/
1866static void
1867lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1868 struct fcf_record *fcf_record,
1869 uint16_t vlan_id,
1870 uint16_t next_fcf_index)
1871{
1872 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1873 "2764 READ_FCF_RECORD:\n"
1874 "\tFCF_Index : x%x\n"
1875 "\tFCF_Avail : x%x\n"
1876 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001877 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001878 "\tFIP_Priority : x%x\n"
1879 "\tMAC_Provider : x%x\n"
1880 "\tLowest VLANID : x%x\n"
1881 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1882 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1883 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1884 "\tNext_FCF_Index: x%x\n",
1885 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1886 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1887 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001888 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001889 fcf_record->fip_priority,
1890 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1891 vlan_id,
1892 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1893 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1894 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1895 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1896 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1897 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1898 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1899 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1900 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1901 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1902 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1903 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1904 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1905 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1906 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1907 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1908 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1909 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1910 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1911 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1912 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1913 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1914 next_fcf_index);
1915}
1916
1917/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001918 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1919 * @phba: pointer to lpfc hba data structure.
1920 * @fcf_rec: pointer to an existing FCF record.
1921 * @new_fcf_record: pointer to a new FCF record.
1922 * @new_vlan_id: vlan id from the new FCF record.
1923 *
1924 * This function performs matching test of a new FCF record against an existing
1925 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1926 * will not be used as part of the FCF record matching criteria.
1927 *
1928 * Returns true if all the fields matching, otherwise returns false.
1929 */
1930static bool
1931lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1932 struct lpfc_fcf_rec *fcf_rec,
1933 struct fcf_record *new_fcf_record,
1934 uint16_t new_vlan_id)
1935{
1936 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1937 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1938 return false;
1939 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1940 return false;
1941 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1942 return false;
1943 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1944 return false;
James Smart7d791df2011-07-22 18:37:52 -04001945 if (fcf_rec->priority != new_fcf_record->fip_priority)
1946 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001947 return true;
1948}
1949
1950/**
James Smarta93ff372010-10-22 11:06:08 -04001951 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1952 * @vport: Pointer to vport object.
1953 * @fcf_index: index to next fcf.
1954 *
1955 * This function processing the roundrobin fcf failover to next fcf index.
1956 * When this function is invoked, there will be a current fcf registered
1957 * for flogi.
1958 * Return: 0 for continue retrying flogi on currently registered fcf;
1959 * 1 for stop flogi on currently registered fcf;
1960 */
1961int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1962{
1963 struct lpfc_hba *phba = vport->phba;
1964 int rc;
1965
1966 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1967 spin_lock_irq(&phba->hbalock);
1968 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1969 spin_unlock_irq(&phba->hbalock);
1970 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1971 "2872 Devloss tmo with no eligible "
1972 "FCF, unregister in-use FCF (x%x) "
1973 "and rescan FCF table\n",
1974 phba->fcf.current_rec.fcf_indx);
1975 lpfc_unregister_fcf_rescan(phba);
1976 goto stop_flogi_current_fcf;
1977 }
1978 /* Mark the end to FLOGI roundrobin failover */
1979 phba->hba_flag &= ~FCF_RR_INPROG;
1980 /* Allow action to new fcf asynchronous event */
1981 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1982 spin_unlock_irq(&phba->hbalock);
1983 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1984 "2865 No FCF available, stop roundrobin FCF "
1985 "failover and change port state:x%x/x%x\n",
1986 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1987 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1988 goto stop_flogi_current_fcf;
1989 } else {
1990 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1991 "2794 Try FLOGI roundrobin FCF failover to "
1992 "(x%x)\n", fcf_index);
1993 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1994 if (rc)
1995 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1996 "2761 FLOGI roundrobin FCF failover "
1997 "failed (rc:x%x) to read FCF (x%x)\n",
1998 rc, phba->fcf.current_rec.fcf_indx);
1999 else
2000 goto stop_flogi_current_fcf;
2001 }
2002 return 0;
2003
2004stop_flogi_current_fcf:
2005 lpfc_can_disctmo(vport);
2006 return 1;
2007}
2008
2009/**
James Smart7d791df2011-07-22 18:37:52 -04002010 * lpfc_sli4_fcf_pri_list_del
2011 * @phba: pointer to lpfc hba data structure.
2012 * @fcf_index the index of the fcf record to delete
2013 * This routine checks the on list flag of the fcf_index to be deleted.
2014 * If it is one the list then it is removed from the list, and the flag
2015 * is cleared. This routine grab the hbalock before removing the fcf
2016 * record from the list.
2017 **/
2018static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2019 uint16_t fcf_index)
2020{
2021 struct lpfc_fcf_pri *new_fcf_pri;
2022
2023 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2024 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2025 "3058 deleting idx x%x pri x%x flg x%x\n",
2026 fcf_index, new_fcf_pri->fcf_rec.priority,
2027 new_fcf_pri->fcf_rec.flag);
2028 spin_lock_irq(&phba->hbalock);
2029 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2030 if (phba->fcf.current_rec.priority ==
2031 new_fcf_pri->fcf_rec.priority)
2032 phba->fcf.eligible_fcf_cnt--;
2033 list_del_init(&new_fcf_pri->list);
2034 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2035 }
2036 spin_unlock_irq(&phba->hbalock);
2037}
2038
2039/**
2040 * lpfc_sli4_set_fcf_flogi_fail
2041 * @phba: pointer to lpfc hba data structure.
2042 * @fcf_index the index of the fcf record to update
2043 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2044 * flag so the the round robin slection for the particular priority level
2045 * will try a different fcf record that does not have this bit set.
2046 * If the fcf record is re-read for any reason this flag is cleared brfore
2047 * adding it to the priority list.
2048 **/
2049void
2050lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2051{
2052 struct lpfc_fcf_pri *new_fcf_pri;
2053 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2054 spin_lock_irq(&phba->hbalock);
2055 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2056 spin_unlock_irq(&phba->hbalock);
2057}
2058
2059/**
2060 * lpfc_sli4_fcf_pri_list_add
2061 * @phba: pointer to lpfc hba data structure.
2062 * @fcf_index the index of the fcf record to add
2063 * This routine checks the priority of the fcf_index to be added.
2064 * If it is a lower priority than the current head of the fcf_pri list
2065 * then it is added to the list in the right order.
2066 * If it is the same priority as the current head of the list then it
2067 * is added to the head of the list and its bit in the rr_bmask is set.
2068 * If the fcf_index to be added is of a higher priority than the current
2069 * head of the list then the rr_bmask is cleared, its bit is set in the
2070 * rr_bmask and it is added to the head of the list.
2071 * returns:
2072 * 0=success 1=failure
2073 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002074static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2075 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002076 struct fcf_record *new_fcf_record)
2077{
2078 uint16_t current_fcf_pri;
2079 uint16_t last_index;
2080 struct lpfc_fcf_pri *fcf_pri;
2081 struct lpfc_fcf_pri *next_fcf_pri;
2082 struct lpfc_fcf_pri *new_fcf_pri;
2083 int ret;
2084
2085 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2086 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2087 "3059 adding idx x%x pri x%x flg x%x\n",
2088 fcf_index, new_fcf_record->fip_priority,
2089 new_fcf_pri->fcf_rec.flag);
2090 spin_lock_irq(&phba->hbalock);
2091 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2092 list_del_init(&new_fcf_pri->list);
2093 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2094 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2095 if (list_empty(&phba->fcf.fcf_pri_list)) {
2096 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2097 ret = lpfc_sli4_fcf_rr_index_set(phba,
2098 new_fcf_pri->fcf_rec.fcf_index);
2099 goto out;
2100 }
2101
2102 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2103 LPFC_SLI4_FCF_TBL_INDX_MAX);
2104 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2105 ret = 0; /* Empty rr list */
2106 goto out;
2107 }
2108 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2109 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2110 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2111 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2112 memset(phba->fcf.fcf_rr_bmask, 0,
2113 sizeof(*phba->fcf.fcf_rr_bmask));
2114 /* fcfs_at_this_priority_level = 1; */
2115 phba->fcf.eligible_fcf_cnt = 1;
2116 } else
2117 /* fcfs_at_this_priority_level++; */
2118 phba->fcf.eligible_fcf_cnt++;
2119 ret = lpfc_sli4_fcf_rr_index_set(phba,
2120 new_fcf_pri->fcf_rec.fcf_index);
2121 goto out;
2122 }
2123
2124 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2125 &phba->fcf.fcf_pri_list, list) {
2126 if (new_fcf_pri->fcf_rec.priority <=
2127 fcf_pri->fcf_rec.priority) {
2128 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2129 list_add(&new_fcf_pri->list,
2130 &phba->fcf.fcf_pri_list);
2131 else
2132 list_add(&new_fcf_pri->list,
2133 &((struct lpfc_fcf_pri *)
2134 fcf_pri->list.prev)->list);
2135 ret = 0;
2136 goto out;
2137 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2138 || new_fcf_pri->fcf_rec.priority <
2139 next_fcf_pri->fcf_rec.priority) {
2140 list_add(&new_fcf_pri->list, &fcf_pri->list);
2141 ret = 0;
2142 goto out;
2143 }
2144 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2145 continue;
2146
2147 }
2148 ret = 1;
2149out:
2150 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2151 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2152 spin_unlock_irq(&phba->hbalock);
2153 return ret;
2154}
2155
2156/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002157 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2158 * @phba: pointer to lpfc hba data structure.
2159 * @mboxq: pointer to mailbox object.
2160 *
2161 * This function iterates through all the fcf records available in
2162 * HBA and chooses the optimal FCF record for discovery. After finding
2163 * the FCF for discovery it registers the FCF record and kicks start
2164 * discovery.
2165 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2166 * use an FCF record which matches fabric name and mac address of the
2167 * currently used FCF record.
2168 * If the driver supports only one FCF, it will try to use the FCF record
2169 * used by BOOT_BIOS.
2170 */
2171void
2172lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2173{
2174 struct fcf_record *new_fcf_record;
2175 uint32_t boot_flag, addr_mode;
2176 uint16_t fcf_index, next_fcf_index;
2177 struct lpfc_fcf_rec *fcf_rec = NULL;
2178 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002179 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002180 int rc;
2181
2182 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002183 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002184 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2185 return;
2186 }
2187
2188 /* Parse the FCF record from the non-embedded mailbox command */
2189 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2190 &next_fcf_index);
2191 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002192 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002193 "2765 Mailbox command READ_FCF_RECORD "
2194 "failed to retrieve a FCF record.\n");
2195 /* Let next new FCF event trigger fast failover */
2196 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002197 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002198 spin_unlock_irq(&phba->hbalock);
2199 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2200 return;
2201 }
2202
2203 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002204 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2205 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002206
2207 /* Log the FCF record information if turned on */
2208 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2209 next_fcf_index);
2210
James Smart6fb120a2009-05-22 14:52:59 -04002211 /*
2212 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002213 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002214 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002215 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002216 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002217 lpfc_sli4_fcf_pri_list_del(phba,
2218 bf_get(lpfc_fcf_record_fcf_index,
2219 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002220 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002221 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002222 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002223 bf_get(lpfc_fcf_record_fcf_index,
2224 new_fcf_record),
2225 bf_get(lpfc_fcf_record_fcf_avail,
2226 new_fcf_record),
2227 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002228 new_fcf_record),
2229 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002230 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002231 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2232 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2233 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002234 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2235 phba->fcf.current_rec.fcf_indx) {
2236 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2237 "2862 FCF (x%x) matches property "
2238 "of in-use FCF (x%x)\n",
2239 bf_get(lpfc_fcf_record_fcf_index,
2240 new_fcf_record),
2241 phba->fcf.current_rec.fcf_indx);
2242 goto read_next_fcf;
2243 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002244 /*
2245 * In case the current in-use FCF record becomes
2246 * invalid/unavailable during FCF discovery that
2247 * was not triggered by fast FCF failover process,
2248 * treat it as fast FCF failover.
2249 */
2250 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2251 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2252 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2253 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002254 "(x%x), enter FCF failover "
2255 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002256 phba->fcf.current_rec.fcf_indx);
2257 spin_lock_irq(&phba->hbalock);
2258 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2259 spin_unlock_irq(&phba->hbalock);
2260 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2261 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2262 LPFC_FCOE_FCF_GET_FIRST);
2263 return;
2264 }
2265 }
James Smart6fb120a2009-05-22 14:52:59 -04002266 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002267 } else {
2268 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002269 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2270 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002271 if (rc)
2272 goto read_next_fcf;
2273 }
2274
James Smart6fb120a2009-05-22 14:52:59 -04002275 /*
2276 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002277 * FCF record for the discovery. The condition that a rescan
2278 * matches the in-use FCF record: fabric name, switch name, mac
2279 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002280 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002281 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002282 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002283 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2284 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002285 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002286 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2287 phba->fcf.current_rec.fcf_indx) {
2288 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2289 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2290 /* Stop FCF redisc wait timer */
2291 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2292 phba);
2293 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2294 /* Fast failover, mark completed */
2295 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2296 spin_unlock_irq(&phba->hbalock);
2297 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2298 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002299 "FCF (x%x), port_state:x%x, "
2300 "fc_flag:x%x\n",
2301 phba->fcf.current_rec.fcf_indx,
2302 phba->pport->port_state,
2303 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002304 goto out;
2305 } else
2306 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2307 "2863 New FCF (x%x) matches "
2308 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002309 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002310 new_fcf_record),
2311 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002312 }
James Smartecfd03c2010-02-12 14:41:27 -05002313 /*
2314 * Read next FCF record from HBA searching for the matching
2315 * with in-use record only if not during the fast failover
2316 * period. In case of fast failover period, it shall try to
2317 * determine whether the FCF record just read should be the
2318 * next candidate.
2319 */
2320 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002321 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002322 goto read_next_fcf;
2323 }
James Smart6fb120a2009-05-22 14:52:59 -04002324 }
James Smartecfd03c2010-02-12 14:41:27 -05002325 /*
2326 * Update on failover FCF record only if it's in FCF fast-failover
2327 * period; otherwise, update on current FCF record.
2328 */
James Smartfc2b9892010-02-26 14:15:29 -05002329 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2330 fcf_rec = &phba->fcf.failover_rec;
2331 else
James Smartecfd03c2010-02-12 14:41:27 -05002332 fcf_rec = &phba->fcf.current_rec;
2333
James Smart6fb120a2009-05-22 14:52:59 -04002334 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2335 /*
James Smartecfd03c2010-02-12 14:41:27 -05002336 * If the driver FCF record does not have boot flag
2337 * set and new hba fcf record has boot flag set, use
2338 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002339 */
James Smartecfd03c2010-02-12 14:41:27 -05002340 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2341 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002342 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2343 "2837 Update current FCF record "
2344 "(x%x) with new FCF record (x%x)\n",
2345 fcf_rec->fcf_indx,
2346 bf_get(lpfc_fcf_record_fcf_index,
2347 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002348 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2349 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002350 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002351 goto read_next_fcf;
2352 }
2353 /*
James Smartecfd03c2010-02-12 14:41:27 -05002354 * If the driver FCF record has boot flag set and the
2355 * new hba FCF record does not have boot flag, read
2356 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002357 */
James Smartecfd03c2010-02-12 14:41:27 -05002358 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002359 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002360 goto read_next_fcf;
2361 }
2362 /*
James Smartecfd03c2010-02-12 14:41:27 -05002363 * If the new hba FCF record has lower priority value
2364 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002365 */
James Smartfc2b9892010-02-26 14:15:29 -05002366 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002367 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002368 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2369 "2838 Update current FCF record "
2370 "(x%x) with new FCF record (x%x)\n",
2371 fcf_rec->fcf_indx,
2372 bf_get(lpfc_fcf_record_fcf_index,
2373 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002374 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2375 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002376 /* Reset running random FCF selection count */
2377 phba->fcf.eligible_fcf_cnt = 1;
2378 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2379 /* Update running random FCF selection count */
2380 phba->fcf.eligible_fcf_cnt++;
2381 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2382 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002383 if (select_new_fcf) {
2384 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2385 "2839 Update current FCF record "
2386 "(x%x) with new FCF record (x%x)\n",
2387 fcf_rec->fcf_indx,
2388 bf_get(lpfc_fcf_record_fcf_index,
2389 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002390 /* Choose the new FCF by random selection */
2391 __lpfc_update_fcf_record(phba, fcf_rec,
2392 new_fcf_record,
2393 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002394 }
James Smart6fb120a2009-05-22 14:52:59 -04002395 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002396 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002397 goto read_next_fcf;
2398 }
2399 /*
James Smartecfd03c2010-02-12 14:41:27 -05002400 * This is the first suitable FCF record, choose this record for
2401 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002402 */
James Smartecfd03c2010-02-12 14:41:27 -05002403 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002404 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002405 "2840 Update initial FCF candidate "
2406 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002407 bf_get(lpfc_fcf_record_fcf_index,
2408 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002409 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2410 addr_mode, vlan_id, (boot_flag ?
2411 BOOT_ENABLE : 0));
2412 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002413 /* Setup initial running random FCF selection count */
2414 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002415 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002416 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002417 goto read_next_fcf;
2418
2419read_next_fcf:
2420 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002421 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2422 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2423 /*
2424 * Case of FCF fast failover scan
2425 */
2426
2427 /*
2428 * It has not found any suitable FCF record, cancel
2429 * FCF scan inprogress, and do nothing
2430 */
2431 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002432 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002433 "2782 No suitable FCF found: "
2434 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002435 phba->fcoe_eventtag_at_fcf_scan,
2436 bf_get(lpfc_fcf_record_fcf_index,
2437 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002438 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002439 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2440 phba->hba_flag &= ~FCF_TS_INPROG;
2441 spin_unlock_irq(&phba->hbalock);
2442 /* Unregister in-use FCF and rescan */
2443 lpfc_printf_log(phba, KERN_INFO,
2444 LOG_FIP,
2445 "2864 On devloss tmo "
2446 "unreg in-use FCF and "
2447 "rescan FCF table\n");
2448 lpfc_unregister_fcf_rescan(phba);
2449 return;
2450 }
2451 /*
2452 * Let next new FCF event trigger fast failover
2453 */
2454 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002455 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002456 return;
2457 }
2458 /*
2459 * It has found a suitable FCF record that is not
2460 * the same as in-use FCF record, unregister the
2461 * in-use FCF record, replace the in-use FCF record
2462 * with the new FCF record, mark FCF fast failover
2463 * completed, and then start register the new FCF
2464 * record.
2465 */
2466
James Smart0c9ab6f2010-02-26 14:15:57 -05002467 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002468 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002469
2470 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002471 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002472 "2842 Replace in-use FCF (x%x) "
2473 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002474 phba->fcf.current_rec.fcf_indx,
2475 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002476 memcpy(&phba->fcf.current_rec,
2477 &phba->fcf.failover_rec,
2478 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002479 /*
2480 * Mark the fast FCF failover rediscovery completed
2481 * and the start of the first round of the roundrobin
2482 * FCF failover.
2483 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002484 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002485 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002486 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002487 /* Register to the new FCF record */
2488 lpfc_register_fcf(phba);
2489 } else {
2490 /*
2491 * In case of transaction period to fast FCF failover,
2492 * do nothing when search to the end of the FCF table.
2493 */
2494 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2495 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2496 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002497
James Smart7d791df2011-07-22 18:37:52 -04002498 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2499 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002500 /*
2501 * In case the current in-use FCF record no
2502 * longer existed during FCF discovery that
2503 * was not triggered by fast FCF failover
2504 * process, treat it as fast FCF failover.
2505 */
2506 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2507 "2841 In-use FCF record (x%x) "
2508 "not reported, entering fast "
2509 "FCF failover mode scanning.\n",
2510 phba->fcf.current_rec.fcf_indx);
2511 spin_lock_irq(&phba->hbalock);
2512 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2513 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002514 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2515 LPFC_FCOE_FCF_GET_FIRST);
2516 return;
2517 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002518 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002519 lpfc_register_fcf(phba);
2520 }
2521 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002522 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002523 return;
2524
2525out:
2526 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2527 lpfc_register_fcf(phba);
2528
2529 return;
2530}
2531
2532/**
James Smarta93ff372010-10-22 11:06:08 -04002533 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002534 * @phba: pointer to lpfc hba data structure.
2535 * @mboxq: pointer to mailbox object.
2536 *
James Smarta93ff372010-10-22 11:06:08 -04002537 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002538 * read FCF record mailbox command from the eligible FCF record bmask for
2539 * performing the failover. If the FCF read back is not valid/available, it
2540 * fails through to retrying FLOGI to the currently registered FCF again.
2541 * Otherwise, if the FCF read back is valid and available, it will set the
2542 * newly read FCF record to the failover FCF record, unregister currently
2543 * registered FCF record, copy the failover FCF record to the current
2544 * FCF record, and then register the current FCF record before proceeding
2545 * to trying FLOGI on the new failover FCF.
2546 */
2547void
2548lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2549{
2550 struct fcf_record *new_fcf_record;
2551 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002552 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002553 uint16_t current_fcf_index;
2554 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002555 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002556
James Smarta93ff372010-10-22 11:06:08 -04002557 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002558 if (phba->link_state < LPFC_LINK_UP) {
2559 spin_lock_irq(&phba->hbalock);
2560 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002561 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002562 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002563 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002564 }
2565
2566 /* Parse the FCF record from the non-embedded mailbox command */
2567 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2568 &next_fcf_index);
2569 if (!new_fcf_record) {
2570 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2571 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002572 "failed to retrieve a FCF record. "
2573 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2574 phba->fcf.fcf_flag);
2575 lpfc_unregister_fcf_rescan(phba);
2576 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002577 }
2578
2579 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002580 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2581 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002582
2583 /* Log the FCF record information if turned on */
2584 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2585 next_fcf_index);
2586
James Smarta93ff372010-10-22 11:06:08 -04002587 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2588 if (!rc) {
2589 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2590 "2848 Remove ineligible FCF (x%x) from "
2591 "from roundrobin bmask\n", fcf_index);
2592 /* Clear roundrobin bmask bit for ineligible FCF */
2593 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2594 /* Perform next round of roundrobin FCF failover */
2595 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2596 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2597 if (rc)
2598 goto out;
2599 goto error_out;
2600 }
2601
2602 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2603 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2604 "2760 Perform FLOGI roundrobin FCF failover: "
2605 "FCF (x%x) back to FCF (x%x)\n",
2606 phba->fcf.current_rec.fcf_indx, fcf_index);
2607 /* Wait 500 ms before retrying FLOGI to current FCF */
2608 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002609 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002610 goto out;
2611 }
2612
James Smart0c9ab6f2010-02-26 14:15:57 -05002613 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002614 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002615 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2616 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002617 spin_lock_irq(&phba->hbalock);
2618 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2619 new_fcf_record, addr_mode, vlan_id,
2620 (boot_flag ? BOOT_ENABLE : 0));
2621 spin_unlock_irq(&phba->hbalock);
2622
2623 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2624
2625 /* Unregister the current in-use FCF record */
2626 lpfc_unregister_fcf(phba);
2627
2628 /* Replace in-use record with the new record */
2629 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2630 sizeof(struct lpfc_fcf_rec));
2631
2632 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002633 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2634 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002635
James Smarta93ff372010-10-22 11:06:08 -04002636error_out:
2637 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002638out:
2639 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002640}
2641
2642/**
2643 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2644 * @phba: pointer to lpfc hba data structure.
2645 * @mboxq: pointer to mailbox object.
2646 *
2647 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002648 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002649 * failover when a new FCF event happened. If the FCF read back is
2650 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002651 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002652 */
2653void
2654lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2655{
2656 struct fcf_record *new_fcf_record;
2657 uint32_t boot_flag, addr_mode;
2658 uint16_t fcf_index, next_fcf_index;
2659 uint16_t vlan_id;
2660 int rc;
2661
2662 /* If link state is not up, no need to proceed */
2663 if (phba->link_state < LPFC_LINK_UP)
2664 goto out;
2665
2666 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002667 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002668 goto out;
2669
2670 /* Parse the FCF record from the non-embedded mailbox command */
2671 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2672 &next_fcf_index);
2673 if (!new_fcf_record) {
2674 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2675 "2767 Mailbox command READ_FCF_RECORD "
2676 "failed to retrieve a FCF record.\n");
2677 goto out;
2678 }
2679
2680 /* Check the connection list for eligibility */
2681 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2682 &addr_mode, &vlan_id);
2683
2684 /* Log the FCF record information if turned on */
2685 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2686 next_fcf_index);
2687
2688 if (!rc)
2689 goto out;
2690
2691 /* Update the eligible FCF record index bmask */
2692 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002693
2694 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002695
2696out:
2697 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2698}
2699
2700/**
James Smart76a95d72010-11-20 23:11:48 -05002701 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2702 * @phba: pointer to lpfc hba data structure.
2703 * @mboxq: pointer to mailbox data structure.
2704 *
2705 * This function handles completion of init vfi mailbox command.
2706 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002707static void
James Smart76a95d72010-11-20 23:11:48 -05002708lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2709{
2710 struct lpfc_vport *vport = mboxq->vport;
2711
James Smartdf9e1b52011-12-13 13:22:17 -05002712 /*
2713 * VFI not supported on interface type 0, just do the flogi
2714 * Also continue if the VFI is in use - just use the same one.
2715 */
2716 if (mboxq->u.mb.mbxStatus &&
2717 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2718 LPFC_SLI_INTF_IF_TYPE_0) &&
2719 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002720 lpfc_printf_vlog(vport, KERN_ERR,
2721 LOG_MBOX,
2722 "2891 Init VFI mailbox failed 0x%x\n",
2723 mboxq->u.mb.mbxStatus);
2724 mempool_free(mboxq, phba->mbox_mem_pool);
2725 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2726 return;
2727 }
James Smart73d91e52011-10-10 21:32:10 -04002728
James Smart76a95d72010-11-20 23:11:48 -05002729 lpfc_initial_flogi(vport);
2730 mempool_free(mboxq, phba->mbox_mem_pool);
2731 return;
2732}
2733
2734/**
2735 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2736 * @vport: pointer to lpfc_vport data structure.
2737 *
2738 * This function issue a init_vfi mailbox command to initialize the VFI and
2739 * VPI for the physical port.
2740 */
2741void
2742lpfc_issue_init_vfi(struct lpfc_vport *vport)
2743{
2744 LPFC_MBOXQ_t *mboxq;
2745 int rc;
2746 struct lpfc_hba *phba = vport->phba;
2747
2748 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2749 if (!mboxq) {
2750 lpfc_printf_vlog(vport, KERN_ERR,
2751 LOG_MBOX, "2892 Failed to allocate "
2752 "init_vfi mailbox\n");
2753 return;
2754 }
2755 lpfc_init_vfi(mboxq, vport);
2756 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2757 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2758 if (rc == MBX_NOT_FINISHED) {
2759 lpfc_printf_vlog(vport, KERN_ERR,
2760 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2761 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2762 }
2763}
2764
2765/**
James Smart1c6834a2009-07-19 10:01:26 -04002766 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2767 * @phba: pointer to lpfc hba data structure.
2768 * @mboxq: pointer to mailbox data structure.
2769 *
2770 * This function handles completion of init vpi mailbox command.
2771 */
James Smart695a8142010-01-26 23:08:03 -05002772void
James Smart1c6834a2009-07-19 10:01:26 -04002773lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2774{
2775 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002776 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002777 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2778
James Smart1c6834a2009-07-19 10:01:26 -04002779 if (mboxq->u.mb.mbxStatus) {
2780 lpfc_printf_vlog(vport, KERN_ERR,
2781 LOG_MBOX,
2782 "2609 Init VPI mailbox failed 0x%x\n",
2783 mboxq->u.mb.mbxStatus);
2784 mempool_free(mboxq, phba->mbox_mem_pool);
2785 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2786 return;
2787 }
James Smart72100cc2010-02-12 14:43:01 -05002788 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002789 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002790 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002791
James Smart695a8142010-01-26 23:08:03 -05002792 /* If this port is physical port or FDISC is done, do reg_vpi */
2793 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2794 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2795 if (!ndlp)
2796 lpfc_printf_vlog(vport, KERN_ERR,
2797 LOG_DISCOVERY,
2798 "2731 Cannot find fabric "
2799 "controller node\n");
2800 else
2801 lpfc_register_new_vport(phba, vport, ndlp);
2802 mempool_free(mboxq, phba->mbox_mem_pool);
2803 return;
2804 }
2805
James Smart1c6834a2009-07-19 10:01:26 -04002806 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2807 lpfc_initial_fdisc(vport);
2808 else {
2809 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002810 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2811 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002812 }
James Smart695a8142010-01-26 23:08:03 -05002813 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002814 return;
2815}
2816
2817/**
James Smartecfd03c2010-02-12 14:41:27 -05002818 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2819 * @vport: pointer to lpfc_vport data structure.
2820 *
2821 * This function issue a init_vpi mailbox command to initialize
2822 * VPI for the vport.
2823 */
2824void
2825lpfc_issue_init_vpi(struct lpfc_vport *vport)
2826{
2827 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002828 int rc, vpi;
2829
2830 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2831 vpi = lpfc_alloc_vpi(vport->phba);
2832 if (!vpi) {
2833 lpfc_printf_vlog(vport, KERN_ERR,
2834 LOG_MBOX,
2835 "3303 Failed to obtain vport vpi\n");
2836 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2837 return;
2838 }
2839 vport->vpi = vpi;
2840 }
James Smartecfd03c2010-02-12 14:41:27 -05002841
2842 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2843 if (!mboxq) {
2844 lpfc_printf_vlog(vport, KERN_ERR,
2845 LOG_MBOX, "2607 Failed to allocate "
2846 "init_vpi mailbox\n");
2847 return;
2848 }
2849 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2850 mboxq->vport = vport;
2851 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2852 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2853 if (rc == MBX_NOT_FINISHED) {
2854 lpfc_printf_vlog(vport, KERN_ERR,
2855 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2856 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2857 }
2858}
2859
2860/**
James Smart6fb120a2009-05-22 14:52:59 -04002861 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2862 * @phba: pointer to lpfc hba data structure.
2863 *
2864 * This function loops through the list of vports on the @phba and issues an
2865 * FDISC if possible.
2866 */
2867void
2868lpfc_start_fdiscs(struct lpfc_hba *phba)
2869{
2870 struct lpfc_vport **vports;
2871 int i;
2872
2873 vports = lpfc_create_vport_work_array(phba);
2874 if (vports != NULL) {
2875 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2876 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2877 continue;
2878 /* There are no vpi for this vport */
2879 if (vports[i]->vpi > phba->max_vpi) {
2880 lpfc_vport_set_state(vports[i],
2881 FC_VPORT_FAILED);
2882 continue;
2883 }
James Smart76a95d72010-11-20 23:11:48 -05002884 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002885 lpfc_vport_set_state(vports[i],
2886 FC_VPORT_LINKDOWN);
2887 continue;
2888 }
James Smart1c6834a2009-07-19 10:01:26 -04002889 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002890 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002891 continue;
2892 }
James Smart6fb120a2009-05-22 14:52:59 -04002893 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2894 lpfc_initial_fdisc(vports[i]);
2895 else {
2896 lpfc_vport_set_state(vports[i],
2897 FC_VPORT_NO_FABRIC_SUPP);
2898 lpfc_printf_vlog(vports[i], KERN_ERR,
2899 LOG_ELS,
2900 "0259 No NPIV "
2901 "Fabric support\n");
2902 }
2903 }
2904 }
2905 lpfc_destroy_vport_work_array(phba, vports);
2906}
2907
2908void
2909lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2910{
2911 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2912 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002913 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002914
James Smartf5eca9b2012-03-01 22:37:54 -05002915 /*
2916 * VFI not supported for interface type 0, so ignore any mailbox
2917 * error (except VFI in use) and continue with the discovery.
2918 */
2919 if (mboxq->u.mb.mbxStatus &&
2920 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2921 LPFC_SLI_INTF_IF_TYPE_0) &&
2922 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002923 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2924 "2018 REG_VFI mbxStatus error x%x "
2925 "HBA state x%x\n",
2926 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002927 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002928 /* FLOGI failed, use loop map to make discovery list */
2929 lpfc_disc_list_loopmap(vport);
2930 /* Start discovery */
2931 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002932 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002933 }
2934 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002935 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002936 }
James Smartae05ebe2013-03-01 16:35:38 -05002937
James Smarte74c03c2013-04-17 20:15:19 -04002938 /* If the VFI is already registered, there is nothing else to do
2939 * Unless this was a VFI update and we are in PT2PT mode, then
2940 * we should drop through to set the port state to ready.
2941 */
James Smartae05ebe2013-03-01 16:35:38 -05002942 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002943 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2944 vport->fc_flag & FC_PT2PT))
2945 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002946
James Smartc8685952009-11-18 15:39:16 -05002947 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002948 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002949 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002950 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002951 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002952 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002953 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002954
James Smart1b511972011-12-13 13:23:09 -05002955 /* In case SLI4 FC loopback test, we are ready */
2956 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2957 (phba->link_flag & LS_LOOPBACK_MODE)) {
2958 phba->link_state = LPFC_HBA_READY;
2959 goto out_free_mem;
2960 }
2961
James Smarte74c03c2013-04-17 20:15:19 -04002962 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2963 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2964 "alpacnt:%d LinkState:%x topology:%x\n",
2965 vport->port_state, vport->fc_flag, vport->fc_myDID,
2966 vport->phba->alpa_map[0],
2967 phba->link_state, phba->fc_topology);
2968
James Smart6fb120a2009-05-22 14:52:59 -04002969 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002970 /*
2971 * For private loop or for NPort pt2pt,
2972 * just start discovery and we are done.
2973 */
2974 if ((vport->fc_flag & FC_PT2PT) ||
2975 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2976 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2977
James Smart76a95d72010-11-20 23:11:48 -05002978 /* Use loop map to make discovery list */
2979 lpfc_disc_list_loopmap(vport);
2980 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002981 if (vport->fc_flag & FC_PT2PT)
2982 vport->port_state = LPFC_VPORT_READY;
2983 else
2984 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002985 } else {
2986 lpfc_start_fdiscs(phba);
2987 lpfc_do_scr_ns_plogi(phba, vport);
2988 }
James Smart6fb120a2009-05-22 14:52:59 -04002989 }
2990
James Smart1b511972011-12-13 13:23:09 -05002991out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002992 mempool_free(mboxq, phba->mbox_mem_pool);
2993 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2994 kfree(dmabuf);
2995 return;
2996}
2997
2998static void
James Smart2e0fef82007-06-17 19:56:36 -05002999lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003000{
James Smart6fb120a2009-05-22 14:52:59 -04003001 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003002 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003003 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003004
3005
3006 /* Check for error */
3007 if (mb->mbxStatus) {
3008 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003009 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3010 "0319 READ_SPARAM mbxStatus error x%x "
3011 "hba state x%x>\n",
3012 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003013 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003014 goto out;
3015 }
3016
James Smart2e0fef82007-06-17 19:56:36 -05003017 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003018 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04003019 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003020 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3021 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3022 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3023 }
3024
dea31012005-04-17 16:05:31 -05003025 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3026 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003027 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003028 return;
3029
3030out:
3031 pmb->context1 = NULL;
3032 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3033 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003034 lpfc_issue_clear_la(phba, vport);
3035 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003036 return;
3037}
3038
3039static void
James Smart76a95d72010-11-20 23:11:48 -05003040lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003041{
James Smart92d7f7b2007-06-17 19:56:38 -05003042 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003043 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003044 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003045 int i;
James Smart14691152006-12-02 13:34:28 -05003046 struct lpfc_dmabuf *mp;
3047 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003048 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003049 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003050
James Smart92d7f7b2007-06-17 19:56:38 -05003051 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003052 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3053 case LPFC_LINK_SPEED_1GHZ:
3054 case LPFC_LINK_SPEED_2GHZ:
3055 case LPFC_LINK_SPEED_4GHZ:
3056 case LPFC_LINK_SPEED_8GHZ:
3057 case LPFC_LINK_SPEED_10GHZ:
3058 case LPFC_LINK_SPEED_16GHZ:
3059 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04003060 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003061 default:
James Smart76a95d72010-11-20 23:11:48 -05003062 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05003063 break;
dea31012005-04-17 16:05:31 -05003064 }
3065
James Smarte74c03c2013-04-17 20:15:19 -04003066 if (phba->fc_topology &&
3067 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3068 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3069 "3314 Toplogy changed was 0x%x is 0x%x\n",
3070 phba->fc_topology,
3071 bf_get(lpfc_mbx_read_top_topology, la));
3072 phba->fc_topology_changed = 1;
3073 }
3074
James Smart76a95d72010-11-20 23:11:48 -05003075 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003076 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003077
James Smartdf9e1b52011-12-13 13:22:17 -05003078 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003079 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003080 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003081
James Smart6a9c52c2009-10-02 15:16:51 -04003082 /* if npiv is enabled and this adapter supports npiv log
3083 * a message that npiv is not supported in this topology
3084 */
3085 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003086 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3087 "1309 Link Up Event npiv not supported in loop "
3088 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003089 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003090 if (bf_get(lpfc_mbx_read_top_il, la))
3091 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003092
James Smart76a95d72010-11-20 23:11:48 -05003093 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3094 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003095
3096 if (i == 0) {
3097 phba->alpa_map[0] = 0;
3098 } else {
James Smarte8b62012007-08-02 11:10:09 -04003099 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003100 int numalpa, j, k;
3101 union {
3102 uint8_t pamap[16];
3103 struct {
3104 uint32_t wd1;
3105 uint32_t wd2;
3106 uint32_t wd3;
3107 uint32_t wd4;
3108 } pa;
3109 } un;
3110 numalpa = phba->alpa_map[0];
3111 j = 0;
3112 while (j < numalpa) {
3113 memset(un.pamap, 0, 16);
3114 for (k = 1; j < numalpa; k++) {
3115 un.pamap[k - 1] =
3116 phba->alpa_map[j + 1];
3117 j++;
3118 if (k == 16)
3119 break;
3120 }
3121 /* Link Up Event ALPA map */
3122 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003123 KERN_WARNING,
3124 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003125 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003126 "ALPA map Data: x%x "
3127 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003128 un.pa.wd1, un.pa.wd2,
3129 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003130 }
3131 }
3132 }
3133 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003134 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003135 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003136 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003137 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3138 }
James Smart2e0fef82007-06-17 19:56:36 -05003139 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003140 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003141 }
James Smart92d7f7b2007-06-17 19:56:38 -05003142 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003143
James Smart38c20672013-03-01 16:37:44 -05003144 if (fc_flags) {
3145 spin_lock_irq(shost->host_lock);
3146 vport->fc_flag |= fc_flags;
3147 spin_unlock_irq(shost->host_lock);
3148 }
3149
dea31012005-04-17 16:05:31 -05003150 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003151 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3152 if (!sparam_mbox)
3153 goto out;
3154
3155 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3156 if (rc) {
3157 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3158 goto out;
3159 }
3160 sparam_mbox->vport = vport;
3161 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3162 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3163 if (rc == MBX_NOT_FINISHED) {
3164 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3165 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3166 kfree(mp);
3167 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3168 goto out;
dea31012005-04-17 16:05:31 -05003169 }
3170
James Smart76a95d72010-11-20 23:11:48 -05003171 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003172 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3173 if (!cfglink_mbox)
3174 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003175 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003176 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003177 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003178 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003179 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003180 if (rc == MBX_NOT_FINISHED) {
3181 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3182 goto out;
3183 }
3184 } else {
James Smart32b97932009-07-19 10:01:21 -04003185 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003186 /*
3187 * Add the driver's default FCF record at FCF index 0 now. This
3188 * is phase 1 implementation that support FCF index 0 and driver
3189 * defaults.
3190 */
James Smart45ed1192009-10-02 15:17:02 -04003191 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003192 fcf_record = kzalloc(sizeof(struct fcf_record),
3193 GFP_KERNEL);
3194 if (unlikely(!fcf_record)) {
3195 lpfc_printf_log(phba, KERN_ERR,
3196 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003197 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003198 "fcf record\n");
3199 rc = -ENODEV;
3200 goto out;
3201 }
3202
3203 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3204 LPFC_FCOE_FCF_DEF_INDEX);
3205 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3206 if (unlikely(rc)) {
3207 lpfc_printf_log(phba, KERN_ERR,
3208 LOG_MBOX | LOG_SLI,
3209 "2013 Could not manually add FCF "
3210 "record 0, status %d\n", rc);
3211 rc = -ENODEV;
3212 kfree(fcf_record);
3213 goto out;
3214 }
3215 kfree(fcf_record);
3216 }
3217 /*
3218 * The driver is expected to do FIP/FCF. Call the port
3219 * and get the FCF Table.
3220 */
James Smart32b97932009-07-19 10:01:21 -04003221 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003222 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003223 spin_unlock_irq(&phba->hbalock);
3224 return;
3225 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003226 /* This is the initial FCF discovery scan */
3227 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003228 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003229 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3230 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003231 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3232 LPFC_FCOE_FCF_GET_FIRST);
3233 if (rc) {
3234 spin_lock_irq(&phba->hbalock);
3235 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3236 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003237 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003238 }
James Smart38b92ef2010-08-04 16:11:39 -04003239 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003240 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003241 }
James Smart6fb120a2009-05-22 14:52:59 -04003242
3243 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003244out:
3245 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003246 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3247 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3248 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003249 lpfc_issue_clear_la(phba, vport);
3250 return;
dea31012005-04-17 16:05:31 -05003251}
3252
3253static void
James Smart84774a42008-08-24 21:50:06 -04003254lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003255{
dea31012005-04-17 16:05:31 -05003256 uint32_t control;
3257 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003258 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003259 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003260 if (phba->sli_rev <= LPFC_SLI_REV3) {
3261 control = readl(phba->HCregaddr);
3262 control |= HC_LAINT_ENA;
3263 writel(control, phba->HCregaddr);
3264 readl(phba->HCregaddr); /* flush */
3265 }
James Smart2e0fef82007-06-17 19:56:36 -05003266 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003267}
3268
James Smart84774a42008-08-24 21:50:06 -04003269static void
3270lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3271{
3272 lpfc_linkdown(phba);
3273 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003274 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003275 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3276}
3277
3278
dea31012005-04-17 16:05:31 -05003279/*
James Smart76a95d72010-11-20 23:11:48 -05003280 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003281 * command upon completion. It is setup in the LPFC_MBOXQ
3282 * as the completion routine when the command is
3283 * handed off to the SLI layer.
3284 */
3285void
James Smart76a95d72010-11-20 23:11:48 -05003286lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003287{
James Smart2e0fef82007-06-17 19:56:36 -05003288 struct lpfc_vport *vport = pmb->vport;
3289 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003290 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003291 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003292 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3293
James Smart0d2b6b82008-06-14 22:52:47 -04003294 /* Unblock ELS traffic */
3295 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003296 /* Check for error */
3297 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003298 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003299 "1307 READ_LA mbox error x%x state x%x\n",
3300 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003301 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003302 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003303 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003304 }
3305
James Smart76a95d72010-11-20 23:11:48 -05003306 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003307
3308 memcpy(&phba->alpa_map[0], mp->virt, 128);
3309
James Smart2e0fef82007-06-17 19:56:36 -05003310 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003311 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003312 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003313 else
James Smart2e0fef82007-06-17 19:56:36 -05003314 vport->fc_flag &= ~FC_BYPASSED_MODE;
3315 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003316
James Smartbe0c0082013-03-01 16:38:14 -05003317 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003318 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003319 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003320 if (phba->fc_eventTag != 0)
3321 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003322 }
dea31012005-04-17 16:05:31 -05003323
3324 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003325 if (phba->sli_rev < LPFC_SLI_REV4) {
3326 spin_lock_irq(&phba->hbalock);
3327 if (bf_get(lpfc_mbx_read_top_mm, la))
3328 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3329 else
3330 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3331 spin_unlock_irq(&phba->hbalock);
3332 }
dea31012005-04-17 16:05:31 -05003333
James Smart4d9ab992009-10-02 15:16:39 -04003334 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003335 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003336 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003337 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003338 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003339 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003340 "1306 Link Up Event in loop back mode "
3341 "x%x received Data: x%x x%x x%x x%x\n",
3342 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003343 bf_get(lpfc_mbx_read_top_alpa_granted,
3344 la),
3345 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003346 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003347 } else {
3348 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003349 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003350 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003351 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003352 bf_get(lpfc_mbx_read_top_alpa_granted,
3353 la),
3354 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003355 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003356 bf_get(lpfc_mbx_read_top_mm, la),
3357 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003358 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003359 }
James Smart92d7f7b2007-06-17 19:56:38 -05003360 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003361 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3362 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003363 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003364 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003365 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3366 "1308 Link Down Event in loop back mode "
3367 "x%x received "
3368 "Data: x%x x%x x%x\n",
3369 la->eventTag, phba->fc_eventTag,
3370 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003371 else
James Smart3163f722008-02-08 18:50:25 -05003372 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003373 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003374 "Data: x%x x%x x%x x%x x%x\n",
3375 la->eventTag, phba->fc_eventTag,
3376 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003377 bf_get(lpfc_mbx_read_top_mm, la),
3378 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003379 lpfc_mbx_issue_link_down(phba);
3380 }
James Smartbe0c0082013-03-01 16:38:14 -05003381 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3382 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003383 if (phba->link_state != LPFC_LINK_DOWN) {
3384 phba->fc_stat.LinkDown++;
3385 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3386 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003387 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003388 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003389 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003390 lpfc_mbx_issue_link_down(phba);
3391 } else
3392 lpfc_enable_la(phba);
3393
3394 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3395 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3396 "Data: x%x x%x x%x\n",
3397 la->eventTag, phba->fc_eventTag,
3398 phba->pport->port_state, vport->fc_flag);
3399 /*
3400 * The cmnd that triggered this will be waiting for this
3401 * signal.
3402 */
3403 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3404 if (phba->wait_4_mlo_maint_flg) {
3405 phba->wait_4_mlo_maint_flg = 0;
3406 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003407 }
James Smart84774a42008-08-24 21:50:06 -04003408 }
3409
James Smartbe0c0082013-03-01 16:38:14 -05003410 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3411 bf_get(lpfc_mbx_read_top_fa, la)) {
3412 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003413 lpfc_issue_clear_la(phba, vport);
3414 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003415 "1311 fa %d\n",
3416 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003417 }
3418
James Smart76a95d72010-11-20 23:11:48 -05003419lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003420 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3421 kfree(mp);
3422 mempool_free(pmb, phba->mbox_mem_pool);
3423 return;
3424}
3425
3426/*
3427 * This routine handles processing a REG_LOGIN mailbox
3428 * command upon completion. It is setup in the LPFC_MBOXQ
3429 * as the completion routine when the command is
3430 * handed off to the SLI layer.
3431 */
3432void
James Smart2e0fef82007-06-17 19:56:36 -05003433lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003434{
James Smart2e0fef82007-06-17 19:56:36 -05003435 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003436 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003437 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003438 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003439
dea31012005-04-17 16:05:31 -05003440 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003441 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003442
James Smartbe6bb942015-04-07 15:07:22 -04003443 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3444 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3445 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3446 atomic_read(&ndlp->kref.refcount),
3447 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003448 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3449 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3450
James Smart40426292010-12-15 17:58:10 -05003451 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3452 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003453 /* We rcvd a rscn after issuing this
3454 * mbox reg login, we may have cycled
3455 * back through the state and be
3456 * back at reg login state so this
3457 * mbox needs to be ignored becase
3458 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003459 * process.
James Smartffc95492010-06-07 15:23:17 -04003460 */
3461 spin_lock_irq(shost->host_lock);
3462 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3463 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003464 } else
3465 /* Good status, call state machine */
3466 lpfc_disc_state_machine(vport, ndlp, pmb,
3467 NLP_EVT_CMPL_REG_LOGIN);
3468
dea31012005-04-17 16:05:31 -05003469 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3470 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003471 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003472 /* decrement the node reference count held for this callback
3473 * function.
3474 */
James Smart329f9bc2007-04-25 09:53:01 -04003475 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003476
3477 return;
3478}
3479
James Smart92d7f7b2007-06-17 19:56:38 -05003480static void
3481lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3482{
James Smart04c68492009-05-22 14:52:52 -04003483 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003484 struct lpfc_vport *vport = pmb->vport;
3485 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3486
3487 switch (mb->mbxStatus) {
3488 case 0x0011:
3489 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003490 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3491 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3492 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003493 break;
James Smart78730cf2010-04-06 15:06:30 -04003494 /* If VPI is busy, reset the HBA */
3495 case 0x9700:
3496 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3497 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3498 vport->vpi, mb->mbxStatus);
3499 if (!(phba->pport->load_flag & FC_UNLOADING))
3500 lpfc_workq_post_event(phba, NULL, NULL,
3501 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003502 }
James Smart72100cc2010-02-12 14:43:01 -05003503 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003504 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003505 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003506 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003507 vport->unreg_vpi_cmpl = VPORT_OK;
3508 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003509 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003510 /*
3511 * This shost reference might have been taken at the beginning of
3512 * lpfc_vport_delete()
3513 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003514 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003515 scsi_host_put(shost);
3516}
3517
James Smartd7c255b2008-08-24 21:50:00 -04003518int
James Smart92d7f7b2007-06-17 19:56:38 -05003519lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3520{
3521 struct lpfc_hba *phba = vport->phba;
3522 LPFC_MBOXQ_t *mbox;
3523 int rc;
3524
3525 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3526 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003527 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003528
3529 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3530 mbox->vport = vport;
3531 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003532 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003533 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003534 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3535 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003536 mempool_free(mbox, phba->mbox_mem_pool);
3537 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003538 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003539 }
James Smartd7c255b2008-08-24 21:50:00 -04003540 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003541}
3542
3543static void
3544lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3545{
3546 struct lpfc_vport *vport = pmb->vport;
3547 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003548 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003549
3550 switch (mb->mbxStatus) {
3551 case 0x0011:
3552 case 0x9601:
3553 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003554 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3555 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3556 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003557 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3558 spin_lock_irq(shost->host_lock);
3559 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3560 spin_unlock_irq(shost->host_lock);
3561 vport->fc_myDID = 0;
3562 goto out;
3563 }
James Smart92d7f7b2007-06-17 19:56:38 -05003564
James Smart72100cc2010-02-12 14:43:01 -05003565 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003566 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003567 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003568 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003569 vport->num_disc_nodes = 0;
3570 /* go thru NPR list and issue ELS PLOGIs */
3571 if (vport->fc_npr_cnt)
3572 lpfc_els_disc_plogi(vport);
3573
3574 if (!vport->num_disc_nodes) {
3575 spin_lock_irq(shost->host_lock);
3576 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3577 spin_unlock_irq(shost->host_lock);
3578 lpfc_can_disctmo(vport);
3579 }
3580 vport->port_state = LPFC_VPORT_READY;
3581
3582out:
3583 mempool_free(pmb, phba->mbox_mem_pool);
3584 return;
3585}
3586
James Smart21e9a0a2009-05-22 14:53:21 -04003587/**
3588 * lpfc_create_static_vport - Read HBA config region to create static vports.
3589 * @phba: pointer to lpfc hba data structure.
3590 *
3591 * This routine issue a DUMP mailbox command for config region 22 to get
3592 * the list of static vports to be created. The function create vports
3593 * based on the information returned from the HBA.
3594 **/
3595void
3596lpfc_create_static_vport(struct lpfc_hba *phba)
3597{
3598 LPFC_MBOXQ_t *pmb = NULL;
3599 MAILBOX_t *mb;
3600 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003601 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003602 struct fc_vport_identifiers vport_id;
3603 struct fc_vport *new_fc_vport;
3604 struct Scsi_Host *shost;
3605 struct lpfc_vport *vport;
3606 uint16_t offset = 0;
3607 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003608 struct lpfc_dmabuf *mp;
3609 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003610
3611 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3612 if (!pmb) {
3613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3614 "0542 lpfc_create_static_vport failed to"
3615 " allocate mailbox memory\n");
3616 return;
3617 }
James Smartcdcc2342012-08-14 14:26:35 -04003618 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003619 mb = &pmb->u.mb;
3620
3621 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3622 if (!vport_info) {
3623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3624 "0543 lpfc_create_static_vport failed to"
3625 " allocate vport_info\n");
3626 mempool_free(pmb, phba->mbox_mem_pool);
3627 return;
3628 }
3629
3630 vport_buff = (uint8_t *) vport_info;
3631 do {
James Smartcdcc2342012-08-14 14:26:35 -04003632 /* free dma buffer from previous round */
3633 if (pmb->context1) {
3634 mp = (struct lpfc_dmabuf *)pmb->context1;
3635 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3636 kfree(mp);
3637 }
James Smart1c6834a2009-07-19 10:01:26 -04003638 if (lpfc_dump_static_vport(phba, pmb, offset))
3639 goto out;
3640
James Smart21e9a0a2009-05-22 14:53:21 -04003641 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003642 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3643 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003644
James Smartcdcc2342012-08-14 14:26:35 -04003645 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003646 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3647 "0544 lpfc_create_static_vport failed to"
3648 " issue dump mailbox command ret 0x%x "
3649 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003650 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003651 goto out;
3652 }
3653
James Smart1c6834a2009-07-19 10:01:26 -04003654 if (phba->sli_rev == LPFC_SLI_REV4) {
3655 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003656 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003657 if (byte_count > sizeof(struct static_vport_info) -
3658 offset)
3659 byte_count = sizeof(struct static_vport_info)
3660 - offset;
3661 memcpy(vport_buff + offset, mp->virt, byte_count);
3662 offset += byte_count;
3663 } else {
3664 if (mb->un.varDmp.word_cnt >
3665 sizeof(struct static_vport_info) - offset)
3666 mb->un.varDmp.word_cnt =
3667 sizeof(struct static_vport_info)
3668 - offset;
3669 byte_count = mb->un.varDmp.word_cnt;
3670 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3671 vport_buff + offset,
3672 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003673
James Smart1c6834a2009-07-19 10:01:26 -04003674 offset += byte_count;
3675 }
James Smart21e9a0a2009-05-22 14:53:21 -04003676
James Smart1c6834a2009-07-19 10:01:26 -04003677 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003678 offset < sizeof(struct static_vport_info));
3679
3680
3681 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3682 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3683 != VPORT_INFO_REV)) {
3684 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3685 "0545 lpfc_create_static_vport bad"
3686 " information header 0x%x 0x%x\n",
3687 le32_to_cpu(vport_info->signature),
3688 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3689
3690 goto out;
3691 }
3692
3693 shost = lpfc_shost_from_vport(phba->pport);
3694
3695 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3696 memset(&vport_id, 0, sizeof(vport_id));
3697 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3698 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3699 if (!vport_id.port_name || !vport_id.node_name)
3700 continue;
3701
3702 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3703 vport_id.vport_type = FC_PORTTYPE_NPIV;
3704 vport_id.disable = false;
3705 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3706
3707 if (!new_fc_vport) {
3708 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3709 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003710 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003711 continue;
3712 }
3713
3714 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3715 vport->vport_flag |= STATIC_VPORT;
3716 }
3717
3718out:
James Smart21e9a0a2009-05-22 14:53:21 -04003719 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003720 if (mbx_wait_rc != MBX_TIMEOUT) {
3721 if (pmb->context1) {
3722 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003723 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3724 kfree(mp);
3725 }
James Smart21e9a0a2009-05-22 14:53:21 -04003726 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003727 }
James Smart21e9a0a2009-05-22 14:53:21 -04003728
3729 return;
3730}
3731
dea31012005-04-17 16:05:31 -05003732/*
3733 * This routine handles processing a Fabric REG_LOGIN mailbox
3734 * command upon completion. It is setup in the LPFC_MBOXQ
3735 * as the completion routine when the command is
3736 * handed off to the SLI layer.
3737 */
3738void
James Smart2e0fef82007-06-17 19:56:36 -05003739lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003740{
James Smart92d7f7b2007-06-17 19:56:38 -05003741 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003742 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003743 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003744 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003745 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003746
James Smart549e55c2007-08-02 11:09:51 -04003747 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003748 pmb->context1 = NULL;
3749 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003750
dea31012005-04-17 16:05:31 -05003751 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003752 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3753 "0258 Register Fabric login error: 0x%x\n",
3754 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003755 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3756 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003757 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003758
James Smart76a95d72010-11-20 23:11:48 -05003759 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003760 /* FLOGI failed, use loop map to make discovery list */
3761 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003762
James Smart92d7f7b2007-06-17 19:56:38 -05003763 /* Start discovery */
3764 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003765 /* Decrement the reference count to ndlp after the
3766 * reference to the ndlp are done.
3767 */
3768 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003769 return;
3770 }
3771
3772 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003773 /* Decrement the reference count to ndlp after the reference
3774 * to the ndlp are done.
3775 */
3776 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003777 return;
3778 }
3779
James Smart6d368e52011-05-24 11:44:12 -04003780 if (phba->sli_rev < LPFC_SLI_REV4)
3781 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003782 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003783 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003784 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003785
James Smart2e0fef82007-06-17 19:56:36 -05003786 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003787 /* when physical port receive logo donot start
3788 * vport discovery */
3789 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3790 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003791 else {
3792 shost = lpfc_shost_from_vport(vport);
3793 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003794 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003795 spin_unlock_irq(shost->host_lock);
3796 }
James Smart92d7f7b2007-06-17 19:56:38 -05003797 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003798 }
3799
3800 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3801 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003802 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003803
3804 /* Drop the reference count from the mbox at the end after
3805 * all the current reference to the ndlp have been done.
3806 */
3807 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003808 return;
3809}
3810
3811/*
3812 * This routine handles processing a NameServer REG_LOGIN mailbox
3813 * command upon completion. It is setup in the LPFC_MBOXQ
3814 * as the completion routine when the command is
3815 * handed off to the SLI layer.
3816 */
3817void
James Smart2e0fef82007-06-17 19:56:36 -05003818lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003819{
James Smart21e9a0a2009-05-22 14:53:21 -04003820 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003821 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3822 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3823 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003824
James Smartd439d282010-09-29 11:18:45 -04003825 pmb->context1 = NULL;
3826 pmb->context2 = NULL;
3827
dea31012005-04-17 16:05:31 -05003828 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003829out:
James Smart21e9a0a2009-05-22 14:53:21 -04003830 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3831 "0260 Register NameServer error: 0x%x\n",
3832 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003833 /* decrement the node reference count held for this
3834 * callback function.
3835 */
James Smart329f9bc2007-04-25 09:53:01 -04003836 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003837 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3838 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003839 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003840
3841 /* If no other thread is using the ndlp, free it */
3842 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003843
James Smart76a95d72010-11-20 23:11:48 -05003844 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003845 /*
3846 * RegLogin failed, use loop map to make discovery
3847 * list
3848 */
3849 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003850
James Smart92d7f7b2007-06-17 19:56:38 -05003851 /* Start discovery */
3852 lpfc_disc_start(vport);
3853 return;
3854 }
3855 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003856 return;
3857 }
3858
James Smart6d368e52011-05-24 11:44:12 -04003859 if (phba->sli_rev < LPFC_SLI_REV4)
3860 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003861 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003862 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003863 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003864 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3865 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3866 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3867 atomic_read(&ndlp->kref.refcount),
3868 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003869
James Smart2e0fef82007-06-17 19:56:36 -05003870 if (vport->port_state < LPFC_VPORT_READY) {
3871 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003872 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);
James Smarte26422f2015-05-21 13:55:16 -04003876 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0);
James Smart92d7f7b2007-06-17 19:56:38 -05003877
3878 /* Issue SCR just before NameServer GID_FT Query */
3879 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003880 }
3881
James Smart2e0fef82007-06-17 19:56:36 -05003882 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003883 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003884 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003885 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003886 goto out;
dea31012005-04-17 16:05:31 -05003887 }
3888
James Smartfa4066b2008-01-11 01:53:27 -05003889 /* decrement the node reference count held for this
3890 * callback function.
3891 */
James Smart329f9bc2007-04-25 09:53:01 -04003892 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003893 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3894 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003895 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003896
3897 return;
3898}
3899
3900static void
James Smart2e0fef82007-06-17 19:56:36 -05003901lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003902{
James Smart2e0fef82007-06-17 19:56:36 -05003903 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3904 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003905 struct lpfc_rport_data *rdata;
3906 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003907 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003908
3909 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003910 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3911 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003912 rport_ids.port_id = ndlp->nlp_DID;
3913 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003914
James Smart329f9bc2007-04-25 09:53:01 -04003915 /*
3916 * We leave our node pointer in rport->dd_data when we unregister a
3917 * FCP target port. But fc_remote_port_add zeros the space to which
3918 * rport->dd_data points. So, if we're reusing a previously
3919 * registered port, drop the reference that we took the last time we
3920 * registered the port.
3921 */
3922 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003923 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003924 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003925
3926 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3927 "rport add: did:x%x flg:x%x type x%x",
3928 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3929
James Smart9589b0622011-04-16 11:03:17 -04003930 /* Don't add the remote port if unloading. */
3931 if (vport->load_flag & FC_UNLOADING)
3932 return;
3933
James Smart2e0fef82007-06-17 19:56:36 -05003934 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003935 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003936 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3937 "Warning: fc_remote_port_add failed\n");
3938 return;
3939 }
3940
3941 /* initialize static port data */
3942 rport->maxframe_size = ndlp->nlp_maxframe;
3943 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003944 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003945 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003946
3947 if (ndlp->nlp_type & NLP_FCP_TARGET)
3948 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3949 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3950 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3951
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003952 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3953 fc_remote_port_rolechg(rport, rport_ids.roles);
3954
James Smart34f5ad82012-08-03 12:35:03 -04003955 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3956 "3183 rport register x%06x, rport %p role x%x\n",
3957 ndlp->nlp_DID, rport, rport_ids.roles);
3958
James Smart071fbd3d2006-04-15 11:53:20 -04003959 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003960 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003961 ndlp->nlp_sid = rport->scsi_target_id;
3962 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003963 return;
3964}
3965
3966static void
James Smart2e0fef82007-06-17 19:56:36 -05003967lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003968{
3969 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003970
James Smart858c9f62007-06-17 19:56:39 -05003971 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3972 "rport delete: did:x%x flg:x%x type x%x",
3973 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3974
James Smart34f5ad82012-08-03 12:35:03 -04003975 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3976 "3184 rport unregister x%06x, rport %p\n",
3977 ndlp->nlp_DID, rport);
3978
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003979 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003980
3981 return;
3982}
3983
James Smartde0c5b32007-04-25 09:52:27 -04003984static void
James Smart2e0fef82007-06-17 19:56:36 -05003985lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003986{
James Smart2e0fef82007-06-17 19:56:36 -05003987 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3988
3989 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003990 switch (state) {
3991 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003992 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003993 break;
3994 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003995 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003996 break;
3997 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003998 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003999 break;
4000 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004001 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004002 break;
4003 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004004 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004005 break;
4006 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004007 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004008 break;
4009 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004010 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004011 break;
4012 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004013 if (vport->fc_npr_cnt == 0 && count == -1)
4014 vport->fc_npr_cnt = 0;
4015 else
4016 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004017 break;
4018 }
James Smart2e0fef82007-06-17 19:56:36 -05004019 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004020}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004021
James Smartde0c5b32007-04-25 09:52:27 -04004022static void
James Smart2e0fef82007-06-17 19:56:36 -05004023lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004024 int old_state, int new_state)
4025{
James Smart2e0fef82007-06-17 19:56:36 -05004026 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4027
James Smartde0c5b32007-04-25 09:52:27 -04004028 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004029 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4030 ndlp->nlp_type |= NLP_FC_NODE;
4031 }
4032 if (new_state == NLP_STE_MAPPED_NODE)
4033 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4034 if (new_state == NLP_STE_NPR_NODE)
4035 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004036
James Smartde0c5b32007-04-25 09:52:27 -04004037 /* Transport interface */
4038 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
4039 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004040 vport->phba->nport_event_cnt++;
4041 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004042 }
dea31012005-04-17 16:05:31 -05004043
James Smartde0c5b32007-04-25 09:52:27 -04004044 if (new_state == NLP_STE_MAPPED_NODE ||
4045 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05004046 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05004047 /*
4048 * Tell the fc transport about the port, if we haven't
4049 * already. If we have, and it's a scsi entity, be
4050 * sure to unblock any attached scsi devices
4051 */
4052 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004053 }
James Smartea2151b2008-09-07 11:52:10 -04004054 if ((new_state == NLP_STE_MAPPED_NODE) &&
4055 (vport->stat_data_enabled)) {
4056 /*
4057 * A new target is discovered, if there is no buffer for
4058 * statistical data collection allocate buffer.
4059 */
4060 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4061 sizeof(struct lpfc_scsicmd_bkt),
4062 GFP_KERNEL);
4063
4064 if (!ndlp->lat_data)
4065 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4066 "0286 lpfc_nlp_state_cleanup failed to "
4067 "allocate statistical data buffer DID "
4068 "0x%x\n", ndlp->nlp_DID);
4069 }
James Smart858c9f62007-06-17 19:56:39 -05004070 /*
4071 * if we added to Mapped list, but the remote port
4072 * registration failed or assigned a target id outside
4073 * our presentable range - move the node to the
4074 * Unmapped List
4075 */
James Smartde0c5b32007-04-25 09:52:27 -04004076 if (new_state == NLP_STE_MAPPED_NODE &&
4077 (!ndlp->rport ||
4078 ndlp->rport->scsi_target_id == -1 ||
4079 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004080 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004081 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004082 spin_unlock_irq(shost->host_lock);
4083 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004084 }
James Smartde0c5b32007-04-25 09:52:27 -04004085}
4086
James Smart685f0bf2007-04-25 09:53:08 -04004087static char *
4088lpfc_nlp_state_name(char *buffer, size_t size, int state)
4089{
4090 static char *states[] = {
4091 [NLP_STE_UNUSED_NODE] = "UNUSED",
4092 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4093 [NLP_STE_ADISC_ISSUE] = "ADISC",
4094 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4095 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004096 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004097 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4098 [NLP_STE_MAPPED_NODE] = "MAPPED",
4099 [NLP_STE_NPR_NODE] = "NPR",
4100 };
4101
James Smart311464e2007-08-02 11:10:37 -04004102 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004103 strlcpy(buffer, states[state], size);
4104 else
4105 snprintf(buffer, size, "unknown (%d)", state);
4106 return buffer;
4107}
4108
James Smartde0c5b32007-04-25 09:52:27 -04004109void
James Smart2e0fef82007-06-17 19:56:36 -05004110lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4111 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004112{
James Smart2e0fef82007-06-17 19:56:36 -05004113 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004114 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004115 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004116
James Smarte8b62012007-08-02 11:10:09 -04004117 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4118 "0904 NPort state transition x%06x, %s -> %s\n",
4119 ndlp->nlp_DID,
4120 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4121 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004122
4123 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4124 "node statechg did:x%x old:%d ste:%d",
4125 ndlp->nlp_DID, old_state, state);
4126
James Smartde0c5b32007-04-25 09:52:27 -04004127 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004128 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004129 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004130 if (old_state == NLP_STE_UNMAPPED_NODE) {
4131 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4132 ndlp->nlp_type &= ~NLP_FC_NODE;
4133 }
4134
James Smart685f0bf2007-04-25 09:53:08 -04004135 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004136 spin_lock_irq(shost->host_lock);
4137 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4138 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004139 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004140 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004141
4142 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004143 lpfc_nlp_counters(vport, state, 1);
4144 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004145}
4146
4147void
James Smarte47c9092008-02-08 18:49:26 -05004148lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4149{
4150 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4151
4152 if (list_empty(&ndlp->nlp_listp)) {
4153 spin_lock_irq(shost->host_lock);
4154 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4155 spin_unlock_irq(shost->host_lock);
4156 }
4157}
4158
4159void
James Smart2e0fef82007-06-17 19:56:36 -05004160lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004161{
James Smart2e0fef82007-06-17 19:56:36 -05004162 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4163
James Smart0d2b6b82008-06-14 22:52:47 -04004164 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004165 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004166 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4167 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004168 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004169 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004170 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004171 NLP_STE_UNUSED_NODE);
4172}
4173
Adrian Bunk4d9db012008-02-14 23:24:02 +02004174static void
James Smarte47c9092008-02-08 18:49:26 -05004175lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4176{
James Smart0d2b6b82008-06-14 22:52:47 -04004177 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004178 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4179 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4180 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4181 NLP_STE_UNUSED_NODE);
4182}
James Smart109f6ed2008-12-04 22:39:08 -05004183/**
James Smart3621a712009-04-06 18:47:14 -04004184 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004185 * @vport: Pointer to Virtual Port object.
4186 * @ndlp: Pointer to FC node object.
4187 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004188 *
4189 * This function is always called when node object need to be initialized.
4190 * It initializes all the fields of the node object. Although the reference
4191 * to phba from @ndlp can be obtained indirectly through it's reference to
4192 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4193 * to the life-span of the @ndlp might go beyond the existence of @vport as
4194 * the final release of ndlp is determined by its reference count. And, the
4195 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004196 **/
4197static inline void
4198lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4199 uint32_t did)
4200{
4201 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4202 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4203 init_timer(&ndlp->nlp_delayfunc);
4204 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4205 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4206 ndlp->nlp_DID = did;
4207 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004208 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004209 ndlp->nlp_sid = NLP_NO_SID;
4210 kref_init(&ndlp->kref);
4211 NLP_INT_NODE_ACT(ndlp);
4212 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004213 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004214}
James Smarte47c9092008-02-08 18:49:26 -05004215
4216struct lpfc_nodelist *
4217lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4218 int state)
4219{
4220 struct lpfc_hba *phba = vport->phba;
4221 uint32_t did;
4222 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004223 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004224
4225 if (!ndlp)
4226 return NULL;
4227
4228 spin_lock_irqsave(&phba->ndlp_lock, flags);
4229 /* The ndlp should not be in memory free mode */
4230 if (NLP_CHK_FREE_REQ(ndlp)) {
4231 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4232 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4233 "0277 lpfc_enable_node: ndlp:x%p "
4234 "usgmap:x%x refcnt:%d\n",
4235 (void *)ndlp, ndlp->nlp_usg_map,
4236 atomic_read(&ndlp->kref.refcount));
4237 return NULL;
4238 }
4239 /* The ndlp should not already be in active mode */
4240 if (NLP_CHK_NODE_ACT(ndlp)) {
4241 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4242 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4243 "0278 lpfc_enable_node: ndlp:x%p "
4244 "usgmap:x%x refcnt:%d\n",
4245 (void *)ndlp, ndlp->nlp_usg_map,
4246 atomic_read(&ndlp->kref.refcount));
4247 return NULL;
4248 }
4249
4250 /* Keep the original DID */
4251 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004252 if (phba->sli_rev == LPFC_SLI_REV4)
4253 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004254
4255 /* re-initialize ndlp except of ndlp linked list pointer */
4256 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4257 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004258 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004259
James Smartcff261f2013-12-17 20:29:47 -05004260 if (phba->sli_rev == LPFC_SLI_REV4)
4261 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4262
James Smarte47c9092008-02-08 18:49:26 -05004263 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004264 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004265 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004266 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4267 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4268 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4269 ndlp->nlp_flag,
4270 atomic_read(&ndlp->kref.refcount),
4271 ndlp->nlp_usg_map, ndlp);
4272 }
James Smart725dd392013-10-10 12:23:30 -04004273
James Smarte47c9092008-02-08 18:49:26 -05004274
4275 if (state != NLP_STE_UNUSED_NODE)
4276 lpfc_nlp_set_state(vport, ndlp, state);
4277
4278 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4279 "node enable: did:x%x",
4280 ndlp->nlp_DID, 0, 0);
4281 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004282}
4283
4284void
James Smart2e0fef82007-06-17 19:56:36 -05004285lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004286{
James Smart87af33f2007-10-27 13:37:43 -04004287 /*
James Smartfa4066b2008-01-11 01:53:27 -05004288 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004289 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004290 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4291 * until ALL other outstanding threads have completed. We check
4292 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004293 */
James Smartfa4066b2008-01-11 01:53:27 -05004294 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4295 return;
James Smart51ef4c22007-08-02 11:10:31 -04004296 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004297 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004298 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004299 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004300 }
James Smart02902172015-05-21 13:55:17 -04004301
4302 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004303 return;
dea31012005-04-17 16:05:31 -05004304}
4305
4306/*
4307 * Start / ReStart rescue timer for Discovery / RSCN handling
4308 */
4309void
James Smart2e0fef82007-06-17 19:56:36 -05004310lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004311{
James Smart2e0fef82007-06-17 19:56:36 -05004312 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4313 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004314 uint32_t tmo;
4315
James Smart2e0fef82007-06-17 19:56:36 -05004316 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004317 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004318 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4319 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004320 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004321 * FC spec states we need 3 * ratov for CT requests
4322 */
4323 tmo = ((phba->fc_ratov * 3) + 3);
4324 }
dea31012005-04-17 16:05:31 -05004325
James Smart858c9f62007-06-17 19:56:39 -05004326
4327 if (!timer_pending(&vport->fc_disctmo)) {
4328 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4329 "set disc timer: tmo:x%x state:x%x flg:x%x",
4330 tmo, vport->port_state, vport->fc_flag);
4331 }
4332
James Smart256ec0d2013-04-17 20:14:58 -04004333 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004334 spin_lock_irq(shost->host_lock);
4335 vport->fc_flag |= FC_DISC_TMO;
4336 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004337
4338 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004339 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4340 "0247 Start Discovery Timer state x%x "
4341 "Data: x%x x%lx x%x x%x\n",
4342 vport->port_state, tmo,
4343 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4344 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004345
4346 return;
4347}
4348
4349/*
4350 * Cancel rescue timer for Discovery / RSCN handling
4351 */
4352int
James Smart2e0fef82007-06-17 19:56:36 -05004353lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004354{
James Smart2e0fef82007-06-17 19:56:36 -05004355 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004356 unsigned long iflags;
4357
James Smart858c9f62007-06-17 19:56:39 -05004358 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4359 "can disc timer: state:x%x rtry:x%x flg:x%x",
4360 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4361
dea31012005-04-17 16:05:31 -05004362 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004363 if (vport->fc_flag & FC_DISC_TMO) {
4364 spin_lock_irqsave(shost->host_lock, iflags);
4365 vport->fc_flag &= ~FC_DISC_TMO;
4366 spin_unlock_irqrestore(shost->host_lock, iflags);
4367 del_timer_sync(&vport->fc_disctmo);
4368 spin_lock_irqsave(&vport->work_port_lock, iflags);
4369 vport->work_port_events &= ~WORKER_DISC_TMO;
4370 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004371 }
4372
4373 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004374 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4375 "0248 Cancel Discovery Timer state x%x "
4376 "Data: x%x x%x x%x\n",
4377 vport->port_state, vport->fc_flag,
4378 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004379 return 0;
dea31012005-04-17 16:05:31 -05004380}
4381
4382/*
4383 * Check specified ring for outstanding IOCB on the SLI queue
4384 * Return true if iocb matches the specified nport
4385 */
4386int
James Smart2e0fef82007-06-17 19:56:36 -05004387lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4388 struct lpfc_sli_ring *pring,
4389 struct lpfc_iocbq *iocb,
4390 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004391{
James Smart2e0fef82007-06-17 19:56:36 -05004392 struct lpfc_sli *psli = &phba->sli;
4393 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004394 struct lpfc_vport *vport = ndlp->vport;
4395
4396 if (iocb->vport != vport)
4397 return 0;
4398
dea31012005-04-17 16:05:31 -05004399 if (pring->ringno == LPFC_ELS_RING) {
4400 switch (icmd->ulpCommand) {
4401 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004402 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004403 return 1;
dea31012005-04-17 16:05:31 -05004404 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004405 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4406 return 1;
dea31012005-04-17 16:05:31 -05004407 case CMD_XMIT_ELS_RSP64_CX:
4408 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004409 return 1;
dea31012005-04-17 16:05:31 -05004410 }
James Smarta4bc3372006-12-02 13:34:16 -05004411 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004412
4413 } else if (pring->ringno == psli->fcp_ring) {
4414 /* Skip match check if waiting to relogin to FCP target */
4415 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004416 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004417 return 0;
dea31012005-04-17 16:05:31 -05004418 }
4419 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004420 return 1;
dea31012005-04-17 16:05:31 -05004421 }
4422 } else if (pring->ringno == psli->next_ring) {
4423
4424 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004425 return 0;
dea31012005-04-17 16:05:31 -05004426}
4427
4428/*
4429 * Free resources / clean up outstanding I/Os
4430 * associated with nlp_rpi in the LPFC_NODELIST entry.
4431 */
4432static int
James Smart2e0fef82007-06-17 19:56:36 -05004433lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004434{
James Smart2534ba72007-04-25 09:52:20 -04004435 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004436 struct lpfc_sli *psli;
4437 struct lpfc_sli_ring *pring;
4438 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004439 uint32_t i;
dea31012005-04-17 16:05:31 -05004440
James Smart92d7f7b2007-06-17 19:56:38 -05004441 lpfc_fabric_abort_nport(ndlp);
4442
dea31012005-04-17 16:05:31 -05004443 /*
4444 * Everything that matches on txcmplq will be returned
4445 * by firmware with a no rpi error.
4446 */
4447 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004448 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004449 /* Now process each ring */
4450 for (i = 0; i < psli->num_rings; i++) {
4451 pring = &psli->ring[i];
4452
James Smart2e0fef82007-06-17 19:56:36 -05004453 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004454 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004455 list) {
dea31012005-04-17 16:05:31 -05004456 /*
4457 * Check to see if iocb matches the nport we are
4458 * looking for
4459 */
James Smart92d7f7b2007-06-17 19:56:38 -05004460 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4461 ndlp))) {
dea31012005-04-17 16:05:31 -05004462 /* It matches, so deque and call compl
4463 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004464 list_move_tail(&iocb->list,
4465 &completions);
dea31012005-04-17 16:05:31 -05004466 }
4467 }
James Smart2e0fef82007-06-17 19:56:36 -05004468 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004469 }
4470 }
James Smart2534ba72007-04-25 09:52:20 -04004471
James Smarta257bf92009-04-06 18:48:10 -04004472 /* Cancel all the IOCBs from the completions list */
4473 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4474 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004475
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004476 return 0;
dea31012005-04-17 16:05:31 -05004477}
4478
James Smart086a3452012-08-14 14:25:21 -04004479/**
4480 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4481 * @phba: Pointer to HBA context object.
4482 * @pmb: Pointer to mailbox object.
4483 *
4484 * This function will issue an ELS LOGO command after completing
4485 * the UNREG_RPI.
4486 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004487static void
James Smart086a3452012-08-14 14:25:21 -04004488lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4489{
4490 struct lpfc_vport *vport = pmb->vport;
4491 struct lpfc_nodelist *ndlp;
4492
4493 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4494 if (!ndlp)
4495 return;
4496 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004497 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004498}
4499
dea31012005-04-17 16:05:31 -05004500/*
4501 * Free rpi associated with LPFC_NODELIST entry.
4502 * This routine is called from lpfc_freenode(), when we are removing
4503 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4504 * LOGO that completes successfully, and we are waiting to PLOGI back
4505 * to the remote NPort. In addition, it is called after we receive
4506 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4507 * we are waiting to PLOGI back to the remote NPort.
4508 */
4509int
James Smart2e0fef82007-06-17 19:56:36 -05004510lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004511{
James Smart2e0fef82007-06-17 19:56:36 -05004512 struct lpfc_hba *phba = vport->phba;
4513 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004514 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004515 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004516
James Smartf454a9a2013-09-06 12:20:07 -04004517 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4518 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4519 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4520 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4521 "3366 RPI x%x needs to be "
4522 "unregistered nlp_flag x%x "
4523 "did x%x\n",
4524 ndlp->nlp_rpi, ndlp->nlp_flag,
4525 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004526 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4527 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004528 /* SLI4 ports require the physical rpi value. */
4529 rpi = ndlp->nlp_rpi;
4530 if (phba->sli_rev == LPFC_SLI_REV4)
4531 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004532
James Smart6d368e52011-05-24 11:44:12 -04004533 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004534 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004535 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4536 mbox->context1 = ndlp;
4537 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4538 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004539 if (phba->sli_rev == LPFC_SLI_REV4 &&
4540 (!(vport->load_flag & FC_UNLOADING)) &&
4541 (bf_get(lpfc_sli_intf_if_type,
4542 &phba->sli4_hba.sli_intf) ==
4543 LPFC_SLI_INTF_IF_TYPE_2)) {
4544 mbox->context1 = lpfc_nlp_get(ndlp);
4545 mbox->mbox_cmpl =
4546 lpfc_sli4_unreg_rpi_cmpl_clr;
4547 } else
4548 mbox->mbox_cmpl =
4549 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004550 }
4551
James Smart0b727fe2007-10-27 13:37:25 -04004552 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004553 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004554 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004555 }
dea31012005-04-17 16:05:31 -05004556 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004557
James Smart40426292010-12-15 17:58:10 -05004558 if (phba->sli_rev != LPFC_SLI_REV4)
4559 ndlp->nlp_rpi = 0;
4560 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004561 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004562 return 1;
4563 }
4564 return 0;
4565}
4566
James Smartecfd03c2010-02-12 14:41:27 -05004567/**
4568 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4569 * @phba: pointer to lpfc hba data structure.
4570 *
4571 * This routine is invoked to unregister all the currently registered RPIs
4572 * to the HBA.
4573 **/
4574void
4575lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4576{
4577 struct lpfc_vport **vports;
4578 struct lpfc_nodelist *ndlp;
4579 struct Scsi_Host *shost;
4580 int i;
4581
4582 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004583 if (!vports) {
4584 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4585 "2884 Vport array allocation failed \n");
4586 return;
4587 }
James Smartecfd03c2010-02-12 14:41:27 -05004588 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4589 shost = lpfc_shost_from_vport(vports[i]);
4590 spin_lock_irq(shost->host_lock);
4591 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004592 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004593 /* The mempool_alloc might sleep */
4594 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004595 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004596 spin_lock_irq(shost->host_lock);
4597 }
James Smartecfd03c2010-02-12 14:41:27 -05004598 }
4599 spin_unlock_irq(shost->host_lock);
4600 }
4601 lpfc_destroy_vport_work_array(phba, vports);
4602}
4603
James Smart92d7f7b2007-06-17 19:56:38 -05004604void
4605lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4606{
4607 struct lpfc_hba *phba = vport->phba;
4608 LPFC_MBOXQ_t *mbox;
4609 int rc;
4610
James Smart5af5eee2010-10-22 11:06:38 -04004611 if (phba->sli_rev == LPFC_SLI_REV4) {
4612 lpfc_sli4_unreg_all_rpis(vport);
4613 return;
4614 }
4615
James Smart92d7f7b2007-06-17 19:56:38 -05004616 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4617 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004618 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4619 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004620 mbox->vport = vport;
4621 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004622 mbox->context1 = NULL;
4623 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004624 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004625 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004626
4627 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4628 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4629 "1836 Could not issue "
4630 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004631 }
4632}
4633
4634void
4635lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4636{
4637 struct lpfc_hba *phba = vport->phba;
4638 LPFC_MBOXQ_t *mbox;
4639 int rc;
4640
4641 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4642 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004643 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4644 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004645 mbox->vport = vport;
4646 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004647 mbox->context1 = NULL;
4648 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004649 if (rc != MBX_TIMEOUT)
4650 mempool_free(mbox, phba->mbox_mem_pool);
4651
4652 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004653 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4654 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004655 "unreg_did (default rpis) status %d\n",
4656 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004657 }
4658}
4659
dea31012005-04-17 16:05:31 -05004660/*
4661 * Free resources associated with LPFC_NODELIST entry
4662 * so it can be freed.
4663 */
4664static int
James Smart2e0fef82007-06-17 19:56:36 -05004665lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004666{
James Smart2e0fef82007-06-17 19:56:36 -05004667 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4668 struct lpfc_hba *phba = vport->phba;
4669 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004670 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004671
4672 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004673 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4674 "0900 Cleanup node for NPort x%x "
4675 "Data: x%x x%x x%x\n",
4676 ndlp->nlp_DID, ndlp->nlp_flag,
4677 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004678 if (NLP_CHK_FREE_REQ(ndlp)) {
4679 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4680 "0280 lpfc_cleanup_node: ndlp:x%p "
4681 "usgmap:x%x refcnt:%d\n",
4682 (void *)ndlp, ndlp->nlp_usg_map,
4683 atomic_read(&ndlp->kref.refcount));
4684 lpfc_dequeue_node(vport, ndlp);
4685 } else {
4686 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4687 "0281 lpfc_cleanup_node: ndlp:x%p "
4688 "usgmap:x%x refcnt:%d\n",
4689 (void *)ndlp, ndlp->nlp_usg_map,
4690 atomic_read(&ndlp->kref.refcount));
4691 lpfc_disable_node(vport, ndlp);
4692 }
dea31012005-04-17 16:05:31 -05004693
James Smart086a3452012-08-14 14:25:21 -04004694
4695 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4696
dea31012005-04-17 16:05:31 -05004697 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4698 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004699 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004700 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004701 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4702 mb->context2 = NULL;
4703 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4704 }
4705 }
James Smart33ccf8d2006-08-17 11:57:58 -04004706
James Smart2e0fef82007-06-17 19:56:36 -05004707 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004708 /* Cleanup REG_LOGIN completions which are not yet processed */
4709 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4710 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004711 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004712 (ndlp != (struct lpfc_nodelist *) mb->context2))
4713 continue;
4714
4715 mb->context2 = NULL;
4716 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4717 }
4718
dea31012005-04-17 16:05:31 -05004719 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004720 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004721 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004722 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004723 mp = (struct lpfc_dmabuf *) (mb->context1);
4724 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004725 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004726 kfree(mp);
4727 }
4728 list_del(&mb->list);
4729 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004730 /* We shall not invoke the lpfc_nlp_put to decrement
4731 * the ndlp reference count as we are in the process
4732 * of lpfc_nlp_release.
4733 */
dea31012005-04-17 16:05:31 -05004734 }
4735 }
James Smart2e0fef82007-06-17 19:56:36 -05004736 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004737
James Smarte47c9092008-02-08 18:49:26 -05004738 lpfc_els_abort(phba, ndlp);
4739
James Smart2e0fef82007-06-17 19:56:36 -05004740 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004741 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004742 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004743
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004744 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004745 del_timer_sync(&ndlp->nlp_delayfunc);
4746
James Smart0d2b6b82008-06-14 22:52:47 -04004747 list_del_init(&ndlp->els_retry_evt.evt_listp);
4748 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004749 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004750 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004751
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004752 return 0;
dea31012005-04-17 16:05:31 -05004753}
4754
4755/*
4756 * Check to see if we can free the nlp back to the freelist.
4757 * If we are in the middle of using the nlp in the discovery state
4758 * machine, defer the free till we reach the end of the state machine.
4759 */
James Smart329f9bc2007-04-25 09:53:01 -04004760static void
James Smart2e0fef82007-06-17 19:56:36 -05004761lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004762{
James Smarta8adb832007-10-27 13:37:53 -04004763 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004764 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004765 LPFC_MBOXQ_t *mbox;
4766 int rc;
dea31012005-04-17 16:05:31 -05004767
James Smart0d2b6b82008-06-14 22:52:47 -04004768 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004769 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004770 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4771 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004772 /* For this case we need to cleanup the default rpi
4773 * allocated by the firmware.
4774 */
James Smartbe6bb942015-04-07 15:07:22 -04004775 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4776 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4777 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4778 atomic_read(&ndlp->kref.refcount),
4779 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004780 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4781 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004782 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004783 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004784 if (rc) {
4785 mempool_free(mbox, phba->mbox_mem_pool);
4786 }
4787 else {
4788 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4789 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4790 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004791 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004792 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4793 if (rc == MBX_NOT_FINISHED) {
4794 mempool_free(mbox, phba->mbox_mem_pool);
4795 }
4796 }
4797 }
4798 }
James Smart2e0fef82007-06-17 19:56:36 -05004799 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004800
James Smart2e0fef82007-06-17 19:56:36 -05004801 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004802 * We can get here with a non-NULL ndlp->rport because when we
4803 * unregister a rport we don't break the rport/node linkage. So if we
4804 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004805 */
James Smart92d7f7b2007-06-17 19:56:38 -05004806 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004807 rdata = ndlp->rport->dd_data;
4808 rdata->pnode = NULL;
4809 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004810 }
dea31012005-04-17 16:05:31 -05004811}
4812
4813static int
James Smart2e0fef82007-06-17 19:56:36 -05004814lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4815 uint32_t did)
dea31012005-04-17 16:05:31 -05004816{
James Smart2e0fef82007-06-17 19:56:36 -05004817 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004818
4819 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004820 return 0;
dea31012005-04-17 16:05:31 -05004821
dea31012005-04-17 16:05:31 -05004822 /* First check for Direct match */
4823 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004824 return 1;
dea31012005-04-17 16:05:31 -05004825
4826 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004827 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004828 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004829 return 0;
dea31012005-04-17 16:05:31 -05004830 }
4831
4832 matchdid.un.word = did;
4833 ndlpdid.un.word = ndlp->nlp_DID;
4834 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4835 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4836 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04004837 /* This code is supposed to match the ID
4838 * for a private loop device that is
4839 * connect to fl_port. But we need to
4840 * check that the port did not just go
4841 * from pt2pt to fabric or we could end
4842 * up matching ndlp->nlp_DID 000001 to
4843 * fabric DID 0x20101
4844 */
dea31012005-04-17 16:05:31 -05004845 if ((ndlpdid.un.b.domain == 0) &&
4846 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04004847 if (ndlpdid.un.b.id &&
4848 vport->phba->fc_topology ==
4849 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004850 return 1;
dea31012005-04-17 16:05:31 -05004851 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004852 return 0;
dea31012005-04-17 16:05:31 -05004853 }
4854
4855 matchdid.un.word = ndlp->nlp_DID;
4856 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4857 (mydid.un.b.area == ndlpdid.un.b.area)) {
4858 if ((matchdid.un.b.domain == 0) &&
4859 (matchdid.un.b.area == 0)) {
4860 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004861 return 1;
dea31012005-04-17 16:05:31 -05004862 }
4863 }
4864 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004865 return 0;
dea31012005-04-17 16:05:31 -05004866}
4867
James Smart685f0bf2007-04-25 09:53:08 -04004868/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004869static struct lpfc_nodelist *
4870__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004871{
James Smart2fb9bd82006-12-02 13:33:57 -05004872 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004873 uint32_t data1;
4874
James Smart2e0fef82007-06-17 19:56:36 -05004875 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4876 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004877 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4878 ((uint32_t) ndlp->nlp_xri << 16) |
4879 ((uint32_t) ndlp->nlp_type << 8) |
4880 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004881 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4882 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05004883 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04004884 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05004885 ndlp->nlp_flag, data1,
4886 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04004887 return ndlp;
dea31012005-04-17 16:05:31 -05004888 }
4889 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004890
dea31012005-04-17 16:05:31 -05004891 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004892 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4893 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004894 return NULL;
4895}
4896
4897struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004898lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004899{
James Smart2e0fef82007-06-17 19:56:36 -05004900 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004901 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004902 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004903
James Smartfedd3b72011-02-16 12:39:24 -05004904 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004905 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004906 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004907 return ndlp;
4908}
4909
4910struct lpfc_nodelist *
4911lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4912{
4913 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4914 struct lpfc_nodelist *ndlp;
4915
4916 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004917 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004918 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4919 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004920 return NULL;
4921 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004922 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004923 if (!ndlp)
4924 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004925 lpfc_nlp_init(vport, ndlp, did);
4926 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4927 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004928 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004929 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004930 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004931 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4932 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4933 if (!ndlp)
4934 return NULL;
4935 spin_lock_irq(shost->host_lock);
4936 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4937 spin_unlock_irq(shost->host_lock);
4938 return ndlp;
dea31012005-04-17 16:05:31 -05004939 }
James Smarte47c9092008-02-08 18:49:26 -05004940
James Smart58da1ff2008-04-07 10:15:56 -04004941 if ((vport->fc_flag & FC_RSCN_MODE) &&
4942 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004943 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004944 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004945 * we don't need to try to discover it again.
4946 */
4947 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4948 return NULL;
4949
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004950 /* Since this node is marked for discovery,
4951 * delay timeout is not needed.
4952 */
James Smart0d2b6b82008-06-14 22:52:47 -04004953 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004954 spin_lock_irq(shost->host_lock);
4955 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4956 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004957 } else
dea31012005-04-17 16:05:31 -05004958 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004959 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004960 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004961 * or we are already in the process of discovery on it,
4962 * we don't need to try to discover it again.
4963 */
James Smart685f0bf2007-04-25 09:53:08 -04004964 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004965 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4966 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004967 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004968 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4969 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004970 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004971 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004972 }
4973 return ndlp;
4974}
4975
4976/* Build a list of nodes to discover based on the loopmap */
4977void
James Smart2e0fef82007-06-17 19:56:36 -05004978lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004979{
James Smart2e0fef82007-06-17 19:56:36 -05004980 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004981 int j;
4982 uint32_t alpa, index;
4983
James Smart2e0fef82007-06-17 19:56:36 -05004984 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004985 return;
James Smart2e0fef82007-06-17 19:56:36 -05004986
James Smart76a95d72010-11-20 23:11:48 -05004987 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004988 return;
dea31012005-04-17 16:05:31 -05004989
4990 /* Check for loop map present or not */
4991 if (phba->alpa_map[0]) {
4992 for (j = 1; j <= phba->alpa_map[0]; j++) {
4993 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004994 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004995 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004996 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004997 }
4998 } else {
4999 /* No alpamap, so try all alpa's */
5000 for (j = 0; j < FC_MAXLOOP; j++) {
5001 /* If cfg_scan_down is set, start from highest
5002 * ALPA (0xef) to lowest (0x1).
5003 */
James Smart3de2a652007-08-02 11:09:59 -04005004 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005005 index = j;
5006 else
5007 index = FC_MAXLOOP - j - 1;
5008 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005009 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005010 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005011 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005012 }
5013 }
5014 return;
5015}
5016
dea31012005-04-17 16:05:31 -05005017void
James Smart2e0fef82007-06-17 19:56:36 -05005018lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005019{
dea31012005-04-17 16:05:31 -05005020 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005021 struct lpfc_sli *psli = &phba->sli;
5022 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
5023 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
5024 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
5025 int rc;
5026
James Smart92d7f7b2007-06-17 19:56:38 -05005027 /*
5028 * if it's not a physical port or if we already send
5029 * clear_la then don't send it.
5030 */
5031 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005032 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5033 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005034 return;
5035
James Smart2e0fef82007-06-17 19:56:36 -05005036 /* Link up discovery */
5037 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5038 phba->link_state = LPFC_CLEAR_LA;
5039 lpfc_clear_la(phba, mbox);
5040 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5041 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005042 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005043 if (rc == MBX_NOT_FINISHED) {
5044 mempool_free(mbox, phba->mbox_mem_pool);
5045 lpfc_disc_flush_list(vport);
5046 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5047 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5048 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005049 phba->link_state = LPFC_HBA_ERROR;
5050 }
5051 }
5052}
5053
5054/* Reg_vpi to tell firmware to resume normal operations */
5055void
5056lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5057{
5058 LPFC_MBOXQ_t *regvpimbox;
5059
5060 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5061 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005062 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005063 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5064 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005065 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005066 == MBX_NOT_FINISHED) {
5067 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005068 }
5069 }
5070}
5071
5072/* Start Link up / RSCN discovery on NPR nodes */
5073void
5074lpfc_disc_start(struct lpfc_vport *vport)
5075{
5076 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5077 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005078 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005079 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005080
James Smarte74c03c2013-04-17 20:15:19 -04005081 if (!lpfc_is_link_up(phba)) {
5082 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5083 "3315 Link is not up %x\n",
5084 phba->link_state);
dea31012005-04-17 16:05:31 -05005085 return;
James Smarte74c03c2013-04-17 20:15:19 -04005086 }
James Smart2e0fef82007-06-17 19:56:36 -05005087
5088 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005089 clear_la_pending = 1;
5090 else
5091 clear_la_pending = 0;
5092
James Smart2e0fef82007-06-17 19:56:36 -05005093 if (vport->port_state < LPFC_VPORT_READY)
5094 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005095
James Smart2e0fef82007-06-17 19:56:36 -05005096 lpfc_set_disctmo(vport);
5097
James Smart2e0fef82007-06-17 19:56:36 -05005098 vport->fc_prevDID = vport->fc_myDID;
5099 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005100
5101 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005102 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5103 "0202 Start Discovery hba state x%x "
5104 "Data: x%x x%x x%x\n",
5105 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5106 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005107
5108 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005109 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005110
5111 if (num_sent)
5112 return;
5113
James Smart6fa139f2013-04-17 20:20:00 -04005114 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005115 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005116 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005117 !(vport->fc_flag & FC_RSCN_MODE) &&
5118 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005119 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005120 lpfc_issue_reg_vpi(phba, vport);
5121 return;
5122 }
James Smart2e0fef82007-06-17 19:56:36 -05005123
James Smart92d7f7b2007-06-17 19:56:38 -05005124 /*
5125 * For SLI2, we need to set port_state to READY and continue
5126 * discovery.
5127 */
5128 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5129 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005130 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005131
5132 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005133 vport->num_disc_nodes = 0;
5134 /* go thru NPR nodes and issue ELS PLOGIs */
5135 if (vport->fc_npr_cnt)
5136 lpfc_els_disc_plogi(vport);
5137
5138 if (!vport->num_disc_nodes) {
5139 spin_lock_irq(shost->host_lock);
5140 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5141 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005142 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005143 }
5144 }
James Smart92d7f7b2007-06-17 19:56:38 -05005145 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005146 } else {
5147 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005148 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005149
5150 if (num_sent)
5151 return;
5152
James Smart2e0fef82007-06-17 19:56:36 -05005153 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005154 /* Check to see if more RSCNs came in while we
5155 * were processing this one.
5156 */
James Smart2e0fef82007-06-17 19:56:36 -05005157 if ((vport->fc_rscn_id_cnt == 0) &&
5158 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5159 spin_lock_irq(shost->host_lock);
5160 vport->fc_flag &= ~FC_RSCN_MODE;
5161 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005162 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005163 } else
James Smart2e0fef82007-06-17 19:56:36 -05005164 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005165 }
5166 }
5167 return;
5168}
5169
5170/*
5171 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5172 * ring the match the sppecified nodelist.
5173 */
5174static void
James Smart2e0fef82007-06-17 19:56:36 -05005175lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005176{
James Smart2534ba72007-04-25 09:52:20 -04005177 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005178 struct lpfc_sli *psli;
5179 IOCB_t *icmd;
5180 struct lpfc_iocbq *iocb, *next_iocb;
5181 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005182
5183 psli = &phba->sli;
5184 pring = &psli->ring[LPFC_ELS_RING];
5185
5186 /* Error matching iocb on txq or txcmplq
5187 * First check the txq.
5188 */
James Smart2e0fef82007-06-17 19:56:36 -05005189 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005190 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5191 if (iocb->context1 != ndlp) {
5192 continue;
5193 }
5194 icmd = &iocb->iocb;
5195 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5196 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5197
James Smart2534ba72007-04-25 09:52:20 -04005198 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005199 }
5200 }
5201
5202 /* Next check the txcmplq */
5203 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5204 if (iocb->context1 != ndlp) {
5205 continue;
5206 }
5207 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005208 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5209 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005210 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005211 }
5212 }
James Smart2e0fef82007-06-17 19:56:36 -05005213 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005214
James Smarta257bf92009-04-06 18:48:10 -04005215 /* Cancel all the IOCBs from the completions list */
5216 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5217 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005218}
5219
Adrian Bunka6ababd2007-11-05 18:07:33 +01005220static void
James Smart2e0fef82007-06-17 19:56:36 -05005221lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005222{
5223 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005224 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005225
James Smart2e0fef82007-06-17 19:56:36 -05005226 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5227 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005228 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005229 if (!NLP_CHK_NODE_ACT(ndlp))
5230 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005231 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5232 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5233 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005234 }
dea31012005-04-17 16:05:31 -05005235 }
5236 }
dea31012005-04-17 16:05:31 -05005237}
5238
James Smart92d7f7b2007-06-17 19:56:38 -05005239void
5240lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5241{
5242 lpfc_els_flush_rscn(vport);
5243 lpfc_els_flush_cmd(vport);
5244 lpfc_disc_flush_list(vport);
5245}
5246
dea31012005-04-17 16:05:31 -05005247/*****************************************************************************/
5248/*
5249 * NAME: lpfc_disc_timeout
5250 *
5251 * FUNCTION: Fibre Channel driver discovery timeout routine.
5252 *
5253 * EXECUTION ENVIRONMENT: interrupt only
5254 *
5255 * CALLED FROM:
5256 * Timer function
5257 *
5258 * RETURNS:
5259 * none
5260 */
5261/*****************************************************************************/
5262void
5263lpfc_disc_timeout(unsigned long ptr)
5264{
James Smart2e0fef82007-06-17 19:56:36 -05005265 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5266 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005267 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005268 unsigned long flags = 0;
5269
5270 if (unlikely(!phba))
5271 return;
5272
James Smart5e9d9b82008-06-14 22:52:53 -04005273 spin_lock_irqsave(&vport->work_port_lock, flags);
5274 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5275 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005276 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005277 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005278
James Smart5e9d9b82008-06-14 22:52:53 -04005279 if (!tmo_posted)
5280 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005281 return;
5282}
5283
5284static void
James Smart2e0fef82007-06-17 19:56:36 -05005285lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005286{
James Smart2e0fef82007-06-17 19:56:36 -05005287 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5288 struct lpfc_hba *phba = vport->phba;
5289 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005290 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005291 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005292 int rc, clrlaerr = 0;
5293
James Smart2e0fef82007-06-17 19:56:36 -05005294 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005295 return;
5296
James Smart2e0fef82007-06-17 19:56:36 -05005297 spin_lock_irq(shost->host_lock);
5298 vport->fc_flag &= ~FC_DISC_TMO;
5299 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005300
James Smart858c9f62007-06-17 19:56:39 -05005301 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5302 "disc timeout: state:x%x rtry:x%x flg:x%x",
5303 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5304
James Smart2e0fef82007-06-17 19:56:36 -05005305 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005306
5307 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005308 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5309 * FAN
5310 */
5311 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005312 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5313 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005314 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005315 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005316 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005317 if (!NLP_CHK_NODE_ACT(ndlp))
5318 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005319 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5320 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005321 if (ndlp->nlp_type & NLP_FABRIC) {
5322 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005323 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005324
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005325 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005326 /* Fail outstanding IO now since device
5327 * is marked for PLOGI.
5328 */
James Smart2e0fef82007-06-17 19:56:36 -05005329 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005330 }
5331 }
James Smart92d7f7b2007-06-17 19:56:38 -05005332 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005333 if (phba->sli_rev <= LPFC_SLI_REV3)
5334 lpfc_initial_flogi(vport);
5335 else
5336 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005337 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005338 }
dea31012005-04-17 16:05:31 -05005339 break;
5340
James Smart92d7f7b2007-06-17 19:56:38 -05005341 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005342 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005343 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005344 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005345 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5346 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005347 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005348
5349 /* Assume no Fabric and go on with discovery.
5350 * Check for outstanding ELS FLOGI to abort.
5351 */
5352
5353 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005354 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005355
5356 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005357 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005358 break;
5359
5360 case LPFC_FABRIC_CFG_LINK:
5361 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5362 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005363 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5364 "0223 Timeout while waiting for "
5365 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005366 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005367 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005368 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005369 lpfc_els_abort(phba, ndlp);
5370
5371 /* ReStart discovery */
5372 goto restart_disc;
dea31012005-04-17 16:05:31 -05005373
5374 case LPFC_NS_QRY:
5375 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005376 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5377 "0224 NameServer Query timeout "
5378 "Data: x%x x%x\n",
5379 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005380
James Smart92d7f7b2007-06-17 19:56:38 -05005381 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5382 /* Try it one more time */
5383 vport->fc_ns_retry++;
5384 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5385 vport->fc_ns_retry, 0);
5386 if (rc == 0)
5387 break;
dea31012005-04-17 16:05:31 -05005388 }
James Smart92d7f7b2007-06-17 19:56:38 -05005389 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005390
James Smart87af33f2007-10-27 13:37:43 -04005391restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005392 /*
5393 * Discovery is over.
5394 * set port_state to PORT_READY if SLI2.
5395 * cmpl_reg_vpi will set port_state to READY for SLI3.
5396 */
James Smart3772a992009-05-22 14:50:54 -04005397 if (phba->sli_rev < LPFC_SLI_REV4) {
5398 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5399 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005400 else {
James Smart3772a992009-05-22 14:50:54 -04005401 lpfc_issue_clear_la(phba, vport);
5402 vport->port_state = LPFC_VPORT_READY;
5403 }
dea31012005-04-17 16:05:31 -05005404 }
5405
5406 /* Setup and issue mailbox INITIALIZE LINK command */
5407 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5408 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005409 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5410 "0206 Device Discovery "
5411 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005412 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005413 break;
5414 }
5415
5416 lpfc_linkdown(phba);
5417 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5418 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005419 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005420 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005421 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005422 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005423 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005424 if (rc == MBX_NOT_FINISHED)
5425 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5426
5427 break;
5428
5429 case LPFC_DISC_AUTH:
5430 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005431 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5432 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005433 lpfc_disc_flush_list(vport);
5434
James Smart92d7f7b2007-06-17 19:56:38 -05005435 /*
5436 * set port_state to PORT_READY if SLI2.
5437 * cmpl_reg_vpi will set port_state to READY for SLI3.
5438 */
James Smart3772a992009-05-22 14:50:54 -04005439 if (phba->sli_rev < LPFC_SLI_REV4) {
5440 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5441 lpfc_issue_reg_vpi(phba, vport);
5442 else { /* NPIV Not enabled */
5443 lpfc_issue_clear_la(phba, vport);
5444 vport->port_state = LPFC_VPORT_READY;
5445 }
dea31012005-04-17 16:05:31 -05005446 }
5447 break;
5448
James Smart2e0fef82007-06-17 19:56:36 -05005449 case LPFC_VPORT_READY:
5450 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005451 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5452 "0231 RSCN timeout Data: x%x "
5453 "x%x\n",
5454 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005455
5456 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005457 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005458
James Smart2e0fef82007-06-17 19:56:36 -05005459 lpfc_els_flush_rscn(vport);
5460 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005461 }
5462 break;
James Smart2e0fef82007-06-17 19:56:36 -05005463
James Smart92d7f7b2007-06-17 19:56:38 -05005464 default:
James Smarte8b62012007-08-02 11:10:09 -04005465 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005466 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005467 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005468 break;
5469 }
5470
5471 switch (phba->link_state) {
5472 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005473 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005474 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5475 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005476 clrlaerr = 1;
5477 break;
5478
James Smart09372822008-01-11 01:52:54 -05005479 case LPFC_LINK_UP:
5480 lpfc_issue_clear_la(phba, vport);
5481 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005482 case LPFC_LINK_UNKNOWN:
5483 case LPFC_WARM_START:
5484 case LPFC_INIT_START:
5485 case LPFC_INIT_MBX_CMDS:
5486 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005487 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005488 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5489 "0230 Unexpected timeout, hba link "
5490 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005491 clrlaerr = 1;
5492 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005493
5494 case LPFC_HBA_READY:
5495 break;
dea31012005-04-17 16:05:31 -05005496 }
5497
5498 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005499 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005500 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005501 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5502 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005503 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005504 }
5505
5506 return;
5507}
5508
dea31012005-04-17 16:05:31 -05005509/*
5510 * This routine handles processing a NameServer REG_LOGIN mailbox
5511 * command upon completion. It is setup in the LPFC_MBOXQ
5512 * as the completion routine when the command is
5513 * handed off to the SLI layer.
5514 */
5515void
James Smart2e0fef82007-06-17 19:56:36 -05005516lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005517{
James Smart04c68492009-05-22 14:52:52 -04005518 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005519 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5520 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5521 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005522
5523 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005524 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005525
James Smart6d368e52011-05-24 11:44:12 -04005526 if (phba->sli_rev < LPFC_SLI_REV4)
5527 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005528 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005529 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005530 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005531 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5532 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5533 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
5534 atomic_read(&ndlp->kref.refcount),
5535 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005536 /*
5537 * Start issuing Fabric-Device Management Interface (FDMI) command to
5538 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5539 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005540 */
James Smart2e0fef82007-06-17 19:56:36 -05005541
James Smart76b2c342015-04-07 15:07:19 -04005542 if (vport->cfg_fdmi_on & LPFC_FDMI_REG_DELAY)
James Smart256ec0d2013-04-17 20:14:58 -04005543 mod_timer(&vport->fc_fdmitmo,
5544 jiffies + msecs_to_jiffies(1000 * 60));
James Smart76b2c342015-04-07 15:07:19 -04005545 else
5546 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
dea31012005-04-17 16:05:31 -05005547
James Smartfa4066b2008-01-11 01:53:27 -05005548 /* decrement the node reference count held for this callback
5549 * function.
5550 */
James Smart329f9bc2007-04-25 09:53:01 -04005551 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005552 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5553 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005554 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005555
5556 return;
5557}
5558
James Smart685f0bf2007-04-25 09:53:08 -04005559static int
5560lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5561{
5562 uint16_t *rpi = param;
5563
James Smarteff4a012012-01-18 16:25:25 -05005564 /* check for active node */
5565 if (!NLP_CHK_NODE_ACT(ndlp))
5566 return 0;
5567
James Smart685f0bf2007-04-25 09:53:08 -04005568 return ndlp->nlp_rpi == *rpi;
5569}
5570
5571static int
5572lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5573{
5574 return memcmp(&ndlp->nlp_portname, param,
5575 sizeof(ndlp->nlp_portname)) == 0;
5576}
5577
Adrian Bunka6ababd2007-11-05 18:07:33 +01005578static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005579__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005580{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005581 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005582
James Smart2e0fef82007-06-17 19:56:36 -05005583 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005584 if (filter(ndlp, param)) {
5585 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5586 "3185 FIND node filter %p DID "
5587 "Data: x%p x%x x%x\n",
5588 filter, ndlp, ndlp->nlp_DID,
5589 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005590 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005591 }
James Smart685f0bf2007-04-25 09:53:08 -04005592 }
James Smart34f5ad82012-08-03 12:35:03 -04005593 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5594 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005595 return NULL;
dea31012005-04-17 16:05:31 -05005596}
5597
James Smart685f0bf2007-04-25 09:53:08 -04005598/*
5599 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005600 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005601 */
5602struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005603__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005604{
James Smart2e0fef82007-06-17 19:56:36 -05005605 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005606}
5607
James Smart488d1462006-03-07 15:02:37 -05005608/*
James Smart685f0bf2007-04-25 09:53:08 -04005609 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005610 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005611 */
5612struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005613lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005614{
James Smart2e0fef82007-06-17 19:56:36 -05005615 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005616 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005617
James Smart2e0fef82007-06-17 19:56:36 -05005618 spin_lock_irq(shost->host_lock);
5619 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5620 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005621 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005622}
5623
James Smartcb69f7d2011-12-13 13:21:57 -05005624/*
5625 * This routine looks up the ndlp lists for the given RPI. If the rpi
5626 * is found, the routine returns the node element list pointer else
5627 * return NULL.
5628 */
5629struct lpfc_nodelist *
5630lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5631{
5632 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5633 struct lpfc_nodelist *ndlp;
5634
5635 spin_lock_irq(shost->host_lock);
5636 ndlp = __lpfc_findnode_rpi(vport, rpi);
5637 spin_unlock_irq(shost->host_lock);
5638 return ndlp;
5639}
5640
5641/**
5642 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5643 * @phba: pointer to lpfc hba data structure.
5644 * @vpi: the physical host virtual N_Port identifier.
5645 *
5646 * This routine finds a vport on a HBA (referred by @phba) through a
5647 * @vpi. The function walks the HBA's vport list and returns the address
5648 * of the vport with the matching @vpi.
5649 *
5650 * Return code
5651 * NULL - No vport with the matching @vpi found
5652 * Otherwise - Address to the vport with the matching @vpi.
5653 **/
5654struct lpfc_vport *
5655lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5656{
5657 struct lpfc_vport *vport;
5658 unsigned long flags;
5659 int i = 0;
5660
5661 /* The physical ports are always vpi 0 - translate is unnecessary. */
5662 if (vpi > 0) {
5663 /*
5664 * Translate the physical vpi to the logical vpi. The
5665 * vport stores the logical vpi.
5666 */
5667 for (i = 0; i < phba->max_vpi; i++) {
5668 if (vpi == phba->vpi_ids[i])
5669 break;
5670 }
5671
5672 if (i >= phba->max_vpi) {
5673 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5674 "2936 Could not find Vport mapped "
5675 "to vpi %d\n", vpi);
5676 return NULL;
5677 }
5678 }
5679
5680 spin_lock_irqsave(&phba->hbalock, flags);
5681 list_for_each_entry(vport, &phba->port_list, listentry) {
5682 if (vport->vpi == i) {
5683 spin_unlock_irqrestore(&phba->hbalock, flags);
5684 return vport;
5685 }
5686 }
5687 spin_unlock_irqrestore(&phba->hbalock, flags);
5688 return NULL;
5689}
5690
dea31012005-04-17 16:05:31 -05005691void
James Smart2e0fef82007-06-17 19:56:36 -05005692lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5693 uint32_t did)
dea31012005-04-17 16:05:31 -05005694{
5695 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005696
5697 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005698 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005699 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005700 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005701 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5702 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5703 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5704 ndlp->nlp_flag,
5705 atomic_read(&ndlp->kref.refcount),
5706 ndlp->nlp_usg_map, ndlp);
5707
James Smartcff261f2013-12-17 20:29:47 -05005708 ndlp->active_rrqs_xri_bitmap =
5709 mempool_alloc(vport->phba->active_rrq_pool,
5710 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005711 if (ndlp->active_rrqs_xri_bitmap)
5712 memset(ndlp->active_rrqs_xri_bitmap, 0,
5713 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005714 }
5715
James Smart725dd392013-10-10 12:23:30 -04005716
James Smart858c9f62007-06-17 19:56:39 -05005717
5718 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5719 "node init: did:x%x",
5720 ndlp->nlp_DID, 0, 0);
5721
dea31012005-04-17 16:05:31 -05005722 return;
5723}
James Smart329f9bc2007-04-25 09:53:01 -04005724
James Smart98c9ea52007-10-27 13:37:33 -04005725/* This routine releases all resources associated with a specifc NPort's ndlp
5726 * and mempool_free's the nodelist.
5727 */
James Smart311464e2007-08-02 11:10:37 -04005728static void
James Smart329f9bc2007-04-25 09:53:01 -04005729lpfc_nlp_release(struct kref *kref)
5730{
James Smarte47c9092008-02-08 18:49:26 -05005731 struct lpfc_hba *phba;
5732 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005733 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5734 kref);
James Smart858c9f62007-06-17 19:56:39 -05005735
5736 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5737 "node release: did:x%x flg:x%x type:x%x",
5738 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5739
James Smarte47c9092008-02-08 18:49:26 -05005740 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005741 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005742 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005743 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smartbe6bb942015-04-07 15:07:22 -04005744 atomic_read(&ndlp->kref.refcount), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005745
5746 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005747 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005748
5749 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005750 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005751 spin_lock_irqsave(&phba->ndlp_lock, flags);
5752 NLP_CLR_NODE_ACT(ndlp);
5753 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005754 if (phba->sli_rev == LPFC_SLI_REV4)
5755 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005756
5757 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005758 if (NLP_CHK_FREE_REQ(ndlp)) {
5759 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005760 if (phba->sli_rev == LPFC_SLI_REV4)
5761 mempool_free(ndlp->active_rrqs_xri_bitmap,
5762 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005763 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005764 }
James Smart329f9bc2007-04-25 09:53:01 -04005765}
5766
James Smart98c9ea52007-10-27 13:37:33 -04005767/* This routine bumps the reference count for a ndlp structure to ensure
5768 * that one discovery thread won't free a ndlp while another discovery thread
5769 * is using it.
5770 */
James Smart329f9bc2007-04-25 09:53:01 -04005771struct lpfc_nodelist *
5772lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5773{
James Smarte47c9092008-02-08 18:49:26 -05005774 struct lpfc_hba *phba;
5775 unsigned long flags;
5776
James Smart98c9ea52007-10-27 13:37:33 -04005777 if (ndlp) {
5778 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5779 "node get: did:x%x flg:x%x refcnt:x%x",
5780 ndlp->nlp_DID, ndlp->nlp_flag,
5781 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005782 /* The check of ndlp usage to prevent incrementing the
5783 * ndlp reference count that is in the process of being
5784 * released.
5785 */
James Smarta257bf92009-04-06 18:48:10 -04005786 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005787 spin_lock_irqsave(&phba->ndlp_lock, flags);
5788 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5789 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5790 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5791 "0276 lpfc_nlp_get: ndlp:x%p "
5792 "usgmap:x%x refcnt:%d\n",
5793 (void *)ndlp, ndlp->nlp_usg_map,
5794 atomic_read(&ndlp->kref.refcount));
5795 return NULL;
5796 } else
5797 kref_get(&ndlp->kref);
5798 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005799 }
James Smart329f9bc2007-04-25 09:53:01 -04005800 return ndlp;
5801}
5802
James Smart98c9ea52007-10-27 13:37:33 -04005803/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005804 * count goes to 0, this indicates the the associated nodelist should be
5805 * freed. Returning 1 indicates the ndlp resource has been released; on the
5806 * other hand, returning 0 indicates the ndlp resource has not been released
5807 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005808 */
James Smart329f9bc2007-04-25 09:53:01 -04005809int
5810lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5811{
James Smarte47c9092008-02-08 18:49:26 -05005812 struct lpfc_hba *phba;
5813 unsigned long flags;
5814
5815 if (!ndlp)
5816 return 1;
5817
5818 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5819 "node put: did:x%x flg:x%x refcnt:x%x",
5820 ndlp->nlp_DID, ndlp->nlp_flag,
5821 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005822 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005823 spin_lock_irqsave(&phba->ndlp_lock, flags);
5824 /* Check the ndlp memory free acknowledge flag to avoid the
5825 * possible race condition that kref_put got invoked again
5826 * after previous one has done ndlp memory free.
5827 */
5828 if (NLP_CHK_FREE_ACK(ndlp)) {
5829 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5830 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5831 "0274 lpfc_nlp_put: ndlp:x%p "
5832 "usgmap:x%x refcnt:%d\n",
5833 (void *)ndlp, ndlp->nlp_usg_map,
5834 atomic_read(&ndlp->kref.refcount));
5835 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005836 }
James Smarte47c9092008-02-08 18:49:26 -05005837 /* Check the ndlp inactivate log flag to avoid the possible
5838 * race condition that kref_put got invoked again after ndlp
5839 * is already in inactivating state.
5840 */
5841 if (NLP_CHK_IACT_REQ(ndlp)) {
5842 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5843 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5844 "0275 lpfc_nlp_put: ndlp:x%p "
5845 "usgmap:x%x refcnt:%d\n",
5846 (void *)ndlp, ndlp->nlp_usg_map,
5847 atomic_read(&ndlp->kref.refcount));
5848 return 1;
5849 }
5850 /* For last put, mark the ndlp usage flags to make sure no
5851 * other kref_get and kref_put on the same ndlp shall get
5852 * in between the process when the final kref_put has been
5853 * invoked on this ndlp.
5854 */
5855 if (atomic_read(&ndlp->kref.refcount) == 1) {
5856 /* Indicate ndlp is put to inactive state. */
5857 NLP_SET_IACT_REQ(ndlp);
5858 /* Acknowledge ndlp memory free has been seen. */
5859 if (NLP_CHK_FREE_REQ(ndlp))
5860 NLP_SET_FREE_ACK(ndlp);
5861 }
5862 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5863 /* Note, the kref_put returns 1 when decrementing a reference
5864 * count that was 1, it invokes the release callback function,
5865 * but it still left the reference count as 1 (not actually
5866 * performs the last decrementation). Otherwise, it actually
5867 * decrements the reference count and returns 0.
5868 */
5869 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005870}
James Smart98c9ea52007-10-27 13:37:33 -04005871
5872/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005873 * by any other discovery thread. This routine returns 1 if the
5874 * ndlp has been freed. A return value of 0 indicates the ndlp is
5875 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005876 */
5877int
5878lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5879{
5880 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5881 "node not used: did:x%x flg:x%x refcnt:x%x",
5882 ndlp->nlp_DID, ndlp->nlp_flag,
5883 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005884 if (atomic_read(&ndlp->kref.refcount) == 1)
5885 if (lpfc_nlp_put(ndlp))
5886 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005887 return 0;
5888}
James Smart6fb120a2009-05-22 14:52:59 -04005889
5890/**
5891 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5892 * @phba: Pointer to hba context object.
5893 *
5894 * This function iterate through all FC nodes associated
5895 * will all vports to check if there is any node with
5896 * fc_rports associated with it. If there is an fc_rport
5897 * associated with the node, then the node is either in
5898 * discovered state or its devloss_timer is pending.
5899 */
5900static int
5901lpfc_fcf_inuse(struct lpfc_hba *phba)
5902{
5903 struct lpfc_vport **vports;
5904 int i, ret = 0;
5905 struct lpfc_nodelist *ndlp;
5906 struct Scsi_Host *shost;
5907
5908 vports = lpfc_create_vport_work_array(phba);
5909
James Smart63e801c2010-11-20 23:14:19 -05005910 /* If driver cannot allocate memory, indicate fcf is in use */
5911 if (!vports)
5912 return 1;
5913
James Smart6fb120a2009-05-22 14:52:59 -04005914 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5915 shost = lpfc_shost_from_vport(vports[i]);
5916 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005917 /*
5918 * IF the CVL_RCVD bit is not set then we have sent the
5919 * flogi.
5920 * If dev_loss fires while we are waiting we do not want to
5921 * unreg the fcf.
5922 */
5923 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5924 spin_unlock_irq(shost->host_lock);
5925 ret = 1;
5926 goto out;
5927 }
James Smart6fb120a2009-05-22 14:52:59 -04005928 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5929 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5930 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5931 ret = 1;
5932 spin_unlock_irq(shost->host_lock);
5933 goto out;
James Smart80c17842012-03-01 22:35:45 -05005934 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5935 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005936 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005937 "2624 RPI %x DID %x flag %x "
5938 "still logged in\n",
5939 ndlp->nlp_rpi, ndlp->nlp_DID,
5940 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005941 }
5942 }
5943 spin_unlock_irq(shost->host_lock);
5944 }
5945out:
5946 lpfc_destroy_vport_work_array(phba, vports);
5947 return ret;
5948}
5949
5950/**
5951 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5952 * @phba: Pointer to hba context object.
5953 * @mboxq: Pointer to mailbox object.
5954 *
5955 * This function frees memory associated with the mailbox command.
5956 */
James Smart1b511972011-12-13 13:23:09 -05005957void
James Smart6fb120a2009-05-22 14:52:59 -04005958lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5959{
5960 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005961 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005962
5963 if (mboxq->u.mb.mbxStatus) {
5964 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5965 "2555 UNREG_VFI mbxStatus error x%x "
5966 "HBA state x%x\n",
5967 mboxq->u.mb.mbxStatus, vport->port_state);
5968 }
James Smart38b92ef2010-08-04 16:11:39 -04005969 spin_lock_irq(shost->host_lock);
5970 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5971 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005972 mempool_free(mboxq, phba->mbox_mem_pool);
5973 return;
5974}
5975
5976/**
5977 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5978 * @phba: Pointer to hba context object.
5979 * @mboxq: Pointer to mailbox object.
5980 *
5981 * This function frees memory associated with the mailbox command.
5982 */
5983static void
5984lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5985{
5986 struct lpfc_vport *vport = mboxq->vport;
5987
5988 if (mboxq->u.mb.mbxStatus) {
5989 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5990 "2550 UNREG_FCFI mbxStatus error x%x "
5991 "HBA state x%x\n",
5992 mboxq->u.mb.mbxStatus, vport->port_state);
5993 }
5994 mempool_free(mboxq, phba->mbox_mem_pool);
5995 return;
5996}
5997
5998/**
James Smartecfd03c2010-02-12 14:41:27 -05005999 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006000 * @phba: Pointer to hba context object.
6001 *
James Smartecfd03c2010-02-12 14:41:27 -05006002 * This function prepare the HBA for unregistering the currently registered
6003 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6004 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006005 */
James Smartecfd03c2010-02-12 14:41:27 -05006006int
6007lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006008{
James Smart6fb120a2009-05-22 14:52:59 -04006009 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006010 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006011 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006012 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006013
James Smartecfd03c2010-02-12 14:41:27 -05006014 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006015 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006016 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006017
James Smart4d9ab992009-10-02 15:16:39 -04006018 /* At this point, all discovery is aborted */
6019 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006020
6021 /* Unregister VPIs */
6022 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006023 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006024 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006025 /* Stop FLOGI/FDISC retries */
6026 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6027 if (ndlp)
6028 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006029 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006030 if (phba->sli_rev == LPFC_SLI_REV4)
6031 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006032 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006033 shost = lpfc_shost_from_vport(vports[i]);
6034 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006035 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006036 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006037 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006038 }
6039 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006040 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6041 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6042 if (ndlp)
6043 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6044 lpfc_cleanup_pending_mbox(phba->pport);
6045 if (phba->sli_rev == LPFC_SLI_REV4)
6046 lpfc_sli4_unreg_all_rpis(phba->pport);
6047 lpfc_mbx_unreg_vpi(phba->pport);
6048 shost = lpfc_shost_from_vport(phba->pport);
6049 spin_lock_irq(shost->host_lock);
6050 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6051 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6052 spin_unlock_irq(shost->host_lock);
6053 }
James Smart6fb120a2009-05-22 14:52:59 -04006054
James Smart695a8142010-01-26 23:08:03 -05006055 /* Cleanup any outstanding ELS commands */
6056 lpfc_els_flush_all_cmd(phba);
6057
James Smart1b511972011-12-13 13:23:09 -05006058 /* Unregister the physical port VFI */
6059 rc = lpfc_issue_unreg_vfi(phba->pport);
6060 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006061}
6062
6063/**
6064 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6065 * @phba: Pointer to hba context object.
6066 *
6067 * This function issues synchronous unregister FCF mailbox command to HBA to
6068 * unregister the currently registered FCF record. The driver does not reset
6069 * the driver FCF usage state flags.
6070 *
6071 * Return 0 if successfully issued, none-zero otherwise.
6072 */
6073int
6074lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6075{
6076 LPFC_MBOXQ_t *mbox;
6077 int rc;
6078
James Smart6fb120a2009-05-22 14:52:59 -04006079 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6080 if (!mbox) {
6081 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006082 "2551 UNREG_FCFI mbox allocation failed"
6083 "HBA state x%x\n", phba->pport->port_state);
6084 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006085 }
James Smart6fb120a2009-05-22 14:52:59 -04006086 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6087 mbox->vport = phba->pport;
6088 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6089 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6090
6091 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6093 "2552 Unregister FCFI command failed rc x%x "
6094 "HBA state x%x\n",
6095 rc, phba->pport->port_state);
6096 return -EINVAL;
6097 }
6098 return 0;
6099}
6100
6101/**
6102 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6103 * @phba: Pointer to hba context object.
6104 *
6105 * This function unregisters the currently reigstered FCF. This function
6106 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6107 */
6108void
6109lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6110{
6111 int rc;
6112
6113 /* Preparation for unregistering fcf */
6114 rc = lpfc_unregister_fcf_prep(phba);
6115 if (rc) {
6116 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6117 "2748 Failed to prepare for unregistering "
6118 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006119 return;
6120 }
6121
James Smartecfd03c2010-02-12 14:41:27 -05006122 /* Now, unregister FCF record and reset HBA FCF state */
6123 rc = lpfc_sli4_unregister_fcf(phba);
6124 if (rc)
6125 return;
6126 /* Reset HBA FCF states after successful unregister FCF */
6127 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006128 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006129
6130 /*
6131 * If driver is not unloading, check if there is any other
6132 * FCF record that can be used for discovery.
6133 */
6134 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006135 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006136 return;
6137
James Smart0c9ab6f2010-02-26 14:15:57 -05006138 /* This is considered as the initial FCF discovery scan */
6139 spin_lock_irq(&phba->hbalock);
6140 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6141 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006142
6143 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006144 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006145
James Smart0c9ab6f2010-02-26 14:15:57 -05006146 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006147
James Smart0c9ab6f2010-02-26 14:15:57 -05006148 if (rc) {
6149 spin_lock_irq(&phba->hbalock);
6150 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6151 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006152 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006153 "2553 lpfc_unregister_unused_fcf failed "
6154 "to read FCF record HBA state x%x\n",
6155 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006156 }
James Smartecfd03c2010-02-12 14:41:27 -05006157}
6158
6159/**
6160 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6161 * @phba: Pointer to hba context object.
6162 *
6163 * This function just unregisters the currently reigstered FCF. It does not
6164 * try to find another FCF for discovery.
6165 */
6166void
6167lpfc_unregister_fcf(struct lpfc_hba *phba)
6168{
6169 int rc;
6170
6171 /* Preparation for unregistering fcf */
6172 rc = lpfc_unregister_fcf_prep(phba);
6173 if (rc) {
6174 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6175 "2749 Failed to prepare for unregistering "
6176 "HBA's FCF record: rc=%d\n", rc);
6177 return;
6178 }
6179
6180 /* Now, unregister FCF record and reset HBA FCF state */
6181 rc = lpfc_sli4_unregister_fcf(phba);
6182 if (rc)
6183 return;
6184 /* Set proper HBA FCF states after successful unregister FCF */
6185 spin_lock_irq(&phba->hbalock);
6186 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6187 spin_unlock_irq(&phba->hbalock);
6188}
6189
6190/**
6191 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6192 * @phba: Pointer to hba context object.
6193 *
6194 * This function check if there are any connected remote port for the FCF and
6195 * if all the devices are disconnected, this function unregister FCFI.
6196 * This function also tries to use another FCF for discovery.
6197 */
6198void
6199lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6200{
6201 /*
James Smart3804dc82010-07-14 15:31:37 -04006202 * If HBA is not running in FIP mode, if HBA does not support
6203 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6204 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006205 */
6206 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006207 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006208 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006209 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006210 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006211 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006212 spin_unlock_irq(&phba->hbalock);
6213 return;
6214 }
6215 spin_unlock_irq(&phba->hbalock);
6216
6217 if (lpfc_fcf_inuse(phba))
6218 return;
6219
6220 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006221}
6222
6223/**
6224 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6225 * @phba: Pointer to hba context object.
6226 * @buff: Buffer containing the FCF connection table as in the config
6227 * region.
6228 * This function create driver data structure for the FCF connection
6229 * record table read from config region 23.
6230 */
6231static void
6232lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6233 uint8_t *buff)
6234{
6235 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6236 struct lpfc_fcf_conn_hdr *conn_hdr;
6237 struct lpfc_fcf_conn_rec *conn_rec;
6238 uint32_t record_count;
6239 int i;
6240
6241 /* Free the current connect table */
6242 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006243 &phba->fcf_conn_rec_list, list) {
6244 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006245 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006246 }
James Smart6fb120a2009-05-22 14:52:59 -04006247
6248 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6249 record_count = conn_hdr->length * sizeof(uint32_t)/
6250 sizeof(struct lpfc_fcf_conn_rec);
6251
6252 conn_rec = (struct lpfc_fcf_conn_rec *)
6253 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6254
6255 for (i = 0; i < record_count; i++) {
6256 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6257 continue;
6258 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6259 GFP_KERNEL);
6260 if (!conn_entry) {
6261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6262 "2566 Failed to allocate connection"
6263 " table entry\n");
6264 return;
6265 }
6266
6267 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6268 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006269 list_add_tail(&conn_entry->list,
6270 &phba->fcf_conn_rec_list);
6271 }
James Smartdf0d0852013-05-31 17:05:08 -04006272
6273 if (!list_empty(&phba->fcf_conn_rec_list)) {
6274 i = 0;
6275 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6276 list) {
6277 conn_rec = &conn_entry->conn_rec;
6278 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6279 "3345 FCF connection list rec[%02d]: "
6280 "flags:x%04x, vtag:x%04x, "
6281 "fabric_name:x%02x:%02x:%02x:%02x:"
6282 "%02x:%02x:%02x:%02x, "
6283 "switch_name:x%02x:%02x:%02x:%02x:"
6284 "%02x:%02x:%02x:%02x\n", i++,
6285 conn_rec->flags, conn_rec->vlan_tag,
6286 conn_rec->fabric_name[0],
6287 conn_rec->fabric_name[1],
6288 conn_rec->fabric_name[2],
6289 conn_rec->fabric_name[3],
6290 conn_rec->fabric_name[4],
6291 conn_rec->fabric_name[5],
6292 conn_rec->fabric_name[6],
6293 conn_rec->fabric_name[7],
6294 conn_rec->switch_name[0],
6295 conn_rec->switch_name[1],
6296 conn_rec->switch_name[2],
6297 conn_rec->switch_name[3],
6298 conn_rec->switch_name[4],
6299 conn_rec->switch_name[5],
6300 conn_rec->switch_name[6],
6301 conn_rec->switch_name[7]);
6302 }
6303 }
James Smart6fb120a2009-05-22 14:52:59 -04006304}
6305
6306/**
6307 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6308 * @phba: Pointer to hba context object.
6309 * @buff: Buffer containing the FCoE parameter data structure.
6310 *
6311 * This function update driver data structure with config
6312 * parameters read from config region 23.
6313 */
6314static void
6315lpfc_read_fcoe_param(struct lpfc_hba *phba,
6316 uint8_t *buff)
6317{
6318 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6319 struct lpfc_fcoe_params *fcoe_param;
6320
6321 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6322 buff;
6323 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006324 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006325
6326 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6327 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6328 return;
6329
James Smart6fb120a2009-05-22 14:52:59 -04006330 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6331 phba->valid_vlan = 1;
6332 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6333 0xFFF;
6334 }
6335
6336 phba->fc_map[0] = fcoe_param->fc_map[0];
6337 phba->fc_map[1] = fcoe_param->fc_map[1];
6338 phba->fc_map[2] = fcoe_param->fc_map[2];
6339 return;
6340}
6341
6342/**
6343 * lpfc_get_rec_conf23 - Get a record type in config region data.
6344 * @buff: Buffer containing config region 23 data.
6345 * @size: Size of the data buffer.
6346 * @rec_type: Record type to be searched.
6347 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006348 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006349 * of the record specified by record_type. If record found, this
6350 * function return pointer to the record else return NULL.
6351 */
6352static uint8_t *
6353lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6354{
6355 uint32_t offset = 0, rec_length;
6356
6357 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6358 (size < sizeof(uint32_t)))
6359 return NULL;
6360
6361 rec_length = buff[offset + 1];
6362
6363 /*
6364 * One TLV record has one word header and number of data words
6365 * specified in the rec_length field of the record header.
6366 */
6367 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6368 <= size) {
6369 if (buff[offset] == rec_type)
6370 return &buff[offset];
6371
6372 if (buff[offset] == LPFC_REGION23_LAST_REC)
6373 return NULL;
6374
6375 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6376 rec_length = buff[offset + 1];
6377 }
6378 return NULL;
6379}
6380
6381/**
6382 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6383 * @phba: Pointer to lpfc_hba data structure.
6384 * @buff: Buffer containing config region 23 data.
6385 * @size: Size of the data buffer.
6386 *
Stefan Weileef35c22010-08-06 21:11:15 +02006387 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006388 * populate driver data structure with the parameters.
6389 */
6390void
6391lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6392 uint8_t *buff,
6393 uint32_t size)
6394{
James Smarteb016562014-09-03 12:58:06 -04006395 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006396 uint8_t *rec_ptr;
6397
6398 /*
6399 * If data size is less than 2 words signature and version cannot be
6400 * verified.
6401 */
6402 if (size < 2*sizeof(uint32_t))
6403 return;
6404
6405 /* Check the region signature first */
6406 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6408 "2567 Config region 23 has bad signature\n");
6409 return;
6410 }
6411
6412 offset += 4;
6413
6414 /* Check the data structure version */
6415 if (buff[offset] != LPFC_REGION23_VERSION) {
6416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6417 "2568 Config region 23 has bad version\n");
6418 return;
6419 }
6420 offset += 4;
6421
James Smart6fb120a2009-05-22 14:52:59 -04006422 /* Read FCoE param record */
6423 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6424 size - offset, FCOE_PARAM_TYPE);
6425 if (rec_ptr)
6426 lpfc_read_fcoe_param(phba, rec_ptr);
6427
6428 /* Read FCF connection table */
6429 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6430 size - offset, FCOE_CONN_TBL_TYPE);
6431 if (rec_ptr)
6432 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6433
6434}