blob: 667d124e3608366a0da3fcd6e46c6f8fded0fb87 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartb857ff32014-04-04 13:52:50 -04004 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
James Smarta93ff372010-10-22 11:06:08 -040023#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050025#include <linux/pci.h>
26#include <linux/kthread.h>
27#include <linux/interrupt.h>
28
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport_fc.h>
33
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040036#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_disc.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050045#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050046
47/* AlpaArray for assignment of scsid for scan-down and bind_method */
48static uint8_t lpfcAlpaArray[] = {
49 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
50 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
51 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
52 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
53 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
54 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
55 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
56 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
57 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
58 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
59 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
60 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
61 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
62};
63
James Smart2e0fef82007-06-17 19:56:36 -050064static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010065static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040066static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
James Smarta93ff372010-10-22 11:06:08 -040067static int lpfc_fcf_inuse(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050068
James Smartc01f3202006-08-18 17:47:08 -040069void
70lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050071{
James Smartc01f3202006-08-18 17:47:08 -040072 struct lpfc_rport_data *rdata;
73 struct lpfc_nodelist * ndlp;
74 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050075
James Smartc01f3202006-08-18 17:47:08 -040076 rdata = rport->dd_data;
77 ndlp = rdata->pnode;
78
James Smart58da1ff2008-04-07 10:15:56 -040079 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040080 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
81 printk(KERN_ERR "Cannot find remote node"
82 " to terminate I/O Data x%x\n",
83 rport->port_id);
dea31012005-04-17 16:05:31 -050084 return;
85 }
86
James Smarta257bf92009-04-06 18:48:10 -040087 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050088
James Smart858c9f62007-06-17 19:56:39 -050089 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
90 "rport terminate: sid:x%x did:x%x flg:x%x",
91 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
92
James Smartc01f3202006-08-18 17:47:08 -040093 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040094 lpfc_sli_abort_iocb(ndlp->vport,
95 &phba->sli.ring[phba->sli.fcp_ring],
96 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040097 }
James Smartc01f3202006-08-18 17:47:08 -040098}
99
100/*
101 * This function will be called when dev_loss_tmo fire.
102 */
103void
104lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
105{
106 struct lpfc_rport_data *rdata;
107 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500108 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500109 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500110 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400111 int put_node;
112 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400113
114 rdata = rport->dd_data;
115 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400116 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400117 return;
James Smartc01f3202006-08-18 17:47:08 -0400118
James Smart858c9f62007-06-17 19:56:39 -0500119 vport = ndlp->vport;
120 phba = vport->phba;
121
122 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
123 "rport devlosscb: sid:x%x did:x%x flg:x%x",
124 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
125
James Smart34f5ad82012-08-03 12:35:03 -0400126 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
127 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
128 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
129
James Smarta8adb832007-10-27 13:37:53 -0400130 /* Don't defer this if we are in the process of deleting the vport
131 * or unloading the driver. The unload will cleanup the node
132 * appropriately we just need to cleanup the ndlp rport info here.
133 */
134 if (vport->load_flag & FC_UNLOADING) {
135 put_node = rdata->pnode != NULL;
136 put_rport = ndlp->rport != NULL;
137 rdata->pnode = NULL;
138 ndlp->rport = NULL;
139 if (put_node)
140 lpfc_nlp_put(ndlp);
141 if (put_rport)
142 put_device(&rport->dev);
143 return;
144 }
145
146 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
147 return;
148
James Smart34f5ad82012-08-03 12:35:03 -0400149 if (ndlp->nlp_type & NLP_FABRIC) {
150
151 /* If the WWPN of the rport and ndlp don't match, ignore it */
152 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn)) {
153 put_device(&rport->dev);
154 return;
155 }
156 }
157
James Smart858c9f62007-06-17 19:56:39 -0500158 evtp = &ndlp->dev_loss_evt;
159
160 if (!list_empty(&evtp->evt_listp))
161 return;
162
James Smarta62a4352013-04-17 20:18:19 -0400163 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
164
James Smart858c9f62007-06-17 19:56:39 -0500165 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500166 /* We need to hold the node by incrementing the reference
167 * count until this queued work is done
168 */
James Smart5e9d9b82008-06-14 22:52:53 -0400169 if (evtp->evt_arg1) {
170 evtp->evt = LPFC_EVT_DEV_LOSS;
171 list_add_tail(&evtp->evt_listp, &phba->work_list);
172 lpfc_worker_wake_up(phba);
173 }
James Smart858c9f62007-06-17 19:56:39 -0500174 spin_unlock_irq(&phba->hbalock);
175
James Smart858c9f62007-06-17 19:56:39 -0500176 return;
177}
178
James Smarta93ff372010-10-22 11:06:08 -0400179/**
180 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
181 * @ndlp: Pointer to remote node object.
182 *
183 * This function is called from the worker thread when devloss timeout timer
184 * expires. For SLI4 host, this routine shall return 1 when at lease one
185 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
186 * routine shall return 0 when there is no remote node is still in use of FCF
187 * when devloss timeout happened to this @ndlp.
188 **/
189static int
James Smart858c9f62007-06-17 19:56:39 -0500190lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
191{
192 struct lpfc_rport_data *rdata;
193 struct fc_rport *rport;
194 struct lpfc_vport *vport;
195 struct lpfc_hba *phba;
196 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400197 int put_node;
198 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500199 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400200 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500201
202 rport = ndlp->rport;
203
204 if (!rport)
James Smarta93ff372010-10-22 11:06:08 -0400205 return fcf_inuse;
James Smart858c9f62007-06-17 19:56:39 -0500206
207 rdata = rport->dd_data;
208 name = (uint8_t *) &ndlp->nlp_portname;
209 vport = ndlp->vport;
210 phba = vport->phba;
211
James Smarta93ff372010-10-22 11:06:08 -0400212 if (phba->sli_rev == LPFC_SLI_REV4)
213 fcf_inuse = lpfc_fcf_inuse(phba);
214
James Smart858c9f62007-06-17 19:56:39 -0500215 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
216 "rport devlosstmo:did:x%x type:x%x id:x%x",
217 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
218
James Smart34f5ad82012-08-03 12:35:03 -0400219 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
220 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
221 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
222
James Smarta8adb832007-10-27 13:37:53 -0400223 /* Don't defer this if we are in the process of deleting the vport
224 * or unloading the driver. The unload will cleanup the node
225 * appropriately we just need to cleanup the ndlp rport info here.
226 */
227 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500228 if (ndlp->nlp_sid != NLP_NO_SID) {
229 /* flush the target */
230 lpfc_sli_abort_iocb(vport,
231 &phba->sli.ring[phba->sli.fcp_ring],
232 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
233 }
James Smarta8adb832007-10-27 13:37:53 -0400234 put_node = rdata->pnode != NULL;
235 put_rport = ndlp->rport != NULL;
236 rdata->pnode = NULL;
237 ndlp->rport = NULL;
238 if (put_node)
239 lpfc_nlp_put(ndlp);
240 if (put_rport)
241 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400242 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400243 }
244
James Smartd7c255b2008-08-24 21:50:00 -0400245 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
246 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
247 "0284 Devloss timeout Ignored on "
248 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
249 "NPort x%x\n",
250 *name, *(name+1), *(name+2), *(name+3),
251 *(name+4), *(name+5), *(name+6), *(name+7),
252 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400253 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400254 }
James Smart858c9f62007-06-17 19:56:39 -0500255
James Smart92d7f7b2007-06-17 19:56:38 -0500256 if (ndlp->nlp_type & NLP_FABRIC) {
257 /* We will clean up these Nodes in linkup */
258 put_node = rdata->pnode != NULL;
259 put_rport = ndlp->rport != NULL;
260 rdata->pnode = NULL;
261 ndlp->rport = NULL;
262 if (put_node)
263 lpfc_nlp_put(ndlp);
264 if (put_rport)
265 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400266 return fcf_inuse;
James Smart92d7f7b2007-06-17 19:56:38 -0500267 }
James Smart82085712007-04-25 09:52:41 -0400268
dea31012005-04-17 16:05:31 -0500269 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400270 warn_on = 1;
dea31012005-04-17 16:05:31 -0500271 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400272 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
273 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500274 }
James Smartc01f3202006-08-18 17:47:08 -0400275
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400276 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400277 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
278 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400279 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
280 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400281 *name, *(name+1), *(name+2), *(name+3),
282 *(name+4), *(name+5), *(name+6), *(name+7),
283 ndlp->nlp_DID, ndlp->nlp_flag,
284 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400285 } else {
James Smarte8b62012007-08-02 11:10:09 -0400286 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
287 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400288 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
289 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400290 *name, *(name+1), *(name+2), *(name+3),
291 *(name+4), *(name+5), *(name+6), *(name+7),
292 ndlp->nlp_DID, ndlp->nlp_flag,
293 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400294 }
295
James Smart87af33f2007-10-27 13:37:43 -0400296 put_node = rdata->pnode != NULL;
297 put_rport = ndlp->rport != NULL;
298 rdata->pnode = NULL;
299 ndlp->rport = NULL;
300 if (put_node)
301 lpfc_nlp_put(ndlp);
302 if (put_rport)
303 put_device(&rport->dev);
304
James Smart2e0fef82007-06-17 19:56:36 -0500305 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400306 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400307 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400308 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400309 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
310 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500311 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400312
James Smarta93ff372010-10-22 11:06:08 -0400313 return fcf_inuse;
314}
315
316/**
317 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
318 * @phba: Pointer to hba context object.
319 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
320 * @nlp_did: remote node identifer with devloss timeout.
321 *
322 * This function is called from the worker thread after invoking devloss
323 * timeout handler and releasing the reference count for the ndlp with
324 * which the devloss timeout was handled for SLI4 host. For the devloss
325 * timeout of the last remote node which had been in use of FCF, when this
326 * routine is invoked, it shall be guaranteed that none of the remote are
327 * in-use of FCF. When devloss timeout to the last remote using the FCF,
328 * if the FIP engine is neither in FCF table scan process nor roundrobin
329 * failover process, the in-use FCF shall be unregistered. If the FIP
330 * engine is in FCF discovery process, the devloss timeout state shall
331 * be set for either the FCF table scan process or roundrobin failover
332 * process to unregister the in-use FCF.
333 **/
334static void
335lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
336 uint32_t nlp_did)
337{
338 /* If devloss timeout happened to a remote node when FCF had no
339 * longer been in-use, do nothing.
340 */
341 if (!fcf_inuse)
342 return;
343
344 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
345 spin_lock_irq(&phba->hbalock);
346 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
347 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
348 spin_unlock_irq(&phba->hbalock);
349 return;
350 }
351 phba->hba_flag |= HBA_DEVLOSS_TMO;
352 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
353 "2847 Last remote node (x%x) using "
354 "FCF devloss tmo\n", nlp_did);
355 }
356 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
357 spin_unlock_irq(&phba->hbalock);
358 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
359 "2868 Devloss tmo to FCF rediscovery "
360 "in progress\n");
361 return;
362 }
363 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
364 spin_unlock_irq(&phba->hbalock);
365 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
366 "2869 Devloss tmo to idle FIP engine, "
367 "unreg in-use FCF and rescan.\n");
368 /* Unregister in-use FCF and rescan */
369 lpfc_unregister_fcf_rescan(phba);
370 return;
371 }
372 spin_unlock_irq(&phba->hbalock);
373 if (phba->hba_flag & FCF_TS_INPROG)
374 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
375 "2870 FCF table scan in progress\n");
376 if (phba->hba_flag & FCF_RR_INPROG)
377 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
378 "2871 FLOGI roundrobin FCF failover "
379 "in progress\n");
380 }
James Smart6fb120a2009-05-22 14:52:59 -0400381 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500382}
James Smartc01f3202006-08-18 17:47:08 -0400383
James Smartea2151b2008-09-07 11:52:10 -0400384/**
James Smart3621a712009-04-06 18:47:14 -0400385 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400386 * @phba: Pointer to hba context object.
387 *
388 * This function is called from the functions which need to post
389 * events from interrupt context. This function allocates data
390 * structure required for posting event. It also keeps track of
391 * number of events pending and prevent event storm when there are
392 * too many events.
393 **/
394struct lpfc_fast_path_event *
395lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
396 struct lpfc_fast_path_event *ret;
397
398 /* If there are lot of fast event do not exhaust memory due to this */
399 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
400 return NULL;
401
402 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
403 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400404 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400405 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400406 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
407 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
408 }
James Smartea2151b2008-09-07 11:52:10 -0400409 return ret;
410}
411
412/**
James Smart3621a712009-04-06 18:47:14 -0400413 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400414 * @phba: Pointer to hba context object.
415 * @evt: Event object which need to be freed.
416 *
417 * This function frees the data structure required for posting
418 * events.
419 **/
420void
421lpfc_free_fast_evt(struct lpfc_hba *phba,
422 struct lpfc_fast_path_event *evt) {
423
424 atomic_dec(&phba->fast_event_count);
425 kfree(evt);
426}
427
428/**
James Smart3621a712009-04-06 18:47:14 -0400429 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400430 * @phba: Pointer to hba context object.
431 * @evtp: Event data structure.
432 *
433 * This function is called from worker thread, when the interrupt
434 * context need to post an event. This function posts the event
435 * to fc transport netlink interface.
436 **/
437static void
438lpfc_send_fastpath_evt(struct lpfc_hba *phba,
439 struct lpfc_work_evt *evtp)
440{
441 unsigned long evt_category, evt_sub_category;
442 struct lpfc_fast_path_event *fast_evt_data;
443 char *evt_data;
444 uint32_t evt_data_size;
445 struct Scsi_Host *shost;
446
447 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
448 work_evt);
449
450 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
451 evt_sub_category = (unsigned long) fast_evt_data->un.
452 fabric_evt.subcategory;
453 shost = lpfc_shost_from_vport(fast_evt_data->vport);
454 if (evt_category == FC_REG_FABRIC_EVENT) {
455 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
456 evt_data = (char *) &fast_evt_data->un.read_check_error;
457 evt_data_size = sizeof(fast_evt_data->un.
458 read_check_error);
459 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500460 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400461 evt_data = (char *) &fast_evt_data->un.fabric_evt;
462 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
463 } else {
464 lpfc_free_fast_evt(phba, fast_evt_data);
465 return;
466 }
467 } else if (evt_category == FC_REG_SCSI_EVENT) {
468 switch (evt_sub_category) {
469 case LPFC_EVENT_QFULL:
470 case LPFC_EVENT_DEVBSY:
471 evt_data = (char *) &fast_evt_data->un.scsi_evt;
472 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
473 break;
474 case LPFC_EVENT_CHECK_COND:
475 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
476 evt_data_size = sizeof(fast_evt_data->un.
477 check_cond_evt);
478 break;
479 case LPFC_EVENT_VARQUEDEPTH:
480 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
481 evt_data_size = sizeof(fast_evt_data->un.
482 queue_depth_evt);
483 break;
484 default:
485 lpfc_free_fast_evt(phba, fast_evt_data);
486 return;
487 }
488 } else {
489 lpfc_free_fast_evt(phba, fast_evt_data);
490 return;
491 }
492
493 fc_host_post_vendor_event(shost,
494 fc_get_event_number(),
495 evt_data_size,
496 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500497 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400498
499 lpfc_free_fast_evt(phba, fast_evt_data);
500 return;
501}
502
dea31012005-04-17 16:05:31 -0500503static void
James Smart2e0fef82007-06-17 19:56:36 -0500504lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500505{
506 struct lpfc_work_evt *evtp = NULL;
507 struct lpfc_nodelist *ndlp;
508 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400509 int fcf_inuse;
510 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500511
James Smart2e0fef82007-06-17 19:56:36 -0500512 spin_lock_irq(&phba->hbalock);
513 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500514 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
515 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500516 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500517 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500518 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500519 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500520 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500521 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500522 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500523 /* decrement the node reference count held
524 * for this queued work
525 */
526 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500527 break;
James Smart858c9f62007-06-17 19:56:39 -0500528 case LPFC_EVT_DEV_LOSS:
529 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400530 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500531 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500532 /* decrement the node reference count held for
533 * this queued work
534 */
James Smarta93ff372010-10-22 11:06:08 -0400535 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500536 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400537 if (phba->sli_rev == LPFC_SLI_REV4)
538 lpfc_sli4_post_dev_loss_tmo_handler(phba,
539 fcf_inuse,
540 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500541 break;
dea31012005-04-17 16:05:31 -0500542 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500543 if (phba->link_state < LPFC_LINK_DOWN)
544 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500545 else
James Smart2e0fef82007-06-17 19:56:36 -0500546 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500547 complete((struct completion *)(evtp->evt_arg2));
548 break;
James Smart46fa3112007-04-25 09:51:45 -0400549 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500550 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400551 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400552 *(int *)(evtp->evt_arg1) = 0;
553 complete((struct completion *)(evtp->evt_arg2));
554 break;
555 case LPFC_EVT_OFFLINE:
556 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500557 lpfc_sli_brdrestart(phba);
558 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400559 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
560 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500561 complete((struct completion *)(evtp->evt_arg2));
562 break;
563 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400564 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500565 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500566 lpfc_sli_brdreset(phba);
567 lpfc_hba_down_post(phba);
568 *(int *)(evtp->evt_arg1) =
569 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400570 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500571 complete((struct completion *)(evtp->evt_arg2));
572 break;
573 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400574 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500575 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500576 = (phba->pport->stopped)
577 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400578 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500579 complete((struct completion *)(evtp->evt_arg2));
580 break;
James Smartea2151b2008-09-07 11:52:10 -0400581 case LPFC_EVT_FASTPATH_MGMT_EVT:
582 lpfc_send_fastpath_evt(phba, evtp);
583 free_evt = 0;
584 break;
James Smart78730cf2010-04-06 15:06:30 -0400585 case LPFC_EVT_RESET_HBA:
586 if (!(phba->pport->load_flag & FC_UNLOADING))
587 lpfc_reset_hba(phba);
588 break;
dea31012005-04-17 16:05:31 -0500589 }
590 if (free_evt)
591 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500592 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500593 }
James Smart2e0fef82007-06-17 19:56:36 -0500594 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500595
596}
597
James Smart311464e2007-08-02 11:10:37 -0400598static void
James Smart2e0fef82007-06-17 19:56:36 -0500599lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500600{
601 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500602 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400603 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400604 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400605 int i;
dea31012005-04-17 16:05:31 -0500606
James Smart2e0fef82007-06-17 19:56:36 -0500607 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500608 ha_copy = phba->work_ha;
609 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500610 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500611
James Smartda0436e2009-05-22 14:51:39 -0400612 /* First, try to post the next mailbox command to SLI4 device */
613 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
614 lpfc_sli4_post_async_mbox(phba);
615
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500616 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400617 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500618 lpfc_handle_eratt(phba);
619
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500620 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500621 lpfc_sli_handle_mb_event(phba);
622
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500623 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500624 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400625
James Smartda0436e2009-05-22 14:51:39 -0400626 /* Process SLI4 events */
627 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500628 if (phba->hba_flag & HBA_RRQ_ACTIVE)
629 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400630 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
631 lpfc_sli4_fcp_xri_abort_event_proc(phba);
632 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
633 lpfc_sli4_els_xri_abort_event_proc(phba);
634 if (phba->hba_flag & ASYNC_EVENT)
635 lpfc_sli4_async_event_proc(phba);
636 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
637 spin_lock_irq(&phba->hbalock);
638 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
639 spin_unlock_irq(&phba->hbalock);
640 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
641 }
James Smartecfd03c2010-02-12 14:41:27 -0500642 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
643 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400644 }
645
James Smart549e55c2007-08-02 11:09:51 -0400646 vports = lpfc_create_vport_work_array(phba);
647 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400648 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400649 /*
650 * We could have no vports in array if unloading, so if
651 * this happens then just use the pport
652 */
653 if (vports[i] == NULL && i == 0)
654 vport = phba->pport;
655 else
656 vport = vports[i];
657 if (vport == NULL)
658 break;
James Smart58da1ff2008-04-07 10:15:56 -0400659 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400660 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400661 vport->work_port_events &= ~work_port_events;
662 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400663 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400664 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400665 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400666 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400667 if (work_port_events & WORKER_HB_TMO)
668 lpfc_hb_timeout_handler(phba);
669 if (work_port_events & WORKER_MBOX_TMO)
670 lpfc_mbox_timeout_handler(phba);
671 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
672 lpfc_unblock_fabric_iocbs(phba);
673 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400674 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400675 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
676 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500677 if (work_port_events & WORKER_DELAYED_DISC_TMO)
678 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500679 }
James Smart09372822008-01-11 01:52:54 -0500680 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500681
James Smart858c9f62007-06-17 19:56:39 -0500682 pring = &phba->sli.ring[LPFC_ELS_RING];
683 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
684 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400685 if ((status & HA_RXMASK) ||
686 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400687 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400688 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500689 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400690 /* Set the lpfc data pending flag */
691 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500692 } else {
James Smart94661502013-03-01 16:37:29 -0500693 if (phba->link_state >= LPFC_LINK_UP) {
694 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
695 lpfc_sli_handle_slow_ring_event(phba, pring,
696 (status &
697 HA_RXMASK));
698 }
James Smart858c9f62007-06-17 19:56:39 -0500699 }
James Smart0e9bb8d2013-03-01 16:35:12 -0500700 if ((phba->sli_rev == LPFC_SLI_REV4) &
701 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400702 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500703 /*
704 * Turn on Ring interrupts
705 */
James Smart3772a992009-05-22 14:50:54 -0400706 if (phba->sli_rev <= LPFC_SLI_REV3) {
707 spin_lock_irq(&phba->hbalock);
708 control = readl(phba->HCregaddr);
709 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
710 lpfc_debugfs_slow_ring_trc(phba,
711 "WRK Enable ring: cntl:x%x hacopy:x%x",
712 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400713
James Smart3772a992009-05-22 14:50:54 -0400714 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
715 writel(control, phba->HCregaddr);
716 readl(phba->HCregaddr); /* flush */
717 } else {
718 lpfc_debugfs_slow_ring_trc(phba,
719 "WRK Ring ok: cntl:x%x hacopy:x%x",
720 control, ha_copy, 0);
721 }
722 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500723 }
724 }
James Smart2e0fef82007-06-17 19:56:36 -0500725 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500726}
727
dea31012005-04-17 16:05:31 -0500728int
729lpfc_do_work(void *p)
730{
731 struct lpfc_hba *phba = p;
732 int rc;
dea31012005-04-17 16:05:31 -0500733
Dongsheng Yang8698a742014-03-11 18:09:12 +0800734 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400735 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400736 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500737
James Smart3a55b532008-12-04 22:38:54 -0500738 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400739 /* wait and check worker queue activities */
740 rc = wait_event_interruptible(phba->work_waitq,
741 (test_and_clear_bit(LPFC_DATA_READY,
742 &phba->data_flags)
743 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500744 /* Signal wakeup shall terminate the worker thread */
745 if (rc) {
746 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
747 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500748 break;
James Smart3a55b532008-12-04 22:38:54 -0500749 }
dea31012005-04-17 16:05:31 -0500750
James Smart5e9d9b82008-06-14 22:52:53 -0400751 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500752 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500753 }
James Smart3a55b532008-12-04 22:38:54 -0500754 phba->worker_thread = NULL;
755 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
756 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500757 return 0;
758}
759
760/*
761 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300762 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500763 * embedding it in the IOCB.
764 */
765int
James Smart2e0fef82007-06-17 19:56:36 -0500766lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500767 uint32_t evt)
768{
769 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500770 unsigned long flags;
dea31012005-04-17 16:05:31 -0500771
772 /*
773 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
774 * be queued to worker thread for processing
775 */
James Smart92d7f7b2007-06-17 19:56:38 -0500776 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500777 if (!evtp)
778 return 0;
779
780 evtp->evt_arg1 = arg1;
781 evtp->evt_arg2 = arg2;
782 evtp->evt = evt;
783
James Smarted957682007-06-17 19:56:37 -0500784 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400785 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500786 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500787
James Smart5e9d9b82008-06-14 22:52:53 -0400788 lpfc_worker_wake_up(phba);
789
dea31012005-04-17 16:05:31 -0500790 return 1;
791}
792
James Smart92d7f7b2007-06-17 19:56:38 -0500793void
794lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
795{
James Smart09372822008-01-11 01:52:54 -0500796 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500797 struct lpfc_hba *phba = vport->phba;
798 struct lpfc_nodelist *ndlp, *next_ndlp;
799 int rc;
800
801 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500802 if (!NLP_CHK_NODE_ACT(ndlp))
803 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500804 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
805 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400806 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
807 ((vport->port_type == LPFC_NPIV_PORT) &&
808 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500809 lpfc_unreg_rpi(vport, ndlp);
810
811 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400812 if ((phba->sli_rev < LPFC_SLI_REV4) &&
813 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500814 continue;
815 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
816 remove
817 ? NLP_EVT_DEVICE_RM
818 : NLP_EVT_DEVICE_RECOVERY);
819 }
820 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400821 if (phba->sli_rev == LPFC_SLI_REV4)
822 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500823 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500824 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500825 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500826 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500827 }
828}
829
James Smart87af33f2007-10-27 13:37:43 -0400830void
James Smart98c9ea52007-10-27 13:37:33 -0400831lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500832{
James Smart695a8142010-01-26 23:08:03 -0500833 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
834
James Smart45ed1192009-10-02 15:17:02 -0400835 /* Cleanup any outstanding received buffers */
836 lpfc_cleanup_rcv_buffers(vport);
837
James Smart92d7f7b2007-06-17 19:56:38 -0500838 /* Cleanup any outstanding RSCN activity */
839 lpfc_els_flush_rscn(vport);
840
841 /* Cleanup any outstanding ELS commands */
842 lpfc_els_flush_cmd(vport);
843
844 lpfc_cleanup_rpis(vport, 0);
845
James Smart92d7f7b2007-06-17 19:56:38 -0500846 /* Turn off discovery timer if its running */
847 lpfc_can_disctmo(vport);
848}
849
James Smart3772a992009-05-22 14:50:54 -0400850void
James Smart98c9ea52007-10-27 13:37:33 -0400851lpfc_linkdown_port(struct lpfc_vport *vport)
852{
853 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
854
855 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
856
857 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
858 "Link Down: state:x%x rtry:x%x flg:x%x",
859 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
860
861 lpfc_port_link_failure(vport);
862
James Smart92494142011-02-16 12:39:44 -0500863 /* Stop delayed Nport discovery */
864 spin_lock_irq(shost->host_lock);
865 vport->fc_flag &= ~FC_DISC_DELAYED;
866 spin_unlock_irq(shost->host_lock);
867 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400868}
869
dea31012005-04-17 16:05:31 -0500870int
James Smart685f0bf2007-04-25 09:53:08 -0400871lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500872{
James Smart2e0fef82007-06-17 19:56:36 -0500873 struct lpfc_vport *vport = phba->pport;
874 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400875 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400876 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400877 int i;
dea31012005-04-17 16:05:31 -0500878
James Smart3163f722008-02-08 18:50:25 -0500879 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500880 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500881
882 /* Block all SCSI stack I/Os */
883 lpfc_scsi_dev_block(phba);
884
James Smart2e0fef82007-06-17 19:56:36 -0500885 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500886 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500887 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500888 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500889 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500890 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500891 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500892 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500893 }
James Smart549e55c2007-08-02 11:09:51 -0400894 vports = lpfc_create_vport_work_array(phba);
895 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400896 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400897 /* Issue a LINK DOWN event to all nodes */
898 lpfc_linkdown_port(vports[i]);
899 }
James Smart09372822008-01-11 01:52:54 -0500900 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500901 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500902 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
903 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400904 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500905 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500906 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400907 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500908 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500909 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500910 }
911 }
912
dea31012005-04-17 16:05:31 -0500913 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500914 if (phba->pport->fc_flag & FC_PT2PT) {
915 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500916 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
917 if (mb) {
dea31012005-04-17 16:05:31 -0500918 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500919 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500920 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400921 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500922 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500923 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500924 }
925 }
James Smart2e0fef82007-06-17 19:56:36 -0500926 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500927 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500928 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500929 }
James Smart2e0fef82007-06-17 19:56:36 -0500930
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500931 return 0;
dea31012005-04-17 16:05:31 -0500932}
933
James Smart92d7f7b2007-06-17 19:56:38 -0500934static void
935lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500936{
James Smart92d7f7b2007-06-17 19:56:38 -0500937 struct lpfc_nodelist *ndlp;
938
939 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500940 if (!NLP_CHK_NODE_ACT(ndlp))
941 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500942 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
943 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500944 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500945 /* On Linkup its safe to clean up the ndlp
946 * from Fabric connections.
947 */
James Smart92d7f7b2007-06-17 19:56:38 -0500948 if (ndlp->nlp_DID != Fabric_DID)
949 lpfc_unreg_rpi(vport, ndlp);
950 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
951 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500952 /* Fail outstanding IO now since device is
953 * marked for PLOGI.
954 */
James Smart92d7f7b2007-06-17 19:56:38 -0500955 lpfc_unreg_rpi(vport, ndlp);
956 }
957 }
958}
959
960static void
961lpfc_linkup_port(struct lpfc_vport *vport)
962{
963 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500964 struct lpfc_hba *phba = vport->phba;
965
966 if ((vport->load_flag & FC_UNLOADING) != 0)
967 return;
968
James Smart858c9f62007-06-17 19:56:39 -0500969 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
970 "Link Up: top:x%x speed:x%x flg:x%x",
971 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
972
James Smart92d7f7b2007-06-17 19:56:38 -0500973 /* If NPIV is not enabled, only bring the physical port up */
974 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
975 (vport != phba->pport))
976 return;
dea31012005-04-17 16:05:31 -0500977
James Smart2e0fef82007-06-17 19:56:36 -0500978 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400979
James Smart2e0fef82007-06-17 19:56:36 -0500980 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500981 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
982 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
983 vport->fc_flag |= FC_NDISC_ACTIVE;
984 vport->fc_ns_retry = 0;
985 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500986
James Smart92d7f7b2007-06-17 19:56:38 -0500987 if (vport->fc_flag & FC_LBIT)
988 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500989
James Smart92d7f7b2007-06-17 19:56:38 -0500990}
991
992static int
993lpfc_linkup(struct lpfc_hba *phba)
994{
James Smart549e55c2007-08-02 11:09:51 -0400995 struct lpfc_vport **vports;
996 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500997
James Smart19ca7602010-11-20 23:11:55 -0500998 lpfc_cleanup_wt_rrqs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500999 phba->link_state = LPFC_LINK_UP;
1000
1001 /* Unblock fabric iocbs if they are blocked */
1002 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1003 del_timer_sync(&phba->fabric_block_timer);
1004
James Smart549e55c2007-08-02 11:09:51 -04001005 vports = lpfc_create_vport_work_array(phba);
1006 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001007 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001008 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001009 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001010
1011 return 0;
1012}
1013
1014/*
1015 * This routine handles processing a CLEAR_LA mailbox
1016 * command upon completion. It is setup in the LPFC_MBOXQ
1017 * as the completion routine when the command is
1018 * handed off to the SLI layer.
1019 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001020static void
James Smart2e0fef82007-06-17 19:56:36 -05001021lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001022{
James Smart2e0fef82007-06-17 19:56:36 -05001023 struct lpfc_vport *vport = pmb->vport;
1024 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1025 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001026 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001027 uint32_t control;
1028
dea31012005-04-17 16:05:31 -05001029 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001030 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001031 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1032 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1033
1034 /* Check for error */
1035 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001036 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001037 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1038 "0320 CLEAR_LA mbxStatus error x%x hba "
1039 "state x%x\n",
1040 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001041 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001042 goto out;
1043 }
1044
James Smart92d7f7b2007-06-17 19:56:38 -05001045 if (vport->port_type == LPFC_PHYSICAL_PORT)
1046 phba->link_state = LPFC_HBA_READY;
1047
1048 spin_lock_irq(&phba->hbalock);
1049 psli->sli_flag |= LPFC_PROCESS_LA;
1050 control = readl(phba->HCregaddr);
1051 control |= HC_LAINT_ENA;
1052 writel(control, phba->HCregaddr);
1053 readl(phba->HCregaddr); /* flush */
1054 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001055 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001056 return;
dea31012005-04-17 16:05:31 -05001057
dea31012005-04-17 16:05:31 -05001058out:
1059 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001060 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1061 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001062 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001063
James Smart2e0fef82007-06-17 19:56:36 -05001064 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001065 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001066 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001067
James Smart2e0fef82007-06-17 19:56:36 -05001068 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001069
1070 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001071
1072 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001073 psli->sli_flag |= LPFC_PROCESS_LA;
1074 control = readl(phba->HCregaddr);
1075 control |= HC_LAINT_ENA;
1076 writel(control, phba->HCregaddr);
1077 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001078 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001079
1080 return;
1081}
1082
James Smart2e0fef82007-06-17 19:56:36 -05001083
dea31012005-04-17 16:05:31 -05001084static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001085lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001086{
James Smart2e0fef82007-06-17 19:56:36 -05001087 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001088
James Smart04c68492009-05-22 14:52:52 -04001089 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001090 goto out;
dea31012005-04-17 16:05:31 -05001091
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001092 mempool_free(pmb, phba->mbox_mem_pool);
1093
James Smart1b511972011-12-13 13:23:09 -05001094 /* don't perform discovery for SLI4 loopback diagnostic test */
1095 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1096 !(phba->hba_flag & HBA_FCOE_MODE) &&
1097 (phba->link_flag & LS_LOOPBACK_MODE))
1098 return;
1099
James Smart76a95d72010-11-20 23:11:48 -05001100 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001101 vport->fc_flag & FC_PUBLIC_LOOP &&
1102 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001103 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001104 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001105 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1106 */
James Smart2e0fef82007-06-17 19:56:36 -05001107 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001108 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001109 }
dea31012005-04-17 16:05:31 -05001110
James Smart2e0fef82007-06-17 19:56:36 -05001111 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001112 * LPFC_FLOGI while waiting for FLOGI cmpl
1113 */
James Smarte6446432012-05-09 21:16:42 -04001114 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001115 lpfc_initial_flogi(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001116 return;
dea31012005-04-17 16:05:31 -05001117
1118out:
James Smarte8b62012007-08-02 11:10:09 -04001119 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1120 "0306 CONFIG_LINK mbxStatus error x%x "
1121 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001122 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001123 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001124
1125 lpfc_linkdown(phba);
1126
James Smarte8b62012007-08-02 11:10:09 -04001127 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1128 "0200 CONFIG_LINK bad hba state x%x\n",
1129 vport->port_state);
dea31012005-04-17 16:05:31 -05001130
James Smart92d7f7b2007-06-17 19:56:38 -05001131 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001132 return;
1133}
1134
James Smart7d791df2011-07-22 18:37:52 -04001135/**
1136 * lpfc_sli4_clear_fcf_rr_bmask
1137 * @phba pointer to the struct lpfc_hba for this port.
1138 * This fucnction resets the round robin bit mask and clears the
1139 * fcf priority list. The list deletions are done while holding the
1140 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1141 * from the lpfc_fcf_pri record.
1142 **/
1143void
1144lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1145{
1146 struct lpfc_fcf_pri *fcf_pri;
1147 struct lpfc_fcf_pri *next_fcf_pri;
1148 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1149 spin_lock_irq(&phba->hbalock);
1150 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1151 &phba->fcf.fcf_pri_list, list) {
1152 list_del_init(&fcf_pri->list);
1153 fcf_pri->fcf_rec.flag = 0;
1154 }
1155 spin_unlock_irq(&phba->hbalock);
1156}
dea31012005-04-17 16:05:31 -05001157static void
James Smart6fb120a2009-05-22 14:52:59 -04001158lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1159{
1160 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001161
1162 if (mboxq->u.mb.mbxStatus) {
1163 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1164 "2017 REG_FCFI mbxStatus error x%x "
1165 "HBA state x%x\n",
1166 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001167 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001168 }
1169
1170 /* Start FCoE discovery by sending a FLOGI. */
1171 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1172 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001173 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001174 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001175 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001176
James Smart32b97932009-07-19 10:01:21 -04001177 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001178 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1179 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001180 goto fail_out;
1181
1182 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001183 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001184 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001185 phba->hba_flag &= ~FCF_TS_INPROG;
1186 if (vport->port_state != LPFC_FLOGI) {
1187 phba->hba_flag |= FCF_RR_INPROG;
1188 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001189 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001190 goto out;
1191 }
1192 spin_unlock_irq(&phba->hbalock);
1193 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001194
James Smarta93ff372010-10-22 11:06:08 -04001195fail_out:
1196 spin_lock_irq(&phba->hbalock);
1197 phba->hba_flag &= ~FCF_RR_INPROG;
1198 spin_unlock_irq(&phba->hbalock);
1199out:
James Smart6fb120a2009-05-22 14:52:59 -04001200 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001201}
1202
1203/**
1204 * lpfc_fab_name_match - Check if the fcf fabric name match.
1205 * @fab_name: pointer to fabric name.
1206 * @new_fcf_record: pointer to fcf record.
1207 *
1208 * This routine compare the fcf record's fabric name with provided
1209 * fabric name. If the fabric name are identical this function
1210 * returns 1 else return 0.
1211 **/
1212static uint32_t
1213lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1214{
James Smartecfd03c2010-02-12 14:41:27 -05001215 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001216 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001217 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1218 return 0;
1219 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1220 return 0;
1221 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1222 return 0;
1223 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1224 return 0;
1225 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1226 return 0;
1227 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1228 return 0;
1229 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1230 return 0;
1231 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001232}
1233
1234/**
James Smart8fa38512009-07-19 10:01:03 -04001235 * lpfc_sw_name_match - Check if the fcf switch name match.
1236 * @fab_name: pointer to fabric name.
1237 * @new_fcf_record: pointer to fcf record.
1238 *
1239 * This routine compare the fcf record's switch name with provided
1240 * switch name. If the switch name are identical this function
1241 * returns 1 else return 0.
1242 **/
1243static uint32_t
1244lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1245{
James Smartecfd03c2010-02-12 14:41:27 -05001246 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001247 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001248 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1249 return 0;
1250 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1251 return 0;
1252 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1253 return 0;
1254 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1255 return 0;
1256 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1257 return 0;
1258 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1259 return 0;
1260 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1261 return 0;
1262 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001263}
1264
1265/**
James Smart6fb120a2009-05-22 14:52:59 -04001266 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001267 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001268 * @new_fcf_record: pointer to fcf record.
1269 *
1270 * This routine compare the fcf record's mac address with HBA's
1271 * FCF mac address. If the mac addresses are identical this function
1272 * returns 1 else return 0.
1273 **/
1274static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001275lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001276{
James Smartecfd03c2010-02-12 14:41:27 -05001277 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001278 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001279 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1280 return 0;
1281 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1282 return 0;
1283 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1284 return 0;
1285 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1286 return 0;
1287 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1288 return 0;
1289 return 1;
1290}
1291
1292static bool
1293lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1294{
1295 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001296}
1297
1298/**
James Smart7d791df2011-07-22 18:37:52 -04001299 * lpfc_update_fcf_record - Update driver fcf record
1300 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1301 * @phba: pointer to lpfc hba data structure.
1302 * @fcf_index: Index for the lpfc_fcf_record.
1303 * @new_fcf_record: pointer to hba fcf record.
1304 *
1305 * This routine updates the driver FCF priority record from the new HBA FCF
1306 * record. This routine is called with the host lock held.
1307 **/
1308static void
1309__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1310 struct fcf_record *new_fcf_record
1311 )
1312{
1313 struct lpfc_fcf_pri *fcf_pri;
1314
1315 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1316 fcf_pri->fcf_rec.fcf_index = fcf_index;
1317 /* FCF record priority */
1318 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1319
1320}
1321
1322/**
James Smart6fb120a2009-05-22 14:52:59 -04001323 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001324 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001325 * @new_fcf_record: pointer to fcf record.
1326 *
1327 * This routine copies the FCF information from the FCF
1328 * record to lpfc_hba data structure.
1329 **/
1330static void
James Smartecfd03c2010-02-12 14:41:27 -05001331lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1332 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001333{
James Smartecfd03c2010-02-12 14:41:27 -05001334 /* Fabric name */
1335 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001336 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001337 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001338 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001339 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001340 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001341 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001342 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001343 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001344 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001345 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001346 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001347 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001348 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001349 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001350 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001351 /* Mac address */
1352 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1353 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1354 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1355 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1356 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1357 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1358 /* FCF record index */
1359 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1360 /* FCF record priority */
1361 fcf_rec->priority = new_fcf_record->fip_priority;
1362 /* Switch name */
1363 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001364 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001365 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001366 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001367 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001368 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001369 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001370 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001371 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001372 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001373 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001374 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001375 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001376 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001377 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001378 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001379}
1380
1381/**
James Smartecfd03c2010-02-12 14:41:27 -05001382 * lpfc_update_fcf_record - Update driver fcf record
1383 * @phba: pointer to lpfc hba data structure.
1384 * @fcf_rec: pointer to driver fcf record.
1385 * @new_fcf_record: pointer to hba fcf record.
1386 * @addr_mode: address mode to be set to the driver fcf record.
1387 * @vlan_id: vlan tag to be set to the driver fcf record.
1388 * @flag: flag bits to be set to the driver fcf record.
1389 *
1390 * This routine updates the driver FCF record from the new HBA FCF record
1391 * together with the address mode, vlan_id, and other informations. This
1392 * routine is called with the host lock held.
1393 **/
1394static void
1395__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1396 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1397 uint16_t vlan_id, uint32_t flag)
1398{
1399 /* Copy the fields from the HBA's FCF record */
1400 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1401 /* Update other fields of driver FCF record */
1402 fcf_rec->addr_mode = addr_mode;
1403 fcf_rec->vlan_id = vlan_id;
1404 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001405 __lpfc_update_fcf_record_pri(phba,
1406 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1407 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001408}
1409
1410/**
James Smart6fb120a2009-05-22 14:52:59 -04001411 * lpfc_register_fcf - Register the FCF with hba.
1412 * @phba: pointer to lpfc hba data structure.
1413 *
1414 * This routine issues a register fcfi mailbox command to register
1415 * the fcf with HBA.
1416 **/
1417static void
1418lpfc_register_fcf(struct lpfc_hba *phba)
1419{
1420 LPFC_MBOXQ_t *fcf_mbxq;
1421 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001422
James Smartd439d282010-09-29 11:18:45 -04001423 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001424 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001425 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001426 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001427 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001428 return;
1429 }
1430
1431 /* The FCF is already registered, start discovery */
1432 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001433 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001434 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001435 if (phba->pport->port_state != LPFC_FLOGI &&
1436 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001437 phba->hba_flag |= FCF_RR_INPROG;
1438 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001439 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001440 return;
1441 }
1442 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001443 return;
1444 }
James Smartd439d282010-09-29 11:18:45 -04001445 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001446
James Smarta93ff372010-10-22 11:06:08 -04001447 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001448 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001449 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001450 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001451 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001452 return;
James Smart4d9ab992009-10-02 15:16:39 -04001453 }
James Smart6fb120a2009-05-22 14:52:59 -04001454
1455 lpfc_reg_fcfi(phba, fcf_mbxq);
1456 fcf_mbxq->vport = phba->pport;
1457 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1458 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001459 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001460 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001461 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001462 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001463 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001464 }
James Smart6fb120a2009-05-22 14:52:59 -04001465
1466 return;
1467}
1468
1469/**
1470 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1471 * @phba: pointer to lpfc hba data structure.
1472 * @new_fcf_record: pointer to fcf record.
1473 * @boot_flag: Indicates if this record used by boot bios.
1474 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001475 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001476 *
1477 * This routine compare the fcf record with connect list obtained from the
1478 * config region to decide if this FCF can be used for SAN discovery. It returns
1479 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1480 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1481 * is used by boot bios and addr_mode will indicate the addressing mode to be
1482 * used for this FCF when the function returns.
1483 * If the FCF record need to be used with a particular vlan id, the vlan is
1484 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001485 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001486 **/
1487static int
1488lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1489 struct fcf_record *new_fcf_record,
1490 uint32_t *boot_flag, uint32_t *addr_mode,
1491 uint16_t *vlan_id)
1492{
1493 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001494 int i, j, fcf_vlan_id = 0;
1495
1496 /* Find the lowest VLAN id in the FCF record */
1497 for (i = 0; i < 512; i++) {
1498 if (new_fcf_record->vlan_bitmap[i]) {
1499 fcf_vlan_id = i * 8;
1500 j = 0;
1501 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1502 j++;
1503 fcf_vlan_id++;
1504 }
1505 break;
1506 }
1507 }
James Smart6fb120a2009-05-22 14:52:59 -04001508
James Smart26979ce2012-09-29 11:31:55 -04001509 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001510 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001511 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1512 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001513 return 0;
1514
James Smart45ed1192009-10-02 15:17:02 -04001515 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001516 *boot_flag = 0;
1517 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1518 new_fcf_record);
1519 if (phba->valid_vlan)
1520 *vlan_id = phba->vlan_id;
1521 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001522 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001523 return 1;
1524 }
1525
1526 /*
1527 * If there are no FCF connection table entry, driver connect to all
1528 * FCFs.
1529 */
1530 if (list_empty(&phba->fcf_conn_rec_list)) {
1531 *boot_flag = 0;
1532 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1533 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001534
1535 /*
1536 * When there are no FCF connect entries, use driver's default
1537 * addressing mode - FPMA.
1538 */
1539 if (*addr_mode & LPFC_FCF_FPMA)
1540 *addr_mode = LPFC_FCF_FPMA;
1541
James Smart4d9ab992009-10-02 15:16:39 -04001542 /* If FCF record report a vlan id use that vlan id */
1543 if (fcf_vlan_id)
1544 *vlan_id = fcf_vlan_id;
1545 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001546 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001547 return 1;
1548 }
1549
James Smartecfd03c2010-02-12 14:41:27 -05001550 list_for_each_entry(conn_entry,
1551 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001552 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1553 continue;
1554
1555 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1556 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001557 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001558 continue;
James Smart8fa38512009-07-19 10:01:03 -04001559 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1560 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1561 new_fcf_record))
1562 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001563 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1564 /*
1565 * If the vlan bit map does not have the bit set for the
1566 * vlan id to be used, then it is not a match.
1567 */
1568 if (!(new_fcf_record->vlan_bitmap
1569 [conn_entry->conn_rec.vlan_tag / 8] &
1570 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1571 continue;
1572 }
1573
1574 /*
James Smart0c287582009-06-10 17:22:56 -04001575 * If connection record does not support any addressing mode,
1576 * skip the FCF record.
1577 */
1578 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1579 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1580 continue;
1581
1582 /*
James Smart6fb120a2009-05-22 14:52:59 -04001583 * Check if the connection record specifies a required
1584 * addressing mode.
1585 */
1586 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1587 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1588
1589 /*
1590 * If SPMA required but FCF not support this continue.
1591 */
1592 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1593 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1594 new_fcf_record) & LPFC_FCF_SPMA))
1595 continue;
1596
1597 /*
1598 * If FPMA required but FCF not support this continue.
1599 */
1600 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1601 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1602 new_fcf_record) & LPFC_FCF_FPMA))
1603 continue;
1604 }
1605
1606 /*
1607 * This fcf record matches filtering criteria.
1608 */
1609 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1610 *boot_flag = 1;
1611 else
1612 *boot_flag = 0;
1613
James Smart0c287582009-06-10 17:22:56 -04001614 /*
1615 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001616 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001617 * by FCF, allow fabric to pick the addressing mode.
1618 */
James Smart6fb120a2009-05-22 14:52:59 -04001619 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1620 new_fcf_record);
1621 /*
1622 * If the user specified a required address mode, assign that
1623 * address mode
1624 */
1625 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1626 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1627 *addr_mode = (conn_entry->conn_rec.flags &
1628 FCFCNCT_AM_SPMA) ?
1629 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1630 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001631 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001632 * addr mode only if FCF support the addr_mode.
1633 */
1634 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1635 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1636 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1637 (*addr_mode & LPFC_FCF_SPMA))
1638 *addr_mode = LPFC_FCF_SPMA;
1639 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1640 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1641 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1642 (*addr_mode & LPFC_FCF_FPMA))
1643 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001644
James Smart4d9ab992009-10-02 15:16:39 -04001645 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001646 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1647 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001648 /*
1649 * If no vlan id is specified in connect list, use the vlan id
1650 * in the FCF record
1651 */
1652 else if (fcf_vlan_id)
1653 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001654 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001655 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001656
1657 return 1;
1658 }
1659
1660 return 0;
1661}
1662
1663/**
James Smart32b97932009-07-19 10:01:21 -04001664 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1665 * @phba: pointer to lpfc hba data structure.
1666 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1667 *
1668 * This function check if there is any fcoe event pending while driver
1669 * scan FCF entries. If there is any pending event, it will restart the
1670 * FCF saning and return 1 else return 0.
1671 */
1672int
1673lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1674{
James Smart32b97932009-07-19 10:01:21 -04001675 /*
1676 * If the Link is up and no FCoE events while in the
1677 * FCF discovery, no need to restart FCF discovery.
1678 */
1679 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001680 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001681 return 0;
1682
James Smart0c9ab6f2010-02-26 14:15:57 -05001683 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1684 "2768 Pending link or FCF event during current "
1685 "handling of the previous event: link_state:x%x, "
1686 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1687 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1688 phba->fcoe_eventtag);
1689
James Smart32b97932009-07-19 10:01:21 -04001690 spin_lock_irq(&phba->hbalock);
1691 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1692 spin_unlock_irq(&phba->hbalock);
1693
James Smart0c9ab6f2010-02-26 14:15:57 -05001694 if (phba->link_state >= LPFC_LINK_UP) {
1695 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1696 "2780 Restart FCF table scan due to "
1697 "pending FCF event:evt_tag_at_scan:x%x, "
1698 "evt_tag_current:x%x\n",
1699 phba->fcoe_eventtag_at_fcf_scan,
1700 phba->fcoe_eventtag);
1701 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1702 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001703 /*
James Smarta93ff372010-10-22 11:06:08 -04001704 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001705 * flag
1706 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001707 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1708 "2833 Stop FCF discovery process due to link "
1709 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001710 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001711 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001712 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001713 spin_unlock_irq(&phba->hbalock);
1714 }
James Smart32b97932009-07-19 10:01:21 -04001715
James Smart0c9ab6f2010-02-26 14:15:57 -05001716 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001717 if (unreg_fcf) {
1718 spin_lock_irq(&phba->hbalock);
1719 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1720 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001721 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001722 }
James Smart32b97932009-07-19 10:01:21 -04001723 return 1;
1724}
1725
1726/**
James Smart999d8132010-03-15 11:24:56 -04001727 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1728 * @phba: pointer to lpfc hba data structure.
1729 * @fcf_cnt: number of eligible fcf record seen so far.
1730 *
1731 * This function makes an running random selection decision on FCF record to
1732 * use through a sequence of @fcf_cnt eligible FCF records with equal
1733 * probability. To perform integer manunipulation of random numbers with
1734 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001735 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001736 *
1737 * Returns true when outcome is for the newly read FCF record should be
1738 * chosen; otherwise, return false when outcome is for keeping the previously
1739 * chosen FCF record.
1740 **/
1741static bool
1742lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1743{
1744 uint32_t rand_num;
1745
1746 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001747 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001748
1749 /* Decision with probability 1/fcf_cnt */
1750 if ((fcf_cnt * rand_num) < 0xFFFF)
1751 return true;
1752 else
1753 return false;
1754}
1755
1756/**
James Smart3804dc82010-07-14 15:31:37 -04001757 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001758 * @phba: pointer to lpfc hba data structure.
1759 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001760 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001761 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001762 * This routine parses the non-embedded fcf mailbox command by performing the
1763 * necessarily error checking, non-embedded read FCF record mailbox command
1764 * SGE parsing, and endianness swapping.
1765 *
1766 * Returns the pointer to the new FCF record in the non-embedded mailbox
1767 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001768 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001769static struct fcf_record *
1770lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1771 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001772{
1773 void *virt_addr;
1774 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001775 struct lpfc_mbx_sge sge;
1776 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1777 uint32_t shdr_status, shdr_add_status;
1778 union lpfc_sli4_cfg_shdr *shdr;
1779 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001780
James Smart6fb120a2009-05-22 14:52:59 -04001781 /* Get the first SGE entry from the non-embedded DMA memory. This
1782 * routine only uses a single SGE.
1783 */
1784 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1785 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1786 if (unlikely(!mboxq->sge_array)) {
1787 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1788 "2524 Failed to get the non-embedded SGE "
1789 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001790 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001791 }
1792 virt_addr = mboxq->sge_array->addr[0];
1793
1794 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001795 lpfc_sli_pcimem_bcopy(shdr, shdr,
1796 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001797 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001798 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001799 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001800 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1801 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001802 "2726 READ_FCF_RECORD Indicates empty "
1803 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001804 else
1805 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001806 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001807 "with status x%x add_status x%x, "
1808 "mbx\n", shdr_status, shdr_add_status);
1809 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001810 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001811
1812 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001813 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1814 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1815 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001816 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001817 new_fcf_record = (struct fcf_record *)(virt_addr +
1818 sizeof(struct lpfc_mbx_read_fcf_tbl));
1819 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001820 offsetof(struct fcf_record, vlan_bitmap));
1821 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1822 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001823
James Smart0c9ab6f2010-02-26 14:15:57 -05001824 return new_fcf_record;
1825}
1826
1827/**
1828 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1829 * @phba: pointer to lpfc hba data structure.
1830 * @fcf_record: pointer to the fcf record.
1831 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1832 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1833 *
1834 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1835 * enabled.
1836 **/
1837static void
1838lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1839 struct fcf_record *fcf_record,
1840 uint16_t vlan_id,
1841 uint16_t next_fcf_index)
1842{
1843 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1844 "2764 READ_FCF_RECORD:\n"
1845 "\tFCF_Index : x%x\n"
1846 "\tFCF_Avail : x%x\n"
1847 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001848 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001849 "\tFIP_Priority : x%x\n"
1850 "\tMAC_Provider : x%x\n"
1851 "\tLowest VLANID : x%x\n"
1852 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1853 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1854 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1855 "\tNext_FCF_Index: x%x\n",
1856 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1857 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1858 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001859 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001860 fcf_record->fip_priority,
1861 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1862 vlan_id,
1863 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1864 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1865 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1866 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1867 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1868 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1869 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1870 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1871 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1872 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1873 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1874 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1875 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1876 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1877 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1878 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1879 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1880 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1881 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1882 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1883 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1884 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1885 next_fcf_index);
1886}
1887
1888/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001889 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1890 * @phba: pointer to lpfc hba data structure.
1891 * @fcf_rec: pointer to an existing FCF record.
1892 * @new_fcf_record: pointer to a new FCF record.
1893 * @new_vlan_id: vlan id from the new FCF record.
1894 *
1895 * This function performs matching test of a new FCF record against an existing
1896 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1897 * will not be used as part of the FCF record matching criteria.
1898 *
1899 * Returns true if all the fields matching, otherwise returns false.
1900 */
1901static bool
1902lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1903 struct lpfc_fcf_rec *fcf_rec,
1904 struct fcf_record *new_fcf_record,
1905 uint16_t new_vlan_id)
1906{
1907 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1908 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1909 return false;
1910 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1911 return false;
1912 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1913 return false;
1914 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1915 return false;
James Smart7d791df2011-07-22 18:37:52 -04001916 if (fcf_rec->priority != new_fcf_record->fip_priority)
1917 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001918 return true;
1919}
1920
1921/**
James Smarta93ff372010-10-22 11:06:08 -04001922 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1923 * @vport: Pointer to vport object.
1924 * @fcf_index: index to next fcf.
1925 *
1926 * This function processing the roundrobin fcf failover to next fcf index.
1927 * When this function is invoked, there will be a current fcf registered
1928 * for flogi.
1929 * Return: 0 for continue retrying flogi on currently registered fcf;
1930 * 1 for stop flogi on currently registered fcf;
1931 */
1932int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1933{
1934 struct lpfc_hba *phba = vport->phba;
1935 int rc;
1936
1937 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1938 spin_lock_irq(&phba->hbalock);
1939 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1940 spin_unlock_irq(&phba->hbalock);
1941 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1942 "2872 Devloss tmo with no eligible "
1943 "FCF, unregister in-use FCF (x%x) "
1944 "and rescan FCF table\n",
1945 phba->fcf.current_rec.fcf_indx);
1946 lpfc_unregister_fcf_rescan(phba);
1947 goto stop_flogi_current_fcf;
1948 }
1949 /* Mark the end to FLOGI roundrobin failover */
1950 phba->hba_flag &= ~FCF_RR_INPROG;
1951 /* Allow action to new fcf asynchronous event */
1952 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1953 spin_unlock_irq(&phba->hbalock);
1954 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1955 "2865 No FCF available, stop roundrobin FCF "
1956 "failover and change port state:x%x/x%x\n",
1957 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1958 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1959 goto stop_flogi_current_fcf;
1960 } else {
1961 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1962 "2794 Try FLOGI roundrobin FCF failover to "
1963 "(x%x)\n", fcf_index);
1964 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1965 if (rc)
1966 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1967 "2761 FLOGI roundrobin FCF failover "
1968 "failed (rc:x%x) to read FCF (x%x)\n",
1969 rc, phba->fcf.current_rec.fcf_indx);
1970 else
1971 goto stop_flogi_current_fcf;
1972 }
1973 return 0;
1974
1975stop_flogi_current_fcf:
1976 lpfc_can_disctmo(vport);
1977 return 1;
1978}
1979
1980/**
James Smart7d791df2011-07-22 18:37:52 -04001981 * lpfc_sli4_fcf_pri_list_del
1982 * @phba: pointer to lpfc hba data structure.
1983 * @fcf_index the index of the fcf record to delete
1984 * This routine checks the on list flag of the fcf_index to be deleted.
1985 * If it is one the list then it is removed from the list, and the flag
1986 * is cleared. This routine grab the hbalock before removing the fcf
1987 * record from the list.
1988 **/
1989static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1990 uint16_t fcf_index)
1991{
1992 struct lpfc_fcf_pri *new_fcf_pri;
1993
1994 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1995 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1996 "3058 deleting idx x%x pri x%x flg x%x\n",
1997 fcf_index, new_fcf_pri->fcf_rec.priority,
1998 new_fcf_pri->fcf_rec.flag);
1999 spin_lock_irq(&phba->hbalock);
2000 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2001 if (phba->fcf.current_rec.priority ==
2002 new_fcf_pri->fcf_rec.priority)
2003 phba->fcf.eligible_fcf_cnt--;
2004 list_del_init(&new_fcf_pri->list);
2005 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2006 }
2007 spin_unlock_irq(&phba->hbalock);
2008}
2009
2010/**
2011 * lpfc_sli4_set_fcf_flogi_fail
2012 * @phba: pointer to lpfc hba data structure.
2013 * @fcf_index the index of the fcf record to update
2014 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2015 * flag so the the round robin slection for the particular priority level
2016 * will try a different fcf record that does not have this bit set.
2017 * If the fcf record is re-read for any reason this flag is cleared brfore
2018 * adding it to the priority list.
2019 **/
2020void
2021lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2022{
2023 struct lpfc_fcf_pri *new_fcf_pri;
2024 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2025 spin_lock_irq(&phba->hbalock);
2026 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2027 spin_unlock_irq(&phba->hbalock);
2028}
2029
2030/**
2031 * lpfc_sli4_fcf_pri_list_add
2032 * @phba: pointer to lpfc hba data structure.
2033 * @fcf_index the index of the fcf record to add
2034 * This routine checks the priority of the fcf_index to be added.
2035 * If it is a lower priority than the current head of the fcf_pri list
2036 * then it is added to the list in the right order.
2037 * If it is the same priority as the current head of the list then it
2038 * is added to the head of the list and its bit in the rr_bmask is set.
2039 * If the fcf_index to be added is of a higher priority than the current
2040 * head of the list then the rr_bmask is cleared, its bit is set in the
2041 * rr_bmask and it is added to the head of the list.
2042 * returns:
2043 * 0=success 1=failure
2044 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002045static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2046 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002047 struct fcf_record *new_fcf_record)
2048{
2049 uint16_t current_fcf_pri;
2050 uint16_t last_index;
2051 struct lpfc_fcf_pri *fcf_pri;
2052 struct lpfc_fcf_pri *next_fcf_pri;
2053 struct lpfc_fcf_pri *new_fcf_pri;
2054 int ret;
2055
2056 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2057 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2058 "3059 adding idx x%x pri x%x flg x%x\n",
2059 fcf_index, new_fcf_record->fip_priority,
2060 new_fcf_pri->fcf_rec.flag);
2061 spin_lock_irq(&phba->hbalock);
2062 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2063 list_del_init(&new_fcf_pri->list);
2064 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2065 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2066 if (list_empty(&phba->fcf.fcf_pri_list)) {
2067 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2068 ret = lpfc_sli4_fcf_rr_index_set(phba,
2069 new_fcf_pri->fcf_rec.fcf_index);
2070 goto out;
2071 }
2072
2073 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2074 LPFC_SLI4_FCF_TBL_INDX_MAX);
2075 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2076 ret = 0; /* Empty rr list */
2077 goto out;
2078 }
2079 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2080 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2081 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2082 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2083 memset(phba->fcf.fcf_rr_bmask, 0,
2084 sizeof(*phba->fcf.fcf_rr_bmask));
2085 /* fcfs_at_this_priority_level = 1; */
2086 phba->fcf.eligible_fcf_cnt = 1;
2087 } else
2088 /* fcfs_at_this_priority_level++; */
2089 phba->fcf.eligible_fcf_cnt++;
2090 ret = lpfc_sli4_fcf_rr_index_set(phba,
2091 new_fcf_pri->fcf_rec.fcf_index);
2092 goto out;
2093 }
2094
2095 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2096 &phba->fcf.fcf_pri_list, list) {
2097 if (new_fcf_pri->fcf_rec.priority <=
2098 fcf_pri->fcf_rec.priority) {
2099 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2100 list_add(&new_fcf_pri->list,
2101 &phba->fcf.fcf_pri_list);
2102 else
2103 list_add(&new_fcf_pri->list,
2104 &((struct lpfc_fcf_pri *)
2105 fcf_pri->list.prev)->list);
2106 ret = 0;
2107 goto out;
2108 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2109 || new_fcf_pri->fcf_rec.priority <
2110 next_fcf_pri->fcf_rec.priority) {
2111 list_add(&new_fcf_pri->list, &fcf_pri->list);
2112 ret = 0;
2113 goto out;
2114 }
2115 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2116 continue;
2117
2118 }
2119 ret = 1;
2120out:
2121 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2122 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2123 spin_unlock_irq(&phba->hbalock);
2124 return ret;
2125}
2126
2127/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002128 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2129 * @phba: pointer to lpfc hba data structure.
2130 * @mboxq: pointer to mailbox object.
2131 *
2132 * This function iterates through all the fcf records available in
2133 * HBA and chooses the optimal FCF record for discovery. After finding
2134 * the FCF for discovery it registers the FCF record and kicks start
2135 * discovery.
2136 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2137 * use an FCF record which matches fabric name and mac address of the
2138 * currently used FCF record.
2139 * If the driver supports only one FCF, it will try to use the FCF record
2140 * used by BOOT_BIOS.
2141 */
2142void
2143lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2144{
2145 struct fcf_record *new_fcf_record;
2146 uint32_t boot_flag, addr_mode;
2147 uint16_t fcf_index, next_fcf_index;
2148 struct lpfc_fcf_rec *fcf_rec = NULL;
2149 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002150 uint32_t seed;
2151 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002152 int rc;
2153
2154 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002155 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002156 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2157 return;
2158 }
2159
2160 /* Parse the FCF record from the non-embedded mailbox command */
2161 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2162 &next_fcf_index);
2163 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002164 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002165 "2765 Mailbox command READ_FCF_RECORD "
2166 "failed to retrieve a FCF record.\n");
2167 /* Let next new FCF event trigger fast failover */
2168 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002169 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002170 spin_unlock_irq(&phba->hbalock);
2171 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2172 return;
2173 }
2174
2175 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002176 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2177 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002178
2179 /* Log the FCF record information if turned on */
2180 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2181 next_fcf_index);
2182
James Smart6fb120a2009-05-22 14:52:59 -04002183 /*
2184 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002185 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002186 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002187 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002188 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002189 lpfc_sli4_fcf_pri_list_del(phba,
2190 bf_get(lpfc_fcf_record_fcf_index,
2191 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002192 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002193 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002194 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002195 bf_get(lpfc_fcf_record_fcf_index,
2196 new_fcf_record),
2197 bf_get(lpfc_fcf_record_fcf_avail,
2198 new_fcf_record),
2199 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002200 new_fcf_record),
2201 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002202 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002203 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2204 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2205 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002206 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2207 phba->fcf.current_rec.fcf_indx) {
2208 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2209 "2862 FCF (x%x) matches property "
2210 "of in-use FCF (x%x)\n",
2211 bf_get(lpfc_fcf_record_fcf_index,
2212 new_fcf_record),
2213 phba->fcf.current_rec.fcf_indx);
2214 goto read_next_fcf;
2215 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002216 /*
2217 * In case the current in-use FCF record becomes
2218 * invalid/unavailable during FCF discovery that
2219 * was not triggered by fast FCF failover process,
2220 * treat it as fast FCF failover.
2221 */
2222 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2223 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2224 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2225 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002226 "(x%x), enter FCF failover "
2227 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002228 phba->fcf.current_rec.fcf_indx);
2229 spin_lock_irq(&phba->hbalock);
2230 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2231 spin_unlock_irq(&phba->hbalock);
2232 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2233 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2234 LPFC_FCOE_FCF_GET_FIRST);
2235 return;
2236 }
2237 }
James Smart6fb120a2009-05-22 14:52:59 -04002238 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002239 } else {
2240 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002241 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2242 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002243 if (rc)
2244 goto read_next_fcf;
2245 }
2246
James Smart6fb120a2009-05-22 14:52:59 -04002247 /*
2248 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002249 * FCF record for the discovery. The condition that a rescan
2250 * matches the in-use FCF record: fabric name, switch name, mac
2251 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002252 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002253 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002254 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002255 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2256 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002257 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002258 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2259 phba->fcf.current_rec.fcf_indx) {
2260 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2261 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2262 /* Stop FCF redisc wait timer */
2263 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2264 phba);
2265 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2266 /* Fast failover, mark completed */
2267 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2268 spin_unlock_irq(&phba->hbalock);
2269 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2270 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002271 "FCF (x%x), port_state:x%x, "
2272 "fc_flag:x%x\n",
2273 phba->fcf.current_rec.fcf_indx,
2274 phba->pport->port_state,
2275 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002276 goto out;
2277 } else
2278 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2279 "2863 New FCF (x%x) matches "
2280 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002281 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002282 new_fcf_record),
2283 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002284 }
James Smartecfd03c2010-02-12 14:41:27 -05002285 /*
2286 * Read next FCF record from HBA searching for the matching
2287 * with in-use record only if not during the fast failover
2288 * period. In case of fast failover period, it shall try to
2289 * determine whether the FCF record just read should be the
2290 * next candidate.
2291 */
2292 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002293 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002294 goto read_next_fcf;
2295 }
James Smart6fb120a2009-05-22 14:52:59 -04002296 }
James Smartecfd03c2010-02-12 14:41:27 -05002297 /*
2298 * Update on failover FCF record only if it's in FCF fast-failover
2299 * period; otherwise, update on current FCF record.
2300 */
James Smartfc2b9892010-02-26 14:15:29 -05002301 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2302 fcf_rec = &phba->fcf.failover_rec;
2303 else
James Smartecfd03c2010-02-12 14:41:27 -05002304 fcf_rec = &phba->fcf.current_rec;
2305
James Smart6fb120a2009-05-22 14:52:59 -04002306 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2307 /*
James Smartecfd03c2010-02-12 14:41:27 -05002308 * If the driver FCF record does not have boot flag
2309 * set and new hba fcf record has boot flag set, use
2310 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002311 */
James Smartecfd03c2010-02-12 14:41:27 -05002312 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2313 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002314 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2315 "2837 Update current FCF record "
2316 "(x%x) with new FCF record (x%x)\n",
2317 fcf_rec->fcf_indx,
2318 bf_get(lpfc_fcf_record_fcf_index,
2319 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002320 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2321 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002322 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002323 goto read_next_fcf;
2324 }
2325 /*
James Smartecfd03c2010-02-12 14:41:27 -05002326 * If the driver FCF record has boot flag set and the
2327 * new hba FCF record does not have boot flag, read
2328 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002329 */
James Smartecfd03c2010-02-12 14:41:27 -05002330 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002331 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002332 goto read_next_fcf;
2333 }
2334 /*
James Smartecfd03c2010-02-12 14:41:27 -05002335 * If the new hba FCF record has lower priority value
2336 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002337 */
James Smartfc2b9892010-02-26 14:15:29 -05002338 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002339 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002340 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2341 "2838 Update current FCF record "
2342 "(x%x) with new FCF record (x%x)\n",
2343 fcf_rec->fcf_indx,
2344 bf_get(lpfc_fcf_record_fcf_index,
2345 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002346 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2347 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002348 /* Reset running random FCF selection count */
2349 phba->fcf.eligible_fcf_cnt = 1;
2350 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2351 /* Update running random FCF selection count */
2352 phba->fcf.eligible_fcf_cnt++;
2353 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2354 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002355 if (select_new_fcf) {
2356 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2357 "2839 Update current FCF record "
2358 "(x%x) with new FCF record (x%x)\n",
2359 fcf_rec->fcf_indx,
2360 bf_get(lpfc_fcf_record_fcf_index,
2361 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002362 /* Choose the new FCF by random selection */
2363 __lpfc_update_fcf_record(phba, fcf_rec,
2364 new_fcf_record,
2365 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002366 }
James Smart6fb120a2009-05-22 14:52:59 -04002367 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002368 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002369 goto read_next_fcf;
2370 }
2371 /*
James Smartecfd03c2010-02-12 14:41:27 -05002372 * This is the first suitable FCF record, choose this record for
2373 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002374 */
James Smartecfd03c2010-02-12 14:41:27 -05002375 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002376 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002377 "2840 Update initial FCF candidate "
2378 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002379 bf_get(lpfc_fcf_record_fcf_index,
2380 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002381 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2382 addr_mode, vlan_id, (boot_flag ?
2383 BOOT_ENABLE : 0));
2384 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002385 /* Setup initial running random FCF selection count */
2386 phba->fcf.eligible_fcf_cnt = 1;
2387 /* Seeding the random number generator for random selection */
2388 seed = (uint32_t)(0xFFFFFFFF & jiffies);
Akinobu Mita3b60a642013-04-29 16:21:35 -07002389 prandom_seed(seed);
James Smart6fb120a2009-05-22 14:52:59 -04002390 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002391 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002392 goto read_next_fcf;
2393
2394read_next_fcf:
2395 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002396 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2397 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2398 /*
2399 * Case of FCF fast failover scan
2400 */
2401
2402 /*
2403 * It has not found any suitable FCF record, cancel
2404 * FCF scan inprogress, and do nothing
2405 */
2406 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002407 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002408 "2782 No suitable FCF found: "
2409 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002410 phba->fcoe_eventtag_at_fcf_scan,
2411 bf_get(lpfc_fcf_record_fcf_index,
2412 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002413 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002414 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2415 phba->hba_flag &= ~FCF_TS_INPROG;
2416 spin_unlock_irq(&phba->hbalock);
2417 /* Unregister in-use FCF and rescan */
2418 lpfc_printf_log(phba, KERN_INFO,
2419 LOG_FIP,
2420 "2864 On devloss tmo "
2421 "unreg in-use FCF and "
2422 "rescan FCF table\n");
2423 lpfc_unregister_fcf_rescan(phba);
2424 return;
2425 }
2426 /*
2427 * Let next new FCF event trigger fast failover
2428 */
2429 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002430 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002431 return;
2432 }
2433 /*
2434 * It has found a suitable FCF record that is not
2435 * the same as in-use FCF record, unregister the
2436 * in-use FCF record, replace the in-use FCF record
2437 * with the new FCF record, mark FCF fast failover
2438 * completed, and then start register the new FCF
2439 * record.
2440 */
2441
James Smart0c9ab6f2010-02-26 14:15:57 -05002442 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002443 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002444
2445 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002446 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002447 "2842 Replace in-use FCF (x%x) "
2448 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002449 phba->fcf.current_rec.fcf_indx,
2450 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002451 memcpy(&phba->fcf.current_rec,
2452 &phba->fcf.failover_rec,
2453 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002454 /*
2455 * Mark the fast FCF failover rediscovery completed
2456 * and the start of the first round of the roundrobin
2457 * FCF failover.
2458 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002459 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002460 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002461 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002462 /* Register to the new FCF record */
2463 lpfc_register_fcf(phba);
2464 } else {
2465 /*
2466 * In case of transaction period to fast FCF failover,
2467 * do nothing when search to the end of the FCF table.
2468 */
2469 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2470 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2471 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002472
James Smart7d791df2011-07-22 18:37:52 -04002473 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2474 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002475 /*
2476 * In case the current in-use FCF record no
2477 * longer existed during FCF discovery that
2478 * was not triggered by fast FCF failover
2479 * process, treat it as fast FCF failover.
2480 */
2481 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2482 "2841 In-use FCF record (x%x) "
2483 "not reported, entering fast "
2484 "FCF failover mode scanning.\n",
2485 phba->fcf.current_rec.fcf_indx);
2486 spin_lock_irq(&phba->hbalock);
2487 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2488 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002489 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2490 LPFC_FCOE_FCF_GET_FIRST);
2491 return;
2492 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002493 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002494 lpfc_register_fcf(phba);
2495 }
2496 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002497 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002498 return;
2499
2500out:
2501 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2502 lpfc_register_fcf(phba);
2503
2504 return;
2505}
2506
2507/**
James Smarta93ff372010-10-22 11:06:08 -04002508 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002509 * @phba: pointer to lpfc hba data structure.
2510 * @mboxq: pointer to mailbox object.
2511 *
James Smarta93ff372010-10-22 11:06:08 -04002512 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002513 * read FCF record mailbox command from the eligible FCF record bmask for
2514 * performing the failover. If the FCF read back is not valid/available, it
2515 * fails through to retrying FLOGI to the currently registered FCF again.
2516 * Otherwise, if the FCF read back is valid and available, it will set the
2517 * newly read FCF record to the failover FCF record, unregister currently
2518 * registered FCF record, copy the failover FCF record to the current
2519 * FCF record, and then register the current FCF record before proceeding
2520 * to trying FLOGI on the new failover FCF.
2521 */
2522void
2523lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2524{
2525 struct fcf_record *new_fcf_record;
2526 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002527 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002528 uint16_t current_fcf_index;
2529 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002530 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002531
James Smarta93ff372010-10-22 11:06:08 -04002532 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002533 if (phba->link_state < LPFC_LINK_UP) {
2534 spin_lock_irq(&phba->hbalock);
2535 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002536 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002537 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002538 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002539 }
2540
2541 /* Parse the FCF record from the non-embedded mailbox command */
2542 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2543 &next_fcf_index);
2544 if (!new_fcf_record) {
2545 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2546 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002547 "failed to retrieve a FCF record. "
2548 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2549 phba->fcf.fcf_flag);
2550 lpfc_unregister_fcf_rescan(phba);
2551 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002552 }
2553
2554 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002555 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2556 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002557
2558 /* Log the FCF record information if turned on */
2559 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2560 next_fcf_index);
2561
James Smarta93ff372010-10-22 11:06:08 -04002562 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2563 if (!rc) {
2564 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2565 "2848 Remove ineligible FCF (x%x) from "
2566 "from roundrobin bmask\n", fcf_index);
2567 /* Clear roundrobin bmask bit for ineligible FCF */
2568 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2569 /* Perform next round of roundrobin FCF failover */
2570 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2571 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2572 if (rc)
2573 goto out;
2574 goto error_out;
2575 }
2576
2577 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2578 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2579 "2760 Perform FLOGI roundrobin FCF failover: "
2580 "FCF (x%x) back to FCF (x%x)\n",
2581 phba->fcf.current_rec.fcf_indx, fcf_index);
2582 /* Wait 500 ms before retrying FLOGI to current FCF */
2583 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002584 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002585 goto out;
2586 }
2587
James Smart0c9ab6f2010-02-26 14:15:57 -05002588 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002589 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002590 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2591 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002592 spin_lock_irq(&phba->hbalock);
2593 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2594 new_fcf_record, addr_mode, vlan_id,
2595 (boot_flag ? BOOT_ENABLE : 0));
2596 spin_unlock_irq(&phba->hbalock);
2597
2598 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2599
2600 /* Unregister the current in-use FCF record */
2601 lpfc_unregister_fcf(phba);
2602
2603 /* Replace in-use record with the new record */
2604 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2605 sizeof(struct lpfc_fcf_rec));
2606
2607 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002608 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2609 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002610
James Smarta93ff372010-10-22 11:06:08 -04002611error_out:
2612 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002613out:
2614 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002615}
2616
2617/**
2618 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2619 * @phba: pointer to lpfc hba data structure.
2620 * @mboxq: pointer to mailbox object.
2621 *
2622 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002623 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002624 * failover when a new FCF event happened. If the FCF read back is
2625 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002626 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002627 */
2628void
2629lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2630{
2631 struct fcf_record *new_fcf_record;
2632 uint32_t boot_flag, addr_mode;
2633 uint16_t fcf_index, next_fcf_index;
2634 uint16_t vlan_id;
2635 int rc;
2636
2637 /* If link state is not up, no need to proceed */
2638 if (phba->link_state < LPFC_LINK_UP)
2639 goto out;
2640
2641 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002642 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002643 goto out;
2644
2645 /* Parse the FCF record from the non-embedded mailbox command */
2646 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2647 &next_fcf_index);
2648 if (!new_fcf_record) {
2649 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2650 "2767 Mailbox command READ_FCF_RECORD "
2651 "failed to retrieve a FCF record.\n");
2652 goto out;
2653 }
2654
2655 /* Check the connection list for eligibility */
2656 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2657 &addr_mode, &vlan_id);
2658
2659 /* Log the FCF record information if turned on */
2660 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2661 next_fcf_index);
2662
2663 if (!rc)
2664 goto out;
2665
2666 /* Update the eligible FCF record index bmask */
2667 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002668
2669 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002670
2671out:
2672 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2673}
2674
2675/**
James Smart76a95d72010-11-20 23:11:48 -05002676 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2677 * @phba: pointer to lpfc hba data structure.
2678 * @mboxq: pointer to mailbox data structure.
2679 *
2680 * This function handles completion of init vfi mailbox command.
2681 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002682static void
James Smart76a95d72010-11-20 23:11:48 -05002683lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2684{
2685 struct lpfc_vport *vport = mboxq->vport;
2686
James Smartdf9e1b52011-12-13 13:22:17 -05002687 /*
2688 * VFI not supported on interface type 0, just do the flogi
2689 * Also continue if the VFI is in use - just use the same one.
2690 */
2691 if (mboxq->u.mb.mbxStatus &&
2692 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2693 LPFC_SLI_INTF_IF_TYPE_0) &&
2694 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002695 lpfc_printf_vlog(vport, KERN_ERR,
2696 LOG_MBOX,
2697 "2891 Init VFI mailbox failed 0x%x\n",
2698 mboxq->u.mb.mbxStatus);
2699 mempool_free(mboxq, phba->mbox_mem_pool);
2700 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2701 return;
2702 }
James Smart73d91e52011-10-10 21:32:10 -04002703
James Smart76a95d72010-11-20 23:11:48 -05002704 lpfc_initial_flogi(vport);
2705 mempool_free(mboxq, phba->mbox_mem_pool);
2706 return;
2707}
2708
2709/**
2710 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2711 * @vport: pointer to lpfc_vport data structure.
2712 *
2713 * This function issue a init_vfi mailbox command to initialize the VFI and
2714 * VPI for the physical port.
2715 */
2716void
2717lpfc_issue_init_vfi(struct lpfc_vport *vport)
2718{
2719 LPFC_MBOXQ_t *mboxq;
2720 int rc;
2721 struct lpfc_hba *phba = vport->phba;
2722
2723 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2724 if (!mboxq) {
2725 lpfc_printf_vlog(vport, KERN_ERR,
2726 LOG_MBOX, "2892 Failed to allocate "
2727 "init_vfi mailbox\n");
2728 return;
2729 }
2730 lpfc_init_vfi(mboxq, vport);
2731 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2732 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2733 if (rc == MBX_NOT_FINISHED) {
2734 lpfc_printf_vlog(vport, KERN_ERR,
2735 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2736 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2737 }
2738}
2739
2740/**
James Smart1c6834a2009-07-19 10:01:26 -04002741 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2742 * @phba: pointer to lpfc hba data structure.
2743 * @mboxq: pointer to mailbox data structure.
2744 *
2745 * This function handles completion of init vpi mailbox command.
2746 */
James Smart695a8142010-01-26 23:08:03 -05002747void
James Smart1c6834a2009-07-19 10:01:26 -04002748lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2749{
2750 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002751 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002752 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2753
James Smart1c6834a2009-07-19 10:01:26 -04002754 if (mboxq->u.mb.mbxStatus) {
2755 lpfc_printf_vlog(vport, KERN_ERR,
2756 LOG_MBOX,
2757 "2609 Init VPI mailbox failed 0x%x\n",
2758 mboxq->u.mb.mbxStatus);
2759 mempool_free(mboxq, phba->mbox_mem_pool);
2760 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2761 return;
2762 }
James Smart72100cc2010-02-12 14:43:01 -05002763 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002764 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002765 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002766
James Smart695a8142010-01-26 23:08:03 -05002767 /* If this port is physical port or FDISC is done, do reg_vpi */
2768 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2769 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2770 if (!ndlp)
2771 lpfc_printf_vlog(vport, KERN_ERR,
2772 LOG_DISCOVERY,
2773 "2731 Cannot find fabric "
2774 "controller node\n");
2775 else
2776 lpfc_register_new_vport(phba, vport, ndlp);
2777 mempool_free(mboxq, phba->mbox_mem_pool);
2778 return;
2779 }
2780
James Smart1c6834a2009-07-19 10:01:26 -04002781 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2782 lpfc_initial_fdisc(vport);
2783 else {
2784 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002785 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2786 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002787 }
James Smart695a8142010-01-26 23:08:03 -05002788 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002789 return;
2790}
2791
2792/**
James Smartecfd03c2010-02-12 14:41:27 -05002793 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2794 * @vport: pointer to lpfc_vport data structure.
2795 *
2796 * This function issue a init_vpi mailbox command to initialize
2797 * VPI for the vport.
2798 */
2799void
2800lpfc_issue_init_vpi(struct lpfc_vport *vport)
2801{
2802 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002803 int rc, vpi;
2804
2805 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2806 vpi = lpfc_alloc_vpi(vport->phba);
2807 if (!vpi) {
2808 lpfc_printf_vlog(vport, KERN_ERR,
2809 LOG_MBOX,
2810 "3303 Failed to obtain vport vpi\n");
2811 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2812 return;
2813 }
2814 vport->vpi = vpi;
2815 }
James Smartecfd03c2010-02-12 14:41:27 -05002816
2817 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2818 if (!mboxq) {
2819 lpfc_printf_vlog(vport, KERN_ERR,
2820 LOG_MBOX, "2607 Failed to allocate "
2821 "init_vpi mailbox\n");
2822 return;
2823 }
2824 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2825 mboxq->vport = vport;
2826 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2827 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2828 if (rc == MBX_NOT_FINISHED) {
2829 lpfc_printf_vlog(vport, KERN_ERR,
2830 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2831 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2832 }
2833}
2834
2835/**
James Smart6fb120a2009-05-22 14:52:59 -04002836 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2837 * @phba: pointer to lpfc hba data structure.
2838 *
2839 * This function loops through the list of vports on the @phba and issues an
2840 * FDISC if possible.
2841 */
2842void
2843lpfc_start_fdiscs(struct lpfc_hba *phba)
2844{
2845 struct lpfc_vport **vports;
2846 int i;
2847
2848 vports = lpfc_create_vport_work_array(phba);
2849 if (vports != NULL) {
2850 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2851 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2852 continue;
2853 /* There are no vpi for this vport */
2854 if (vports[i]->vpi > phba->max_vpi) {
2855 lpfc_vport_set_state(vports[i],
2856 FC_VPORT_FAILED);
2857 continue;
2858 }
James Smart76a95d72010-11-20 23:11:48 -05002859 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002860 lpfc_vport_set_state(vports[i],
2861 FC_VPORT_LINKDOWN);
2862 continue;
2863 }
James Smart1c6834a2009-07-19 10:01:26 -04002864 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002865 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002866 continue;
2867 }
James Smart6fb120a2009-05-22 14:52:59 -04002868 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2869 lpfc_initial_fdisc(vports[i]);
2870 else {
2871 lpfc_vport_set_state(vports[i],
2872 FC_VPORT_NO_FABRIC_SUPP);
2873 lpfc_printf_vlog(vports[i], KERN_ERR,
2874 LOG_ELS,
2875 "0259 No NPIV "
2876 "Fabric support\n");
2877 }
2878 }
2879 }
2880 lpfc_destroy_vport_work_array(phba, vports);
2881}
2882
2883void
2884lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2885{
2886 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2887 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002888 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002889
James Smartf5eca9b2012-03-01 22:37:54 -05002890 /*
2891 * VFI not supported for interface type 0, so ignore any mailbox
2892 * error (except VFI in use) and continue with the discovery.
2893 */
2894 if (mboxq->u.mb.mbxStatus &&
2895 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2896 LPFC_SLI_INTF_IF_TYPE_0) &&
2897 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002898 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2899 "2018 REG_VFI mbxStatus error x%x "
2900 "HBA state x%x\n",
2901 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002902 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002903 /* FLOGI failed, use loop map to make discovery list */
2904 lpfc_disc_list_loopmap(vport);
2905 /* Start discovery */
2906 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002907 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002908 }
2909 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002910 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002911 }
James Smartae05ebe2013-03-01 16:35:38 -05002912
James Smarte74c03c2013-04-17 20:15:19 -04002913 /* If the VFI is already registered, there is nothing else to do
2914 * Unless this was a VFI update and we are in PT2PT mode, then
2915 * we should drop through to set the port state to ready.
2916 */
James Smartae05ebe2013-03-01 16:35:38 -05002917 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002918 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2919 vport->fc_flag & FC_PT2PT))
2920 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002921
James Smartc8685952009-11-18 15:39:16 -05002922 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002923 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002924 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002925 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002926 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002927 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002928 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002929
James Smart1b511972011-12-13 13:23:09 -05002930 /* In case SLI4 FC loopback test, we are ready */
2931 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2932 (phba->link_flag & LS_LOOPBACK_MODE)) {
2933 phba->link_state = LPFC_HBA_READY;
2934 goto out_free_mem;
2935 }
2936
James Smarte74c03c2013-04-17 20:15:19 -04002937 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2938 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2939 "alpacnt:%d LinkState:%x topology:%x\n",
2940 vport->port_state, vport->fc_flag, vport->fc_myDID,
2941 vport->phba->alpa_map[0],
2942 phba->link_state, phba->fc_topology);
2943
James Smart6fb120a2009-05-22 14:52:59 -04002944 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002945 /*
2946 * For private loop or for NPort pt2pt,
2947 * just start discovery and we are done.
2948 */
2949 if ((vport->fc_flag & FC_PT2PT) ||
2950 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2951 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2952
James Smart76a95d72010-11-20 23:11:48 -05002953 /* Use loop map to make discovery list */
2954 lpfc_disc_list_loopmap(vport);
2955 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002956 if (vport->fc_flag & FC_PT2PT)
2957 vport->port_state = LPFC_VPORT_READY;
2958 else
2959 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002960 } else {
2961 lpfc_start_fdiscs(phba);
2962 lpfc_do_scr_ns_plogi(phba, vport);
2963 }
James Smart6fb120a2009-05-22 14:52:59 -04002964 }
2965
James Smart1b511972011-12-13 13:23:09 -05002966out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002967 mempool_free(mboxq, phba->mbox_mem_pool);
2968 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2969 kfree(dmabuf);
2970 return;
2971}
2972
2973static void
James Smart2e0fef82007-06-17 19:56:36 -05002974lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002975{
James Smart6fb120a2009-05-22 14:52:59 -04002976 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002977 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002978 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002979
2980
2981 /* Check for error */
2982 if (mb->mbxStatus) {
2983 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002984 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2985 "0319 READ_SPARAM mbxStatus error x%x "
2986 "hba state x%x>\n",
2987 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002988 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002989 goto out;
2990 }
2991
James Smart2e0fef82007-06-17 19:56:36 -05002992 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002993 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04002994 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002995 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2996 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2997 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2998 }
2999
dea31012005-04-17 16:05:31 -05003000 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3001 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003002 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003003 return;
3004
3005out:
3006 pmb->context1 = NULL;
3007 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3008 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003009 lpfc_issue_clear_la(phba, vport);
3010 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003011 return;
3012}
3013
3014static void
James Smart76a95d72010-11-20 23:11:48 -05003015lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003016{
James Smart92d7f7b2007-06-17 19:56:38 -05003017 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003018 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003019 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003020 int i;
James Smart14691152006-12-02 13:34:28 -05003021 struct lpfc_dmabuf *mp;
3022 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003023 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003024 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003025
James Smart92d7f7b2007-06-17 19:56:38 -05003026 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003027 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3028 case LPFC_LINK_SPEED_1GHZ:
3029 case LPFC_LINK_SPEED_2GHZ:
3030 case LPFC_LINK_SPEED_4GHZ:
3031 case LPFC_LINK_SPEED_8GHZ:
3032 case LPFC_LINK_SPEED_10GHZ:
3033 case LPFC_LINK_SPEED_16GHZ:
3034 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04003035 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003036 default:
James Smart76a95d72010-11-20 23:11:48 -05003037 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05003038 break;
dea31012005-04-17 16:05:31 -05003039 }
3040
James Smarte74c03c2013-04-17 20:15:19 -04003041 if (phba->fc_topology &&
3042 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3043 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3044 "3314 Toplogy changed was 0x%x is 0x%x\n",
3045 phba->fc_topology,
3046 bf_get(lpfc_mbx_read_top_topology, la));
3047 phba->fc_topology_changed = 1;
3048 }
3049
James Smart76a95d72010-11-20 23:11:48 -05003050 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003051 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003052
James Smartdf9e1b52011-12-13 13:22:17 -05003053 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003054 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003055 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003056
James Smart6a9c52c2009-10-02 15:16:51 -04003057 /* if npiv is enabled and this adapter supports npiv log
3058 * a message that npiv is not supported in this topology
3059 */
3060 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003061 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3062 "1309 Link Up Event npiv not supported in loop "
3063 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003064 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003065 if (bf_get(lpfc_mbx_read_top_il, la))
3066 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003067
James Smart76a95d72010-11-20 23:11:48 -05003068 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3069 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003070
3071 if (i == 0) {
3072 phba->alpa_map[0] = 0;
3073 } else {
James Smarte8b62012007-08-02 11:10:09 -04003074 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003075 int numalpa, j, k;
3076 union {
3077 uint8_t pamap[16];
3078 struct {
3079 uint32_t wd1;
3080 uint32_t wd2;
3081 uint32_t wd3;
3082 uint32_t wd4;
3083 } pa;
3084 } un;
3085 numalpa = phba->alpa_map[0];
3086 j = 0;
3087 while (j < numalpa) {
3088 memset(un.pamap, 0, 16);
3089 for (k = 1; j < numalpa; k++) {
3090 un.pamap[k - 1] =
3091 phba->alpa_map[j + 1];
3092 j++;
3093 if (k == 16)
3094 break;
3095 }
3096 /* Link Up Event ALPA map */
3097 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003098 KERN_WARNING,
3099 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003100 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003101 "ALPA map Data: x%x "
3102 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003103 un.pa.wd1, un.pa.wd2,
3104 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003105 }
3106 }
3107 }
3108 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003109 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003110 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003111 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003112 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3113 }
James Smart2e0fef82007-06-17 19:56:36 -05003114 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003115 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003116 }
James Smart92d7f7b2007-06-17 19:56:38 -05003117 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003118
James Smart38c20672013-03-01 16:37:44 -05003119 if (fc_flags) {
3120 spin_lock_irq(shost->host_lock);
3121 vport->fc_flag |= fc_flags;
3122 spin_unlock_irq(shost->host_lock);
3123 }
3124
dea31012005-04-17 16:05:31 -05003125 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003126 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3127 if (!sparam_mbox)
3128 goto out;
3129
3130 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3131 if (rc) {
3132 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3133 goto out;
3134 }
3135 sparam_mbox->vport = vport;
3136 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3137 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3138 if (rc == MBX_NOT_FINISHED) {
3139 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3140 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3141 kfree(mp);
3142 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3143 goto out;
dea31012005-04-17 16:05:31 -05003144 }
3145
James Smart76a95d72010-11-20 23:11:48 -05003146 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003147 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3148 if (!cfglink_mbox)
3149 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003150 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003151 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003152 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003153 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003154 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003155 if (rc == MBX_NOT_FINISHED) {
3156 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3157 goto out;
3158 }
3159 } else {
James Smart32b97932009-07-19 10:01:21 -04003160 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003161 /*
3162 * Add the driver's default FCF record at FCF index 0 now. This
3163 * is phase 1 implementation that support FCF index 0 and driver
3164 * defaults.
3165 */
James Smart45ed1192009-10-02 15:17:02 -04003166 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003167 fcf_record = kzalloc(sizeof(struct fcf_record),
3168 GFP_KERNEL);
3169 if (unlikely(!fcf_record)) {
3170 lpfc_printf_log(phba, KERN_ERR,
3171 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003172 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003173 "fcf record\n");
3174 rc = -ENODEV;
3175 goto out;
3176 }
3177
3178 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3179 LPFC_FCOE_FCF_DEF_INDEX);
3180 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3181 if (unlikely(rc)) {
3182 lpfc_printf_log(phba, KERN_ERR,
3183 LOG_MBOX | LOG_SLI,
3184 "2013 Could not manually add FCF "
3185 "record 0, status %d\n", rc);
3186 rc = -ENODEV;
3187 kfree(fcf_record);
3188 goto out;
3189 }
3190 kfree(fcf_record);
3191 }
3192 /*
3193 * The driver is expected to do FIP/FCF. Call the port
3194 * and get the FCF Table.
3195 */
James Smart32b97932009-07-19 10:01:21 -04003196 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003197 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003198 spin_unlock_irq(&phba->hbalock);
3199 return;
3200 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003201 /* This is the initial FCF discovery scan */
3202 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003203 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003204 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3205 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003206 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3207 LPFC_FCOE_FCF_GET_FIRST);
3208 if (rc) {
3209 spin_lock_irq(&phba->hbalock);
3210 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3211 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003212 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003213 }
James Smart38b92ef2010-08-04 16:11:39 -04003214 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003215 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003216 }
James Smart6fb120a2009-05-22 14:52:59 -04003217
3218 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003219out:
3220 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003221 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3222 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3223 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003224 lpfc_issue_clear_la(phba, vport);
3225 return;
dea31012005-04-17 16:05:31 -05003226}
3227
3228static void
James Smart84774a42008-08-24 21:50:06 -04003229lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003230{
dea31012005-04-17 16:05:31 -05003231 uint32_t control;
3232 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003233 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003234 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003235 if (phba->sli_rev <= LPFC_SLI_REV3) {
3236 control = readl(phba->HCregaddr);
3237 control |= HC_LAINT_ENA;
3238 writel(control, phba->HCregaddr);
3239 readl(phba->HCregaddr); /* flush */
3240 }
James Smart2e0fef82007-06-17 19:56:36 -05003241 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003242}
3243
James Smart84774a42008-08-24 21:50:06 -04003244static void
3245lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3246{
3247 lpfc_linkdown(phba);
3248 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003249 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003250 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3251}
3252
3253
dea31012005-04-17 16:05:31 -05003254/*
James Smart76a95d72010-11-20 23:11:48 -05003255 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003256 * command upon completion. It is setup in the LPFC_MBOXQ
3257 * as the completion routine when the command is
3258 * handed off to the SLI layer.
3259 */
3260void
James Smart76a95d72010-11-20 23:11:48 -05003261lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003262{
James Smart2e0fef82007-06-17 19:56:36 -05003263 struct lpfc_vport *vport = pmb->vport;
3264 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003265 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003266 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003267 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3268
James Smart0d2b6b82008-06-14 22:52:47 -04003269 /* Unblock ELS traffic */
3270 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003271 /* Check for error */
3272 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003273 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003274 "1307 READ_LA mbox error x%x state x%x\n",
3275 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003276 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003277 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003278 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003279 }
3280
James Smart76a95d72010-11-20 23:11:48 -05003281 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003282
3283 memcpy(&phba->alpa_map[0], mp->virt, 128);
3284
James Smart2e0fef82007-06-17 19:56:36 -05003285 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003286 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003287 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003288 else
James Smart2e0fef82007-06-17 19:56:36 -05003289 vport->fc_flag &= ~FC_BYPASSED_MODE;
3290 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003291
James Smartbe0c0082013-03-01 16:38:14 -05003292 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003293 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003294 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003295 if (phba->fc_eventTag != 0)
3296 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003297 }
dea31012005-04-17 16:05:31 -05003298
3299 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003300 if (phba->sli_rev < LPFC_SLI_REV4) {
3301 spin_lock_irq(&phba->hbalock);
3302 if (bf_get(lpfc_mbx_read_top_mm, la))
3303 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3304 else
3305 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3306 spin_unlock_irq(&phba->hbalock);
3307 }
dea31012005-04-17 16:05:31 -05003308
James Smart4d9ab992009-10-02 15:16:39 -04003309 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003310 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003311 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003312 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003313 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003314 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003315 "1306 Link Up Event in loop back mode "
3316 "x%x received Data: x%x x%x x%x x%x\n",
3317 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003318 bf_get(lpfc_mbx_read_top_alpa_granted,
3319 la),
3320 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003321 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003322 } else {
3323 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003324 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003325 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003326 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003327 bf_get(lpfc_mbx_read_top_alpa_granted,
3328 la),
3329 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003330 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003331 bf_get(lpfc_mbx_read_top_mm, la),
3332 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003333 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003334 }
James Smart92d7f7b2007-06-17 19:56:38 -05003335 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003336 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3337 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003338 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003339 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003340 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3341 "1308 Link Down Event in loop back mode "
3342 "x%x received "
3343 "Data: x%x x%x x%x\n",
3344 la->eventTag, phba->fc_eventTag,
3345 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003346 else
James Smart3163f722008-02-08 18:50:25 -05003347 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003348 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003349 "Data: x%x x%x x%x x%x x%x\n",
3350 la->eventTag, phba->fc_eventTag,
3351 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003352 bf_get(lpfc_mbx_read_top_mm, la),
3353 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003354 lpfc_mbx_issue_link_down(phba);
3355 }
James Smartbe0c0082013-03-01 16:38:14 -05003356 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3357 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003358 if (phba->link_state != LPFC_LINK_DOWN) {
3359 phba->fc_stat.LinkDown++;
3360 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3361 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003362 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003363 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003364 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003365 lpfc_mbx_issue_link_down(phba);
3366 } else
3367 lpfc_enable_la(phba);
3368
3369 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3370 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3371 "Data: x%x x%x x%x\n",
3372 la->eventTag, phba->fc_eventTag,
3373 phba->pport->port_state, vport->fc_flag);
3374 /*
3375 * The cmnd that triggered this will be waiting for this
3376 * signal.
3377 */
3378 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3379 if (phba->wait_4_mlo_maint_flg) {
3380 phba->wait_4_mlo_maint_flg = 0;
3381 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003382 }
James Smart84774a42008-08-24 21:50:06 -04003383 }
3384
James Smartbe0c0082013-03-01 16:38:14 -05003385 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3386 bf_get(lpfc_mbx_read_top_fa, la)) {
3387 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003388 lpfc_issue_clear_la(phba, vport);
3389 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003390 "1311 fa %d\n",
3391 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003392 }
3393
James Smart76a95d72010-11-20 23:11:48 -05003394lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003395 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3396 kfree(mp);
3397 mempool_free(pmb, phba->mbox_mem_pool);
3398 return;
3399}
3400
3401/*
3402 * This routine handles processing a REG_LOGIN mailbox
3403 * command upon completion. It is setup in the LPFC_MBOXQ
3404 * as the completion routine when the command is
3405 * handed off to the SLI layer.
3406 */
3407void
James Smart2e0fef82007-06-17 19:56:36 -05003408lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003409{
James Smart2e0fef82007-06-17 19:56:36 -05003410 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003411 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003412 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003413 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003414
dea31012005-04-17 16:05:31 -05003415 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003416 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003417
James Smartffc95492010-06-07 15:23:17 -04003418 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3419 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3420
James Smart40426292010-12-15 17:58:10 -05003421 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3422 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003423 /* We rcvd a rscn after issuing this
3424 * mbox reg login, we may have cycled
3425 * back through the state and be
3426 * back at reg login state so this
3427 * mbox needs to be ignored becase
3428 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003429 * process.
James Smartffc95492010-06-07 15:23:17 -04003430 */
3431 spin_lock_irq(shost->host_lock);
3432 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3433 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003434 } else
3435 /* Good status, call state machine */
3436 lpfc_disc_state_machine(vport, ndlp, pmb,
3437 NLP_EVT_CMPL_REG_LOGIN);
3438
dea31012005-04-17 16:05:31 -05003439 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3440 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003441 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003442 /* decrement the node reference count held for this callback
3443 * function.
3444 */
James Smart329f9bc2007-04-25 09:53:01 -04003445 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003446
3447 return;
3448}
3449
James Smart92d7f7b2007-06-17 19:56:38 -05003450static void
3451lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3452{
James Smart04c68492009-05-22 14:52:52 -04003453 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003454 struct lpfc_vport *vport = pmb->vport;
3455 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3456
3457 switch (mb->mbxStatus) {
3458 case 0x0011:
3459 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003460 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3461 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3462 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003463 break;
James Smart78730cf2010-04-06 15:06:30 -04003464 /* If VPI is busy, reset the HBA */
3465 case 0x9700:
3466 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3467 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3468 vport->vpi, mb->mbxStatus);
3469 if (!(phba->pport->load_flag & FC_UNLOADING))
3470 lpfc_workq_post_event(phba, NULL, NULL,
3471 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003472 }
James Smart72100cc2010-02-12 14:43:01 -05003473 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003474 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003475 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003476 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003477 vport->unreg_vpi_cmpl = VPORT_OK;
3478 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003479 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003480 /*
3481 * This shost reference might have been taken at the beginning of
3482 * lpfc_vport_delete()
3483 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003484 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003485 scsi_host_put(shost);
3486}
3487
James Smartd7c255b2008-08-24 21:50:00 -04003488int
James Smart92d7f7b2007-06-17 19:56:38 -05003489lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3490{
3491 struct lpfc_hba *phba = vport->phba;
3492 LPFC_MBOXQ_t *mbox;
3493 int rc;
3494
3495 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3496 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003497 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003498
3499 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3500 mbox->vport = vport;
3501 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003502 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003503 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003504 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3505 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003506 mempool_free(mbox, phba->mbox_mem_pool);
3507 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003508 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003509 }
James Smartd7c255b2008-08-24 21:50:00 -04003510 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003511}
3512
3513static void
3514lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3515{
3516 struct lpfc_vport *vport = pmb->vport;
3517 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003518 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003519
3520 switch (mb->mbxStatus) {
3521 case 0x0011:
3522 case 0x9601:
3523 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003524 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3525 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3526 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003527 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3528 spin_lock_irq(shost->host_lock);
3529 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3530 spin_unlock_irq(shost->host_lock);
3531 vport->fc_myDID = 0;
3532 goto out;
3533 }
James Smart92d7f7b2007-06-17 19:56:38 -05003534
James Smart72100cc2010-02-12 14:43:01 -05003535 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003536 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003537 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003538 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003539 vport->num_disc_nodes = 0;
3540 /* go thru NPR list and issue ELS PLOGIs */
3541 if (vport->fc_npr_cnt)
3542 lpfc_els_disc_plogi(vport);
3543
3544 if (!vport->num_disc_nodes) {
3545 spin_lock_irq(shost->host_lock);
3546 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3547 spin_unlock_irq(shost->host_lock);
3548 lpfc_can_disctmo(vport);
3549 }
3550 vport->port_state = LPFC_VPORT_READY;
3551
3552out:
3553 mempool_free(pmb, phba->mbox_mem_pool);
3554 return;
3555}
3556
James Smart21e9a0a2009-05-22 14:53:21 -04003557/**
3558 * lpfc_create_static_vport - Read HBA config region to create static vports.
3559 * @phba: pointer to lpfc hba data structure.
3560 *
3561 * This routine issue a DUMP mailbox command for config region 22 to get
3562 * the list of static vports to be created. The function create vports
3563 * based on the information returned from the HBA.
3564 **/
3565void
3566lpfc_create_static_vport(struct lpfc_hba *phba)
3567{
3568 LPFC_MBOXQ_t *pmb = NULL;
3569 MAILBOX_t *mb;
3570 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003571 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003572 struct fc_vport_identifiers vport_id;
3573 struct fc_vport *new_fc_vport;
3574 struct Scsi_Host *shost;
3575 struct lpfc_vport *vport;
3576 uint16_t offset = 0;
3577 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003578 struct lpfc_dmabuf *mp;
3579 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003580
3581 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3582 if (!pmb) {
3583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3584 "0542 lpfc_create_static_vport failed to"
3585 " allocate mailbox memory\n");
3586 return;
3587 }
James Smartcdcc2342012-08-14 14:26:35 -04003588 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003589 mb = &pmb->u.mb;
3590
3591 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3592 if (!vport_info) {
3593 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3594 "0543 lpfc_create_static_vport failed to"
3595 " allocate vport_info\n");
3596 mempool_free(pmb, phba->mbox_mem_pool);
3597 return;
3598 }
3599
3600 vport_buff = (uint8_t *) vport_info;
3601 do {
James Smartcdcc2342012-08-14 14:26:35 -04003602 /* free dma buffer from previous round */
3603 if (pmb->context1) {
3604 mp = (struct lpfc_dmabuf *)pmb->context1;
3605 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3606 kfree(mp);
3607 }
James Smart1c6834a2009-07-19 10:01:26 -04003608 if (lpfc_dump_static_vport(phba, pmb, offset))
3609 goto out;
3610
James Smart21e9a0a2009-05-22 14:53:21 -04003611 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003612 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3613 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003614
James Smartcdcc2342012-08-14 14:26:35 -04003615 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003616 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3617 "0544 lpfc_create_static_vport failed to"
3618 " issue dump mailbox command ret 0x%x "
3619 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003620 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003621 goto out;
3622 }
3623
James Smart1c6834a2009-07-19 10:01:26 -04003624 if (phba->sli_rev == LPFC_SLI_REV4) {
3625 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003626 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003627 if (byte_count > sizeof(struct static_vport_info) -
3628 offset)
3629 byte_count = sizeof(struct static_vport_info)
3630 - offset;
3631 memcpy(vport_buff + offset, mp->virt, byte_count);
3632 offset += byte_count;
3633 } else {
3634 if (mb->un.varDmp.word_cnt >
3635 sizeof(struct static_vport_info) - offset)
3636 mb->un.varDmp.word_cnt =
3637 sizeof(struct static_vport_info)
3638 - offset;
3639 byte_count = mb->un.varDmp.word_cnt;
3640 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3641 vport_buff + offset,
3642 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003643
James Smart1c6834a2009-07-19 10:01:26 -04003644 offset += byte_count;
3645 }
James Smart21e9a0a2009-05-22 14:53:21 -04003646
James Smart1c6834a2009-07-19 10:01:26 -04003647 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003648 offset < sizeof(struct static_vport_info));
3649
3650
3651 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3652 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3653 != VPORT_INFO_REV)) {
3654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3655 "0545 lpfc_create_static_vport bad"
3656 " information header 0x%x 0x%x\n",
3657 le32_to_cpu(vport_info->signature),
3658 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3659
3660 goto out;
3661 }
3662
3663 shost = lpfc_shost_from_vport(phba->pport);
3664
3665 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3666 memset(&vport_id, 0, sizeof(vport_id));
3667 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3668 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3669 if (!vport_id.port_name || !vport_id.node_name)
3670 continue;
3671
3672 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3673 vport_id.vport_type = FC_PORTTYPE_NPIV;
3674 vport_id.disable = false;
3675 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3676
3677 if (!new_fc_vport) {
3678 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3679 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003680 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003681 continue;
3682 }
3683
3684 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3685 vport->vport_flag |= STATIC_VPORT;
3686 }
3687
3688out:
James Smart21e9a0a2009-05-22 14:53:21 -04003689 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003690 if (mbx_wait_rc != MBX_TIMEOUT) {
3691 if (pmb->context1) {
3692 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003693 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3694 kfree(mp);
3695 }
James Smart21e9a0a2009-05-22 14:53:21 -04003696 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003697 }
James Smart21e9a0a2009-05-22 14:53:21 -04003698
3699 return;
3700}
3701
dea31012005-04-17 16:05:31 -05003702/*
3703 * This routine handles processing a Fabric REG_LOGIN mailbox
3704 * command upon completion. It is setup in the LPFC_MBOXQ
3705 * as the completion routine when the command is
3706 * handed off to the SLI layer.
3707 */
3708void
James Smart2e0fef82007-06-17 19:56:36 -05003709lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003710{
James Smart92d7f7b2007-06-17 19:56:38 -05003711 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003712 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003713 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003714 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003715 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003716
James Smart549e55c2007-08-02 11:09:51 -04003717 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003718 pmb->context1 = NULL;
3719 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003720
dea31012005-04-17 16:05:31 -05003721 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003722 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3723 "0258 Register Fabric login error: 0x%x\n",
3724 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003725 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3726 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003727 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003728
James Smart76a95d72010-11-20 23:11:48 -05003729 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003730 /* FLOGI failed, use loop map to make discovery list */
3731 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003732
James Smart92d7f7b2007-06-17 19:56:38 -05003733 /* Start discovery */
3734 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003735 /* Decrement the reference count to ndlp after the
3736 * reference to the ndlp are done.
3737 */
3738 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003739 return;
3740 }
3741
3742 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003743 /* Decrement the reference count to ndlp after the reference
3744 * to the ndlp are done.
3745 */
3746 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003747 return;
3748 }
3749
James Smart6d368e52011-05-24 11:44:12 -04003750 if (phba->sli_rev < LPFC_SLI_REV4)
3751 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003752 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003753 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003754 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003755
James Smart2e0fef82007-06-17 19:56:36 -05003756 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003757 /* when physical port receive logo donot start
3758 * vport discovery */
3759 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3760 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003761 else {
3762 shost = lpfc_shost_from_vport(vport);
3763 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003764 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003765 spin_unlock_irq(shost->host_lock);
3766 }
James Smart92d7f7b2007-06-17 19:56:38 -05003767 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003768 }
3769
3770 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3771 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003772 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003773
3774 /* Drop the reference count from the mbox at the end after
3775 * all the current reference to the ndlp have been done.
3776 */
3777 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003778 return;
3779}
3780
3781/*
3782 * This routine handles processing a NameServer REG_LOGIN mailbox
3783 * command upon completion. It is setup in the LPFC_MBOXQ
3784 * as the completion routine when the command is
3785 * handed off to the SLI layer.
3786 */
3787void
James Smart2e0fef82007-06-17 19:56:36 -05003788lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003789{
James Smart21e9a0a2009-05-22 14:53:21 -04003790 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003791 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3792 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3793 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003794
James Smartd439d282010-09-29 11:18:45 -04003795 pmb->context1 = NULL;
3796 pmb->context2 = NULL;
3797
dea31012005-04-17 16:05:31 -05003798 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003799out:
James Smart21e9a0a2009-05-22 14:53:21 -04003800 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3801 "0260 Register NameServer error: 0x%x\n",
3802 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003803 /* decrement the node reference count held for this
3804 * callback function.
3805 */
James Smart329f9bc2007-04-25 09:53:01 -04003806 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003807 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3808 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003809 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003810
3811 /* If no other thread is using the ndlp, free it */
3812 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003813
James Smart76a95d72010-11-20 23:11:48 -05003814 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003815 /*
3816 * RegLogin failed, use loop map to make discovery
3817 * list
3818 */
3819 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003820
James Smart92d7f7b2007-06-17 19:56:38 -05003821 /* Start discovery */
3822 lpfc_disc_start(vport);
3823 return;
3824 }
3825 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003826 return;
3827 }
3828
James Smart6d368e52011-05-24 11:44:12 -04003829 if (phba->sli_rev < LPFC_SLI_REV4)
3830 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003831 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003832 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003833 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003834
James Smart2e0fef82007-06-17 19:56:36 -05003835 if (vport->port_state < LPFC_VPORT_READY) {
3836 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003837 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3838 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3839 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3840 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3841 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3842
3843 /* Issue SCR just before NameServer GID_FT Query */
3844 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003845 }
3846
James Smart2e0fef82007-06-17 19:56:36 -05003847 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003848 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003849 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003850 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003851 goto out;
dea31012005-04-17 16:05:31 -05003852 }
3853
James Smartfa4066b2008-01-11 01:53:27 -05003854 /* decrement the node reference count held for this
3855 * callback function.
3856 */
James Smart329f9bc2007-04-25 09:53:01 -04003857 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003858 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3859 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003860 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003861
3862 return;
3863}
3864
3865static void
James Smart2e0fef82007-06-17 19:56:36 -05003866lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003867{
James Smart2e0fef82007-06-17 19:56:36 -05003868 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3869 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003870 struct lpfc_rport_data *rdata;
3871 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003872 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003873
3874 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003875 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3876 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003877 rport_ids.port_id = ndlp->nlp_DID;
3878 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003879
James Smart329f9bc2007-04-25 09:53:01 -04003880 /*
3881 * We leave our node pointer in rport->dd_data when we unregister a
3882 * FCP target port. But fc_remote_port_add zeros the space to which
3883 * rport->dd_data points. So, if we're reusing a previously
3884 * registered port, drop the reference that we took the last time we
3885 * registered the port.
3886 */
3887 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003888 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003889 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003890
3891 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3892 "rport add: did:x%x flg:x%x type x%x",
3893 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3894
James Smart9589b0622011-04-16 11:03:17 -04003895 /* Don't add the remote port if unloading. */
3896 if (vport->load_flag & FC_UNLOADING)
3897 return;
3898
James Smart2e0fef82007-06-17 19:56:36 -05003899 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003900 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003901 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3902 "Warning: fc_remote_port_add failed\n");
3903 return;
3904 }
3905
3906 /* initialize static port data */
3907 rport->maxframe_size = ndlp->nlp_maxframe;
3908 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003909 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003910 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003911
3912 if (ndlp->nlp_type & NLP_FCP_TARGET)
3913 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3914 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3915 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3916
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003917 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3918 fc_remote_port_rolechg(rport, rport_ids.roles);
3919
James Smart34f5ad82012-08-03 12:35:03 -04003920 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3921 "3183 rport register x%06x, rport %p role x%x\n",
3922 ndlp->nlp_DID, rport, rport_ids.roles);
3923
James Smart071fbd3d2006-04-15 11:53:20 -04003924 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003925 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003926 ndlp->nlp_sid = rport->scsi_target_id;
3927 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003928 return;
3929}
3930
3931static void
James Smart2e0fef82007-06-17 19:56:36 -05003932lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003933{
3934 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003935
James Smart858c9f62007-06-17 19:56:39 -05003936 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3937 "rport delete: did:x%x flg:x%x type x%x",
3938 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3939
James Smart34f5ad82012-08-03 12:35:03 -04003940 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3941 "3184 rport unregister x%06x, rport %p\n",
3942 ndlp->nlp_DID, rport);
3943
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003944 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003945
3946 return;
3947}
3948
James Smartde0c5b32007-04-25 09:52:27 -04003949static void
James Smart2e0fef82007-06-17 19:56:36 -05003950lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003951{
James Smart2e0fef82007-06-17 19:56:36 -05003952 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3953
3954 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003955 switch (state) {
3956 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003957 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003958 break;
3959 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003960 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003961 break;
3962 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003963 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003964 break;
3965 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003966 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003967 break;
3968 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003969 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003970 break;
3971 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003972 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003973 break;
3974 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003975 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003976 break;
3977 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05003978 if (vport->fc_npr_cnt == 0 && count == -1)
3979 vport->fc_npr_cnt = 0;
3980 else
3981 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003982 break;
3983 }
James Smart2e0fef82007-06-17 19:56:36 -05003984 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003985}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003986
James Smartde0c5b32007-04-25 09:52:27 -04003987static void
James Smart2e0fef82007-06-17 19:56:36 -05003988lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003989 int old_state, int new_state)
3990{
James Smart2e0fef82007-06-17 19:56:36 -05003991 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3992
James Smartde0c5b32007-04-25 09:52:27 -04003993 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003994 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3995 ndlp->nlp_type |= NLP_FC_NODE;
3996 }
3997 if (new_state == NLP_STE_MAPPED_NODE)
3998 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3999 if (new_state == NLP_STE_NPR_NODE)
4000 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004001
James Smartde0c5b32007-04-25 09:52:27 -04004002 /* Transport interface */
4003 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
4004 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004005 vport->phba->nport_event_cnt++;
4006 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004007 }
dea31012005-04-17 16:05:31 -05004008
James Smartde0c5b32007-04-25 09:52:27 -04004009 if (new_state == NLP_STE_MAPPED_NODE ||
4010 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05004011 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05004012 /*
4013 * Tell the fc transport about the port, if we haven't
4014 * already. If we have, and it's a scsi entity, be
4015 * sure to unblock any attached scsi devices
4016 */
4017 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004018 }
James Smartea2151b2008-09-07 11:52:10 -04004019 if ((new_state == NLP_STE_MAPPED_NODE) &&
4020 (vport->stat_data_enabled)) {
4021 /*
4022 * A new target is discovered, if there is no buffer for
4023 * statistical data collection allocate buffer.
4024 */
4025 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4026 sizeof(struct lpfc_scsicmd_bkt),
4027 GFP_KERNEL);
4028
4029 if (!ndlp->lat_data)
4030 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4031 "0286 lpfc_nlp_state_cleanup failed to "
4032 "allocate statistical data buffer DID "
4033 "0x%x\n", ndlp->nlp_DID);
4034 }
James Smart858c9f62007-06-17 19:56:39 -05004035 /*
4036 * if we added to Mapped list, but the remote port
4037 * registration failed or assigned a target id outside
4038 * our presentable range - move the node to the
4039 * Unmapped List
4040 */
James Smartde0c5b32007-04-25 09:52:27 -04004041 if (new_state == NLP_STE_MAPPED_NODE &&
4042 (!ndlp->rport ||
4043 ndlp->rport->scsi_target_id == -1 ||
4044 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004045 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004046 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004047 spin_unlock_irq(shost->host_lock);
4048 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004049 }
James Smartde0c5b32007-04-25 09:52:27 -04004050}
4051
James Smart685f0bf2007-04-25 09:53:08 -04004052static char *
4053lpfc_nlp_state_name(char *buffer, size_t size, int state)
4054{
4055 static char *states[] = {
4056 [NLP_STE_UNUSED_NODE] = "UNUSED",
4057 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4058 [NLP_STE_ADISC_ISSUE] = "ADISC",
4059 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4060 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004061 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004062 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4063 [NLP_STE_MAPPED_NODE] = "MAPPED",
4064 [NLP_STE_NPR_NODE] = "NPR",
4065 };
4066
James Smart311464e2007-08-02 11:10:37 -04004067 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004068 strlcpy(buffer, states[state], size);
4069 else
4070 snprintf(buffer, size, "unknown (%d)", state);
4071 return buffer;
4072}
4073
James Smartde0c5b32007-04-25 09:52:27 -04004074void
James Smart2e0fef82007-06-17 19:56:36 -05004075lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4076 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004077{
James Smart2e0fef82007-06-17 19:56:36 -05004078 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004079 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004080 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004081
James Smarte8b62012007-08-02 11:10:09 -04004082 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4083 "0904 NPort state transition x%06x, %s -> %s\n",
4084 ndlp->nlp_DID,
4085 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4086 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004087
4088 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4089 "node statechg did:x%x old:%d ste:%d",
4090 ndlp->nlp_DID, old_state, state);
4091
James Smartde0c5b32007-04-25 09:52:27 -04004092 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004093 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004094 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004095 if (old_state == NLP_STE_UNMAPPED_NODE) {
4096 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4097 ndlp->nlp_type &= ~NLP_FC_NODE;
4098 }
4099
James Smart685f0bf2007-04-25 09:53:08 -04004100 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004101 spin_lock_irq(shost->host_lock);
4102 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4103 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004104 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004105 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004106
4107 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004108 lpfc_nlp_counters(vport, state, 1);
4109 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004110}
4111
4112void
James Smarte47c9092008-02-08 18:49:26 -05004113lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4114{
4115 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4116
4117 if (list_empty(&ndlp->nlp_listp)) {
4118 spin_lock_irq(shost->host_lock);
4119 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4120 spin_unlock_irq(shost->host_lock);
4121 }
4122}
4123
4124void
James Smart2e0fef82007-06-17 19:56:36 -05004125lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004126{
James Smart2e0fef82007-06-17 19:56:36 -05004127 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4128
James Smart0d2b6b82008-06-14 22:52:47 -04004129 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004130 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004131 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4132 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004133 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004134 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004135 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004136 NLP_STE_UNUSED_NODE);
4137}
4138
Adrian Bunk4d9db012008-02-14 23:24:02 +02004139static void
James Smarte47c9092008-02-08 18:49:26 -05004140lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4141{
James Smart0d2b6b82008-06-14 22:52:47 -04004142 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004143 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4144 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4145 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4146 NLP_STE_UNUSED_NODE);
4147}
James Smart109f6ed2008-12-04 22:39:08 -05004148/**
James Smart3621a712009-04-06 18:47:14 -04004149 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004150 * @vport: Pointer to Virtual Port object.
4151 * @ndlp: Pointer to FC node object.
4152 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004153 *
4154 * This function is always called when node object need to be initialized.
4155 * It initializes all the fields of the node object. Although the reference
4156 * to phba from @ndlp can be obtained indirectly through it's reference to
4157 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4158 * to the life-span of the @ndlp might go beyond the existence of @vport as
4159 * the final release of ndlp is determined by its reference count. And, the
4160 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004161 **/
4162static inline void
4163lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4164 uint32_t did)
4165{
4166 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4167 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4168 init_timer(&ndlp->nlp_delayfunc);
4169 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4170 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4171 ndlp->nlp_DID = did;
4172 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004173 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004174 ndlp->nlp_sid = NLP_NO_SID;
4175 kref_init(&ndlp->kref);
4176 NLP_INT_NODE_ACT(ndlp);
4177 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004178 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004179}
James Smarte47c9092008-02-08 18:49:26 -05004180
4181struct lpfc_nodelist *
4182lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4183 int state)
4184{
4185 struct lpfc_hba *phba = vport->phba;
4186 uint32_t did;
4187 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004188 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004189
4190 if (!ndlp)
4191 return NULL;
4192
4193 spin_lock_irqsave(&phba->ndlp_lock, flags);
4194 /* The ndlp should not be in memory free mode */
4195 if (NLP_CHK_FREE_REQ(ndlp)) {
4196 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4197 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4198 "0277 lpfc_enable_node: ndlp:x%p "
4199 "usgmap:x%x refcnt:%d\n",
4200 (void *)ndlp, ndlp->nlp_usg_map,
4201 atomic_read(&ndlp->kref.refcount));
4202 return NULL;
4203 }
4204 /* The ndlp should not already be in active mode */
4205 if (NLP_CHK_NODE_ACT(ndlp)) {
4206 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4207 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4208 "0278 lpfc_enable_node: ndlp:x%p "
4209 "usgmap:x%x refcnt:%d\n",
4210 (void *)ndlp, ndlp->nlp_usg_map,
4211 atomic_read(&ndlp->kref.refcount));
4212 return NULL;
4213 }
4214
4215 /* Keep the original DID */
4216 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004217 if (phba->sli_rev == LPFC_SLI_REV4)
4218 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004219
4220 /* re-initialize ndlp except of ndlp linked list pointer */
4221 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4222 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004223 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004224
James Smartcff261f2013-12-17 20:29:47 -05004225 if (phba->sli_rev == LPFC_SLI_REV4)
4226 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4227
James Smarte47c9092008-02-08 18:49:26 -05004228 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart725dd392013-10-10 12:23:30 -04004229 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4230 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
4231
James Smarte47c9092008-02-08 18:49:26 -05004232
4233 if (state != NLP_STE_UNUSED_NODE)
4234 lpfc_nlp_set_state(vport, ndlp, state);
4235
4236 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4237 "node enable: did:x%x",
4238 ndlp->nlp_DID, 0, 0);
4239 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004240}
4241
4242void
James Smart2e0fef82007-06-17 19:56:36 -05004243lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004244{
James Smart87af33f2007-10-27 13:37:43 -04004245 /*
James Smartfa4066b2008-01-11 01:53:27 -05004246 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004247 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004248 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4249 * until ALL other outstanding threads have completed. We check
4250 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004251 */
James Smartfa4066b2008-01-11 01:53:27 -05004252 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4253 return;
James Smart51ef4c22007-08-02 11:10:31 -04004254 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart1151e3e2011-02-16 12:39:35 -05004255 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4256 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004257 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004258 return;
dea31012005-04-17 16:05:31 -05004259}
4260
4261/*
4262 * Start / ReStart rescue timer for Discovery / RSCN handling
4263 */
4264void
James Smart2e0fef82007-06-17 19:56:36 -05004265lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004266{
James Smart2e0fef82007-06-17 19:56:36 -05004267 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4268 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004269 uint32_t tmo;
4270
James Smart2e0fef82007-06-17 19:56:36 -05004271 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004272 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004273 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4274 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004275 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004276 * FC spec states we need 3 * ratov for CT requests
4277 */
4278 tmo = ((phba->fc_ratov * 3) + 3);
4279 }
dea31012005-04-17 16:05:31 -05004280
James Smart858c9f62007-06-17 19:56:39 -05004281
4282 if (!timer_pending(&vport->fc_disctmo)) {
4283 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4284 "set disc timer: tmo:x%x state:x%x flg:x%x",
4285 tmo, vport->port_state, vport->fc_flag);
4286 }
4287
James Smart256ec0d2013-04-17 20:14:58 -04004288 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004289 spin_lock_irq(shost->host_lock);
4290 vport->fc_flag |= FC_DISC_TMO;
4291 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004292
4293 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004294 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4295 "0247 Start Discovery Timer state x%x "
4296 "Data: x%x x%lx x%x x%x\n",
4297 vport->port_state, tmo,
4298 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4299 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004300
4301 return;
4302}
4303
4304/*
4305 * Cancel rescue timer for Discovery / RSCN handling
4306 */
4307int
James Smart2e0fef82007-06-17 19:56:36 -05004308lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004309{
James Smart2e0fef82007-06-17 19:56:36 -05004310 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004311 unsigned long iflags;
4312
James Smart858c9f62007-06-17 19:56:39 -05004313 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4314 "can disc timer: state:x%x rtry:x%x flg:x%x",
4315 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4316
dea31012005-04-17 16:05:31 -05004317 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004318 if (vport->fc_flag & FC_DISC_TMO) {
4319 spin_lock_irqsave(shost->host_lock, iflags);
4320 vport->fc_flag &= ~FC_DISC_TMO;
4321 spin_unlock_irqrestore(shost->host_lock, iflags);
4322 del_timer_sync(&vport->fc_disctmo);
4323 spin_lock_irqsave(&vport->work_port_lock, iflags);
4324 vport->work_port_events &= ~WORKER_DISC_TMO;
4325 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004326 }
4327
4328 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004329 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4330 "0248 Cancel Discovery Timer state x%x "
4331 "Data: x%x x%x x%x\n",
4332 vport->port_state, vport->fc_flag,
4333 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004334 return 0;
dea31012005-04-17 16:05:31 -05004335}
4336
4337/*
4338 * Check specified ring for outstanding IOCB on the SLI queue
4339 * Return true if iocb matches the specified nport
4340 */
4341int
James Smart2e0fef82007-06-17 19:56:36 -05004342lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4343 struct lpfc_sli_ring *pring,
4344 struct lpfc_iocbq *iocb,
4345 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004346{
James Smart2e0fef82007-06-17 19:56:36 -05004347 struct lpfc_sli *psli = &phba->sli;
4348 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004349 struct lpfc_vport *vport = ndlp->vport;
4350
4351 if (iocb->vport != vport)
4352 return 0;
4353
dea31012005-04-17 16:05:31 -05004354 if (pring->ringno == LPFC_ELS_RING) {
4355 switch (icmd->ulpCommand) {
4356 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004357 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004358 return 1;
dea31012005-04-17 16:05:31 -05004359 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004360 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4361 return 1;
dea31012005-04-17 16:05:31 -05004362 case CMD_XMIT_ELS_RSP64_CX:
4363 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004364 return 1;
dea31012005-04-17 16:05:31 -05004365 }
James Smarta4bc3372006-12-02 13:34:16 -05004366 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004367
4368 } else if (pring->ringno == psli->fcp_ring) {
4369 /* Skip match check if waiting to relogin to FCP target */
4370 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004371 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004372 return 0;
dea31012005-04-17 16:05:31 -05004373 }
4374 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004375 return 1;
dea31012005-04-17 16:05:31 -05004376 }
4377 } else if (pring->ringno == psli->next_ring) {
4378
4379 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004380 return 0;
dea31012005-04-17 16:05:31 -05004381}
4382
4383/*
4384 * Free resources / clean up outstanding I/Os
4385 * associated with nlp_rpi in the LPFC_NODELIST entry.
4386 */
4387static int
James Smart2e0fef82007-06-17 19:56:36 -05004388lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004389{
James Smart2534ba72007-04-25 09:52:20 -04004390 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004391 struct lpfc_sli *psli;
4392 struct lpfc_sli_ring *pring;
4393 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004394 uint32_t i;
dea31012005-04-17 16:05:31 -05004395
James Smart92d7f7b2007-06-17 19:56:38 -05004396 lpfc_fabric_abort_nport(ndlp);
4397
dea31012005-04-17 16:05:31 -05004398 /*
4399 * Everything that matches on txcmplq will be returned
4400 * by firmware with a no rpi error.
4401 */
4402 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004403 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004404 /* Now process each ring */
4405 for (i = 0; i < psli->num_rings; i++) {
4406 pring = &psli->ring[i];
4407
James Smart2e0fef82007-06-17 19:56:36 -05004408 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004409 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004410 list) {
dea31012005-04-17 16:05:31 -05004411 /*
4412 * Check to see if iocb matches the nport we are
4413 * looking for
4414 */
James Smart92d7f7b2007-06-17 19:56:38 -05004415 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4416 ndlp))) {
dea31012005-04-17 16:05:31 -05004417 /* It matches, so deque and call compl
4418 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004419 list_move_tail(&iocb->list,
4420 &completions);
dea31012005-04-17 16:05:31 -05004421 }
4422 }
James Smart2e0fef82007-06-17 19:56:36 -05004423 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004424 }
4425 }
James Smart2534ba72007-04-25 09:52:20 -04004426
James Smarta257bf92009-04-06 18:48:10 -04004427 /* Cancel all the IOCBs from the completions list */
4428 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4429 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004430
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004431 return 0;
dea31012005-04-17 16:05:31 -05004432}
4433
James Smart086a3452012-08-14 14:25:21 -04004434/**
4435 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4436 * @phba: Pointer to HBA context object.
4437 * @pmb: Pointer to mailbox object.
4438 *
4439 * This function will issue an ELS LOGO command after completing
4440 * the UNREG_RPI.
4441 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004442static void
James Smart086a3452012-08-14 14:25:21 -04004443lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4444{
4445 struct lpfc_vport *vport = pmb->vport;
4446 struct lpfc_nodelist *ndlp;
4447
4448 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4449 if (!ndlp)
4450 return;
4451 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004452 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004453}
4454
dea31012005-04-17 16:05:31 -05004455/*
4456 * Free rpi associated with LPFC_NODELIST entry.
4457 * This routine is called from lpfc_freenode(), when we are removing
4458 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4459 * LOGO that completes successfully, and we are waiting to PLOGI back
4460 * to the remote NPort. In addition, it is called after we receive
4461 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4462 * we are waiting to PLOGI back to the remote NPort.
4463 */
4464int
James Smart2e0fef82007-06-17 19:56:36 -05004465lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004466{
James Smart2e0fef82007-06-17 19:56:36 -05004467 struct lpfc_hba *phba = vport->phba;
4468 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004469 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004470 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004471
James Smartf454a9a2013-09-06 12:20:07 -04004472 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4473 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4474 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4475 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4476 "3366 RPI x%x needs to be "
4477 "unregistered nlp_flag x%x "
4478 "did x%x\n",
4479 ndlp->nlp_rpi, ndlp->nlp_flag,
4480 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004481 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4482 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004483 /* SLI4 ports require the physical rpi value. */
4484 rpi = ndlp->nlp_rpi;
4485 if (phba->sli_rev == LPFC_SLI_REV4)
4486 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004487
James Smart6d368e52011-05-24 11:44:12 -04004488 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004489 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004490 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4491 mbox->context1 = ndlp;
4492 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4493 } else {
4494 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4495 }
4496
James Smart0b727fe2007-10-27 13:37:25 -04004497 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004498 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004499 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004500 }
dea31012005-04-17 16:05:31 -05004501 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004502
James Smart40426292010-12-15 17:58:10 -05004503 if (phba->sli_rev != LPFC_SLI_REV4)
4504 ndlp->nlp_rpi = 0;
4505 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004506 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004507 return 1;
4508 }
4509 return 0;
4510}
4511
James Smartecfd03c2010-02-12 14:41:27 -05004512/**
4513 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4514 * @phba: pointer to lpfc hba data structure.
4515 *
4516 * This routine is invoked to unregister all the currently registered RPIs
4517 * to the HBA.
4518 **/
4519void
4520lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4521{
4522 struct lpfc_vport **vports;
4523 struct lpfc_nodelist *ndlp;
4524 struct Scsi_Host *shost;
4525 int i;
4526
4527 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004528 if (!vports) {
4529 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4530 "2884 Vport array allocation failed \n");
4531 return;
4532 }
James Smartecfd03c2010-02-12 14:41:27 -05004533 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4534 shost = lpfc_shost_from_vport(vports[i]);
4535 spin_lock_irq(shost->host_lock);
4536 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004537 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004538 /* The mempool_alloc might sleep */
4539 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004540 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004541 spin_lock_irq(shost->host_lock);
4542 }
James Smartecfd03c2010-02-12 14:41:27 -05004543 }
4544 spin_unlock_irq(shost->host_lock);
4545 }
4546 lpfc_destroy_vport_work_array(phba, vports);
4547}
4548
James Smart92d7f7b2007-06-17 19:56:38 -05004549void
4550lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4551{
4552 struct lpfc_hba *phba = vport->phba;
4553 LPFC_MBOXQ_t *mbox;
4554 int rc;
4555
James Smart5af5eee2010-10-22 11:06:38 -04004556 if (phba->sli_rev == LPFC_SLI_REV4) {
4557 lpfc_sli4_unreg_all_rpis(vport);
4558 return;
4559 }
4560
James Smart92d7f7b2007-06-17 19:56:38 -05004561 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4562 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004563 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4564 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004565 mbox->vport = vport;
4566 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004567 mbox->context1 = NULL;
4568 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004569 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004570 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004571
4572 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4573 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4574 "1836 Could not issue "
4575 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004576 }
4577}
4578
4579void
4580lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4581{
4582 struct lpfc_hba *phba = vport->phba;
4583 LPFC_MBOXQ_t *mbox;
4584 int rc;
4585
4586 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4587 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004588 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4589 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004590 mbox->vport = vport;
4591 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004592 mbox->context1 = NULL;
4593 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004594 if (rc != MBX_TIMEOUT)
4595 mempool_free(mbox, phba->mbox_mem_pool);
4596
4597 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004598 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4599 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004600 "unreg_did (default rpis) status %d\n",
4601 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004602 }
4603}
4604
dea31012005-04-17 16:05:31 -05004605/*
4606 * Free resources associated with LPFC_NODELIST entry
4607 * so it can be freed.
4608 */
4609static int
James Smart2e0fef82007-06-17 19:56:36 -05004610lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004611{
James Smart2e0fef82007-06-17 19:56:36 -05004612 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4613 struct lpfc_hba *phba = vport->phba;
4614 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004615 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004616
4617 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004618 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4619 "0900 Cleanup node for NPort x%x "
4620 "Data: x%x x%x x%x\n",
4621 ndlp->nlp_DID, ndlp->nlp_flag,
4622 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004623 if (NLP_CHK_FREE_REQ(ndlp)) {
4624 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4625 "0280 lpfc_cleanup_node: ndlp:x%p "
4626 "usgmap:x%x refcnt:%d\n",
4627 (void *)ndlp, ndlp->nlp_usg_map,
4628 atomic_read(&ndlp->kref.refcount));
4629 lpfc_dequeue_node(vport, ndlp);
4630 } else {
4631 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4632 "0281 lpfc_cleanup_node: ndlp:x%p "
4633 "usgmap:x%x refcnt:%d\n",
4634 (void *)ndlp, ndlp->nlp_usg_map,
4635 atomic_read(&ndlp->kref.refcount));
4636 lpfc_disable_node(vport, ndlp);
4637 }
dea31012005-04-17 16:05:31 -05004638
James Smart086a3452012-08-14 14:25:21 -04004639
4640 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4641
dea31012005-04-17 16:05:31 -05004642 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4643 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004644 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004645 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004646 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4647 mb->context2 = NULL;
4648 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4649 }
4650 }
James Smart33ccf8d2006-08-17 11:57:58 -04004651
James Smart2e0fef82007-06-17 19:56:36 -05004652 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004653 /* Cleanup REG_LOGIN completions which are not yet processed */
4654 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4655 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004656 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004657 (ndlp != (struct lpfc_nodelist *) mb->context2))
4658 continue;
4659
4660 mb->context2 = NULL;
4661 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4662 }
4663
dea31012005-04-17 16:05:31 -05004664 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004665 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004666 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004667 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004668 mp = (struct lpfc_dmabuf *) (mb->context1);
4669 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004670 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004671 kfree(mp);
4672 }
4673 list_del(&mb->list);
4674 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004675 /* We shall not invoke the lpfc_nlp_put to decrement
4676 * the ndlp reference count as we are in the process
4677 * of lpfc_nlp_release.
4678 */
dea31012005-04-17 16:05:31 -05004679 }
4680 }
James Smart2e0fef82007-06-17 19:56:36 -05004681 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004682
James Smarte47c9092008-02-08 18:49:26 -05004683 lpfc_els_abort(phba, ndlp);
4684
James Smart2e0fef82007-06-17 19:56:36 -05004685 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004686 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004687 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004688
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004689 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004690 del_timer_sync(&ndlp->nlp_delayfunc);
4691
James Smart0d2b6b82008-06-14 22:52:47 -04004692 list_del_init(&ndlp->els_retry_evt.evt_listp);
4693 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004694 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004695 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004696
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004697 return 0;
dea31012005-04-17 16:05:31 -05004698}
4699
4700/*
4701 * Check to see if we can free the nlp back to the freelist.
4702 * If we are in the middle of using the nlp in the discovery state
4703 * machine, defer the free till we reach the end of the state machine.
4704 */
James Smart329f9bc2007-04-25 09:53:01 -04004705static void
James Smart2e0fef82007-06-17 19:56:36 -05004706lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004707{
James Smarta8adb832007-10-27 13:37:53 -04004708 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004709 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004710 LPFC_MBOXQ_t *mbox;
4711 int rc;
dea31012005-04-17 16:05:31 -05004712
James Smart0d2b6b82008-06-14 22:52:47 -04004713 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004714 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004715 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4716 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004717 /* For this case we need to cleanup the default rpi
4718 * allocated by the firmware.
4719 */
4720 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4721 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004722 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004723 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004724 if (rc) {
4725 mempool_free(mbox, phba->mbox_mem_pool);
4726 }
4727 else {
4728 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4729 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4730 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004731 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004732 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4733 if (rc == MBX_NOT_FINISHED) {
4734 mempool_free(mbox, phba->mbox_mem_pool);
4735 }
4736 }
4737 }
4738 }
James Smart2e0fef82007-06-17 19:56:36 -05004739 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004740
James Smart2e0fef82007-06-17 19:56:36 -05004741 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004742 * We can get here with a non-NULL ndlp->rport because when we
4743 * unregister a rport we don't break the rport/node linkage. So if we
4744 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004745 */
James Smart92d7f7b2007-06-17 19:56:38 -05004746 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004747 rdata = ndlp->rport->dd_data;
4748 rdata->pnode = NULL;
4749 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004750 }
dea31012005-04-17 16:05:31 -05004751}
4752
4753static int
James Smart2e0fef82007-06-17 19:56:36 -05004754lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4755 uint32_t did)
dea31012005-04-17 16:05:31 -05004756{
James Smart2e0fef82007-06-17 19:56:36 -05004757 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004758
4759 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004760 return 0;
dea31012005-04-17 16:05:31 -05004761
dea31012005-04-17 16:05:31 -05004762 /* First check for Direct match */
4763 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004764 return 1;
dea31012005-04-17 16:05:31 -05004765
4766 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004767 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004768 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004769 return 0;
dea31012005-04-17 16:05:31 -05004770 }
4771
4772 matchdid.un.word = did;
4773 ndlpdid.un.word = ndlp->nlp_DID;
4774 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4775 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4776 (mydid.un.b.area == matchdid.un.b.area)) {
4777 if ((ndlpdid.un.b.domain == 0) &&
4778 (ndlpdid.un.b.area == 0)) {
4779 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004780 return 1;
dea31012005-04-17 16:05:31 -05004781 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004782 return 0;
dea31012005-04-17 16:05:31 -05004783 }
4784
4785 matchdid.un.word = ndlp->nlp_DID;
4786 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4787 (mydid.un.b.area == ndlpdid.un.b.area)) {
4788 if ((matchdid.un.b.domain == 0) &&
4789 (matchdid.un.b.area == 0)) {
4790 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004791 return 1;
dea31012005-04-17 16:05:31 -05004792 }
4793 }
4794 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004795 return 0;
dea31012005-04-17 16:05:31 -05004796}
4797
James Smart685f0bf2007-04-25 09:53:08 -04004798/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004799static struct lpfc_nodelist *
4800__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004801{
James Smart2fb9bd82006-12-02 13:33:57 -05004802 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004803 uint32_t data1;
4804
James Smart2e0fef82007-06-17 19:56:36 -05004805 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4806 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004807 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4808 ((uint32_t) ndlp->nlp_xri << 16) |
4809 ((uint32_t) ndlp->nlp_type << 8) |
4810 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004811 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4812 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05004813 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04004814 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05004815 ndlp->nlp_flag, data1,
4816 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04004817 return ndlp;
dea31012005-04-17 16:05:31 -05004818 }
4819 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004820
dea31012005-04-17 16:05:31 -05004821 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004822 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4823 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004824 return NULL;
4825}
4826
4827struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004828lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004829{
James Smart2e0fef82007-06-17 19:56:36 -05004830 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004831 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004832 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004833
James Smartfedd3b72011-02-16 12:39:24 -05004834 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004835 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004836 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004837 return ndlp;
4838}
4839
4840struct lpfc_nodelist *
4841lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4842{
4843 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4844 struct lpfc_nodelist *ndlp;
4845
4846 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004847 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004848 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4849 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004850 return NULL;
4851 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004852 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004853 if (!ndlp)
4854 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004855 lpfc_nlp_init(vport, ndlp, did);
4856 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4857 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004858 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004859 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004860 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004861 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4862 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4863 if (!ndlp)
4864 return NULL;
4865 spin_lock_irq(shost->host_lock);
4866 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4867 spin_unlock_irq(shost->host_lock);
4868 return ndlp;
dea31012005-04-17 16:05:31 -05004869 }
James Smarte47c9092008-02-08 18:49:26 -05004870
James Smart58da1ff2008-04-07 10:15:56 -04004871 if ((vport->fc_flag & FC_RSCN_MODE) &&
4872 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004873 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004874 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004875 * we don't need to try to discover it again.
4876 */
4877 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4878 return NULL;
4879
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004880 /* Since this node is marked for discovery,
4881 * delay timeout is not needed.
4882 */
James Smart0d2b6b82008-06-14 22:52:47 -04004883 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004884 spin_lock_irq(shost->host_lock);
4885 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4886 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004887 } else
dea31012005-04-17 16:05:31 -05004888 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004889 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004890 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004891 * or we are already in the process of discovery on it,
4892 * we don't need to try to discover it again.
4893 */
James Smart685f0bf2007-04-25 09:53:08 -04004894 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004895 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4896 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004897 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004898 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4899 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004900 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004901 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004902 }
4903 return ndlp;
4904}
4905
4906/* Build a list of nodes to discover based on the loopmap */
4907void
James Smart2e0fef82007-06-17 19:56:36 -05004908lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004909{
James Smart2e0fef82007-06-17 19:56:36 -05004910 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004911 int j;
4912 uint32_t alpa, index;
4913
James Smart2e0fef82007-06-17 19:56:36 -05004914 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004915 return;
James Smart2e0fef82007-06-17 19:56:36 -05004916
James Smart76a95d72010-11-20 23:11:48 -05004917 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004918 return;
dea31012005-04-17 16:05:31 -05004919
4920 /* Check for loop map present or not */
4921 if (phba->alpa_map[0]) {
4922 for (j = 1; j <= phba->alpa_map[0]; j++) {
4923 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004924 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004925 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004926 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004927 }
4928 } else {
4929 /* No alpamap, so try all alpa's */
4930 for (j = 0; j < FC_MAXLOOP; j++) {
4931 /* If cfg_scan_down is set, start from highest
4932 * ALPA (0xef) to lowest (0x1).
4933 */
James Smart3de2a652007-08-02 11:09:59 -04004934 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004935 index = j;
4936 else
4937 index = FC_MAXLOOP - j - 1;
4938 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004939 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004940 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004941 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004942 }
4943 }
4944 return;
4945}
4946
dea31012005-04-17 16:05:31 -05004947void
James Smart2e0fef82007-06-17 19:56:36 -05004948lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004949{
dea31012005-04-17 16:05:31 -05004950 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004951 struct lpfc_sli *psli = &phba->sli;
4952 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4953 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4954 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4955 int rc;
4956
James Smart92d7f7b2007-06-17 19:56:38 -05004957 /*
4958 * if it's not a physical port or if we already send
4959 * clear_la then don't send it.
4960 */
4961 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004962 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4963 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004964 return;
4965
James Smart2e0fef82007-06-17 19:56:36 -05004966 /* Link up discovery */
4967 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4968 phba->link_state = LPFC_CLEAR_LA;
4969 lpfc_clear_la(phba, mbox);
4970 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4971 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004972 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004973 if (rc == MBX_NOT_FINISHED) {
4974 mempool_free(mbox, phba->mbox_mem_pool);
4975 lpfc_disc_flush_list(vport);
4976 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4977 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4978 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004979 phba->link_state = LPFC_HBA_ERROR;
4980 }
4981 }
4982}
4983
4984/* Reg_vpi to tell firmware to resume normal operations */
4985void
4986lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4987{
4988 LPFC_MBOXQ_t *regvpimbox;
4989
4990 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4991 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004992 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004993 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4994 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004995 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004996 == MBX_NOT_FINISHED) {
4997 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004998 }
4999 }
5000}
5001
5002/* Start Link up / RSCN discovery on NPR nodes */
5003void
5004lpfc_disc_start(struct lpfc_vport *vport)
5005{
5006 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5007 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005008 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005009 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04005010 int did_changed;
dea31012005-04-17 16:05:31 -05005011
James Smarte74c03c2013-04-17 20:15:19 -04005012 if (!lpfc_is_link_up(phba)) {
5013 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5014 "3315 Link is not up %x\n",
5015 phba->link_state);
dea31012005-04-17 16:05:31 -05005016 return;
James Smarte74c03c2013-04-17 20:15:19 -04005017 }
James Smart2e0fef82007-06-17 19:56:36 -05005018
5019 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005020 clear_la_pending = 1;
5021 else
5022 clear_la_pending = 0;
5023
James Smart2e0fef82007-06-17 19:56:36 -05005024 if (vport->port_state < LPFC_VPORT_READY)
5025 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005026
James Smart2e0fef82007-06-17 19:56:36 -05005027 lpfc_set_disctmo(vport);
5028
5029 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05005030 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005031 else
dea31012005-04-17 16:05:31 -05005032 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05005033
5034 vport->fc_prevDID = vport->fc_myDID;
5035 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005036
5037 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005038 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5039 "0202 Start Discovery hba state x%x "
5040 "Data: x%x x%x x%x\n",
5041 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5042 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005043
5044 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005045 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005046
5047 if (num_sent)
5048 return;
5049
James Smart6fa139f2013-04-17 20:20:00 -04005050 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005051 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005052 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005053 !(vport->fc_flag & FC_RSCN_MODE) &&
5054 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart6fa139f2013-04-17 20:20:00 -04005055 if (vport->port_type == LPFC_PHYSICAL_PORT)
5056 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005057 lpfc_issue_reg_vpi(phba, vport);
5058 return;
5059 }
James Smart2e0fef82007-06-17 19:56:36 -05005060
James Smart92d7f7b2007-06-17 19:56:38 -05005061 /*
5062 * For SLI2, we need to set port_state to READY and continue
5063 * discovery.
5064 */
5065 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5066 /* If we get here, there is nothing to ADISC */
5067 if (vport->port_type == LPFC_PHYSICAL_PORT)
5068 lpfc_issue_clear_la(phba, vport);
5069
5070 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005071 vport->num_disc_nodes = 0;
5072 /* go thru NPR nodes and issue ELS PLOGIs */
5073 if (vport->fc_npr_cnt)
5074 lpfc_els_disc_plogi(vport);
5075
5076 if (!vport->num_disc_nodes) {
5077 spin_lock_irq(shost->host_lock);
5078 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5079 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005080 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005081 }
5082 }
James Smart92d7f7b2007-06-17 19:56:38 -05005083 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005084 } else {
5085 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005086 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005087
5088 if (num_sent)
5089 return;
5090
James Smart2e0fef82007-06-17 19:56:36 -05005091 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005092 /* Check to see if more RSCNs came in while we
5093 * were processing this one.
5094 */
James Smart2e0fef82007-06-17 19:56:36 -05005095 if ((vport->fc_rscn_id_cnt == 0) &&
5096 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5097 spin_lock_irq(shost->host_lock);
5098 vport->fc_flag &= ~FC_RSCN_MODE;
5099 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005100 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005101 } else
James Smart2e0fef82007-06-17 19:56:36 -05005102 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005103 }
5104 }
5105 return;
5106}
5107
5108/*
5109 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5110 * ring the match the sppecified nodelist.
5111 */
5112static void
James Smart2e0fef82007-06-17 19:56:36 -05005113lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005114{
James Smart2534ba72007-04-25 09:52:20 -04005115 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005116 struct lpfc_sli *psli;
5117 IOCB_t *icmd;
5118 struct lpfc_iocbq *iocb, *next_iocb;
5119 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005120
5121 psli = &phba->sli;
5122 pring = &psli->ring[LPFC_ELS_RING];
5123
5124 /* Error matching iocb on txq or txcmplq
5125 * First check the txq.
5126 */
James Smart2e0fef82007-06-17 19:56:36 -05005127 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005128 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5129 if (iocb->context1 != ndlp) {
5130 continue;
5131 }
5132 icmd = &iocb->iocb;
5133 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5134 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5135
James Smart2534ba72007-04-25 09:52:20 -04005136 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005137 }
5138 }
5139
5140 /* Next check the txcmplq */
5141 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5142 if (iocb->context1 != ndlp) {
5143 continue;
5144 }
5145 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005146 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5147 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005148 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005149 }
5150 }
James Smart2e0fef82007-06-17 19:56:36 -05005151 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005152
James Smarta257bf92009-04-06 18:48:10 -04005153 /* Cancel all the IOCBs from the completions list */
5154 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5155 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005156}
5157
Adrian Bunka6ababd2007-11-05 18:07:33 +01005158static void
James Smart2e0fef82007-06-17 19:56:36 -05005159lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005160{
5161 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005162 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005163
James Smart2e0fef82007-06-17 19:56:36 -05005164 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5165 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005166 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005167 if (!NLP_CHK_NODE_ACT(ndlp))
5168 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005169 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5170 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5171 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005172 }
dea31012005-04-17 16:05:31 -05005173 }
5174 }
dea31012005-04-17 16:05:31 -05005175}
5176
James Smart92d7f7b2007-06-17 19:56:38 -05005177void
5178lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5179{
5180 lpfc_els_flush_rscn(vport);
5181 lpfc_els_flush_cmd(vport);
5182 lpfc_disc_flush_list(vport);
5183}
5184
dea31012005-04-17 16:05:31 -05005185/*****************************************************************************/
5186/*
5187 * NAME: lpfc_disc_timeout
5188 *
5189 * FUNCTION: Fibre Channel driver discovery timeout routine.
5190 *
5191 * EXECUTION ENVIRONMENT: interrupt only
5192 *
5193 * CALLED FROM:
5194 * Timer function
5195 *
5196 * RETURNS:
5197 * none
5198 */
5199/*****************************************************************************/
5200void
5201lpfc_disc_timeout(unsigned long ptr)
5202{
James Smart2e0fef82007-06-17 19:56:36 -05005203 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5204 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005205 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005206 unsigned long flags = 0;
5207
5208 if (unlikely(!phba))
5209 return;
5210
James Smart5e9d9b82008-06-14 22:52:53 -04005211 spin_lock_irqsave(&vport->work_port_lock, flags);
5212 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5213 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005214 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005215 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005216
James Smart5e9d9b82008-06-14 22:52:53 -04005217 if (!tmo_posted)
5218 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005219 return;
5220}
5221
5222static void
James Smart2e0fef82007-06-17 19:56:36 -05005223lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005224{
James Smart2e0fef82007-06-17 19:56:36 -05005225 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5226 struct lpfc_hba *phba = vport->phba;
5227 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005228 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005229 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005230 int rc, clrlaerr = 0;
5231
James Smart2e0fef82007-06-17 19:56:36 -05005232 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005233 return;
5234
James Smart2e0fef82007-06-17 19:56:36 -05005235 spin_lock_irq(shost->host_lock);
5236 vport->fc_flag &= ~FC_DISC_TMO;
5237 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005238
James Smart858c9f62007-06-17 19:56:39 -05005239 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5240 "disc timeout: state:x%x rtry:x%x flg:x%x",
5241 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5242
James Smart2e0fef82007-06-17 19:56:36 -05005243 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005244
5245 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005246 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5247 * FAN
5248 */
5249 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005250 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5251 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005252 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005253 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005254 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005255 if (!NLP_CHK_NODE_ACT(ndlp))
5256 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005257 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5258 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005259 if (ndlp->nlp_type & NLP_FABRIC) {
5260 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005261 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005262
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005263 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005264 /* Fail outstanding IO now since device
5265 * is marked for PLOGI.
5266 */
James Smart2e0fef82007-06-17 19:56:36 -05005267 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005268 }
5269 }
James Smart92d7f7b2007-06-17 19:56:38 -05005270 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005271 if (phba->sli_rev <= LPFC_SLI_REV3)
5272 lpfc_initial_flogi(vport);
5273 else
5274 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005275 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005276 }
dea31012005-04-17 16:05:31 -05005277 break;
5278
James Smart92d7f7b2007-06-17 19:56:38 -05005279 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005280 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005281 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005282 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005283 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5284 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005285 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005286
5287 /* Assume no Fabric and go on with discovery.
5288 * Check for outstanding ELS FLOGI to abort.
5289 */
5290
5291 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005292 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005293
5294 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005295 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005296 break;
5297
5298 case LPFC_FABRIC_CFG_LINK:
5299 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5300 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005301 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5302 "0223 Timeout while waiting for "
5303 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005304 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005305 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005306 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005307 lpfc_els_abort(phba, ndlp);
5308
5309 /* ReStart discovery */
5310 goto restart_disc;
dea31012005-04-17 16:05:31 -05005311
5312 case LPFC_NS_QRY:
5313 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005314 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5315 "0224 NameServer Query timeout "
5316 "Data: x%x x%x\n",
5317 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005318
James Smart92d7f7b2007-06-17 19:56:38 -05005319 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5320 /* Try it one more time */
5321 vport->fc_ns_retry++;
5322 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5323 vport->fc_ns_retry, 0);
5324 if (rc == 0)
5325 break;
dea31012005-04-17 16:05:31 -05005326 }
James Smart92d7f7b2007-06-17 19:56:38 -05005327 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005328
James Smart87af33f2007-10-27 13:37:43 -04005329restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005330 /*
5331 * Discovery is over.
5332 * set port_state to PORT_READY if SLI2.
5333 * cmpl_reg_vpi will set port_state to READY for SLI3.
5334 */
James Smart3772a992009-05-22 14:50:54 -04005335 if (phba->sli_rev < LPFC_SLI_REV4) {
5336 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5337 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005338 else {
James Smart3772a992009-05-22 14:50:54 -04005339 lpfc_issue_clear_la(phba, vport);
5340 vport->port_state = LPFC_VPORT_READY;
5341 }
dea31012005-04-17 16:05:31 -05005342 }
5343
5344 /* Setup and issue mailbox INITIALIZE LINK command */
5345 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5346 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005347 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5348 "0206 Device Discovery "
5349 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005350 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005351 break;
5352 }
5353
5354 lpfc_linkdown(phba);
5355 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5356 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005357 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005358 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005359 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005360 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005361 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005362 if (rc == MBX_NOT_FINISHED)
5363 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5364
5365 break;
5366
5367 case LPFC_DISC_AUTH:
5368 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005369 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5370 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005371 lpfc_disc_flush_list(vport);
5372
James Smart92d7f7b2007-06-17 19:56:38 -05005373 /*
5374 * set port_state to PORT_READY if SLI2.
5375 * cmpl_reg_vpi will set port_state to READY for SLI3.
5376 */
James Smart3772a992009-05-22 14:50:54 -04005377 if (phba->sli_rev < LPFC_SLI_REV4) {
5378 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5379 lpfc_issue_reg_vpi(phba, vport);
5380 else { /* NPIV Not enabled */
5381 lpfc_issue_clear_la(phba, vport);
5382 vport->port_state = LPFC_VPORT_READY;
5383 }
dea31012005-04-17 16:05:31 -05005384 }
5385 break;
5386
James Smart2e0fef82007-06-17 19:56:36 -05005387 case LPFC_VPORT_READY:
5388 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005389 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5390 "0231 RSCN timeout Data: x%x "
5391 "x%x\n",
5392 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005393
5394 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005395 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005396
James Smart2e0fef82007-06-17 19:56:36 -05005397 lpfc_els_flush_rscn(vport);
5398 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005399 }
5400 break;
James Smart2e0fef82007-06-17 19:56:36 -05005401
James Smart92d7f7b2007-06-17 19:56:38 -05005402 default:
James Smarte8b62012007-08-02 11:10:09 -04005403 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005404 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005405 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005406 break;
5407 }
5408
5409 switch (phba->link_state) {
5410 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005411 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005412 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5413 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005414 clrlaerr = 1;
5415 break;
5416
James Smart09372822008-01-11 01:52:54 -05005417 case LPFC_LINK_UP:
5418 lpfc_issue_clear_la(phba, vport);
5419 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005420 case LPFC_LINK_UNKNOWN:
5421 case LPFC_WARM_START:
5422 case LPFC_INIT_START:
5423 case LPFC_INIT_MBX_CMDS:
5424 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005425 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005426 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5427 "0230 Unexpected timeout, hba link "
5428 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005429 clrlaerr = 1;
5430 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005431
5432 case LPFC_HBA_READY:
5433 break;
dea31012005-04-17 16:05:31 -05005434 }
5435
5436 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005437 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005438 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005439 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5440 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005441 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005442 }
5443
5444 return;
5445}
5446
dea31012005-04-17 16:05:31 -05005447/*
5448 * This routine handles processing a NameServer REG_LOGIN mailbox
5449 * command upon completion. It is setup in the LPFC_MBOXQ
5450 * as the completion routine when the command is
5451 * handed off to the SLI layer.
5452 */
5453void
James Smart2e0fef82007-06-17 19:56:36 -05005454lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005455{
James Smart04c68492009-05-22 14:52:52 -04005456 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005457 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5458 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5459 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005460
5461 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005462 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005463
James Smart6d368e52011-05-24 11:44:12 -04005464 if (phba->sli_rev < LPFC_SLI_REV4)
5465 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005466 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005467 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005468 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05005469
James Smart2e0fef82007-06-17 19:56:36 -05005470 /*
5471 * Start issuing Fabric-Device Management Interface (FDMI) command to
5472 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5473 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005474 */
James Smart2e0fef82007-06-17 19:56:36 -05005475
James Smart3de2a652007-08-02 11:09:59 -04005476 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05005477 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
5478 else
James Smart256ec0d2013-04-17 20:14:58 -04005479 mod_timer(&vport->fc_fdmitmo,
5480 jiffies + msecs_to_jiffies(1000 * 60));
dea31012005-04-17 16:05:31 -05005481
James Smartfa4066b2008-01-11 01:53:27 -05005482 /* decrement the node reference count held for this callback
5483 * function.
5484 */
James Smart329f9bc2007-04-25 09:53:01 -04005485 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005486 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5487 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005488 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005489
5490 return;
5491}
5492
James Smart685f0bf2007-04-25 09:53:08 -04005493static int
5494lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5495{
5496 uint16_t *rpi = param;
5497
James Smarteff4a012012-01-18 16:25:25 -05005498 /* check for active node */
5499 if (!NLP_CHK_NODE_ACT(ndlp))
5500 return 0;
5501
James Smart685f0bf2007-04-25 09:53:08 -04005502 return ndlp->nlp_rpi == *rpi;
5503}
5504
5505static int
5506lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5507{
5508 return memcmp(&ndlp->nlp_portname, param,
5509 sizeof(ndlp->nlp_portname)) == 0;
5510}
5511
Adrian Bunka6ababd2007-11-05 18:07:33 +01005512static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005513__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005514{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005515 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005516
James Smart2e0fef82007-06-17 19:56:36 -05005517 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005518 if (filter(ndlp, param)) {
5519 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5520 "3185 FIND node filter %p DID "
5521 "Data: x%p x%x x%x\n",
5522 filter, ndlp, ndlp->nlp_DID,
5523 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005524 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005525 }
James Smart685f0bf2007-04-25 09:53:08 -04005526 }
James Smart34f5ad82012-08-03 12:35:03 -04005527 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5528 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005529 return NULL;
dea31012005-04-17 16:05:31 -05005530}
5531
James Smart685f0bf2007-04-25 09:53:08 -04005532/*
5533 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005534 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005535 */
5536struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005537__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005538{
James Smart2e0fef82007-06-17 19:56:36 -05005539 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005540}
5541
James Smart488d1462006-03-07 15:02:37 -05005542/*
James Smart685f0bf2007-04-25 09:53:08 -04005543 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005544 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005545 */
5546struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005547lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005548{
James Smart2e0fef82007-06-17 19:56:36 -05005549 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005550 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005551
James Smart2e0fef82007-06-17 19:56:36 -05005552 spin_lock_irq(shost->host_lock);
5553 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5554 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005555 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005556}
5557
James Smartcb69f7d2011-12-13 13:21:57 -05005558/*
5559 * This routine looks up the ndlp lists for the given RPI. If the rpi
5560 * is found, the routine returns the node element list pointer else
5561 * return NULL.
5562 */
5563struct lpfc_nodelist *
5564lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5565{
5566 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5567 struct lpfc_nodelist *ndlp;
5568
5569 spin_lock_irq(shost->host_lock);
5570 ndlp = __lpfc_findnode_rpi(vport, rpi);
5571 spin_unlock_irq(shost->host_lock);
5572 return ndlp;
5573}
5574
5575/**
5576 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5577 * @phba: pointer to lpfc hba data structure.
5578 * @vpi: the physical host virtual N_Port identifier.
5579 *
5580 * This routine finds a vport on a HBA (referred by @phba) through a
5581 * @vpi. The function walks the HBA's vport list and returns the address
5582 * of the vport with the matching @vpi.
5583 *
5584 * Return code
5585 * NULL - No vport with the matching @vpi found
5586 * Otherwise - Address to the vport with the matching @vpi.
5587 **/
5588struct lpfc_vport *
5589lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5590{
5591 struct lpfc_vport *vport;
5592 unsigned long flags;
5593 int i = 0;
5594
5595 /* The physical ports are always vpi 0 - translate is unnecessary. */
5596 if (vpi > 0) {
5597 /*
5598 * Translate the physical vpi to the logical vpi. The
5599 * vport stores the logical vpi.
5600 */
5601 for (i = 0; i < phba->max_vpi; i++) {
5602 if (vpi == phba->vpi_ids[i])
5603 break;
5604 }
5605
5606 if (i >= phba->max_vpi) {
5607 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5608 "2936 Could not find Vport mapped "
5609 "to vpi %d\n", vpi);
5610 return NULL;
5611 }
5612 }
5613
5614 spin_lock_irqsave(&phba->hbalock, flags);
5615 list_for_each_entry(vport, &phba->port_list, listentry) {
5616 if (vport->vpi == i) {
5617 spin_unlock_irqrestore(&phba->hbalock, flags);
5618 return vport;
5619 }
5620 }
5621 spin_unlock_irqrestore(&phba->hbalock, flags);
5622 return NULL;
5623}
5624
dea31012005-04-17 16:05:31 -05005625void
James Smart2e0fef82007-06-17 19:56:36 -05005626lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5627 uint32_t did)
dea31012005-04-17 16:05:31 -05005628{
5629 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005630
5631 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005632 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005633 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005634 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartcff261f2013-12-17 20:29:47 -05005635 ndlp->active_rrqs_xri_bitmap =
5636 mempool_alloc(vport->phba->active_rrq_pool,
5637 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005638 if (ndlp->active_rrqs_xri_bitmap)
5639 memset(ndlp->active_rrqs_xri_bitmap, 0,
5640 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005641 }
5642
James Smart725dd392013-10-10 12:23:30 -04005643
James Smart858c9f62007-06-17 19:56:39 -05005644
5645 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5646 "node init: did:x%x",
5647 ndlp->nlp_DID, 0, 0);
5648
dea31012005-04-17 16:05:31 -05005649 return;
5650}
James Smart329f9bc2007-04-25 09:53:01 -04005651
James Smart98c9ea52007-10-27 13:37:33 -04005652/* This routine releases all resources associated with a specifc NPort's ndlp
5653 * and mempool_free's the nodelist.
5654 */
James Smart311464e2007-08-02 11:10:37 -04005655static void
James Smart329f9bc2007-04-25 09:53:01 -04005656lpfc_nlp_release(struct kref *kref)
5657{
James Smarte47c9092008-02-08 18:49:26 -05005658 struct lpfc_hba *phba;
5659 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005660 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5661 kref);
James Smart858c9f62007-06-17 19:56:39 -05005662
5663 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5664 "node release: did:x%x flg:x%x type:x%x",
5665 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5666
James Smarte47c9092008-02-08 18:49:26 -05005667 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005668 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smarte47c9092008-02-08 18:49:26 -05005669 "usgmap:x%x refcnt:%d\n",
James Smart939723a2012-05-09 21:19:03 -04005670 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smarte47c9092008-02-08 18:49:26 -05005671 atomic_read(&ndlp->kref.refcount));
5672
5673 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005674 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005675
5676 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005677 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005678 spin_lock_irqsave(&phba->ndlp_lock, flags);
5679 NLP_CLR_NODE_ACT(ndlp);
5680 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005681 if (phba->sli_rev == LPFC_SLI_REV4)
5682 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005683
5684 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005685 if (NLP_CHK_FREE_REQ(ndlp)) {
5686 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005687 if (phba->sli_rev == LPFC_SLI_REV4)
5688 mempool_free(ndlp->active_rrqs_xri_bitmap,
5689 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005690 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005691 }
James Smart329f9bc2007-04-25 09:53:01 -04005692}
5693
James Smart98c9ea52007-10-27 13:37:33 -04005694/* This routine bumps the reference count for a ndlp structure to ensure
5695 * that one discovery thread won't free a ndlp while another discovery thread
5696 * is using it.
5697 */
James Smart329f9bc2007-04-25 09:53:01 -04005698struct lpfc_nodelist *
5699lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5700{
James Smarte47c9092008-02-08 18:49:26 -05005701 struct lpfc_hba *phba;
5702 unsigned long flags;
5703
James Smart98c9ea52007-10-27 13:37:33 -04005704 if (ndlp) {
5705 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5706 "node get: did:x%x flg:x%x refcnt:x%x",
5707 ndlp->nlp_DID, ndlp->nlp_flag,
5708 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005709 /* The check of ndlp usage to prevent incrementing the
5710 * ndlp reference count that is in the process of being
5711 * released.
5712 */
James Smarta257bf92009-04-06 18:48:10 -04005713 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005714 spin_lock_irqsave(&phba->ndlp_lock, flags);
5715 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5716 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5717 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5718 "0276 lpfc_nlp_get: ndlp:x%p "
5719 "usgmap:x%x refcnt:%d\n",
5720 (void *)ndlp, ndlp->nlp_usg_map,
5721 atomic_read(&ndlp->kref.refcount));
5722 return NULL;
5723 } else
5724 kref_get(&ndlp->kref);
5725 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005726 }
James Smart329f9bc2007-04-25 09:53:01 -04005727 return ndlp;
5728}
5729
James Smart98c9ea52007-10-27 13:37:33 -04005730/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005731 * count goes to 0, this indicates the the associated nodelist should be
5732 * freed. Returning 1 indicates the ndlp resource has been released; on the
5733 * other hand, returning 0 indicates the ndlp resource has not been released
5734 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005735 */
James Smart329f9bc2007-04-25 09:53:01 -04005736int
5737lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5738{
James Smarte47c9092008-02-08 18:49:26 -05005739 struct lpfc_hba *phba;
5740 unsigned long flags;
5741
5742 if (!ndlp)
5743 return 1;
5744
5745 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5746 "node put: did:x%x flg:x%x refcnt:x%x",
5747 ndlp->nlp_DID, ndlp->nlp_flag,
5748 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005749 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005750 spin_lock_irqsave(&phba->ndlp_lock, flags);
5751 /* Check the ndlp memory free acknowledge flag to avoid the
5752 * possible race condition that kref_put got invoked again
5753 * after previous one has done ndlp memory free.
5754 */
5755 if (NLP_CHK_FREE_ACK(ndlp)) {
5756 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5757 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5758 "0274 lpfc_nlp_put: ndlp:x%p "
5759 "usgmap:x%x refcnt:%d\n",
5760 (void *)ndlp, ndlp->nlp_usg_map,
5761 atomic_read(&ndlp->kref.refcount));
5762 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005763 }
James Smarte47c9092008-02-08 18:49:26 -05005764 /* Check the ndlp inactivate log flag to avoid the possible
5765 * race condition that kref_put got invoked again after ndlp
5766 * is already in inactivating state.
5767 */
5768 if (NLP_CHK_IACT_REQ(ndlp)) {
5769 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5770 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5771 "0275 lpfc_nlp_put: ndlp:x%p "
5772 "usgmap:x%x refcnt:%d\n",
5773 (void *)ndlp, ndlp->nlp_usg_map,
5774 atomic_read(&ndlp->kref.refcount));
5775 return 1;
5776 }
5777 /* For last put, mark the ndlp usage flags to make sure no
5778 * other kref_get and kref_put on the same ndlp shall get
5779 * in between the process when the final kref_put has been
5780 * invoked on this ndlp.
5781 */
5782 if (atomic_read(&ndlp->kref.refcount) == 1) {
5783 /* Indicate ndlp is put to inactive state. */
5784 NLP_SET_IACT_REQ(ndlp);
5785 /* Acknowledge ndlp memory free has been seen. */
5786 if (NLP_CHK_FREE_REQ(ndlp))
5787 NLP_SET_FREE_ACK(ndlp);
5788 }
5789 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5790 /* Note, the kref_put returns 1 when decrementing a reference
5791 * count that was 1, it invokes the release callback function,
5792 * but it still left the reference count as 1 (not actually
5793 * performs the last decrementation). Otherwise, it actually
5794 * decrements the reference count and returns 0.
5795 */
5796 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005797}
James Smart98c9ea52007-10-27 13:37:33 -04005798
5799/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005800 * by any other discovery thread. This routine returns 1 if the
5801 * ndlp has been freed. A return value of 0 indicates the ndlp is
5802 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005803 */
5804int
5805lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5806{
5807 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5808 "node not used: did:x%x flg:x%x refcnt:x%x",
5809 ndlp->nlp_DID, ndlp->nlp_flag,
5810 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005811 if (atomic_read(&ndlp->kref.refcount) == 1)
5812 if (lpfc_nlp_put(ndlp))
5813 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005814 return 0;
5815}
James Smart6fb120a2009-05-22 14:52:59 -04005816
5817/**
5818 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5819 * @phba: Pointer to hba context object.
5820 *
5821 * This function iterate through all FC nodes associated
5822 * will all vports to check if there is any node with
5823 * fc_rports associated with it. If there is an fc_rport
5824 * associated with the node, then the node is either in
5825 * discovered state or its devloss_timer is pending.
5826 */
5827static int
5828lpfc_fcf_inuse(struct lpfc_hba *phba)
5829{
5830 struct lpfc_vport **vports;
5831 int i, ret = 0;
5832 struct lpfc_nodelist *ndlp;
5833 struct Scsi_Host *shost;
5834
5835 vports = lpfc_create_vport_work_array(phba);
5836
James Smart63e801c2010-11-20 23:14:19 -05005837 /* If driver cannot allocate memory, indicate fcf is in use */
5838 if (!vports)
5839 return 1;
5840
James Smart6fb120a2009-05-22 14:52:59 -04005841 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5842 shost = lpfc_shost_from_vport(vports[i]);
5843 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005844 /*
5845 * IF the CVL_RCVD bit is not set then we have sent the
5846 * flogi.
5847 * If dev_loss fires while we are waiting we do not want to
5848 * unreg the fcf.
5849 */
5850 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5851 spin_unlock_irq(shost->host_lock);
5852 ret = 1;
5853 goto out;
5854 }
James Smart6fb120a2009-05-22 14:52:59 -04005855 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5856 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5857 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5858 ret = 1;
5859 spin_unlock_irq(shost->host_lock);
5860 goto out;
James Smart80c17842012-03-01 22:35:45 -05005861 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5862 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005863 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005864 "2624 RPI %x DID %x flag %x "
5865 "still logged in\n",
5866 ndlp->nlp_rpi, ndlp->nlp_DID,
5867 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005868 }
5869 }
5870 spin_unlock_irq(shost->host_lock);
5871 }
5872out:
5873 lpfc_destroy_vport_work_array(phba, vports);
5874 return ret;
5875}
5876
5877/**
5878 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5879 * @phba: Pointer to hba context object.
5880 * @mboxq: Pointer to mailbox object.
5881 *
5882 * This function frees memory associated with the mailbox command.
5883 */
James Smart1b511972011-12-13 13:23:09 -05005884void
James Smart6fb120a2009-05-22 14:52:59 -04005885lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5886{
5887 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005888 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005889
5890 if (mboxq->u.mb.mbxStatus) {
5891 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5892 "2555 UNREG_VFI mbxStatus error x%x "
5893 "HBA state x%x\n",
5894 mboxq->u.mb.mbxStatus, vport->port_state);
5895 }
James Smart38b92ef2010-08-04 16:11:39 -04005896 spin_lock_irq(shost->host_lock);
5897 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5898 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005899 mempool_free(mboxq, phba->mbox_mem_pool);
5900 return;
5901}
5902
5903/**
5904 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5905 * @phba: Pointer to hba context object.
5906 * @mboxq: Pointer to mailbox object.
5907 *
5908 * This function frees memory associated with the mailbox command.
5909 */
5910static void
5911lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5912{
5913 struct lpfc_vport *vport = mboxq->vport;
5914
5915 if (mboxq->u.mb.mbxStatus) {
5916 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5917 "2550 UNREG_FCFI mbxStatus error x%x "
5918 "HBA state x%x\n",
5919 mboxq->u.mb.mbxStatus, vport->port_state);
5920 }
5921 mempool_free(mboxq, phba->mbox_mem_pool);
5922 return;
5923}
5924
5925/**
James Smartecfd03c2010-02-12 14:41:27 -05005926 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005927 * @phba: Pointer to hba context object.
5928 *
James Smartecfd03c2010-02-12 14:41:27 -05005929 * This function prepare the HBA for unregistering the currently registered
5930 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5931 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005932 */
James Smartecfd03c2010-02-12 14:41:27 -05005933int
5934lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005935{
James Smart6fb120a2009-05-22 14:52:59 -04005936 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005937 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005938 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04005939 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005940
James Smartecfd03c2010-02-12 14:41:27 -05005941 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005942 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005943 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005944
James Smart4d9ab992009-10-02 15:16:39 -04005945 /* At this point, all discovery is aborted */
5946 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005947
5948 /* Unregister VPIs */
5949 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005950 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005951 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005952 /* Stop FLOGI/FDISC retries */
5953 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5954 if (ndlp)
5955 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005956 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04005957 if (phba->sli_rev == LPFC_SLI_REV4)
5958 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005959 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005960 shost = lpfc_shost_from_vport(vports[i]);
5961 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005962 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005963 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005964 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005965 }
5966 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04005967 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
5968 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
5969 if (ndlp)
5970 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
5971 lpfc_cleanup_pending_mbox(phba->pport);
5972 if (phba->sli_rev == LPFC_SLI_REV4)
5973 lpfc_sli4_unreg_all_rpis(phba->pport);
5974 lpfc_mbx_unreg_vpi(phba->pport);
5975 shost = lpfc_shost_from_vport(phba->pport);
5976 spin_lock_irq(shost->host_lock);
5977 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
5978 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
5979 spin_unlock_irq(shost->host_lock);
5980 }
James Smart6fb120a2009-05-22 14:52:59 -04005981
James Smart695a8142010-01-26 23:08:03 -05005982 /* Cleanup any outstanding ELS commands */
5983 lpfc_els_flush_all_cmd(phba);
5984
James Smart1b511972011-12-13 13:23:09 -05005985 /* Unregister the physical port VFI */
5986 rc = lpfc_issue_unreg_vfi(phba->pport);
5987 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05005988}
5989
5990/**
5991 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5992 * @phba: Pointer to hba context object.
5993 *
5994 * This function issues synchronous unregister FCF mailbox command to HBA to
5995 * unregister the currently registered FCF record. The driver does not reset
5996 * the driver FCF usage state flags.
5997 *
5998 * Return 0 if successfully issued, none-zero otherwise.
5999 */
6000int
6001lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6002{
6003 LPFC_MBOXQ_t *mbox;
6004 int rc;
6005
James Smart6fb120a2009-05-22 14:52:59 -04006006 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6007 if (!mbox) {
6008 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006009 "2551 UNREG_FCFI mbox allocation failed"
6010 "HBA state x%x\n", phba->pport->port_state);
6011 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006012 }
James Smart6fb120a2009-05-22 14:52:59 -04006013 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6014 mbox->vport = phba->pport;
6015 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6016 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6017
6018 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6020 "2552 Unregister FCFI command failed rc x%x "
6021 "HBA state x%x\n",
6022 rc, phba->pport->port_state);
6023 return -EINVAL;
6024 }
6025 return 0;
6026}
6027
6028/**
6029 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6030 * @phba: Pointer to hba context object.
6031 *
6032 * This function unregisters the currently reigstered FCF. This function
6033 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6034 */
6035void
6036lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6037{
6038 int rc;
6039
6040 /* Preparation for unregistering fcf */
6041 rc = lpfc_unregister_fcf_prep(phba);
6042 if (rc) {
6043 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6044 "2748 Failed to prepare for unregistering "
6045 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006046 return;
6047 }
6048
James Smartecfd03c2010-02-12 14:41:27 -05006049 /* Now, unregister FCF record and reset HBA FCF state */
6050 rc = lpfc_sli4_unregister_fcf(phba);
6051 if (rc)
6052 return;
6053 /* Reset HBA FCF states after successful unregister FCF */
6054 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006055 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006056
6057 /*
6058 * If driver is not unloading, check if there is any other
6059 * FCF record that can be used for discovery.
6060 */
6061 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006062 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006063 return;
6064
James Smart0c9ab6f2010-02-26 14:15:57 -05006065 /* This is considered as the initial FCF discovery scan */
6066 spin_lock_irq(&phba->hbalock);
6067 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6068 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006069
6070 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006071 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006072
James Smart0c9ab6f2010-02-26 14:15:57 -05006073 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006074
James Smart0c9ab6f2010-02-26 14:15:57 -05006075 if (rc) {
6076 spin_lock_irq(&phba->hbalock);
6077 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6078 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006079 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006080 "2553 lpfc_unregister_unused_fcf failed "
6081 "to read FCF record HBA state x%x\n",
6082 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006083 }
James Smartecfd03c2010-02-12 14:41:27 -05006084}
6085
6086/**
6087 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6088 * @phba: Pointer to hba context object.
6089 *
6090 * This function just unregisters the currently reigstered FCF. It does not
6091 * try to find another FCF for discovery.
6092 */
6093void
6094lpfc_unregister_fcf(struct lpfc_hba *phba)
6095{
6096 int rc;
6097
6098 /* Preparation for unregistering fcf */
6099 rc = lpfc_unregister_fcf_prep(phba);
6100 if (rc) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6102 "2749 Failed to prepare for unregistering "
6103 "HBA's FCF record: rc=%d\n", rc);
6104 return;
6105 }
6106
6107 /* Now, unregister FCF record and reset HBA FCF state */
6108 rc = lpfc_sli4_unregister_fcf(phba);
6109 if (rc)
6110 return;
6111 /* Set proper HBA FCF states after successful unregister FCF */
6112 spin_lock_irq(&phba->hbalock);
6113 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6114 spin_unlock_irq(&phba->hbalock);
6115}
6116
6117/**
6118 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6119 * @phba: Pointer to hba context object.
6120 *
6121 * This function check if there are any connected remote port for the FCF and
6122 * if all the devices are disconnected, this function unregister FCFI.
6123 * This function also tries to use another FCF for discovery.
6124 */
6125void
6126lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6127{
6128 /*
James Smart3804dc82010-07-14 15:31:37 -04006129 * If HBA is not running in FIP mode, if HBA does not support
6130 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6131 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006132 */
6133 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006134 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006135 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006136 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006137 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006138 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006139 spin_unlock_irq(&phba->hbalock);
6140 return;
6141 }
6142 spin_unlock_irq(&phba->hbalock);
6143
6144 if (lpfc_fcf_inuse(phba))
6145 return;
6146
6147 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006148}
6149
6150/**
6151 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6152 * @phba: Pointer to hba context object.
6153 * @buff: Buffer containing the FCF connection table as in the config
6154 * region.
6155 * This function create driver data structure for the FCF connection
6156 * record table read from config region 23.
6157 */
6158static void
6159lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6160 uint8_t *buff)
6161{
6162 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6163 struct lpfc_fcf_conn_hdr *conn_hdr;
6164 struct lpfc_fcf_conn_rec *conn_rec;
6165 uint32_t record_count;
6166 int i;
6167
6168 /* Free the current connect table */
6169 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006170 &phba->fcf_conn_rec_list, list) {
6171 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006172 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006173 }
James Smart6fb120a2009-05-22 14:52:59 -04006174
6175 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6176 record_count = conn_hdr->length * sizeof(uint32_t)/
6177 sizeof(struct lpfc_fcf_conn_rec);
6178
6179 conn_rec = (struct lpfc_fcf_conn_rec *)
6180 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6181
6182 for (i = 0; i < record_count; i++) {
6183 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6184 continue;
6185 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6186 GFP_KERNEL);
6187 if (!conn_entry) {
6188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6189 "2566 Failed to allocate connection"
6190 " table entry\n");
6191 return;
6192 }
6193
6194 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6195 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006196 list_add_tail(&conn_entry->list,
6197 &phba->fcf_conn_rec_list);
6198 }
James Smartdf0d0852013-05-31 17:05:08 -04006199
6200 if (!list_empty(&phba->fcf_conn_rec_list)) {
6201 i = 0;
6202 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6203 list) {
6204 conn_rec = &conn_entry->conn_rec;
6205 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6206 "3345 FCF connection list rec[%02d]: "
6207 "flags:x%04x, vtag:x%04x, "
6208 "fabric_name:x%02x:%02x:%02x:%02x:"
6209 "%02x:%02x:%02x:%02x, "
6210 "switch_name:x%02x:%02x:%02x:%02x:"
6211 "%02x:%02x:%02x:%02x\n", i++,
6212 conn_rec->flags, conn_rec->vlan_tag,
6213 conn_rec->fabric_name[0],
6214 conn_rec->fabric_name[1],
6215 conn_rec->fabric_name[2],
6216 conn_rec->fabric_name[3],
6217 conn_rec->fabric_name[4],
6218 conn_rec->fabric_name[5],
6219 conn_rec->fabric_name[6],
6220 conn_rec->fabric_name[7],
6221 conn_rec->switch_name[0],
6222 conn_rec->switch_name[1],
6223 conn_rec->switch_name[2],
6224 conn_rec->switch_name[3],
6225 conn_rec->switch_name[4],
6226 conn_rec->switch_name[5],
6227 conn_rec->switch_name[6],
6228 conn_rec->switch_name[7]);
6229 }
6230 }
James Smart6fb120a2009-05-22 14:52:59 -04006231}
6232
6233/**
6234 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6235 * @phba: Pointer to hba context object.
6236 * @buff: Buffer containing the FCoE parameter data structure.
6237 *
6238 * This function update driver data structure with config
6239 * parameters read from config region 23.
6240 */
6241static void
6242lpfc_read_fcoe_param(struct lpfc_hba *phba,
6243 uint8_t *buff)
6244{
6245 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6246 struct lpfc_fcoe_params *fcoe_param;
6247
6248 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6249 buff;
6250 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006251 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006252
6253 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6254 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6255 return;
6256
James Smart6fb120a2009-05-22 14:52:59 -04006257 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6258 phba->valid_vlan = 1;
6259 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6260 0xFFF;
6261 }
6262
6263 phba->fc_map[0] = fcoe_param->fc_map[0];
6264 phba->fc_map[1] = fcoe_param->fc_map[1];
6265 phba->fc_map[2] = fcoe_param->fc_map[2];
6266 return;
6267}
6268
6269/**
6270 * lpfc_get_rec_conf23 - Get a record type in config region data.
6271 * @buff: Buffer containing config region 23 data.
6272 * @size: Size of the data buffer.
6273 * @rec_type: Record type to be searched.
6274 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006275 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006276 * of the record specified by record_type. If record found, this
6277 * function return pointer to the record else return NULL.
6278 */
6279static uint8_t *
6280lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6281{
6282 uint32_t offset = 0, rec_length;
6283
6284 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6285 (size < sizeof(uint32_t)))
6286 return NULL;
6287
6288 rec_length = buff[offset + 1];
6289
6290 /*
6291 * One TLV record has one word header and number of data words
6292 * specified in the rec_length field of the record header.
6293 */
6294 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6295 <= size) {
6296 if (buff[offset] == rec_type)
6297 return &buff[offset];
6298
6299 if (buff[offset] == LPFC_REGION23_LAST_REC)
6300 return NULL;
6301
6302 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6303 rec_length = buff[offset + 1];
6304 }
6305 return NULL;
6306}
6307
6308/**
6309 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6310 * @phba: Pointer to lpfc_hba data structure.
6311 * @buff: Buffer containing config region 23 data.
6312 * @size: Size of the data buffer.
6313 *
Stefan Weileef35c22010-08-06 21:11:15 +02006314 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006315 * populate driver data structure with the parameters.
6316 */
6317void
6318lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6319 uint8_t *buff,
6320 uint32_t size)
6321{
6322 uint32_t offset = 0, rec_length;
6323 uint8_t *rec_ptr;
6324
6325 /*
6326 * If data size is less than 2 words signature and version cannot be
6327 * verified.
6328 */
6329 if (size < 2*sizeof(uint32_t))
6330 return;
6331
6332 /* Check the region signature first */
6333 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6335 "2567 Config region 23 has bad signature\n");
6336 return;
6337 }
6338
6339 offset += 4;
6340
6341 /* Check the data structure version */
6342 if (buff[offset] != LPFC_REGION23_VERSION) {
6343 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6344 "2568 Config region 23 has bad version\n");
6345 return;
6346 }
6347 offset += 4;
6348
6349 rec_length = buff[offset + 1];
6350
6351 /* Read FCoE param record */
6352 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6353 size - offset, FCOE_PARAM_TYPE);
6354 if (rec_ptr)
6355 lpfc_read_fcoe_param(phba, rec_ptr);
6356
6357 /* Read FCF connection table */
6358 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6359 size - offset, FCOE_CONN_TBL_TYPE);
6360 if (rec_ptr)
6361 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6362
6363}