blob: 3081db730b44ac5744c14703a9fbc2c4409ede45 [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 Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 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
163 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500164 /* We need to hold the node by incrementing the reference
165 * count until this queued work is done
166 */
167 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400168 if (evtp->evt_arg1) {
169 evtp->evt = LPFC_EVT_DEV_LOSS;
170 list_add_tail(&evtp->evt_listp, &phba->work_list);
171 lpfc_worker_wake_up(phba);
172 }
James Smart858c9f62007-06-17 19:56:39 -0500173 spin_unlock_irq(&phba->hbalock);
174
James Smart858c9f62007-06-17 19:56:39 -0500175 return;
176}
177
James Smarta93ff372010-10-22 11:06:08 -0400178/**
179 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
180 * @ndlp: Pointer to remote node object.
181 *
182 * This function is called from the worker thread when devloss timeout timer
183 * expires. For SLI4 host, this routine shall return 1 when at lease one
184 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
185 * routine shall return 0 when there is no remote node is still in use of FCF
186 * when devloss timeout happened to this @ndlp.
187 **/
188static int
James Smart858c9f62007-06-17 19:56:39 -0500189lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
190{
191 struct lpfc_rport_data *rdata;
192 struct fc_rport *rport;
193 struct lpfc_vport *vport;
194 struct lpfc_hba *phba;
195 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400196 int put_node;
197 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500198 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400199 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500200
201 rport = ndlp->rport;
202
203 if (!rport)
James Smarta93ff372010-10-22 11:06:08 -0400204 return fcf_inuse;
James Smart858c9f62007-06-17 19:56:39 -0500205
206 rdata = rport->dd_data;
207 name = (uint8_t *) &ndlp->nlp_portname;
208 vport = ndlp->vport;
209 phba = vport->phba;
210
James Smarta93ff372010-10-22 11:06:08 -0400211 if (phba->sli_rev == LPFC_SLI_REV4)
212 fcf_inuse = lpfc_fcf_inuse(phba);
213
James Smart858c9f62007-06-17 19:56:39 -0500214 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
215 "rport devlosstmo:did:x%x type:x%x id:x%x",
216 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
217
James Smart34f5ad82012-08-03 12:35:03 -0400218 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
219 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
220 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
221
James Smarta8adb832007-10-27 13:37:53 -0400222 /* Don't defer this if we are in the process of deleting the vport
223 * or unloading the driver. The unload will cleanup the node
224 * appropriately we just need to cleanup the ndlp rport info here.
225 */
226 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500227 if (ndlp->nlp_sid != NLP_NO_SID) {
228 /* flush the target */
229 lpfc_sli_abort_iocb(vport,
230 &phba->sli.ring[phba->sli.fcp_ring],
231 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
232 }
James Smarta8adb832007-10-27 13:37:53 -0400233 put_node = rdata->pnode != NULL;
234 put_rport = ndlp->rport != NULL;
235 rdata->pnode = NULL;
236 ndlp->rport = NULL;
237 if (put_node)
238 lpfc_nlp_put(ndlp);
239 if (put_rport)
240 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400241 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400242 }
243
James Smartd7c255b2008-08-24 21:50:00 -0400244 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
245 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
246 "0284 Devloss timeout Ignored on "
247 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
248 "NPort x%x\n",
249 *name, *(name+1), *(name+2), *(name+3),
250 *(name+4), *(name+5), *(name+6), *(name+7),
251 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400252 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400253 }
James Smart858c9f62007-06-17 19:56:39 -0500254
James Smart92d7f7b2007-06-17 19:56:38 -0500255 if (ndlp->nlp_type & NLP_FABRIC) {
256 /* We will clean up these Nodes in linkup */
257 put_node = rdata->pnode != NULL;
258 put_rport = ndlp->rport != NULL;
259 rdata->pnode = NULL;
260 ndlp->rport = NULL;
261 if (put_node)
262 lpfc_nlp_put(ndlp);
263 if (put_rport)
264 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400265 return fcf_inuse;
James Smart92d7f7b2007-06-17 19:56:38 -0500266 }
James Smart82085712007-04-25 09:52:41 -0400267
dea31012005-04-17 16:05:31 -0500268 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400269 warn_on = 1;
dea31012005-04-17 16:05:31 -0500270 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400271 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
272 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500273 }
James Smartc01f3202006-08-18 17:47:08 -0400274
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400275 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400276 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
277 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400278 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
279 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400280 *name, *(name+1), *(name+2), *(name+3),
281 *(name+4), *(name+5), *(name+6), *(name+7),
282 ndlp->nlp_DID, ndlp->nlp_flag,
283 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400284 } else {
James Smarte8b62012007-08-02 11:10:09 -0400285 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
286 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400287 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
288 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400289 *name, *(name+1), *(name+2), *(name+3),
290 *(name+4), *(name+5), *(name+6), *(name+7),
291 ndlp->nlp_DID, ndlp->nlp_flag,
292 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400293 }
294
James Smart87af33f2007-10-27 13:37:43 -0400295 put_node = rdata->pnode != NULL;
296 put_rport = ndlp->rport != NULL;
297 rdata->pnode = NULL;
298 ndlp->rport = NULL;
299 if (put_node)
300 lpfc_nlp_put(ndlp);
301 if (put_rport)
302 put_device(&rport->dev);
303
James Smart2e0fef82007-06-17 19:56:36 -0500304 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400305 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400306 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400307 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400308 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
309 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500310 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400311
James Smarta93ff372010-10-22 11:06:08 -0400312 return fcf_inuse;
313}
314
315/**
316 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
317 * @phba: Pointer to hba context object.
318 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
319 * @nlp_did: remote node identifer with devloss timeout.
320 *
321 * This function is called from the worker thread after invoking devloss
322 * timeout handler and releasing the reference count for the ndlp with
323 * which the devloss timeout was handled for SLI4 host. For the devloss
324 * timeout of the last remote node which had been in use of FCF, when this
325 * routine is invoked, it shall be guaranteed that none of the remote are
326 * in-use of FCF. When devloss timeout to the last remote using the FCF,
327 * if the FIP engine is neither in FCF table scan process nor roundrobin
328 * failover process, the in-use FCF shall be unregistered. If the FIP
329 * engine is in FCF discovery process, the devloss timeout state shall
330 * be set for either the FCF table scan process or roundrobin failover
331 * process to unregister the in-use FCF.
332 **/
333static void
334lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
335 uint32_t nlp_did)
336{
337 /* If devloss timeout happened to a remote node when FCF had no
338 * longer been in-use, do nothing.
339 */
340 if (!fcf_inuse)
341 return;
342
343 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
344 spin_lock_irq(&phba->hbalock);
345 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
346 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
347 spin_unlock_irq(&phba->hbalock);
348 return;
349 }
350 phba->hba_flag |= HBA_DEVLOSS_TMO;
351 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
352 "2847 Last remote node (x%x) using "
353 "FCF devloss tmo\n", nlp_did);
354 }
355 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
356 spin_unlock_irq(&phba->hbalock);
357 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
358 "2868 Devloss tmo to FCF rediscovery "
359 "in progress\n");
360 return;
361 }
362 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
363 spin_unlock_irq(&phba->hbalock);
364 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
365 "2869 Devloss tmo to idle FIP engine, "
366 "unreg in-use FCF and rescan.\n");
367 /* Unregister in-use FCF and rescan */
368 lpfc_unregister_fcf_rescan(phba);
369 return;
370 }
371 spin_unlock_irq(&phba->hbalock);
372 if (phba->hba_flag & FCF_TS_INPROG)
373 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
374 "2870 FCF table scan in progress\n");
375 if (phba->hba_flag & FCF_RR_INPROG)
376 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
377 "2871 FLOGI roundrobin FCF failover "
378 "in progress\n");
379 }
James Smart6fb120a2009-05-22 14:52:59 -0400380 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500381}
James Smartc01f3202006-08-18 17:47:08 -0400382
James Smartea2151b2008-09-07 11:52:10 -0400383/**
James Smart3621a712009-04-06 18:47:14 -0400384 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400385 * @phba: Pointer to hba context object.
386 *
387 * This function is called from the functions which need to post
388 * events from interrupt context. This function allocates data
389 * structure required for posting event. It also keeps track of
390 * number of events pending and prevent event storm when there are
391 * too many events.
392 **/
393struct lpfc_fast_path_event *
394lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
395 struct lpfc_fast_path_event *ret;
396
397 /* If there are lot of fast event do not exhaust memory due to this */
398 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
399 return NULL;
400
401 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
402 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400403 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400404 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400405 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
406 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
407 }
James Smartea2151b2008-09-07 11:52:10 -0400408 return ret;
409}
410
411/**
James Smart3621a712009-04-06 18:47:14 -0400412 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400413 * @phba: Pointer to hba context object.
414 * @evt: Event object which need to be freed.
415 *
416 * This function frees the data structure required for posting
417 * events.
418 **/
419void
420lpfc_free_fast_evt(struct lpfc_hba *phba,
421 struct lpfc_fast_path_event *evt) {
422
423 atomic_dec(&phba->fast_event_count);
424 kfree(evt);
425}
426
427/**
James Smart3621a712009-04-06 18:47:14 -0400428 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400429 * @phba: Pointer to hba context object.
430 * @evtp: Event data structure.
431 *
432 * This function is called from worker thread, when the interrupt
433 * context need to post an event. This function posts the event
434 * to fc transport netlink interface.
435 **/
436static void
437lpfc_send_fastpath_evt(struct lpfc_hba *phba,
438 struct lpfc_work_evt *evtp)
439{
440 unsigned long evt_category, evt_sub_category;
441 struct lpfc_fast_path_event *fast_evt_data;
442 char *evt_data;
443 uint32_t evt_data_size;
444 struct Scsi_Host *shost;
445
446 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
447 work_evt);
448
449 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
450 evt_sub_category = (unsigned long) fast_evt_data->un.
451 fabric_evt.subcategory;
452 shost = lpfc_shost_from_vport(fast_evt_data->vport);
453 if (evt_category == FC_REG_FABRIC_EVENT) {
454 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
455 evt_data = (char *) &fast_evt_data->un.read_check_error;
456 evt_data_size = sizeof(fast_evt_data->un.
457 read_check_error);
458 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500459 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400460 evt_data = (char *) &fast_evt_data->un.fabric_evt;
461 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
462 } else {
463 lpfc_free_fast_evt(phba, fast_evt_data);
464 return;
465 }
466 } else if (evt_category == FC_REG_SCSI_EVENT) {
467 switch (evt_sub_category) {
468 case LPFC_EVENT_QFULL:
469 case LPFC_EVENT_DEVBSY:
470 evt_data = (char *) &fast_evt_data->un.scsi_evt;
471 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
472 break;
473 case LPFC_EVENT_CHECK_COND:
474 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
475 evt_data_size = sizeof(fast_evt_data->un.
476 check_cond_evt);
477 break;
478 case LPFC_EVENT_VARQUEDEPTH:
479 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
480 evt_data_size = sizeof(fast_evt_data->un.
481 queue_depth_evt);
482 break;
483 default:
484 lpfc_free_fast_evt(phba, fast_evt_data);
485 return;
486 }
487 } else {
488 lpfc_free_fast_evt(phba, fast_evt_data);
489 return;
490 }
491
492 fc_host_post_vendor_event(shost,
493 fc_get_event_number(),
494 evt_data_size,
495 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500496 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400497
498 lpfc_free_fast_evt(phba, fast_evt_data);
499 return;
500}
501
dea31012005-04-17 16:05:31 -0500502static void
James Smart2e0fef82007-06-17 19:56:36 -0500503lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500504{
505 struct lpfc_work_evt *evtp = NULL;
506 struct lpfc_nodelist *ndlp;
507 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400508 int fcf_inuse;
509 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500510
James Smart2e0fef82007-06-17 19:56:36 -0500511 spin_lock_irq(&phba->hbalock);
512 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500513 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
514 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500515 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500516 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500517 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500518 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500519 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500520 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500521 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500522 /* decrement the node reference count held
523 * for this queued work
524 */
525 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500526 break;
James Smart858c9f62007-06-17 19:56:39 -0500527 case LPFC_EVT_DEV_LOSS:
528 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400529 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500530 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500531 /* decrement the node reference count held for
532 * this queued work
533 */
James Smarta93ff372010-10-22 11:06:08 -0400534 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500535 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400536 if (phba->sli_rev == LPFC_SLI_REV4)
537 lpfc_sli4_post_dev_loss_tmo_handler(phba,
538 fcf_inuse,
539 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500540 break;
dea31012005-04-17 16:05:31 -0500541 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500542 if (phba->link_state < LPFC_LINK_DOWN)
543 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500544 else
James Smart2e0fef82007-06-17 19:56:36 -0500545 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500546 complete((struct completion *)(evtp->evt_arg2));
547 break;
James Smart46fa3112007-04-25 09:51:45 -0400548 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500549 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400550 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400551 *(int *)(evtp->evt_arg1) = 0;
552 complete((struct completion *)(evtp->evt_arg2));
553 break;
554 case LPFC_EVT_OFFLINE:
555 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500556 lpfc_sli_brdrestart(phba);
557 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400558 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
559 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500560 complete((struct completion *)(evtp->evt_arg2));
561 break;
562 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400563 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500564 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500565 lpfc_sli_brdreset(phba);
566 lpfc_hba_down_post(phba);
567 *(int *)(evtp->evt_arg1) =
568 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400569 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500570 complete((struct completion *)(evtp->evt_arg2));
571 break;
572 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400573 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500574 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500575 = (phba->pport->stopped)
576 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400577 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500578 complete((struct completion *)(evtp->evt_arg2));
579 break;
James Smartea2151b2008-09-07 11:52:10 -0400580 case LPFC_EVT_FASTPATH_MGMT_EVT:
581 lpfc_send_fastpath_evt(phba, evtp);
582 free_evt = 0;
583 break;
James Smart78730cf2010-04-06 15:06:30 -0400584 case LPFC_EVT_RESET_HBA:
585 if (!(phba->pport->load_flag & FC_UNLOADING))
586 lpfc_reset_hba(phba);
587 break;
dea31012005-04-17 16:05:31 -0500588 }
589 if (free_evt)
590 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500591 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500592 }
James Smart2e0fef82007-06-17 19:56:36 -0500593 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500594
595}
596
James Smart311464e2007-08-02 11:10:37 -0400597static void
James Smart2e0fef82007-06-17 19:56:36 -0500598lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500599{
600 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500601 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400602 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400603 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400604 int i;
dea31012005-04-17 16:05:31 -0500605
James Smart2e0fef82007-06-17 19:56:36 -0500606 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500607 ha_copy = phba->work_ha;
608 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500609 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500610
James Smartda0436e2009-05-22 14:51:39 -0400611 /* First, try to post the next mailbox command to SLI4 device */
612 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
613 lpfc_sli4_post_async_mbox(phba);
614
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500615 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400616 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500617 lpfc_handle_eratt(phba);
618
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500619 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500620 lpfc_sli_handle_mb_event(phba);
621
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500622 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500623 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400624
James Smartda0436e2009-05-22 14:51:39 -0400625 /* Process SLI4 events */
626 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500627 if (phba->hba_flag & HBA_RRQ_ACTIVE)
628 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400629 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
630 lpfc_sli4_fcp_xri_abort_event_proc(phba);
631 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
632 lpfc_sli4_els_xri_abort_event_proc(phba);
633 if (phba->hba_flag & ASYNC_EVENT)
634 lpfc_sli4_async_event_proc(phba);
635 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
636 spin_lock_irq(&phba->hbalock);
637 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
638 spin_unlock_irq(&phba->hbalock);
639 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
640 }
James Smartecfd03c2010-02-12 14:41:27 -0500641 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
642 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400643 }
644
James Smart549e55c2007-08-02 11:09:51 -0400645 vports = lpfc_create_vport_work_array(phba);
646 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400647 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400648 /*
649 * We could have no vports in array if unloading, so if
650 * this happens then just use the pport
651 */
652 if (vports[i] == NULL && i == 0)
653 vport = phba->pport;
654 else
655 vport = vports[i];
656 if (vport == NULL)
657 break;
James Smart58da1ff2008-04-07 10:15:56 -0400658 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400659 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400660 vport->work_port_events &= ~work_port_events;
661 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400662 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400663 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400664 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400665 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400666 if (work_port_events & WORKER_HB_TMO)
667 lpfc_hb_timeout_handler(phba);
668 if (work_port_events & WORKER_MBOX_TMO)
669 lpfc_mbox_timeout_handler(phba);
670 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
671 lpfc_unblock_fabric_iocbs(phba);
672 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400673 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400674 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
675 lpfc_ramp_down_queue_handler(phba);
676 if (work_port_events & WORKER_RAMP_UP_QUEUE)
677 lpfc_ramp_up_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500678 if (work_port_events & WORKER_DELAYED_DISC_TMO)
679 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500680 }
James Smart09372822008-01-11 01:52:54 -0500681 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500682
James Smart858c9f62007-06-17 19:56:39 -0500683 pring = &phba->sli.ring[LPFC_ELS_RING];
684 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
685 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400686 if ((status & HA_RXMASK) ||
687 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400688 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400689 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500690 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400691 /* Set the lpfc data pending flag */
692 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500693 } else {
James Smart94661502013-03-01 16:37:29 -0500694 if (phba->link_state >= LPFC_LINK_UP) {
695 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
696 lpfc_sli_handle_slow_ring_event(phba, pring,
697 (status &
698 HA_RXMASK));
699 }
James Smart858c9f62007-06-17 19:56:39 -0500700 }
James Smart0e9bb8d2013-03-01 16:35:12 -0500701 if ((phba->sli_rev == LPFC_SLI_REV4) &
702 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400703 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500704 /*
705 * Turn on Ring interrupts
706 */
James Smart3772a992009-05-22 14:50:54 -0400707 if (phba->sli_rev <= LPFC_SLI_REV3) {
708 spin_lock_irq(&phba->hbalock);
709 control = readl(phba->HCregaddr);
710 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
711 lpfc_debugfs_slow_ring_trc(phba,
712 "WRK Enable ring: cntl:x%x hacopy:x%x",
713 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400714
James Smart3772a992009-05-22 14:50:54 -0400715 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
716 writel(control, phba->HCregaddr);
717 readl(phba->HCregaddr); /* flush */
718 } else {
719 lpfc_debugfs_slow_ring_trc(phba,
720 "WRK Ring ok: cntl:x%x hacopy:x%x",
721 control, ha_copy, 0);
722 }
723 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500724 }
725 }
James Smart2e0fef82007-06-17 19:56:36 -0500726 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500727}
728
dea31012005-04-17 16:05:31 -0500729int
730lpfc_do_work(void *p)
731{
732 struct lpfc_hba *phba = p;
733 int rc;
dea31012005-04-17 16:05:31 -0500734
735 set_user_nice(current, -20);
James Smart043c9562012-05-09 21:17:43 -0400736 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400737 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500738
James Smart3a55b532008-12-04 22:38:54 -0500739 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400740 /* wait and check worker queue activities */
741 rc = wait_event_interruptible(phba->work_waitq,
742 (test_and_clear_bit(LPFC_DATA_READY,
743 &phba->data_flags)
744 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500745 /* Signal wakeup shall terminate the worker thread */
746 if (rc) {
747 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
748 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500749 break;
James Smart3a55b532008-12-04 22:38:54 -0500750 }
dea31012005-04-17 16:05:31 -0500751
James Smart5e9d9b82008-06-14 22:52:53 -0400752 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500753 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500754 }
James Smart3a55b532008-12-04 22:38:54 -0500755 phba->worker_thread = NULL;
756 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
757 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500758 return 0;
759}
760
761/*
762 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300763 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500764 * embedding it in the IOCB.
765 */
766int
James Smart2e0fef82007-06-17 19:56:36 -0500767lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500768 uint32_t evt)
769{
770 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500771 unsigned long flags;
dea31012005-04-17 16:05:31 -0500772
773 /*
774 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
775 * be queued to worker thread for processing
776 */
James Smart92d7f7b2007-06-17 19:56:38 -0500777 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500778 if (!evtp)
779 return 0;
780
781 evtp->evt_arg1 = arg1;
782 evtp->evt_arg2 = arg2;
783 evtp->evt = evt;
784
James Smarted957682007-06-17 19:56:37 -0500785 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400786 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500787 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500788
James Smart5e9d9b82008-06-14 22:52:53 -0400789 lpfc_worker_wake_up(phba);
790
dea31012005-04-17 16:05:31 -0500791 return 1;
792}
793
James Smart92d7f7b2007-06-17 19:56:38 -0500794void
795lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
796{
James Smart09372822008-01-11 01:52:54 -0500797 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500798 struct lpfc_hba *phba = vport->phba;
799 struct lpfc_nodelist *ndlp, *next_ndlp;
800 int rc;
801
802 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500803 if (!NLP_CHK_NODE_ACT(ndlp))
804 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500805 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
806 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400807 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
808 ((vport->port_type == LPFC_NPIV_PORT) &&
809 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500810 lpfc_unreg_rpi(vport, ndlp);
811
812 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400813 if ((phba->sli_rev < LPFC_SLI_REV4) &&
814 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500815 continue;
816 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
817 remove
818 ? NLP_EVT_DEVICE_RM
819 : NLP_EVT_DEVICE_RECOVERY);
820 }
821 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400822 if (phba->sli_rev == LPFC_SLI_REV4)
823 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500824 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500825 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500826 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500827 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500828 }
829}
830
James Smart87af33f2007-10-27 13:37:43 -0400831void
James Smart98c9ea52007-10-27 13:37:33 -0400832lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500833{
James Smart695a8142010-01-26 23:08:03 -0500834 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
835
James Smart45ed1192009-10-02 15:17:02 -0400836 /* Cleanup any outstanding received buffers */
837 lpfc_cleanup_rcv_buffers(vport);
838
James Smart92d7f7b2007-06-17 19:56:38 -0500839 /* Cleanup any outstanding RSCN activity */
840 lpfc_els_flush_rscn(vport);
841
842 /* Cleanup any outstanding ELS commands */
843 lpfc_els_flush_cmd(vport);
844
845 lpfc_cleanup_rpis(vport, 0);
846
James Smart92d7f7b2007-06-17 19:56:38 -0500847 /* Turn off discovery timer if its running */
848 lpfc_can_disctmo(vport);
849}
850
James Smart3772a992009-05-22 14:50:54 -0400851void
James Smart98c9ea52007-10-27 13:37:33 -0400852lpfc_linkdown_port(struct lpfc_vport *vport)
853{
854 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
855
856 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
857
858 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
859 "Link Down: state:x%x rtry:x%x flg:x%x",
860 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
861
862 lpfc_port_link_failure(vport);
863
James Smart92494142011-02-16 12:39:44 -0500864 /* Stop delayed Nport discovery */
865 spin_lock_irq(shost->host_lock);
866 vport->fc_flag &= ~FC_DISC_DELAYED;
867 spin_unlock_irq(shost->host_lock);
868 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400869}
870
dea31012005-04-17 16:05:31 -0500871int
James Smart685f0bf2007-04-25 09:53:08 -0400872lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500873{
James Smart2e0fef82007-06-17 19:56:36 -0500874 struct lpfc_vport *vport = phba->pport;
875 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400876 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400877 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400878 int i;
dea31012005-04-17 16:05:31 -0500879
James Smart3163f722008-02-08 18:50:25 -0500880 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500881 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500882
883 /* Block all SCSI stack I/Os */
884 lpfc_scsi_dev_block(phba);
885
James Smart2e0fef82007-06-17 19:56:36 -0500886 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500887 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500888 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500889 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500890 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500891 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500892 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500893 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500894 }
James Smart549e55c2007-08-02 11:09:51 -0400895 vports = lpfc_create_vport_work_array(phba);
896 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400897 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400898 /* Issue a LINK DOWN event to all nodes */
899 lpfc_linkdown_port(vports[i]);
900 }
James Smart09372822008-01-11 01:52:54 -0500901 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500902 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500903 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
904 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400905 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500906 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500907 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400908 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500909 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500910 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500911 }
912 }
913
dea31012005-04-17 16:05:31 -0500914 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500915 if (phba->pport->fc_flag & FC_PT2PT) {
916 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500917 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
918 if (mb) {
dea31012005-04-17 16:05:31 -0500919 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500920 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500921 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400922 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500923 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500924 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500925 }
926 }
James Smart2e0fef82007-06-17 19:56:36 -0500927 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500928 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500929 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500930 }
James Smart2e0fef82007-06-17 19:56:36 -0500931
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500932 return 0;
dea31012005-04-17 16:05:31 -0500933}
934
James Smart92d7f7b2007-06-17 19:56:38 -0500935static void
936lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500937{
James Smart92d7f7b2007-06-17 19:56:38 -0500938 struct lpfc_nodelist *ndlp;
939
940 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500941 if (!NLP_CHK_NODE_ACT(ndlp))
942 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500943 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
944 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500945 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500946 /* On Linkup its safe to clean up the ndlp
947 * from Fabric connections.
948 */
James Smart92d7f7b2007-06-17 19:56:38 -0500949 if (ndlp->nlp_DID != Fabric_DID)
950 lpfc_unreg_rpi(vport, ndlp);
951 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
952 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500953 /* Fail outstanding IO now since device is
954 * marked for PLOGI.
955 */
James Smart92d7f7b2007-06-17 19:56:38 -0500956 lpfc_unreg_rpi(vport, ndlp);
957 }
958 }
959}
960
961static void
962lpfc_linkup_port(struct lpfc_vport *vport)
963{
964 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500965 struct lpfc_hba *phba = vport->phba;
966
967 if ((vport->load_flag & FC_UNLOADING) != 0)
968 return;
969
James Smart858c9f62007-06-17 19:56:39 -0500970 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
971 "Link Up: top:x%x speed:x%x flg:x%x",
972 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
973
James Smart92d7f7b2007-06-17 19:56:38 -0500974 /* If NPIV is not enabled, only bring the physical port up */
975 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
976 (vport != phba->pport))
977 return;
dea31012005-04-17 16:05:31 -0500978
James Smart2e0fef82007-06-17 19:56:36 -0500979 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400980
James Smart2e0fef82007-06-17 19:56:36 -0500981 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500982 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
983 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
984 vport->fc_flag |= FC_NDISC_ACTIVE;
985 vport->fc_ns_retry = 0;
986 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500987
James Smart92d7f7b2007-06-17 19:56:38 -0500988 if (vport->fc_flag & FC_LBIT)
989 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500990
James Smart92d7f7b2007-06-17 19:56:38 -0500991}
992
993static int
994lpfc_linkup(struct lpfc_hba *phba)
995{
James Smart549e55c2007-08-02 11:09:51 -0400996 struct lpfc_vport **vports;
997 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500998
James Smart19ca7602010-11-20 23:11:55 -0500999 lpfc_cleanup_wt_rrqs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001000 phba->link_state = LPFC_LINK_UP;
1001
1002 /* Unblock fabric iocbs if they are blocked */
1003 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1004 del_timer_sync(&phba->fabric_block_timer);
1005
James Smart549e55c2007-08-02 11:09:51 -04001006 vports = lpfc_create_vport_work_array(phba);
1007 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001008 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001009 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001010 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -04001011 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1012 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05001013 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -05001014
1015 return 0;
1016}
1017
1018/*
1019 * This routine handles processing a CLEAR_LA mailbox
1020 * command upon completion. It is setup in the LPFC_MBOXQ
1021 * as the completion routine when the command is
1022 * handed off to the SLI layer.
1023 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001024static void
James Smart2e0fef82007-06-17 19:56:36 -05001025lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001026{
James Smart2e0fef82007-06-17 19:56:36 -05001027 struct lpfc_vport *vport = pmb->vport;
1028 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1029 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001030 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001031 uint32_t control;
1032
dea31012005-04-17 16:05:31 -05001033 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001034 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001035 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1036 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1037
1038 /* Check for error */
1039 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001040 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001041 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1042 "0320 CLEAR_LA mbxStatus error x%x hba "
1043 "state x%x\n",
1044 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001045 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001046 goto out;
1047 }
1048
James Smart92d7f7b2007-06-17 19:56:38 -05001049 if (vport->port_type == LPFC_PHYSICAL_PORT)
1050 phba->link_state = LPFC_HBA_READY;
1051
1052 spin_lock_irq(&phba->hbalock);
1053 psli->sli_flag |= LPFC_PROCESS_LA;
1054 control = readl(phba->HCregaddr);
1055 control |= HC_LAINT_ENA;
1056 writel(control, phba->HCregaddr);
1057 readl(phba->HCregaddr); /* flush */
1058 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001059 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001060 return;
dea31012005-04-17 16:05:31 -05001061
dea31012005-04-17 16:05:31 -05001062out:
1063 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001064 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1065 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001066 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001067
James Smart2e0fef82007-06-17 19:56:36 -05001068 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001069 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001070 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001071
James Smart2e0fef82007-06-17 19:56:36 -05001072 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001073
1074 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001075
1076 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001077 psli->sli_flag |= LPFC_PROCESS_LA;
1078 control = readl(phba->HCregaddr);
1079 control |= HC_LAINT_ENA;
1080 writel(control, phba->HCregaddr);
1081 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001082 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001083
1084 return;
1085}
1086
James Smart2e0fef82007-06-17 19:56:36 -05001087
dea31012005-04-17 16:05:31 -05001088static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001089lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001090{
James Smart2e0fef82007-06-17 19:56:36 -05001091 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001092
James Smart04c68492009-05-22 14:52:52 -04001093 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001094 goto out;
dea31012005-04-17 16:05:31 -05001095
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001096 mempool_free(pmb, phba->mbox_mem_pool);
1097
James Smart1b511972011-12-13 13:23:09 -05001098 /* don't perform discovery for SLI4 loopback diagnostic test */
1099 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1100 !(phba->hba_flag & HBA_FCOE_MODE) &&
1101 (phba->link_flag & LS_LOOPBACK_MODE))
1102 return;
1103
James Smart76a95d72010-11-20 23:11:48 -05001104 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001105 vport->fc_flag & FC_PUBLIC_LOOP &&
1106 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001107 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001108 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001109 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1110 */
James Smart2e0fef82007-06-17 19:56:36 -05001111 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001112 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001113 }
dea31012005-04-17 16:05:31 -05001114
James Smart2e0fef82007-06-17 19:56:36 -05001115 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001116 * LPFC_FLOGI while waiting for FLOGI cmpl
1117 */
James Smarte6446432012-05-09 21:16:42 -04001118 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001119 lpfc_initial_flogi(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001120 return;
dea31012005-04-17 16:05:31 -05001121
1122out:
James Smarte8b62012007-08-02 11:10:09 -04001123 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1124 "0306 CONFIG_LINK mbxStatus error x%x "
1125 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001126 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001127 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001128
1129 lpfc_linkdown(phba);
1130
James Smarte8b62012007-08-02 11:10:09 -04001131 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1132 "0200 CONFIG_LINK bad hba state x%x\n",
1133 vport->port_state);
dea31012005-04-17 16:05:31 -05001134
James Smart92d7f7b2007-06-17 19:56:38 -05001135 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001136 return;
1137}
1138
James Smart7d791df2011-07-22 18:37:52 -04001139/**
1140 * lpfc_sli4_clear_fcf_rr_bmask
1141 * @phba pointer to the struct lpfc_hba for this port.
1142 * This fucnction resets the round robin bit mask and clears the
1143 * fcf priority list. The list deletions are done while holding the
1144 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1145 * from the lpfc_fcf_pri record.
1146 **/
1147void
1148lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1149{
1150 struct lpfc_fcf_pri *fcf_pri;
1151 struct lpfc_fcf_pri *next_fcf_pri;
1152 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1153 spin_lock_irq(&phba->hbalock);
1154 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1155 &phba->fcf.fcf_pri_list, list) {
1156 list_del_init(&fcf_pri->list);
1157 fcf_pri->fcf_rec.flag = 0;
1158 }
1159 spin_unlock_irq(&phba->hbalock);
1160}
dea31012005-04-17 16:05:31 -05001161static void
James Smart6fb120a2009-05-22 14:52:59 -04001162lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1163{
1164 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001165
1166 if (mboxq->u.mb.mbxStatus) {
1167 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1168 "2017 REG_FCFI mbxStatus error x%x "
1169 "HBA state x%x\n",
1170 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001171 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001172 }
1173
1174 /* Start FCoE discovery by sending a FLOGI. */
1175 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1176 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001177 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001178 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001179 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001180
James Smart32b97932009-07-19 10:01:21 -04001181 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001182 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1183 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001184 goto fail_out;
1185
1186 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001187 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001188 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001189 phba->hba_flag &= ~FCF_TS_INPROG;
1190 if (vport->port_state != LPFC_FLOGI) {
1191 phba->hba_flag |= FCF_RR_INPROG;
1192 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001193 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001194 goto out;
1195 }
1196 spin_unlock_irq(&phba->hbalock);
1197 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001198
James Smarta93ff372010-10-22 11:06:08 -04001199fail_out:
1200 spin_lock_irq(&phba->hbalock);
1201 phba->hba_flag &= ~FCF_RR_INPROG;
1202 spin_unlock_irq(&phba->hbalock);
1203out:
James Smart6fb120a2009-05-22 14:52:59 -04001204 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001205}
1206
1207/**
1208 * lpfc_fab_name_match - Check if the fcf fabric name match.
1209 * @fab_name: pointer to fabric name.
1210 * @new_fcf_record: pointer to fcf record.
1211 *
1212 * This routine compare the fcf record's fabric name with provided
1213 * fabric name. If the fabric name are identical this function
1214 * returns 1 else return 0.
1215 **/
1216static uint32_t
1217lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1218{
James Smartecfd03c2010-02-12 14:41:27 -05001219 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001220 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001221 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1222 return 0;
1223 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1224 return 0;
1225 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1226 return 0;
1227 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1228 return 0;
1229 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1230 return 0;
1231 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1232 return 0;
1233 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1234 return 0;
1235 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001236}
1237
1238/**
James Smart8fa38512009-07-19 10:01:03 -04001239 * lpfc_sw_name_match - Check if the fcf switch name match.
1240 * @fab_name: pointer to fabric name.
1241 * @new_fcf_record: pointer to fcf record.
1242 *
1243 * This routine compare the fcf record's switch name with provided
1244 * switch name. If the switch name are identical this function
1245 * returns 1 else return 0.
1246 **/
1247static uint32_t
1248lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1249{
James Smartecfd03c2010-02-12 14:41:27 -05001250 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001251 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001252 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1253 return 0;
1254 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1255 return 0;
1256 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1257 return 0;
1258 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1259 return 0;
1260 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1261 return 0;
1262 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1263 return 0;
1264 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1265 return 0;
1266 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001267}
1268
1269/**
James Smart6fb120a2009-05-22 14:52:59 -04001270 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001271 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001272 * @new_fcf_record: pointer to fcf record.
1273 *
1274 * This routine compare the fcf record's mac address with HBA's
1275 * FCF mac address. If the mac addresses are identical this function
1276 * returns 1 else return 0.
1277 **/
1278static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001279lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001280{
James Smartecfd03c2010-02-12 14:41:27 -05001281 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001282 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001283 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1284 return 0;
1285 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1286 return 0;
1287 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1288 return 0;
1289 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1290 return 0;
1291 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1292 return 0;
1293 return 1;
1294}
1295
1296static bool
1297lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1298{
1299 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001300}
1301
1302/**
James Smart7d791df2011-07-22 18:37:52 -04001303 * lpfc_update_fcf_record - Update driver fcf record
1304 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1305 * @phba: pointer to lpfc hba data structure.
1306 * @fcf_index: Index for the lpfc_fcf_record.
1307 * @new_fcf_record: pointer to hba fcf record.
1308 *
1309 * This routine updates the driver FCF priority record from the new HBA FCF
1310 * record. This routine is called with the host lock held.
1311 **/
1312static void
1313__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1314 struct fcf_record *new_fcf_record
1315 )
1316{
1317 struct lpfc_fcf_pri *fcf_pri;
1318
1319 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1320 fcf_pri->fcf_rec.fcf_index = fcf_index;
1321 /* FCF record priority */
1322 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1323
1324}
1325
1326/**
James Smart6fb120a2009-05-22 14:52:59 -04001327 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001328 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001329 * @new_fcf_record: pointer to fcf record.
1330 *
1331 * This routine copies the FCF information from the FCF
1332 * record to lpfc_hba data structure.
1333 **/
1334static void
James Smartecfd03c2010-02-12 14:41:27 -05001335lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1336 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001337{
James Smartecfd03c2010-02-12 14:41:27 -05001338 /* Fabric name */
1339 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001340 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001341 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001342 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001343 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001344 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001345 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001346 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001347 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001348 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001349 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001350 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001351 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001352 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001353 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001354 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001355 /* Mac address */
1356 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1357 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1358 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1359 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1360 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1361 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1362 /* FCF record index */
1363 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1364 /* FCF record priority */
1365 fcf_rec->priority = new_fcf_record->fip_priority;
1366 /* Switch name */
1367 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001368 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001369 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001370 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001371 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001372 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001373 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001374 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001375 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001376 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001377 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001378 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001379 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001380 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001381 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001382 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001383}
1384
1385/**
James Smartecfd03c2010-02-12 14:41:27 -05001386 * lpfc_update_fcf_record - Update driver fcf record
1387 * @phba: pointer to lpfc hba data structure.
1388 * @fcf_rec: pointer to driver fcf record.
1389 * @new_fcf_record: pointer to hba fcf record.
1390 * @addr_mode: address mode to be set to the driver fcf record.
1391 * @vlan_id: vlan tag to be set to the driver fcf record.
1392 * @flag: flag bits to be set to the driver fcf record.
1393 *
1394 * This routine updates the driver FCF record from the new HBA FCF record
1395 * together with the address mode, vlan_id, and other informations. This
1396 * routine is called with the host lock held.
1397 **/
1398static void
1399__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1400 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1401 uint16_t vlan_id, uint32_t flag)
1402{
1403 /* Copy the fields from the HBA's FCF record */
1404 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1405 /* Update other fields of driver FCF record */
1406 fcf_rec->addr_mode = addr_mode;
1407 fcf_rec->vlan_id = vlan_id;
1408 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001409 __lpfc_update_fcf_record_pri(phba,
1410 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1411 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001412}
1413
1414/**
James Smart6fb120a2009-05-22 14:52:59 -04001415 * lpfc_register_fcf - Register the FCF with hba.
1416 * @phba: pointer to lpfc hba data structure.
1417 *
1418 * This routine issues a register fcfi mailbox command to register
1419 * the fcf with HBA.
1420 **/
1421static void
1422lpfc_register_fcf(struct lpfc_hba *phba)
1423{
1424 LPFC_MBOXQ_t *fcf_mbxq;
1425 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001426
James Smartd439d282010-09-29 11:18:45 -04001427 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001428 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001429 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001430 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001431 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001432 return;
1433 }
1434
1435 /* The FCF is already registered, start discovery */
1436 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001437 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001438 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001439 if (phba->pport->port_state != LPFC_FLOGI &&
1440 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001441 phba->hba_flag |= FCF_RR_INPROG;
1442 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001443 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001444 return;
1445 }
1446 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001447 return;
1448 }
James Smartd439d282010-09-29 11:18:45 -04001449 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001450
James Smarta93ff372010-10-22 11:06:08 -04001451 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001452 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001453 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001454 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001455 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001456 return;
James Smart4d9ab992009-10-02 15:16:39 -04001457 }
James Smart6fb120a2009-05-22 14:52:59 -04001458
1459 lpfc_reg_fcfi(phba, fcf_mbxq);
1460 fcf_mbxq->vport = phba->pport;
1461 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1462 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001463 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001464 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001465 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001466 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001467 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001468 }
James Smart6fb120a2009-05-22 14:52:59 -04001469
1470 return;
1471}
1472
1473/**
1474 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1475 * @phba: pointer to lpfc hba data structure.
1476 * @new_fcf_record: pointer to fcf record.
1477 * @boot_flag: Indicates if this record used by boot bios.
1478 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001479 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001480 *
1481 * This routine compare the fcf record with connect list obtained from the
1482 * config region to decide if this FCF can be used for SAN discovery. It returns
1483 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1484 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1485 * is used by boot bios and addr_mode will indicate the addressing mode to be
1486 * used for this FCF when the function returns.
1487 * If the FCF record need to be used with a particular vlan id, the vlan is
1488 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001489 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001490 **/
1491static int
1492lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1493 struct fcf_record *new_fcf_record,
1494 uint32_t *boot_flag, uint32_t *addr_mode,
1495 uint16_t *vlan_id)
1496{
1497 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001498 int i, j, fcf_vlan_id = 0;
1499
1500 /* Find the lowest VLAN id in the FCF record */
1501 for (i = 0; i < 512; i++) {
1502 if (new_fcf_record->vlan_bitmap[i]) {
1503 fcf_vlan_id = i * 8;
1504 j = 0;
1505 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1506 j++;
1507 fcf_vlan_id++;
1508 }
1509 break;
1510 }
1511 }
James Smart6fb120a2009-05-22 14:52:59 -04001512
James Smart26979ce2012-09-29 11:31:55 -04001513 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001514 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001515 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1516 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001517 return 0;
1518
James Smart45ed1192009-10-02 15:17:02 -04001519 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001520 *boot_flag = 0;
1521 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1522 new_fcf_record);
1523 if (phba->valid_vlan)
1524 *vlan_id = phba->vlan_id;
1525 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001526 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001527 return 1;
1528 }
1529
1530 /*
1531 * If there are no FCF connection table entry, driver connect to all
1532 * FCFs.
1533 */
1534 if (list_empty(&phba->fcf_conn_rec_list)) {
1535 *boot_flag = 0;
1536 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1537 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001538
1539 /*
1540 * When there are no FCF connect entries, use driver's default
1541 * addressing mode - FPMA.
1542 */
1543 if (*addr_mode & LPFC_FCF_FPMA)
1544 *addr_mode = LPFC_FCF_FPMA;
1545
James Smart4d9ab992009-10-02 15:16:39 -04001546 /* If FCF record report a vlan id use that vlan id */
1547 if (fcf_vlan_id)
1548 *vlan_id = fcf_vlan_id;
1549 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001550 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001551 return 1;
1552 }
1553
James Smartecfd03c2010-02-12 14:41:27 -05001554 list_for_each_entry(conn_entry,
1555 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001556 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1557 continue;
1558
1559 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1560 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001561 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001562 continue;
James Smart8fa38512009-07-19 10:01:03 -04001563 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1564 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1565 new_fcf_record))
1566 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001567 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1568 /*
1569 * If the vlan bit map does not have the bit set for the
1570 * vlan id to be used, then it is not a match.
1571 */
1572 if (!(new_fcf_record->vlan_bitmap
1573 [conn_entry->conn_rec.vlan_tag / 8] &
1574 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1575 continue;
1576 }
1577
1578 /*
James Smart0c287582009-06-10 17:22:56 -04001579 * If connection record does not support any addressing mode,
1580 * skip the FCF record.
1581 */
1582 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1583 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1584 continue;
1585
1586 /*
James Smart6fb120a2009-05-22 14:52:59 -04001587 * Check if the connection record specifies a required
1588 * addressing mode.
1589 */
1590 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1591 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1592
1593 /*
1594 * If SPMA required but FCF not support this continue.
1595 */
1596 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1597 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1598 new_fcf_record) & LPFC_FCF_SPMA))
1599 continue;
1600
1601 /*
1602 * If FPMA required but FCF not support this continue.
1603 */
1604 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1605 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1606 new_fcf_record) & LPFC_FCF_FPMA))
1607 continue;
1608 }
1609
1610 /*
1611 * This fcf record matches filtering criteria.
1612 */
1613 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1614 *boot_flag = 1;
1615 else
1616 *boot_flag = 0;
1617
James Smart0c287582009-06-10 17:22:56 -04001618 /*
1619 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001620 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001621 * by FCF, allow fabric to pick the addressing mode.
1622 */
James Smart6fb120a2009-05-22 14:52:59 -04001623 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1624 new_fcf_record);
1625 /*
1626 * If the user specified a required address mode, assign that
1627 * address mode
1628 */
1629 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1630 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1631 *addr_mode = (conn_entry->conn_rec.flags &
1632 FCFCNCT_AM_SPMA) ?
1633 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1634 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001635 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001636 * addr mode only if FCF support the addr_mode.
1637 */
1638 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1639 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1640 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1641 (*addr_mode & LPFC_FCF_SPMA))
1642 *addr_mode = LPFC_FCF_SPMA;
1643 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1644 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1645 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1646 (*addr_mode & LPFC_FCF_FPMA))
1647 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001648
James Smart4d9ab992009-10-02 15:16:39 -04001649 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001650 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1651 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001652 /*
1653 * If no vlan id is specified in connect list, use the vlan id
1654 * in the FCF record
1655 */
1656 else if (fcf_vlan_id)
1657 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001658 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001659 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001660
1661 return 1;
1662 }
1663
1664 return 0;
1665}
1666
1667/**
James Smart32b97932009-07-19 10:01:21 -04001668 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1669 * @phba: pointer to lpfc hba data structure.
1670 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1671 *
1672 * This function check if there is any fcoe event pending while driver
1673 * scan FCF entries. If there is any pending event, it will restart the
1674 * FCF saning and return 1 else return 0.
1675 */
1676int
1677lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1678{
James Smart32b97932009-07-19 10:01:21 -04001679 /*
1680 * If the Link is up and no FCoE events while in the
1681 * FCF discovery, no need to restart FCF discovery.
1682 */
1683 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001684 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001685 return 0;
1686
James Smart0c9ab6f2010-02-26 14:15:57 -05001687 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1688 "2768 Pending link or FCF event during current "
1689 "handling of the previous event: link_state:x%x, "
1690 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1691 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1692 phba->fcoe_eventtag);
1693
James Smart32b97932009-07-19 10:01:21 -04001694 spin_lock_irq(&phba->hbalock);
1695 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1696 spin_unlock_irq(&phba->hbalock);
1697
James Smart0c9ab6f2010-02-26 14:15:57 -05001698 if (phba->link_state >= LPFC_LINK_UP) {
1699 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1700 "2780 Restart FCF table scan due to "
1701 "pending FCF event:evt_tag_at_scan:x%x, "
1702 "evt_tag_current:x%x\n",
1703 phba->fcoe_eventtag_at_fcf_scan,
1704 phba->fcoe_eventtag);
1705 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1706 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001707 /*
James Smarta93ff372010-10-22 11:06:08 -04001708 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001709 * flag
1710 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001711 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1712 "2833 Stop FCF discovery process due to link "
1713 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001714 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001715 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001716 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001717 spin_unlock_irq(&phba->hbalock);
1718 }
James Smart32b97932009-07-19 10:01:21 -04001719
James Smart0c9ab6f2010-02-26 14:15:57 -05001720 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001721 if (unreg_fcf) {
1722 spin_lock_irq(&phba->hbalock);
1723 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1724 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001725 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001726 }
James Smart32b97932009-07-19 10:01:21 -04001727 return 1;
1728}
1729
1730/**
James Smart999d8132010-03-15 11:24:56 -04001731 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1732 * @phba: pointer to lpfc hba data structure.
1733 * @fcf_cnt: number of eligible fcf record seen so far.
1734 *
1735 * This function makes an running random selection decision on FCF record to
1736 * use through a sequence of @fcf_cnt eligible FCF records with equal
1737 * probability. To perform integer manunipulation of random numbers with
1738 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001739 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001740 *
1741 * Returns true when outcome is for the newly read FCF record should be
1742 * chosen; otherwise, return false when outcome is for keeping the previously
1743 * chosen FCF record.
1744 **/
1745static bool
1746lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1747{
1748 uint32_t rand_num;
1749
1750 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001751 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001752
1753 /* Decision with probability 1/fcf_cnt */
1754 if ((fcf_cnt * rand_num) < 0xFFFF)
1755 return true;
1756 else
1757 return false;
1758}
1759
1760/**
James Smart3804dc82010-07-14 15:31:37 -04001761 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001762 * @phba: pointer to lpfc hba data structure.
1763 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001764 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001765 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001766 * This routine parses the non-embedded fcf mailbox command by performing the
1767 * necessarily error checking, non-embedded read FCF record mailbox command
1768 * SGE parsing, and endianness swapping.
1769 *
1770 * Returns the pointer to the new FCF record in the non-embedded mailbox
1771 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001772 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001773static struct fcf_record *
1774lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1775 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001776{
1777 void *virt_addr;
1778 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001779 struct lpfc_mbx_sge sge;
1780 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1781 uint32_t shdr_status, shdr_add_status;
1782 union lpfc_sli4_cfg_shdr *shdr;
1783 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001784
James Smart6fb120a2009-05-22 14:52:59 -04001785 /* Get the first SGE entry from the non-embedded DMA memory. This
1786 * routine only uses a single SGE.
1787 */
1788 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1789 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1790 if (unlikely(!mboxq->sge_array)) {
1791 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1792 "2524 Failed to get the non-embedded SGE "
1793 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001794 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001795 }
1796 virt_addr = mboxq->sge_array->addr[0];
1797
1798 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001799 lpfc_sli_pcimem_bcopy(shdr, shdr,
1800 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001801 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001802 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001803 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001804 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1805 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001806 "2726 READ_FCF_RECORD Indicates empty "
1807 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001808 else
1809 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001810 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001811 "with status x%x add_status x%x, "
1812 "mbx\n", shdr_status, shdr_add_status);
1813 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001814 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001815
1816 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001817 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1818 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1819 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001820 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001821 new_fcf_record = (struct fcf_record *)(virt_addr +
1822 sizeof(struct lpfc_mbx_read_fcf_tbl));
1823 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001824 offsetof(struct fcf_record, vlan_bitmap));
1825 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1826 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001827
James Smart0c9ab6f2010-02-26 14:15:57 -05001828 return new_fcf_record;
1829}
1830
1831/**
1832 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1833 * @phba: pointer to lpfc hba data structure.
1834 * @fcf_record: pointer to the fcf record.
1835 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1836 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1837 *
1838 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1839 * enabled.
1840 **/
1841static void
1842lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1843 struct fcf_record *fcf_record,
1844 uint16_t vlan_id,
1845 uint16_t next_fcf_index)
1846{
1847 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1848 "2764 READ_FCF_RECORD:\n"
1849 "\tFCF_Index : x%x\n"
1850 "\tFCF_Avail : x%x\n"
1851 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001852 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001853 "\tFIP_Priority : x%x\n"
1854 "\tMAC_Provider : x%x\n"
1855 "\tLowest VLANID : x%x\n"
1856 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1857 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1858 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1859 "\tNext_FCF_Index: x%x\n",
1860 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1861 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1862 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001863 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001864 fcf_record->fip_priority,
1865 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1866 vlan_id,
1867 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1868 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1869 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1870 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1871 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1872 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1873 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1874 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1875 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1876 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1877 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1878 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1879 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1880 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1881 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1882 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1883 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1884 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1885 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1886 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1887 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1888 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1889 next_fcf_index);
1890}
1891
1892/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001893 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1894 * @phba: pointer to lpfc hba data structure.
1895 * @fcf_rec: pointer to an existing FCF record.
1896 * @new_fcf_record: pointer to a new FCF record.
1897 * @new_vlan_id: vlan id from the new FCF record.
1898 *
1899 * This function performs matching test of a new FCF record against an existing
1900 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1901 * will not be used as part of the FCF record matching criteria.
1902 *
1903 * Returns true if all the fields matching, otherwise returns false.
1904 */
1905static bool
1906lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1907 struct lpfc_fcf_rec *fcf_rec,
1908 struct fcf_record *new_fcf_record,
1909 uint16_t new_vlan_id)
1910{
1911 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1912 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1913 return false;
1914 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1915 return false;
1916 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1917 return false;
1918 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1919 return false;
James Smart7d791df2011-07-22 18:37:52 -04001920 if (fcf_rec->priority != new_fcf_record->fip_priority)
1921 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001922 return true;
1923}
1924
1925/**
James Smarta93ff372010-10-22 11:06:08 -04001926 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1927 * @vport: Pointer to vport object.
1928 * @fcf_index: index to next fcf.
1929 *
1930 * This function processing the roundrobin fcf failover to next fcf index.
1931 * When this function is invoked, there will be a current fcf registered
1932 * for flogi.
1933 * Return: 0 for continue retrying flogi on currently registered fcf;
1934 * 1 for stop flogi on currently registered fcf;
1935 */
1936int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1937{
1938 struct lpfc_hba *phba = vport->phba;
1939 int rc;
1940
1941 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1942 spin_lock_irq(&phba->hbalock);
1943 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1944 spin_unlock_irq(&phba->hbalock);
1945 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1946 "2872 Devloss tmo with no eligible "
1947 "FCF, unregister in-use FCF (x%x) "
1948 "and rescan FCF table\n",
1949 phba->fcf.current_rec.fcf_indx);
1950 lpfc_unregister_fcf_rescan(phba);
1951 goto stop_flogi_current_fcf;
1952 }
1953 /* Mark the end to FLOGI roundrobin failover */
1954 phba->hba_flag &= ~FCF_RR_INPROG;
1955 /* Allow action to new fcf asynchronous event */
1956 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1957 spin_unlock_irq(&phba->hbalock);
1958 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1959 "2865 No FCF available, stop roundrobin FCF "
1960 "failover and change port state:x%x/x%x\n",
1961 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1962 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1963 goto stop_flogi_current_fcf;
1964 } else {
1965 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1966 "2794 Try FLOGI roundrobin FCF failover to "
1967 "(x%x)\n", fcf_index);
1968 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1969 if (rc)
1970 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1971 "2761 FLOGI roundrobin FCF failover "
1972 "failed (rc:x%x) to read FCF (x%x)\n",
1973 rc, phba->fcf.current_rec.fcf_indx);
1974 else
1975 goto stop_flogi_current_fcf;
1976 }
1977 return 0;
1978
1979stop_flogi_current_fcf:
1980 lpfc_can_disctmo(vport);
1981 return 1;
1982}
1983
1984/**
James Smart7d791df2011-07-22 18:37:52 -04001985 * lpfc_sli4_fcf_pri_list_del
1986 * @phba: pointer to lpfc hba data structure.
1987 * @fcf_index the index of the fcf record to delete
1988 * This routine checks the on list flag of the fcf_index to be deleted.
1989 * If it is one the list then it is removed from the list, and the flag
1990 * is cleared. This routine grab the hbalock before removing the fcf
1991 * record from the list.
1992 **/
1993static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1994 uint16_t fcf_index)
1995{
1996 struct lpfc_fcf_pri *new_fcf_pri;
1997
1998 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1999 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2000 "3058 deleting idx x%x pri x%x flg x%x\n",
2001 fcf_index, new_fcf_pri->fcf_rec.priority,
2002 new_fcf_pri->fcf_rec.flag);
2003 spin_lock_irq(&phba->hbalock);
2004 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2005 if (phba->fcf.current_rec.priority ==
2006 new_fcf_pri->fcf_rec.priority)
2007 phba->fcf.eligible_fcf_cnt--;
2008 list_del_init(&new_fcf_pri->list);
2009 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2010 }
2011 spin_unlock_irq(&phba->hbalock);
2012}
2013
2014/**
2015 * lpfc_sli4_set_fcf_flogi_fail
2016 * @phba: pointer to lpfc hba data structure.
2017 * @fcf_index the index of the fcf record to update
2018 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2019 * flag so the the round robin slection for the particular priority level
2020 * will try a different fcf record that does not have this bit set.
2021 * If the fcf record is re-read for any reason this flag is cleared brfore
2022 * adding it to the priority list.
2023 **/
2024void
2025lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2026{
2027 struct lpfc_fcf_pri *new_fcf_pri;
2028 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2029 spin_lock_irq(&phba->hbalock);
2030 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2031 spin_unlock_irq(&phba->hbalock);
2032}
2033
2034/**
2035 * lpfc_sli4_fcf_pri_list_add
2036 * @phba: pointer to lpfc hba data structure.
2037 * @fcf_index the index of the fcf record to add
2038 * This routine checks the priority of the fcf_index to be added.
2039 * If it is a lower priority than the current head of the fcf_pri list
2040 * then it is added to the list in the right order.
2041 * If it is the same priority as the current head of the list then it
2042 * is added to the head of the list and its bit in the rr_bmask is set.
2043 * If the fcf_index to be added is of a higher priority than the current
2044 * head of the list then the rr_bmask is cleared, its bit is set in the
2045 * rr_bmask and it is added to the head of the list.
2046 * returns:
2047 * 0=success 1=failure
2048 **/
2049int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba, uint16_t fcf_index,
2050 struct fcf_record *new_fcf_record)
2051{
2052 uint16_t current_fcf_pri;
2053 uint16_t last_index;
2054 struct lpfc_fcf_pri *fcf_pri;
2055 struct lpfc_fcf_pri *next_fcf_pri;
2056 struct lpfc_fcf_pri *new_fcf_pri;
2057 int ret;
2058
2059 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2060 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2061 "3059 adding idx x%x pri x%x flg x%x\n",
2062 fcf_index, new_fcf_record->fip_priority,
2063 new_fcf_pri->fcf_rec.flag);
2064 spin_lock_irq(&phba->hbalock);
2065 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2066 list_del_init(&new_fcf_pri->list);
2067 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2068 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2069 if (list_empty(&phba->fcf.fcf_pri_list)) {
2070 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2071 ret = lpfc_sli4_fcf_rr_index_set(phba,
2072 new_fcf_pri->fcf_rec.fcf_index);
2073 goto out;
2074 }
2075
2076 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2077 LPFC_SLI4_FCF_TBL_INDX_MAX);
2078 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2079 ret = 0; /* Empty rr list */
2080 goto out;
2081 }
2082 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2083 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2084 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2085 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2086 memset(phba->fcf.fcf_rr_bmask, 0,
2087 sizeof(*phba->fcf.fcf_rr_bmask));
2088 /* fcfs_at_this_priority_level = 1; */
2089 phba->fcf.eligible_fcf_cnt = 1;
2090 } else
2091 /* fcfs_at_this_priority_level++; */
2092 phba->fcf.eligible_fcf_cnt++;
2093 ret = lpfc_sli4_fcf_rr_index_set(phba,
2094 new_fcf_pri->fcf_rec.fcf_index);
2095 goto out;
2096 }
2097
2098 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2099 &phba->fcf.fcf_pri_list, list) {
2100 if (new_fcf_pri->fcf_rec.priority <=
2101 fcf_pri->fcf_rec.priority) {
2102 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2103 list_add(&new_fcf_pri->list,
2104 &phba->fcf.fcf_pri_list);
2105 else
2106 list_add(&new_fcf_pri->list,
2107 &((struct lpfc_fcf_pri *)
2108 fcf_pri->list.prev)->list);
2109 ret = 0;
2110 goto out;
2111 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2112 || new_fcf_pri->fcf_rec.priority <
2113 next_fcf_pri->fcf_rec.priority) {
2114 list_add(&new_fcf_pri->list, &fcf_pri->list);
2115 ret = 0;
2116 goto out;
2117 }
2118 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2119 continue;
2120
2121 }
2122 ret = 1;
2123out:
2124 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2125 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2126 spin_unlock_irq(&phba->hbalock);
2127 return ret;
2128}
2129
2130/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002131 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2132 * @phba: pointer to lpfc hba data structure.
2133 * @mboxq: pointer to mailbox object.
2134 *
2135 * This function iterates through all the fcf records available in
2136 * HBA and chooses the optimal FCF record for discovery. After finding
2137 * the FCF for discovery it registers the FCF record and kicks start
2138 * discovery.
2139 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2140 * use an FCF record which matches fabric name and mac address of the
2141 * currently used FCF record.
2142 * If the driver supports only one FCF, it will try to use the FCF record
2143 * used by BOOT_BIOS.
2144 */
2145void
2146lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2147{
2148 struct fcf_record *new_fcf_record;
2149 uint32_t boot_flag, addr_mode;
2150 uint16_t fcf_index, next_fcf_index;
2151 struct lpfc_fcf_rec *fcf_rec = NULL;
2152 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002153 uint32_t seed;
2154 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002155 int rc;
2156
2157 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002158 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002159 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2160 return;
2161 }
2162
2163 /* Parse the FCF record from the non-embedded mailbox command */
2164 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2165 &next_fcf_index);
2166 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002167 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002168 "2765 Mailbox command READ_FCF_RECORD "
2169 "failed to retrieve a FCF record.\n");
2170 /* Let next new FCF event trigger fast failover */
2171 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002172 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002173 spin_unlock_irq(&phba->hbalock);
2174 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2175 return;
2176 }
2177
2178 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002179 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2180 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002181
2182 /* Log the FCF record information if turned on */
2183 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2184 next_fcf_index);
2185
James Smart6fb120a2009-05-22 14:52:59 -04002186 /*
2187 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002188 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002189 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002190 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002191 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002192 lpfc_sli4_fcf_pri_list_del(phba,
2193 bf_get(lpfc_fcf_record_fcf_index,
2194 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002195 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002196 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002197 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002198 bf_get(lpfc_fcf_record_fcf_index,
2199 new_fcf_record),
2200 bf_get(lpfc_fcf_record_fcf_avail,
2201 new_fcf_record),
2202 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002203 new_fcf_record),
2204 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002205 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002206 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2207 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2208 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002209 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2210 phba->fcf.current_rec.fcf_indx) {
2211 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2212 "2862 FCF (x%x) matches property "
2213 "of in-use FCF (x%x)\n",
2214 bf_get(lpfc_fcf_record_fcf_index,
2215 new_fcf_record),
2216 phba->fcf.current_rec.fcf_indx);
2217 goto read_next_fcf;
2218 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002219 /*
2220 * In case the current in-use FCF record becomes
2221 * invalid/unavailable during FCF discovery that
2222 * was not triggered by fast FCF failover process,
2223 * treat it as fast FCF failover.
2224 */
2225 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2226 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2227 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2228 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002229 "(x%x), enter FCF failover "
2230 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002231 phba->fcf.current_rec.fcf_indx);
2232 spin_lock_irq(&phba->hbalock);
2233 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2234 spin_unlock_irq(&phba->hbalock);
2235 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2236 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2237 LPFC_FCOE_FCF_GET_FIRST);
2238 return;
2239 }
2240 }
James Smart6fb120a2009-05-22 14:52:59 -04002241 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002242 } else {
2243 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002244 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2245 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002246 if (rc)
2247 goto read_next_fcf;
2248 }
2249
James Smart6fb120a2009-05-22 14:52:59 -04002250 /*
2251 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002252 * FCF record for the discovery. The condition that a rescan
2253 * matches the in-use FCF record: fabric name, switch name, mac
2254 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002255 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002256 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002257 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002258 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2259 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002260 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002261 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2262 phba->fcf.current_rec.fcf_indx) {
2263 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2264 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2265 /* Stop FCF redisc wait timer */
2266 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2267 phba);
2268 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2269 /* Fast failover, mark completed */
2270 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2271 spin_unlock_irq(&phba->hbalock);
2272 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2273 "2836 New FCF matches in-use "
2274 "FCF (x%x)\n",
2275 phba->fcf.current_rec.fcf_indx);
2276 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 "
2547 "failed to retrieve a FCF record.\n");
James Smarta93ff372010-10-22 11:06:08 -04002548 goto error_out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002549 }
2550
2551 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002552 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2553 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002554
2555 /* Log the FCF record information if turned on */
2556 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2557 next_fcf_index);
2558
James Smarta93ff372010-10-22 11:06:08 -04002559 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2560 if (!rc) {
2561 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2562 "2848 Remove ineligible FCF (x%x) from "
2563 "from roundrobin bmask\n", fcf_index);
2564 /* Clear roundrobin bmask bit for ineligible FCF */
2565 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2566 /* Perform next round of roundrobin FCF failover */
2567 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2568 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2569 if (rc)
2570 goto out;
2571 goto error_out;
2572 }
2573
2574 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2575 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2576 "2760 Perform FLOGI roundrobin FCF failover: "
2577 "FCF (x%x) back to FCF (x%x)\n",
2578 phba->fcf.current_rec.fcf_indx, fcf_index);
2579 /* Wait 500 ms before retrying FLOGI to current FCF */
2580 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002581 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002582 goto out;
2583 }
2584
James Smart0c9ab6f2010-02-26 14:15:57 -05002585 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002586 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002587 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2588 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002589 spin_lock_irq(&phba->hbalock);
2590 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2591 new_fcf_record, addr_mode, vlan_id,
2592 (boot_flag ? BOOT_ENABLE : 0));
2593 spin_unlock_irq(&phba->hbalock);
2594
2595 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2596
2597 /* Unregister the current in-use FCF record */
2598 lpfc_unregister_fcf(phba);
2599
2600 /* Replace in-use record with the new record */
2601 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2602 sizeof(struct lpfc_fcf_rec));
2603
2604 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002605 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2606 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002607
James Smarta93ff372010-10-22 11:06:08 -04002608error_out:
2609 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002610out:
2611 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002612}
2613
2614/**
2615 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2616 * @phba: pointer to lpfc hba data structure.
2617 * @mboxq: pointer to mailbox object.
2618 *
2619 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002620 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002621 * failover when a new FCF event happened. If the FCF read back is
2622 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002623 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002624 */
2625void
2626lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2627{
2628 struct fcf_record *new_fcf_record;
2629 uint32_t boot_flag, addr_mode;
2630 uint16_t fcf_index, next_fcf_index;
2631 uint16_t vlan_id;
2632 int rc;
2633
2634 /* If link state is not up, no need to proceed */
2635 if (phba->link_state < LPFC_LINK_UP)
2636 goto out;
2637
2638 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002639 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002640 goto out;
2641
2642 /* Parse the FCF record from the non-embedded mailbox command */
2643 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2644 &next_fcf_index);
2645 if (!new_fcf_record) {
2646 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2647 "2767 Mailbox command READ_FCF_RECORD "
2648 "failed to retrieve a FCF record.\n");
2649 goto out;
2650 }
2651
2652 /* Check the connection list for eligibility */
2653 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2654 &addr_mode, &vlan_id);
2655
2656 /* Log the FCF record information if turned on */
2657 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2658 next_fcf_index);
2659
2660 if (!rc)
2661 goto out;
2662
2663 /* Update the eligible FCF record index bmask */
2664 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002665
2666 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002667
2668out:
2669 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2670}
2671
2672/**
James Smart76a95d72010-11-20 23:11:48 -05002673 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2674 * @phba: pointer to lpfc hba data structure.
2675 * @mboxq: pointer to mailbox data structure.
2676 *
2677 * This function handles completion of init vfi mailbox command.
2678 */
2679void
2680lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2681{
2682 struct lpfc_vport *vport = mboxq->vport;
2683
James Smartdf9e1b52011-12-13 13:22:17 -05002684 /*
2685 * VFI not supported on interface type 0, just do the flogi
2686 * Also continue if the VFI is in use - just use the same one.
2687 */
2688 if (mboxq->u.mb.mbxStatus &&
2689 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2690 LPFC_SLI_INTF_IF_TYPE_0) &&
2691 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002692 lpfc_printf_vlog(vport, KERN_ERR,
2693 LOG_MBOX,
2694 "2891 Init VFI mailbox failed 0x%x\n",
2695 mboxq->u.mb.mbxStatus);
2696 mempool_free(mboxq, phba->mbox_mem_pool);
2697 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2698 return;
2699 }
James Smart73d91e52011-10-10 21:32:10 -04002700
James Smart76a95d72010-11-20 23:11:48 -05002701 lpfc_initial_flogi(vport);
2702 mempool_free(mboxq, phba->mbox_mem_pool);
2703 return;
2704}
2705
2706/**
2707 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2708 * @vport: pointer to lpfc_vport data structure.
2709 *
2710 * This function issue a init_vfi mailbox command to initialize the VFI and
2711 * VPI for the physical port.
2712 */
2713void
2714lpfc_issue_init_vfi(struct lpfc_vport *vport)
2715{
2716 LPFC_MBOXQ_t *mboxq;
2717 int rc;
2718 struct lpfc_hba *phba = vport->phba;
2719
2720 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2721 if (!mboxq) {
2722 lpfc_printf_vlog(vport, KERN_ERR,
2723 LOG_MBOX, "2892 Failed to allocate "
2724 "init_vfi mailbox\n");
2725 return;
2726 }
2727 lpfc_init_vfi(mboxq, vport);
2728 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2729 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2730 if (rc == MBX_NOT_FINISHED) {
2731 lpfc_printf_vlog(vport, KERN_ERR,
2732 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2733 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2734 }
2735}
2736
2737/**
James Smart1c6834a2009-07-19 10:01:26 -04002738 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2739 * @phba: pointer to lpfc hba data structure.
2740 * @mboxq: pointer to mailbox data structure.
2741 *
2742 * This function handles completion of init vpi mailbox command.
2743 */
James Smart695a8142010-01-26 23:08:03 -05002744void
James Smart1c6834a2009-07-19 10:01:26 -04002745lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2746{
2747 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002748 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002749 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2750
James Smart1c6834a2009-07-19 10:01:26 -04002751 if (mboxq->u.mb.mbxStatus) {
2752 lpfc_printf_vlog(vport, KERN_ERR,
2753 LOG_MBOX,
2754 "2609 Init VPI mailbox failed 0x%x\n",
2755 mboxq->u.mb.mbxStatus);
2756 mempool_free(mboxq, phba->mbox_mem_pool);
2757 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2758 return;
2759 }
James Smart72100cc2010-02-12 14:43:01 -05002760 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002761 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002762 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002763
James Smart695a8142010-01-26 23:08:03 -05002764 /* If this port is physical port or FDISC is done, do reg_vpi */
2765 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2766 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2767 if (!ndlp)
2768 lpfc_printf_vlog(vport, KERN_ERR,
2769 LOG_DISCOVERY,
2770 "2731 Cannot find fabric "
2771 "controller node\n");
2772 else
2773 lpfc_register_new_vport(phba, vport, ndlp);
2774 mempool_free(mboxq, phba->mbox_mem_pool);
2775 return;
2776 }
2777
James Smart1c6834a2009-07-19 10:01:26 -04002778 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2779 lpfc_initial_fdisc(vport);
2780 else {
2781 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002782 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2783 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002784 }
James Smart695a8142010-01-26 23:08:03 -05002785 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002786 return;
2787}
2788
2789/**
James Smartecfd03c2010-02-12 14:41:27 -05002790 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2791 * @vport: pointer to lpfc_vport data structure.
2792 *
2793 * This function issue a init_vpi mailbox command to initialize
2794 * VPI for the vport.
2795 */
2796void
2797lpfc_issue_init_vpi(struct lpfc_vport *vport)
2798{
2799 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002800 int rc, vpi;
2801
2802 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2803 vpi = lpfc_alloc_vpi(vport->phba);
2804 if (!vpi) {
2805 lpfc_printf_vlog(vport, KERN_ERR,
2806 LOG_MBOX,
2807 "3303 Failed to obtain vport vpi\n");
2808 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2809 return;
2810 }
2811 vport->vpi = vpi;
2812 }
James Smartecfd03c2010-02-12 14:41:27 -05002813
2814 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2815 if (!mboxq) {
2816 lpfc_printf_vlog(vport, KERN_ERR,
2817 LOG_MBOX, "2607 Failed to allocate "
2818 "init_vpi mailbox\n");
2819 return;
2820 }
2821 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2822 mboxq->vport = vport;
2823 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2824 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2825 if (rc == MBX_NOT_FINISHED) {
2826 lpfc_printf_vlog(vport, KERN_ERR,
2827 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2828 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2829 }
2830}
2831
2832/**
James Smart6fb120a2009-05-22 14:52:59 -04002833 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2834 * @phba: pointer to lpfc hba data structure.
2835 *
2836 * This function loops through the list of vports on the @phba and issues an
2837 * FDISC if possible.
2838 */
2839void
2840lpfc_start_fdiscs(struct lpfc_hba *phba)
2841{
2842 struct lpfc_vport **vports;
2843 int i;
2844
2845 vports = lpfc_create_vport_work_array(phba);
2846 if (vports != NULL) {
2847 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2848 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2849 continue;
2850 /* There are no vpi for this vport */
2851 if (vports[i]->vpi > phba->max_vpi) {
2852 lpfc_vport_set_state(vports[i],
2853 FC_VPORT_FAILED);
2854 continue;
2855 }
James Smart76a95d72010-11-20 23:11:48 -05002856 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002857 lpfc_vport_set_state(vports[i],
2858 FC_VPORT_LINKDOWN);
2859 continue;
2860 }
James Smart1c6834a2009-07-19 10:01:26 -04002861 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002862 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002863 continue;
2864 }
James Smart6fb120a2009-05-22 14:52:59 -04002865 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2866 lpfc_initial_fdisc(vports[i]);
2867 else {
2868 lpfc_vport_set_state(vports[i],
2869 FC_VPORT_NO_FABRIC_SUPP);
2870 lpfc_printf_vlog(vports[i], KERN_ERR,
2871 LOG_ELS,
2872 "0259 No NPIV "
2873 "Fabric support\n");
2874 }
2875 }
2876 }
2877 lpfc_destroy_vport_work_array(phba, vports);
2878}
2879
2880void
2881lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2882{
2883 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2884 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002886
James Smartf5eca9b2012-03-01 22:37:54 -05002887 /*
2888 * VFI not supported for interface type 0, so ignore any mailbox
2889 * error (except VFI in use) and continue with the discovery.
2890 */
2891 if (mboxq->u.mb.mbxStatus &&
2892 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2893 LPFC_SLI_INTF_IF_TYPE_0) &&
2894 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002895 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2896 "2018 REG_VFI mbxStatus error x%x "
2897 "HBA state x%x\n",
2898 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002899 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002900 /* FLOGI failed, use loop map to make discovery list */
2901 lpfc_disc_list_loopmap(vport);
2902 /* Start discovery */
2903 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002904 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002905 }
2906 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002907 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002908 }
James Smartae05ebe2013-03-01 16:35:38 -05002909
James Smarte74c03c2013-04-17 20:15:19 -04002910 /* If the VFI is already registered, there is nothing else to do
2911 * Unless this was a VFI update and we are in PT2PT mode, then
2912 * we should drop through to set the port state to ready.
2913 */
James Smartae05ebe2013-03-01 16:35:38 -05002914 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002915 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2916 vport->fc_flag & FC_PT2PT))
2917 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002918
James Smartc8685952009-11-18 15:39:16 -05002919 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002920 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002921 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002922 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002923 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002924 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002925 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002926
James Smart1b511972011-12-13 13:23:09 -05002927 /* In case SLI4 FC loopback test, we are ready */
2928 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2929 (phba->link_flag & LS_LOOPBACK_MODE)) {
2930 phba->link_state = LPFC_HBA_READY;
2931 goto out_free_mem;
2932 }
2933
James Smarte74c03c2013-04-17 20:15:19 -04002934 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2935 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2936 "alpacnt:%d LinkState:%x topology:%x\n",
2937 vport->port_state, vport->fc_flag, vport->fc_myDID,
2938 vport->phba->alpa_map[0],
2939 phba->link_state, phba->fc_topology);
2940
James Smart6fb120a2009-05-22 14:52:59 -04002941 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002942 /*
2943 * For private loop or for NPort pt2pt,
2944 * just start discovery and we are done.
2945 */
2946 if ((vport->fc_flag & FC_PT2PT) ||
2947 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2948 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2949
James Smart76a95d72010-11-20 23:11:48 -05002950 /* Use loop map to make discovery list */
2951 lpfc_disc_list_loopmap(vport);
2952 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002953 if (vport->fc_flag & FC_PT2PT)
2954 vport->port_state = LPFC_VPORT_READY;
2955 else
2956 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002957 } else {
2958 lpfc_start_fdiscs(phba);
2959 lpfc_do_scr_ns_plogi(phba, vport);
2960 }
James Smart6fb120a2009-05-22 14:52:59 -04002961 }
2962
James Smart1b511972011-12-13 13:23:09 -05002963out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002964 mempool_free(mboxq, phba->mbox_mem_pool);
2965 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2966 kfree(dmabuf);
2967 return;
2968}
2969
2970static void
James Smart2e0fef82007-06-17 19:56:36 -05002971lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002972{
James Smart6fb120a2009-05-22 14:52:59 -04002973 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002974 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002975 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002976
2977
2978 /* Check for error */
2979 if (mb->mbxStatus) {
2980 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002981 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2982 "0319 READ_SPARAM mbxStatus error x%x "
2983 "hba state x%x>\n",
2984 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002985 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002986 goto out;
2987 }
2988
James Smart2e0fef82007-06-17 19:56:36 -05002989 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002990 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04002991 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002992 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2993 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2994 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2995 }
2996
dea31012005-04-17 16:05:31 -05002997 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2998 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002999 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003000 return;
3001
3002out:
3003 pmb->context1 = NULL;
3004 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3005 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003006 lpfc_issue_clear_la(phba, vport);
3007 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003008 return;
3009}
3010
3011static void
James Smart76a95d72010-11-20 23:11:48 -05003012lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003013{
James Smart92d7f7b2007-06-17 19:56:38 -05003014 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003015 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003016 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003017 int i;
James Smart14691152006-12-02 13:34:28 -05003018 struct lpfc_dmabuf *mp;
3019 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003020 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003021 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003022
James Smart92d7f7b2007-06-17 19:56:38 -05003023 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003024 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3025 case LPFC_LINK_SPEED_1GHZ:
3026 case LPFC_LINK_SPEED_2GHZ:
3027 case LPFC_LINK_SPEED_4GHZ:
3028 case LPFC_LINK_SPEED_8GHZ:
3029 case LPFC_LINK_SPEED_10GHZ:
3030 case LPFC_LINK_SPEED_16GHZ:
3031 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04003032 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003033 default:
James Smart76a95d72010-11-20 23:11:48 -05003034 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05003035 break;
dea31012005-04-17 16:05:31 -05003036 }
3037
James Smarte74c03c2013-04-17 20:15:19 -04003038 if (phba->fc_topology &&
3039 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3040 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3041 "3314 Toplogy changed was 0x%x is 0x%x\n",
3042 phba->fc_topology,
3043 bf_get(lpfc_mbx_read_top_topology, la));
3044 phba->fc_topology_changed = 1;
3045 }
3046
James Smart76a95d72010-11-20 23:11:48 -05003047 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003048 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003049
James Smartdf9e1b52011-12-13 13:22:17 -05003050 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003051 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003052 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003053
James Smart6a9c52c2009-10-02 15:16:51 -04003054 /* if npiv is enabled and this adapter supports npiv log
3055 * a message that npiv is not supported in this topology
3056 */
3057 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003058 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3059 "1309 Link Up Event npiv not supported in loop "
3060 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003061 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003062 if (bf_get(lpfc_mbx_read_top_il, la))
3063 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003064
James Smart76a95d72010-11-20 23:11:48 -05003065 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3066 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003067
3068 if (i == 0) {
3069 phba->alpa_map[0] = 0;
3070 } else {
James Smarte8b62012007-08-02 11:10:09 -04003071 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003072 int numalpa, j, k;
3073 union {
3074 uint8_t pamap[16];
3075 struct {
3076 uint32_t wd1;
3077 uint32_t wd2;
3078 uint32_t wd3;
3079 uint32_t wd4;
3080 } pa;
3081 } un;
3082 numalpa = phba->alpa_map[0];
3083 j = 0;
3084 while (j < numalpa) {
3085 memset(un.pamap, 0, 16);
3086 for (k = 1; j < numalpa; k++) {
3087 un.pamap[k - 1] =
3088 phba->alpa_map[j + 1];
3089 j++;
3090 if (k == 16)
3091 break;
3092 }
3093 /* Link Up Event ALPA map */
3094 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003095 KERN_WARNING,
3096 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003097 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003098 "ALPA map Data: x%x "
3099 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003100 un.pa.wd1, un.pa.wd2,
3101 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003102 }
3103 }
3104 }
3105 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003106 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003107 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003108 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003109 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3110 }
James Smart2e0fef82007-06-17 19:56:36 -05003111 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003112 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003113 }
James Smart92d7f7b2007-06-17 19:56:38 -05003114 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003115
James Smart38c20672013-03-01 16:37:44 -05003116 if (fc_flags) {
3117 spin_lock_irq(shost->host_lock);
3118 vport->fc_flag |= fc_flags;
3119 spin_unlock_irq(shost->host_lock);
3120 }
3121
dea31012005-04-17 16:05:31 -05003122 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003123 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3124 if (!sparam_mbox)
3125 goto out;
3126
3127 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3128 if (rc) {
3129 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3130 goto out;
3131 }
3132 sparam_mbox->vport = vport;
3133 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3134 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3135 if (rc == MBX_NOT_FINISHED) {
3136 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3137 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3138 kfree(mp);
3139 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3140 goto out;
dea31012005-04-17 16:05:31 -05003141 }
3142
James Smart76a95d72010-11-20 23:11:48 -05003143 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003144 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3145 if (!cfglink_mbox)
3146 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003147 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003148 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003149 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003150 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003151 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003152 if (rc == MBX_NOT_FINISHED) {
3153 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3154 goto out;
3155 }
3156 } else {
James Smart32b97932009-07-19 10:01:21 -04003157 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003158 /*
3159 * Add the driver's default FCF record at FCF index 0 now. This
3160 * is phase 1 implementation that support FCF index 0 and driver
3161 * defaults.
3162 */
James Smart45ed1192009-10-02 15:17:02 -04003163 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003164 fcf_record = kzalloc(sizeof(struct fcf_record),
3165 GFP_KERNEL);
3166 if (unlikely(!fcf_record)) {
3167 lpfc_printf_log(phba, KERN_ERR,
3168 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003169 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003170 "fcf record\n");
3171 rc = -ENODEV;
3172 goto out;
3173 }
3174
3175 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3176 LPFC_FCOE_FCF_DEF_INDEX);
3177 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3178 if (unlikely(rc)) {
3179 lpfc_printf_log(phba, KERN_ERR,
3180 LOG_MBOX | LOG_SLI,
3181 "2013 Could not manually add FCF "
3182 "record 0, status %d\n", rc);
3183 rc = -ENODEV;
3184 kfree(fcf_record);
3185 goto out;
3186 }
3187 kfree(fcf_record);
3188 }
3189 /*
3190 * The driver is expected to do FIP/FCF. Call the port
3191 * and get the FCF Table.
3192 */
James Smart32b97932009-07-19 10:01:21 -04003193 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003194 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003195 spin_unlock_irq(&phba->hbalock);
3196 return;
3197 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003198 /* This is the initial FCF discovery scan */
3199 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003200 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003201 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3202 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003203 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3204 LPFC_FCOE_FCF_GET_FIRST);
3205 if (rc) {
3206 spin_lock_irq(&phba->hbalock);
3207 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3208 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003209 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003210 }
James Smart38b92ef2010-08-04 16:11:39 -04003211 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003212 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003213 }
James Smart6fb120a2009-05-22 14:52:59 -04003214
3215 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003216out:
3217 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003218 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3219 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3220 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003221 lpfc_issue_clear_la(phba, vport);
3222 return;
dea31012005-04-17 16:05:31 -05003223}
3224
3225static void
James Smart84774a42008-08-24 21:50:06 -04003226lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003227{
dea31012005-04-17 16:05:31 -05003228 uint32_t control;
3229 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003230 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003231 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003232 if (phba->sli_rev <= LPFC_SLI_REV3) {
3233 control = readl(phba->HCregaddr);
3234 control |= HC_LAINT_ENA;
3235 writel(control, phba->HCregaddr);
3236 readl(phba->HCregaddr); /* flush */
3237 }
James Smart2e0fef82007-06-17 19:56:36 -05003238 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003239}
3240
James Smart84774a42008-08-24 21:50:06 -04003241static void
3242lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3243{
3244 lpfc_linkdown(phba);
3245 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003246 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003247 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3248}
3249
3250
dea31012005-04-17 16:05:31 -05003251/*
James Smart76a95d72010-11-20 23:11:48 -05003252 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003253 * command upon completion. It is setup in the LPFC_MBOXQ
3254 * as the completion routine when the command is
3255 * handed off to the SLI layer.
3256 */
3257void
James Smart76a95d72010-11-20 23:11:48 -05003258lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003259{
James Smart2e0fef82007-06-17 19:56:36 -05003260 struct lpfc_vport *vport = pmb->vport;
3261 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003262 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003263 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003264 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3265
James Smart0d2b6b82008-06-14 22:52:47 -04003266 /* Unblock ELS traffic */
3267 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003268 /* Check for error */
3269 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003270 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003271 "1307 READ_LA mbox error x%x state x%x\n",
3272 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003273 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003274 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003275 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003276 }
3277
James Smart76a95d72010-11-20 23:11:48 -05003278 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003279
3280 memcpy(&phba->alpa_map[0], mp->virt, 128);
3281
James Smart2e0fef82007-06-17 19:56:36 -05003282 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003283 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003284 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003285 else
James Smart2e0fef82007-06-17 19:56:36 -05003286 vport->fc_flag &= ~FC_BYPASSED_MODE;
3287 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003288
James Smartbe0c0082013-03-01 16:38:14 -05003289 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003290 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003291 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003292 if (phba->fc_eventTag != 0)
3293 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003294 }
dea31012005-04-17 16:05:31 -05003295
3296 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003297 if (phba->sli_rev < LPFC_SLI_REV4) {
3298 spin_lock_irq(&phba->hbalock);
3299 if (bf_get(lpfc_mbx_read_top_mm, la))
3300 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3301 else
3302 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3303 spin_unlock_irq(&phba->hbalock);
3304 }
dea31012005-04-17 16:05:31 -05003305
James Smart4d9ab992009-10-02 15:16:39 -04003306 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003307 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003308 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003309 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003310 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003311 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003312 "1306 Link Up Event in loop back mode "
3313 "x%x received Data: x%x x%x x%x x%x\n",
3314 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003315 bf_get(lpfc_mbx_read_top_alpa_granted,
3316 la),
3317 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003318 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003319 } else {
3320 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003321 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003322 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003323 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003324 bf_get(lpfc_mbx_read_top_alpa_granted,
3325 la),
3326 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003327 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003328 bf_get(lpfc_mbx_read_top_mm, la),
3329 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003330 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003331 }
James Smart92d7f7b2007-06-17 19:56:38 -05003332 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003333 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3334 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003335 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003336 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003337 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3338 "1308 Link Down Event in loop back mode "
3339 "x%x received "
3340 "Data: x%x x%x x%x\n",
3341 la->eventTag, phba->fc_eventTag,
3342 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003343 else
James Smart3163f722008-02-08 18:50:25 -05003344 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003345 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003346 "Data: x%x x%x x%x x%x x%x\n",
3347 la->eventTag, phba->fc_eventTag,
3348 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003349 bf_get(lpfc_mbx_read_top_mm, la),
3350 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003351 lpfc_mbx_issue_link_down(phba);
3352 }
James Smartbe0c0082013-03-01 16:38:14 -05003353 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3354 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003355 if (phba->link_state != LPFC_LINK_DOWN) {
3356 phba->fc_stat.LinkDown++;
3357 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3358 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003359 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003360 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003361 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003362 lpfc_mbx_issue_link_down(phba);
3363 } else
3364 lpfc_enable_la(phba);
3365
3366 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3367 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3368 "Data: x%x x%x x%x\n",
3369 la->eventTag, phba->fc_eventTag,
3370 phba->pport->port_state, vport->fc_flag);
3371 /*
3372 * The cmnd that triggered this will be waiting for this
3373 * signal.
3374 */
3375 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3376 if (phba->wait_4_mlo_maint_flg) {
3377 phba->wait_4_mlo_maint_flg = 0;
3378 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003379 }
James Smart84774a42008-08-24 21:50:06 -04003380 }
3381
James Smartbe0c0082013-03-01 16:38:14 -05003382 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3383 bf_get(lpfc_mbx_read_top_fa, la)) {
3384 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003385 lpfc_issue_clear_la(phba, vport);
3386 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003387 "1311 fa %d\n",
3388 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003389 }
3390
James Smart76a95d72010-11-20 23:11:48 -05003391lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003392 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3393 kfree(mp);
3394 mempool_free(pmb, phba->mbox_mem_pool);
3395 return;
3396}
3397
3398/*
3399 * This routine handles processing a REG_LOGIN mailbox
3400 * command upon completion. It is setup in the LPFC_MBOXQ
3401 * as the completion routine when the command is
3402 * handed off to the SLI layer.
3403 */
3404void
James Smart2e0fef82007-06-17 19:56:36 -05003405lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003406{
James Smart2e0fef82007-06-17 19:56:36 -05003407 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003408 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003409 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003410 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003411
dea31012005-04-17 16:05:31 -05003412 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003413 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003414
James Smartffc95492010-06-07 15:23:17 -04003415 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3416 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3417
James Smart40426292010-12-15 17:58:10 -05003418 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3419 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003420 /* We rcvd a rscn after issuing this
3421 * mbox reg login, we may have cycled
3422 * back through the state and be
3423 * back at reg login state so this
3424 * mbox needs to be ignored becase
3425 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003426 * process.
James Smartffc95492010-06-07 15:23:17 -04003427 */
3428 spin_lock_irq(shost->host_lock);
3429 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3430 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003431 } else
3432 /* Good status, call state machine */
3433 lpfc_disc_state_machine(vport, ndlp, pmb,
3434 NLP_EVT_CMPL_REG_LOGIN);
3435
dea31012005-04-17 16:05:31 -05003436 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3437 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003438 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003439 /* decrement the node reference count held for this callback
3440 * function.
3441 */
James Smart329f9bc2007-04-25 09:53:01 -04003442 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003443
3444 return;
3445}
3446
James Smart92d7f7b2007-06-17 19:56:38 -05003447static void
3448lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3449{
James Smart04c68492009-05-22 14:52:52 -04003450 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003451 struct lpfc_vport *vport = pmb->vport;
3452 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3453
3454 switch (mb->mbxStatus) {
3455 case 0x0011:
3456 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003457 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3458 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3459 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003460 break;
James Smart78730cf2010-04-06 15:06:30 -04003461 /* If VPI is busy, reset the HBA */
3462 case 0x9700:
3463 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3464 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3465 vport->vpi, mb->mbxStatus);
3466 if (!(phba->pport->load_flag & FC_UNLOADING))
3467 lpfc_workq_post_event(phba, NULL, NULL,
3468 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003469 }
James Smart72100cc2010-02-12 14:43:01 -05003470 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003471 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003472 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003473 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003474 vport->unreg_vpi_cmpl = VPORT_OK;
3475 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003476 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003477 /*
3478 * This shost reference might have been taken at the beginning of
3479 * lpfc_vport_delete()
3480 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003481 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003482 scsi_host_put(shost);
3483}
3484
James Smartd7c255b2008-08-24 21:50:00 -04003485int
James Smart92d7f7b2007-06-17 19:56:38 -05003486lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3487{
3488 struct lpfc_hba *phba = vport->phba;
3489 LPFC_MBOXQ_t *mbox;
3490 int rc;
3491
3492 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3493 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003494 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003495
3496 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3497 mbox->vport = vport;
3498 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003499 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003500 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003501 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3502 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003503 mempool_free(mbox, phba->mbox_mem_pool);
3504 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003505 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003506 }
James Smartd7c255b2008-08-24 21:50:00 -04003507 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003508}
3509
3510static void
3511lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3512{
3513 struct lpfc_vport *vport = pmb->vport;
3514 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003515 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003516
3517 switch (mb->mbxStatus) {
3518 case 0x0011:
3519 case 0x9601:
3520 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003521 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3522 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3523 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003524 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3525 spin_lock_irq(shost->host_lock);
3526 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3527 spin_unlock_irq(shost->host_lock);
3528 vport->fc_myDID = 0;
3529 goto out;
3530 }
James Smart92d7f7b2007-06-17 19:56:38 -05003531
James Smart72100cc2010-02-12 14:43:01 -05003532 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003533 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003534 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003535 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003536 vport->num_disc_nodes = 0;
3537 /* go thru NPR list and issue ELS PLOGIs */
3538 if (vport->fc_npr_cnt)
3539 lpfc_els_disc_plogi(vport);
3540
3541 if (!vport->num_disc_nodes) {
3542 spin_lock_irq(shost->host_lock);
3543 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3544 spin_unlock_irq(shost->host_lock);
3545 lpfc_can_disctmo(vport);
3546 }
3547 vport->port_state = LPFC_VPORT_READY;
3548
3549out:
3550 mempool_free(pmb, phba->mbox_mem_pool);
3551 return;
3552}
3553
James Smart21e9a0a2009-05-22 14:53:21 -04003554/**
3555 * lpfc_create_static_vport - Read HBA config region to create static vports.
3556 * @phba: pointer to lpfc hba data structure.
3557 *
3558 * This routine issue a DUMP mailbox command for config region 22 to get
3559 * the list of static vports to be created. The function create vports
3560 * based on the information returned from the HBA.
3561 **/
3562void
3563lpfc_create_static_vport(struct lpfc_hba *phba)
3564{
3565 LPFC_MBOXQ_t *pmb = NULL;
3566 MAILBOX_t *mb;
3567 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003568 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003569 struct fc_vport_identifiers vport_id;
3570 struct fc_vport *new_fc_vport;
3571 struct Scsi_Host *shost;
3572 struct lpfc_vport *vport;
3573 uint16_t offset = 0;
3574 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003575 struct lpfc_dmabuf *mp;
3576 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003577
3578 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3579 if (!pmb) {
3580 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3581 "0542 lpfc_create_static_vport failed to"
3582 " allocate mailbox memory\n");
3583 return;
3584 }
James Smartcdcc2342012-08-14 14:26:35 -04003585 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003586 mb = &pmb->u.mb;
3587
3588 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3589 if (!vport_info) {
3590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3591 "0543 lpfc_create_static_vport failed to"
3592 " allocate vport_info\n");
3593 mempool_free(pmb, phba->mbox_mem_pool);
3594 return;
3595 }
3596
3597 vport_buff = (uint8_t *) vport_info;
3598 do {
James Smartcdcc2342012-08-14 14:26:35 -04003599 /* free dma buffer from previous round */
3600 if (pmb->context1) {
3601 mp = (struct lpfc_dmabuf *)pmb->context1;
3602 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3603 kfree(mp);
3604 }
James Smart1c6834a2009-07-19 10:01:26 -04003605 if (lpfc_dump_static_vport(phba, pmb, offset))
3606 goto out;
3607
James Smart21e9a0a2009-05-22 14:53:21 -04003608 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003609 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3610 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003611
James Smartcdcc2342012-08-14 14:26:35 -04003612 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003613 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3614 "0544 lpfc_create_static_vport failed to"
3615 " issue dump mailbox command ret 0x%x "
3616 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003617 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003618 goto out;
3619 }
3620
James Smart1c6834a2009-07-19 10:01:26 -04003621 if (phba->sli_rev == LPFC_SLI_REV4) {
3622 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003623 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003624 if (byte_count > sizeof(struct static_vport_info) -
3625 offset)
3626 byte_count = sizeof(struct static_vport_info)
3627 - offset;
3628 memcpy(vport_buff + offset, mp->virt, byte_count);
3629 offset += byte_count;
3630 } else {
3631 if (mb->un.varDmp.word_cnt >
3632 sizeof(struct static_vport_info) - offset)
3633 mb->un.varDmp.word_cnt =
3634 sizeof(struct static_vport_info)
3635 - offset;
3636 byte_count = mb->un.varDmp.word_cnt;
3637 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3638 vport_buff + offset,
3639 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003640
James Smart1c6834a2009-07-19 10:01:26 -04003641 offset += byte_count;
3642 }
James Smart21e9a0a2009-05-22 14:53:21 -04003643
James Smart1c6834a2009-07-19 10:01:26 -04003644 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003645 offset < sizeof(struct static_vport_info));
3646
3647
3648 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3649 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3650 != VPORT_INFO_REV)) {
3651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3652 "0545 lpfc_create_static_vport bad"
3653 " information header 0x%x 0x%x\n",
3654 le32_to_cpu(vport_info->signature),
3655 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3656
3657 goto out;
3658 }
3659
3660 shost = lpfc_shost_from_vport(phba->pport);
3661
3662 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3663 memset(&vport_id, 0, sizeof(vport_id));
3664 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3665 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3666 if (!vport_id.port_name || !vport_id.node_name)
3667 continue;
3668
3669 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3670 vport_id.vport_type = FC_PORTTYPE_NPIV;
3671 vport_id.disable = false;
3672 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3673
3674 if (!new_fc_vport) {
3675 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3676 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003677 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003678 continue;
3679 }
3680
3681 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3682 vport->vport_flag |= STATIC_VPORT;
3683 }
3684
3685out:
James Smart21e9a0a2009-05-22 14:53:21 -04003686 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003687 if (mbx_wait_rc != MBX_TIMEOUT) {
3688 if (pmb->context1) {
3689 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003690 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3691 kfree(mp);
3692 }
James Smart21e9a0a2009-05-22 14:53:21 -04003693 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003694 }
James Smart21e9a0a2009-05-22 14:53:21 -04003695
3696 return;
3697}
3698
dea31012005-04-17 16:05:31 -05003699/*
3700 * This routine handles processing a Fabric REG_LOGIN mailbox
3701 * command upon completion. It is setup in the LPFC_MBOXQ
3702 * as the completion routine when the command is
3703 * handed off to the SLI layer.
3704 */
3705void
James Smart2e0fef82007-06-17 19:56:36 -05003706lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003707{
James Smart92d7f7b2007-06-17 19:56:38 -05003708 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003709 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003710 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003711 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003712 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003713
James Smart549e55c2007-08-02 11:09:51 -04003714 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003715 pmb->context1 = NULL;
3716 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003717
dea31012005-04-17 16:05:31 -05003718 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003719 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3720 "0258 Register Fabric login error: 0x%x\n",
3721 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003722 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3723 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003724 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003725
James Smart76a95d72010-11-20 23:11:48 -05003726 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003727 /* FLOGI failed, use loop map to make discovery list */
3728 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003729
James Smart92d7f7b2007-06-17 19:56:38 -05003730 /* Start discovery */
3731 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003732 /* Decrement the reference count to ndlp after the
3733 * reference to the ndlp are done.
3734 */
3735 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003736 return;
3737 }
3738
3739 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003740 /* Decrement the reference count to ndlp after the reference
3741 * to the ndlp are done.
3742 */
3743 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003744 return;
3745 }
3746
James Smart6d368e52011-05-24 11:44:12 -04003747 if (phba->sli_rev < LPFC_SLI_REV4)
3748 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003749 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003750 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003751 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003752
James Smart2e0fef82007-06-17 19:56:36 -05003753 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003754 /* when physical port receive logo donot start
3755 * vport discovery */
3756 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3757 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003758 else {
3759 shost = lpfc_shost_from_vport(vport);
3760 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003761 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003762 spin_unlock_irq(shost->host_lock);
3763 }
James Smart92d7f7b2007-06-17 19:56:38 -05003764 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003765 }
3766
3767 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3768 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003769 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003770
3771 /* Drop the reference count from the mbox at the end after
3772 * all the current reference to the ndlp have been done.
3773 */
3774 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003775 return;
3776}
3777
3778/*
3779 * This routine handles processing a NameServer REG_LOGIN mailbox
3780 * command upon completion. It is setup in the LPFC_MBOXQ
3781 * as the completion routine when the command is
3782 * handed off to the SLI layer.
3783 */
3784void
James Smart2e0fef82007-06-17 19:56:36 -05003785lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003786{
James Smart21e9a0a2009-05-22 14:53:21 -04003787 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003788 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3789 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3790 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003791
James Smartd439d282010-09-29 11:18:45 -04003792 pmb->context1 = NULL;
3793 pmb->context2 = NULL;
3794
dea31012005-04-17 16:05:31 -05003795 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003796out:
James Smart21e9a0a2009-05-22 14:53:21 -04003797 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3798 "0260 Register NameServer error: 0x%x\n",
3799 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003800 /* decrement the node reference count held for this
3801 * callback function.
3802 */
James Smart329f9bc2007-04-25 09:53:01 -04003803 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003804 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3805 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003806 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003807
3808 /* If no other thread is using the ndlp, free it */
3809 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003810
James Smart76a95d72010-11-20 23:11:48 -05003811 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003812 /*
3813 * RegLogin failed, use loop map to make discovery
3814 * list
3815 */
3816 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003817
James Smart92d7f7b2007-06-17 19:56:38 -05003818 /* Start discovery */
3819 lpfc_disc_start(vport);
3820 return;
3821 }
3822 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003823 return;
3824 }
3825
James Smart6d368e52011-05-24 11:44:12 -04003826 if (phba->sli_rev < LPFC_SLI_REV4)
3827 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003828 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003829 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003830 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003831
James Smart2e0fef82007-06-17 19:56:36 -05003832 if (vport->port_state < LPFC_VPORT_READY) {
3833 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003834 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3835 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3836 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3837 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3838 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3839
3840 /* Issue SCR just before NameServer GID_FT Query */
3841 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003842 }
3843
James Smart2e0fef82007-06-17 19:56:36 -05003844 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003845 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003846 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003847 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003848 goto out;
dea31012005-04-17 16:05:31 -05003849 }
3850
James Smartfa4066b2008-01-11 01:53:27 -05003851 /* decrement the node reference count held for this
3852 * callback function.
3853 */
James Smart329f9bc2007-04-25 09:53:01 -04003854 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3856 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003857 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003858
3859 return;
3860}
3861
3862static void
James Smart2e0fef82007-06-17 19:56:36 -05003863lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003864{
James Smart2e0fef82007-06-17 19:56:36 -05003865 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3866 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003867 struct lpfc_rport_data *rdata;
3868 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003869 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003870
3871 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003872 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3873 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003874 rport_ids.port_id = ndlp->nlp_DID;
3875 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003876
James Smart329f9bc2007-04-25 09:53:01 -04003877 /*
3878 * We leave our node pointer in rport->dd_data when we unregister a
3879 * FCP target port. But fc_remote_port_add zeros the space to which
3880 * rport->dd_data points. So, if we're reusing a previously
3881 * registered port, drop the reference that we took the last time we
3882 * registered the port.
3883 */
3884 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003885 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003886 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003887
3888 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3889 "rport add: did:x%x flg:x%x type x%x",
3890 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3891
James Smart9589b0622011-04-16 11:03:17 -04003892 /* Don't add the remote port if unloading. */
3893 if (vport->load_flag & FC_UNLOADING)
3894 return;
3895
James Smart2e0fef82007-06-17 19:56:36 -05003896 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003897 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003898 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3899 "Warning: fc_remote_port_add failed\n");
3900 return;
3901 }
3902
3903 /* initialize static port data */
3904 rport->maxframe_size = ndlp->nlp_maxframe;
3905 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003906 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003907 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003908
3909 if (ndlp->nlp_type & NLP_FCP_TARGET)
3910 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3911 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3912 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3913
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003914 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3915 fc_remote_port_rolechg(rport, rport_ids.roles);
3916
James Smart34f5ad82012-08-03 12:35:03 -04003917 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3918 "3183 rport register x%06x, rport %p role x%x\n",
3919 ndlp->nlp_DID, rport, rport_ids.roles);
3920
James Smart071fbd3d2006-04-15 11:53:20 -04003921 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003922 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003923 ndlp->nlp_sid = rport->scsi_target_id;
3924 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003925 return;
3926}
3927
3928static void
James Smart2e0fef82007-06-17 19:56:36 -05003929lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003930{
3931 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003932
James Smart858c9f62007-06-17 19:56:39 -05003933 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3934 "rport delete: did:x%x flg:x%x type x%x",
3935 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3936
James Smart34f5ad82012-08-03 12:35:03 -04003937 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3938 "3184 rport unregister x%06x, rport %p\n",
3939 ndlp->nlp_DID, rport);
3940
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003941 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003942
3943 return;
3944}
3945
James Smartde0c5b32007-04-25 09:52:27 -04003946static void
James Smart2e0fef82007-06-17 19:56:36 -05003947lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003948{
James Smart2e0fef82007-06-17 19:56:36 -05003949 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3950
3951 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003952 switch (state) {
3953 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003954 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003955 break;
3956 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003957 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003958 break;
3959 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003960 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003961 break;
3962 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003963 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003964 break;
3965 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003966 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003967 break;
3968 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003969 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003970 break;
3971 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003972 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003973 break;
3974 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003975 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003976 break;
3977 }
James Smart2e0fef82007-06-17 19:56:36 -05003978 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003979}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003980
James Smartde0c5b32007-04-25 09:52:27 -04003981static void
James Smart2e0fef82007-06-17 19:56:36 -05003982lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003983 int old_state, int new_state)
3984{
James Smart2e0fef82007-06-17 19:56:36 -05003985 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3986
James Smartde0c5b32007-04-25 09:52:27 -04003987 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003988 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3989 ndlp->nlp_type |= NLP_FC_NODE;
3990 }
3991 if (new_state == NLP_STE_MAPPED_NODE)
3992 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3993 if (new_state == NLP_STE_NPR_NODE)
3994 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003995
James Smartde0c5b32007-04-25 09:52:27 -04003996 /* Transport interface */
3997 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3998 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003999 vport->phba->nport_event_cnt++;
4000 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004001 }
dea31012005-04-17 16:05:31 -05004002
James Smartde0c5b32007-04-25 09:52:27 -04004003 if (new_state == NLP_STE_MAPPED_NODE ||
4004 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05004005 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05004006 /*
4007 * Tell the fc transport about the port, if we haven't
4008 * already. If we have, and it's a scsi entity, be
4009 * sure to unblock any attached scsi devices
4010 */
4011 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004012 }
James Smartea2151b2008-09-07 11:52:10 -04004013 if ((new_state == NLP_STE_MAPPED_NODE) &&
4014 (vport->stat_data_enabled)) {
4015 /*
4016 * A new target is discovered, if there is no buffer for
4017 * statistical data collection allocate buffer.
4018 */
4019 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4020 sizeof(struct lpfc_scsicmd_bkt),
4021 GFP_KERNEL);
4022
4023 if (!ndlp->lat_data)
4024 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4025 "0286 lpfc_nlp_state_cleanup failed to "
4026 "allocate statistical data buffer DID "
4027 "0x%x\n", ndlp->nlp_DID);
4028 }
James Smart858c9f62007-06-17 19:56:39 -05004029 /*
4030 * if we added to Mapped list, but the remote port
4031 * registration failed or assigned a target id outside
4032 * our presentable range - move the node to the
4033 * Unmapped List
4034 */
James Smartde0c5b32007-04-25 09:52:27 -04004035 if (new_state == NLP_STE_MAPPED_NODE &&
4036 (!ndlp->rport ||
4037 ndlp->rport->scsi_target_id == -1 ||
4038 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004039 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004040 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004041 spin_unlock_irq(shost->host_lock);
4042 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004043 }
James Smartde0c5b32007-04-25 09:52:27 -04004044}
4045
James Smart685f0bf2007-04-25 09:53:08 -04004046static char *
4047lpfc_nlp_state_name(char *buffer, size_t size, int state)
4048{
4049 static char *states[] = {
4050 [NLP_STE_UNUSED_NODE] = "UNUSED",
4051 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4052 [NLP_STE_ADISC_ISSUE] = "ADISC",
4053 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4054 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004055 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004056 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4057 [NLP_STE_MAPPED_NODE] = "MAPPED",
4058 [NLP_STE_NPR_NODE] = "NPR",
4059 };
4060
James Smart311464e2007-08-02 11:10:37 -04004061 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004062 strlcpy(buffer, states[state], size);
4063 else
4064 snprintf(buffer, size, "unknown (%d)", state);
4065 return buffer;
4066}
4067
James Smartde0c5b32007-04-25 09:52:27 -04004068void
James Smart2e0fef82007-06-17 19:56:36 -05004069lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4070 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004071{
James Smart2e0fef82007-06-17 19:56:36 -05004072 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004073 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004074 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004075
James Smarte8b62012007-08-02 11:10:09 -04004076 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4077 "0904 NPort state transition x%06x, %s -> %s\n",
4078 ndlp->nlp_DID,
4079 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4080 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004081
4082 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4083 "node statechg did:x%x old:%d ste:%d",
4084 ndlp->nlp_DID, old_state, state);
4085
James Smartde0c5b32007-04-25 09:52:27 -04004086 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004087 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004088 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004089 if (old_state == NLP_STE_UNMAPPED_NODE) {
4090 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4091 ndlp->nlp_type &= ~NLP_FC_NODE;
4092 }
4093
James Smart685f0bf2007-04-25 09:53:08 -04004094 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004095 spin_lock_irq(shost->host_lock);
4096 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4097 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004098 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004099 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004100
4101 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004102 lpfc_nlp_counters(vport, state, 1);
4103 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004104}
4105
4106void
James Smarte47c9092008-02-08 18:49:26 -05004107lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4108{
4109 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4110
4111 if (list_empty(&ndlp->nlp_listp)) {
4112 spin_lock_irq(shost->host_lock);
4113 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4114 spin_unlock_irq(shost->host_lock);
4115 }
4116}
4117
4118void
James Smart2e0fef82007-06-17 19:56:36 -05004119lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004120{
James Smart2e0fef82007-06-17 19:56:36 -05004121 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4122
James Smart0d2b6b82008-06-14 22:52:47 -04004123 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004124 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004125 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4126 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004127 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004128 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004129 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004130 NLP_STE_UNUSED_NODE);
4131}
4132
Adrian Bunk4d9db012008-02-14 23:24:02 +02004133static void
James Smarte47c9092008-02-08 18:49:26 -05004134lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4135{
James Smart0d2b6b82008-06-14 22:52:47 -04004136 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004137 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4138 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4139 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4140 NLP_STE_UNUSED_NODE);
4141}
James Smart109f6ed2008-12-04 22:39:08 -05004142/**
James Smart3621a712009-04-06 18:47:14 -04004143 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004144 * @vport: Pointer to Virtual Port object.
4145 * @ndlp: Pointer to FC node object.
4146 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004147 *
4148 * This function is always called when node object need to be initialized.
4149 * It initializes all the fields of the node object. Although the reference
4150 * to phba from @ndlp can be obtained indirectly through it's reference to
4151 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4152 * to the life-span of the @ndlp might go beyond the existence of @vport as
4153 * the final release of ndlp is determined by its reference count. And, the
4154 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004155 **/
4156static inline void
4157lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4158 uint32_t did)
4159{
4160 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4161 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4162 init_timer(&ndlp->nlp_delayfunc);
4163 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4164 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4165 ndlp->nlp_DID = did;
4166 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004167 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004168 ndlp->nlp_sid = NLP_NO_SID;
4169 kref_init(&ndlp->kref);
4170 NLP_INT_NODE_ACT(ndlp);
4171 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004172 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart40426292010-12-15 17:58:10 -05004173 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4174 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smart109f6ed2008-12-04 22:39:08 -05004175}
James Smarte47c9092008-02-08 18:49:26 -05004176
4177struct lpfc_nodelist *
4178lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4179 int state)
4180{
4181 struct lpfc_hba *phba = vport->phba;
4182 uint32_t did;
4183 unsigned long flags;
4184
4185 if (!ndlp)
4186 return NULL;
4187
4188 spin_lock_irqsave(&phba->ndlp_lock, flags);
4189 /* The ndlp should not be in memory free mode */
4190 if (NLP_CHK_FREE_REQ(ndlp)) {
4191 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4192 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4193 "0277 lpfc_enable_node: ndlp:x%p "
4194 "usgmap:x%x refcnt:%d\n",
4195 (void *)ndlp, ndlp->nlp_usg_map,
4196 atomic_read(&ndlp->kref.refcount));
4197 return NULL;
4198 }
4199 /* The ndlp should not already be in active mode */
4200 if (NLP_CHK_NODE_ACT(ndlp)) {
4201 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4202 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4203 "0278 lpfc_enable_node: ndlp:x%p "
4204 "usgmap:x%x refcnt:%d\n",
4205 (void *)ndlp, ndlp->nlp_usg_map,
4206 atomic_read(&ndlp->kref.refcount));
4207 return NULL;
4208 }
4209
4210 /* Keep the original DID */
4211 did = ndlp->nlp_DID;
4212
4213 /* re-initialize ndlp except of ndlp linked list pointer */
4214 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4215 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004216 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004217
4218 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4219
4220 if (state != NLP_STE_UNUSED_NODE)
4221 lpfc_nlp_set_state(vport, ndlp, state);
4222
4223 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4224 "node enable: did:x%x",
4225 ndlp->nlp_DID, 0, 0);
4226 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004227}
4228
4229void
James Smart2e0fef82007-06-17 19:56:36 -05004230lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004231{
James Smart87af33f2007-10-27 13:37:43 -04004232 /*
James Smartfa4066b2008-01-11 01:53:27 -05004233 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004234 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004235 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4236 * until ALL other outstanding threads have completed. We check
4237 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004238 */
James Smartfa4066b2008-01-11 01:53:27 -05004239 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4240 return;
James Smart51ef4c22007-08-02 11:10:31 -04004241 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart1151e3e2011-02-16 12:39:35 -05004242 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4243 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004244 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004245 return;
dea31012005-04-17 16:05:31 -05004246}
4247
4248/*
4249 * Start / ReStart rescue timer for Discovery / RSCN handling
4250 */
4251void
James Smart2e0fef82007-06-17 19:56:36 -05004252lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004253{
James Smart2e0fef82007-06-17 19:56:36 -05004254 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4255 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004256 uint32_t tmo;
4257
James Smart2e0fef82007-06-17 19:56:36 -05004258 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004259 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004260 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4261 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004262 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004263 * FC spec states we need 3 * ratov for CT requests
4264 */
4265 tmo = ((phba->fc_ratov * 3) + 3);
4266 }
dea31012005-04-17 16:05:31 -05004267
James Smart858c9f62007-06-17 19:56:39 -05004268
4269 if (!timer_pending(&vport->fc_disctmo)) {
4270 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4271 "set disc timer: tmo:x%x state:x%x flg:x%x",
4272 tmo, vport->port_state, vport->fc_flag);
4273 }
4274
James Smart256ec0d2013-04-17 20:14:58 -04004275 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004276 spin_lock_irq(shost->host_lock);
4277 vport->fc_flag |= FC_DISC_TMO;
4278 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004279
4280 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004281 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4282 "0247 Start Discovery Timer state x%x "
4283 "Data: x%x x%lx x%x x%x\n",
4284 vport->port_state, tmo,
4285 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4286 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004287
4288 return;
4289}
4290
4291/*
4292 * Cancel rescue timer for Discovery / RSCN handling
4293 */
4294int
James Smart2e0fef82007-06-17 19:56:36 -05004295lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004296{
James Smart2e0fef82007-06-17 19:56:36 -05004297 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004298 unsigned long iflags;
4299
James Smart858c9f62007-06-17 19:56:39 -05004300 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4301 "can disc timer: state:x%x rtry:x%x flg:x%x",
4302 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4303
dea31012005-04-17 16:05:31 -05004304 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004305 if (vport->fc_flag & FC_DISC_TMO) {
4306 spin_lock_irqsave(shost->host_lock, iflags);
4307 vport->fc_flag &= ~FC_DISC_TMO;
4308 spin_unlock_irqrestore(shost->host_lock, iflags);
4309 del_timer_sync(&vport->fc_disctmo);
4310 spin_lock_irqsave(&vport->work_port_lock, iflags);
4311 vport->work_port_events &= ~WORKER_DISC_TMO;
4312 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004313 }
4314
4315 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004316 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4317 "0248 Cancel Discovery Timer state x%x "
4318 "Data: x%x x%x x%x\n",
4319 vport->port_state, vport->fc_flag,
4320 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004321 return 0;
dea31012005-04-17 16:05:31 -05004322}
4323
4324/*
4325 * Check specified ring for outstanding IOCB on the SLI queue
4326 * Return true if iocb matches the specified nport
4327 */
4328int
James Smart2e0fef82007-06-17 19:56:36 -05004329lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4330 struct lpfc_sli_ring *pring,
4331 struct lpfc_iocbq *iocb,
4332 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004333{
James Smart2e0fef82007-06-17 19:56:36 -05004334 struct lpfc_sli *psli = &phba->sli;
4335 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004336 struct lpfc_vport *vport = ndlp->vport;
4337
4338 if (iocb->vport != vport)
4339 return 0;
4340
dea31012005-04-17 16:05:31 -05004341 if (pring->ringno == LPFC_ELS_RING) {
4342 switch (icmd->ulpCommand) {
4343 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004344 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004345 return 1;
dea31012005-04-17 16:05:31 -05004346 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004347 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4348 return 1;
dea31012005-04-17 16:05:31 -05004349 case CMD_XMIT_ELS_RSP64_CX:
4350 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004351 return 1;
dea31012005-04-17 16:05:31 -05004352 }
James Smarta4bc3372006-12-02 13:34:16 -05004353 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004354
4355 } else if (pring->ringno == psli->fcp_ring) {
4356 /* Skip match check if waiting to relogin to FCP target */
4357 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004358 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004359 return 0;
dea31012005-04-17 16:05:31 -05004360 }
4361 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004362 return 1;
dea31012005-04-17 16:05:31 -05004363 }
4364 } else if (pring->ringno == psli->next_ring) {
4365
4366 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004367 return 0;
dea31012005-04-17 16:05:31 -05004368}
4369
4370/*
4371 * Free resources / clean up outstanding I/Os
4372 * associated with nlp_rpi in the LPFC_NODELIST entry.
4373 */
4374static int
James Smart2e0fef82007-06-17 19:56:36 -05004375lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004376{
James Smart2534ba72007-04-25 09:52:20 -04004377 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004378 struct lpfc_sli *psli;
4379 struct lpfc_sli_ring *pring;
4380 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004381 uint32_t i;
dea31012005-04-17 16:05:31 -05004382
James Smart92d7f7b2007-06-17 19:56:38 -05004383 lpfc_fabric_abort_nport(ndlp);
4384
dea31012005-04-17 16:05:31 -05004385 /*
4386 * Everything that matches on txcmplq will be returned
4387 * by firmware with a no rpi error.
4388 */
4389 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004390 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004391 /* Now process each ring */
4392 for (i = 0; i < psli->num_rings; i++) {
4393 pring = &psli->ring[i];
4394
James Smart2e0fef82007-06-17 19:56:36 -05004395 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004396 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004397 list) {
dea31012005-04-17 16:05:31 -05004398 /*
4399 * Check to see if iocb matches the nport we are
4400 * looking for
4401 */
James Smart92d7f7b2007-06-17 19:56:38 -05004402 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4403 ndlp))) {
dea31012005-04-17 16:05:31 -05004404 /* It matches, so deque and call compl
4405 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004406 list_move_tail(&iocb->list,
4407 &completions);
dea31012005-04-17 16:05:31 -05004408 }
4409 }
James Smart2e0fef82007-06-17 19:56:36 -05004410 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004411 }
4412 }
James Smart2534ba72007-04-25 09:52:20 -04004413
James Smarta257bf92009-04-06 18:48:10 -04004414 /* Cancel all the IOCBs from the completions list */
4415 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4416 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004417
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004418 return 0;
dea31012005-04-17 16:05:31 -05004419}
4420
James Smart086a3452012-08-14 14:25:21 -04004421/**
4422 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4423 * @phba: Pointer to HBA context object.
4424 * @pmb: Pointer to mailbox object.
4425 *
4426 * This function will issue an ELS LOGO command after completing
4427 * the UNREG_RPI.
4428 **/
4429void
4430lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4431{
4432 struct lpfc_vport *vport = pmb->vport;
4433 struct lpfc_nodelist *ndlp;
4434
4435 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4436 if (!ndlp)
4437 return;
4438 lpfc_issue_els_logo(vport, ndlp, 0);
4439}
4440
dea31012005-04-17 16:05:31 -05004441/*
4442 * Free rpi associated with LPFC_NODELIST entry.
4443 * This routine is called from lpfc_freenode(), when we are removing
4444 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4445 * LOGO that completes successfully, and we are waiting to PLOGI back
4446 * to the remote NPort. In addition, it is called after we receive
4447 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4448 * we are waiting to PLOGI back to the remote NPort.
4449 */
4450int
James Smart2e0fef82007-06-17 19:56:36 -05004451lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004452{
James Smart2e0fef82007-06-17 19:56:36 -05004453 struct lpfc_hba *phba = vport->phba;
4454 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004455 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004456 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004457
James Smart40426292010-12-15 17:58:10 -05004458 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart2e0fef82007-06-17 19:56:36 -05004459 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4460 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004461 /* SLI4 ports require the physical rpi value. */
4462 rpi = ndlp->nlp_rpi;
4463 if (phba->sli_rev == LPFC_SLI_REV4)
4464 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004465
James Smart6d368e52011-05-24 11:44:12 -04004466 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004467 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004468 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4469 mbox->context1 = ndlp;
4470 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4471 } else {
4472 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4473 }
4474
James Smart0b727fe2007-10-27 13:37:25 -04004475 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004476 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004477 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004478 }
dea31012005-04-17 16:05:31 -05004479 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004480
James Smart40426292010-12-15 17:58:10 -05004481 if (phba->sli_rev != LPFC_SLI_REV4)
4482 ndlp->nlp_rpi = 0;
4483 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004484 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004485 return 1;
4486 }
4487 return 0;
4488}
4489
James Smartecfd03c2010-02-12 14:41:27 -05004490/**
4491 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4492 * @phba: pointer to lpfc hba data structure.
4493 *
4494 * This routine is invoked to unregister all the currently registered RPIs
4495 * to the HBA.
4496 **/
4497void
4498lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4499{
4500 struct lpfc_vport **vports;
4501 struct lpfc_nodelist *ndlp;
4502 struct Scsi_Host *shost;
4503 int i;
4504
4505 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004506 if (!vports) {
4507 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4508 "2884 Vport array allocation failed \n");
4509 return;
4510 }
James Smartecfd03c2010-02-12 14:41:27 -05004511 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4512 shost = lpfc_shost_from_vport(vports[i]);
4513 spin_lock_irq(shost->host_lock);
4514 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004515 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004516 /* The mempool_alloc might sleep */
4517 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004518 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004519 spin_lock_irq(shost->host_lock);
4520 }
James Smartecfd03c2010-02-12 14:41:27 -05004521 }
4522 spin_unlock_irq(shost->host_lock);
4523 }
4524 lpfc_destroy_vport_work_array(phba, vports);
4525}
4526
James Smart92d7f7b2007-06-17 19:56:38 -05004527void
4528lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4529{
4530 struct lpfc_hba *phba = vport->phba;
4531 LPFC_MBOXQ_t *mbox;
4532 int rc;
4533
James Smart5af5eee2010-10-22 11:06:38 -04004534 if (phba->sli_rev == LPFC_SLI_REV4) {
4535 lpfc_sli4_unreg_all_rpis(vport);
4536 return;
4537 }
4538
James Smart92d7f7b2007-06-17 19:56:38 -05004539 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4540 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004541 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4542 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004543 mbox->vport = vport;
4544 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004545 mbox->context1 = NULL;
4546 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004547 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004548 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004549
4550 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4551 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4552 "1836 Could not issue "
4553 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004554 }
4555}
4556
4557void
4558lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4559{
4560 struct lpfc_hba *phba = vport->phba;
4561 LPFC_MBOXQ_t *mbox;
4562 int rc;
4563
4564 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4565 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004566 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4567 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004568 mbox->vport = vport;
4569 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004570 mbox->context1 = NULL;
4571 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004572 if (rc != MBX_TIMEOUT)
4573 mempool_free(mbox, phba->mbox_mem_pool);
4574
4575 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004576 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4577 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004578 "unreg_did (default rpis) status %d\n",
4579 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004580 }
4581}
4582
dea31012005-04-17 16:05:31 -05004583/*
4584 * Free resources associated with LPFC_NODELIST entry
4585 * so it can be freed.
4586 */
4587static int
James Smart2e0fef82007-06-17 19:56:36 -05004588lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004589{
James Smart2e0fef82007-06-17 19:56:36 -05004590 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4591 struct lpfc_hba *phba = vport->phba;
4592 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004593 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004594
4595 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004596 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4597 "0900 Cleanup node for NPort x%x "
4598 "Data: x%x x%x x%x\n",
4599 ndlp->nlp_DID, ndlp->nlp_flag,
4600 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004601 if (NLP_CHK_FREE_REQ(ndlp)) {
4602 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4603 "0280 lpfc_cleanup_node: ndlp:x%p "
4604 "usgmap:x%x refcnt:%d\n",
4605 (void *)ndlp, ndlp->nlp_usg_map,
4606 atomic_read(&ndlp->kref.refcount));
4607 lpfc_dequeue_node(vport, ndlp);
4608 } else {
4609 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4610 "0281 lpfc_cleanup_node: ndlp:x%p "
4611 "usgmap:x%x refcnt:%d\n",
4612 (void *)ndlp, ndlp->nlp_usg_map,
4613 atomic_read(&ndlp->kref.refcount));
4614 lpfc_disable_node(vport, ndlp);
4615 }
dea31012005-04-17 16:05:31 -05004616
James Smart086a3452012-08-14 14:25:21 -04004617
4618 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4619
dea31012005-04-17 16:05:31 -05004620 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4621 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004622 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004623 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004624 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4625 mb->context2 = NULL;
4626 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4627 }
4628 }
James Smart33ccf8d2006-08-17 11:57:58 -04004629
James Smart2e0fef82007-06-17 19:56:36 -05004630 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004631 /* Cleanup REG_LOGIN completions which are not yet processed */
4632 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4633 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004634 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004635 (ndlp != (struct lpfc_nodelist *) mb->context2))
4636 continue;
4637
4638 mb->context2 = NULL;
4639 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4640 }
4641
dea31012005-04-17 16:05:31 -05004642 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004643 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004644 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004645 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004646 mp = (struct lpfc_dmabuf *) (mb->context1);
4647 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004648 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004649 kfree(mp);
4650 }
4651 list_del(&mb->list);
4652 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004653 /* We shall not invoke the lpfc_nlp_put to decrement
4654 * the ndlp reference count as we are in the process
4655 * of lpfc_nlp_release.
4656 */
dea31012005-04-17 16:05:31 -05004657 }
4658 }
James Smart2e0fef82007-06-17 19:56:36 -05004659 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004660
James Smarte47c9092008-02-08 18:49:26 -05004661 lpfc_els_abort(phba, ndlp);
4662
James Smart2e0fef82007-06-17 19:56:36 -05004663 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004664 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004665 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004666
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004667 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004668 del_timer_sync(&ndlp->nlp_delayfunc);
4669
James Smart0d2b6b82008-06-14 22:52:47 -04004670 list_del_init(&ndlp->els_retry_evt.evt_listp);
4671 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004672 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004673 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004674
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004675 return 0;
dea31012005-04-17 16:05:31 -05004676}
4677
4678/*
4679 * Check to see if we can free the nlp back to the freelist.
4680 * If we are in the middle of using the nlp in the discovery state
4681 * machine, defer the free till we reach the end of the state machine.
4682 */
James Smart329f9bc2007-04-25 09:53:01 -04004683static void
James Smart2e0fef82007-06-17 19:56:36 -05004684lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004685{
James Smarta8adb832007-10-27 13:37:53 -04004686 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004687 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004688 LPFC_MBOXQ_t *mbox;
4689 int rc;
dea31012005-04-17 16:05:31 -05004690
James Smart0d2b6b82008-06-14 22:52:47 -04004691 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004692 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004693 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4694 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004695 /* For this case we need to cleanup the default rpi
4696 * allocated by the firmware.
4697 */
4698 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4699 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004700 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004701 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004702 if (rc) {
4703 mempool_free(mbox, phba->mbox_mem_pool);
4704 }
4705 else {
4706 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4707 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4708 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004709 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004710 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4711 if (rc == MBX_NOT_FINISHED) {
4712 mempool_free(mbox, phba->mbox_mem_pool);
4713 }
4714 }
4715 }
4716 }
James Smart2e0fef82007-06-17 19:56:36 -05004717 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004718
James Smart2e0fef82007-06-17 19:56:36 -05004719 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004720 * We can get here with a non-NULL ndlp->rport because when we
4721 * unregister a rport we don't break the rport/node linkage. So if we
4722 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004723 */
James Smart92d7f7b2007-06-17 19:56:38 -05004724 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004725 rdata = ndlp->rport->dd_data;
4726 rdata->pnode = NULL;
4727 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004728 }
dea31012005-04-17 16:05:31 -05004729}
4730
4731static int
James Smart2e0fef82007-06-17 19:56:36 -05004732lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4733 uint32_t did)
dea31012005-04-17 16:05:31 -05004734{
James Smart2e0fef82007-06-17 19:56:36 -05004735 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004736
4737 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004738 return 0;
dea31012005-04-17 16:05:31 -05004739
dea31012005-04-17 16:05:31 -05004740 /* First check for Direct match */
4741 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004742 return 1;
dea31012005-04-17 16:05:31 -05004743
4744 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004745 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004746 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004747 return 0;
dea31012005-04-17 16:05:31 -05004748 }
4749
4750 matchdid.un.word = did;
4751 ndlpdid.un.word = ndlp->nlp_DID;
4752 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4753 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4754 (mydid.un.b.area == matchdid.un.b.area)) {
4755 if ((ndlpdid.un.b.domain == 0) &&
4756 (ndlpdid.un.b.area == 0)) {
4757 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004758 return 1;
dea31012005-04-17 16:05:31 -05004759 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004760 return 0;
dea31012005-04-17 16:05:31 -05004761 }
4762
4763 matchdid.un.word = ndlp->nlp_DID;
4764 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4765 (mydid.un.b.area == ndlpdid.un.b.area)) {
4766 if ((matchdid.un.b.domain == 0) &&
4767 (matchdid.un.b.area == 0)) {
4768 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004769 return 1;
dea31012005-04-17 16:05:31 -05004770 }
4771 }
4772 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004773 return 0;
dea31012005-04-17 16:05:31 -05004774}
4775
James Smart685f0bf2007-04-25 09:53:08 -04004776/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004777static struct lpfc_nodelist *
4778__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004779{
James Smart2fb9bd82006-12-02 13:33:57 -05004780 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004781 uint32_t data1;
4782
James Smart2e0fef82007-06-17 19:56:36 -05004783 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4784 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004785 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4786 ((uint32_t) ndlp->nlp_xri << 16) |
4787 ((uint32_t) ndlp->nlp_type << 8) |
4788 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004789 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4790 "0929 FIND node DID "
4791 "Data: x%p x%x x%x x%x\n",
4792 ndlp, ndlp->nlp_DID,
4793 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004794 return ndlp;
dea31012005-04-17 16:05:31 -05004795 }
4796 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004797
dea31012005-04-17 16:05:31 -05004798 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004799 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4800 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004801 return NULL;
4802}
4803
4804struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004805lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004806{
James Smart2e0fef82007-06-17 19:56:36 -05004807 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004808 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004809 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004810
James Smartfedd3b72011-02-16 12:39:24 -05004811 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004812 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004813 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004814 return ndlp;
4815}
4816
4817struct lpfc_nodelist *
4818lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4819{
4820 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4821 struct lpfc_nodelist *ndlp;
4822
4823 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004824 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004825 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4826 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004827 return NULL;
4828 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004829 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004830 if (!ndlp)
4831 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004832 lpfc_nlp_init(vport, ndlp, did);
4833 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4834 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004835 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004836 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004837 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004838 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4839 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4840 if (!ndlp)
4841 return NULL;
4842 spin_lock_irq(shost->host_lock);
4843 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4844 spin_unlock_irq(shost->host_lock);
4845 return ndlp;
dea31012005-04-17 16:05:31 -05004846 }
James Smarte47c9092008-02-08 18:49:26 -05004847
James Smart58da1ff2008-04-07 10:15:56 -04004848 if ((vport->fc_flag & FC_RSCN_MODE) &&
4849 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004850 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004851 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004852 * we don't need to try to discover it again.
4853 */
4854 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4855 return NULL;
4856
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004857 /* Since this node is marked for discovery,
4858 * delay timeout is not needed.
4859 */
James Smart0d2b6b82008-06-14 22:52:47 -04004860 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004861 spin_lock_irq(shost->host_lock);
4862 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4863 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004864 } else
dea31012005-04-17 16:05:31 -05004865 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004866 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004867 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004868 * or we are already in the process of discovery on it,
4869 * we don't need to try to discover it again.
4870 */
James Smart685f0bf2007-04-25 09:53:08 -04004871 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004872 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4873 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004874 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004875 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4876 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004877 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004878 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004879 }
4880 return ndlp;
4881}
4882
4883/* Build a list of nodes to discover based on the loopmap */
4884void
James Smart2e0fef82007-06-17 19:56:36 -05004885lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004886{
James Smart2e0fef82007-06-17 19:56:36 -05004887 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004888 int j;
4889 uint32_t alpa, index;
4890
James Smart2e0fef82007-06-17 19:56:36 -05004891 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004892 return;
James Smart2e0fef82007-06-17 19:56:36 -05004893
James Smart76a95d72010-11-20 23:11:48 -05004894 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004895 return;
dea31012005-04-17 16:05:31 -05004896
4897 /* Check for loop map present or not */
4898 if (phba->alpa_map[0]) {
4899 for (j = 1; j <= phba->alpa_map[0]; j++) {
4900 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004901 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004902 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004903 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004904 }
4905 } else {
4906 /* No alpamap, so try all alpa's */
4907 for (j = 0; j < FC_MAXLOOP; j++) {
4908 /* If cfg_scan_down is set, start from highest
4909 * ALPA (0xef) to lowest (0x1).
4910 */
James Smart3de2a652007-08-02 11:09:59 -04004911 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004912 index = j;
4913 else
4914 index = FC_MAXLOOP - j - 1;
4915 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004916 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004917 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004918 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004919 }
4920 }
4921 return;
4922}
4923
dea31012005-04-17 16:05:31 -05004924void
James Smart2e0fef82007-06-17 19:56:36 -05004925lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004926{
dea31012005-04-17 16:05:31 -05004927 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004928 struct lpfc_sli *psli = &phba->sli;
4929 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4930 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4931 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4932 int rc;
4933
James Smart92d7f7b2007-06-17 19:56:38 -05004934 /*
4935 * if it's not a physical port or if we already send
4936 * clear_la then don't send it.
4937 */
4938 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004939 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4940 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004941 return;
4942
James Smart2e0fef82007-06-17 19:56:36 -05004943 /* Link up discovery */
4944 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4945 phba->link_state = LPFC_CLEAR_LA;
4946 lpfc_clear_la(phba, mbox);
4947 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4948 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004949 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004950 if (rc == MBX_NOT_FINISHED) {
4951 mempool_free(mbox, phba->mbox_mem_pool);
4952 lpfc_disc_flush_list(vport);
4953 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4954 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4955 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004956 phba->link_state = LPFC_HBA_ERROR;
4957 }
4958 }
4959}
4960
4961/* Reg_vpi to tell firmware to resume normal operations */
4962void
4963lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4964{
4965 LPFC_MBOXQ_t *regvpimbox;
4966
4967 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4968 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004969 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004970 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4971 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004972 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004973 == MBX_NOT_FINISHED) {
4974 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004975 }
4976 }
4977}
4978
4979/* Start Link up / RSCN discovery on NPR nodes */
4980void
4981lpfc_disc_start(struct lpfc_vport *vport)
4982{
4983 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4984 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004985 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004986 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004987 int did_changed;
dea31012005-04-17 16:05:31 -05004988
James Smarte74c03c2013-04-17 20:15:19 -04004989 if (!lpfc_is_link_up(phba)) {
4990 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4991 "3315 Link is not up %x\n",
4992 phba->link_state);
dea31012005-04-17 16:05:31 -05004993 return;
James Smarte74c03c2013-04-17 20:15:19 -04004994 }
James Smart2e0fef82007-06-17 19:56:36 -05004995
4996 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004997 clear_la_pending = 1;
4998 else
4999 clear_la_pending = 0;
5000
James Smart2e0fef82007-06-17 19:56:36 -05005001 if (vport->port_state < LPFC_VPORT_READY)
5002 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005003
James Smart2e0fef82007-06-17 19:56:36 -05005004 lpfc_set_disctmo(vport);
5005
5006 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05005007 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005008 else
dea31012005-04-17 16:05:31 -05005009 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05005010
5011 vport->fc_prevDID = vport->fc_myDID;
5012 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005013
5014 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005015 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5016 "0202 Start Discovery hba state x%x "
5017 "Data: x%x x%x x%x\n",
5018 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5019 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005020
5021 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005022 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005023
5024 if (num_sent)
5025 return;
5026
James Smart6d368e52011-05-24 11:44:12 -04005027 /* Register the VPI for SLI3, NON-NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005028 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005029 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005030 !(vport->fc_flag & FC_RSCN_MODE) &&
5031 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005032 lpfc_issue_reg_vpi(phba, vport);
5033 return;
5034 }
James Smart2e0fef82007-06-17 19:56:36 -05005035
James Smart92d7f7b2007-06-17 19:56:38 -05005036 /*
5037 * For SLI2, we need to set port_state to READY and continue
5038 * discovery.
5039 */
5040 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5041 /* If we get here, there is nothing to ADISC */
5042 if (vport->port_type == LPFC_PHYSICAL_PORT)
5043 lpfc_issue_clear_la(phba, vport);
5044
5045 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005046 vport->num_disc_nodes = 0;
5047 /* go thru NPR nodes and issue ELS PLOGIs */
5048 if (vport->fc_npr_cnt)
5049 lpfc_els_disc_plogi(vport);
5050
5051 if (!vport->num_disc_nodes) {
5052 spin_lock_irq(shost->host_lock);
5053 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5054 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005055 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005056 }
5057 }
James Smart92d7f7b2007-06-17 19:56:38 -05005058 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005059 } else {
5060 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005061 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005062
5063 if (num_sent)
5064 return;
5065
James Smart2e0fef82007-06-17 19:56:36 -05005066 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005067 /* Check to see if more RSCNs came in while we
5068 * were processing this one.
5069 */
James Smart2e0fef82007-06-17 19:56:36 -05005070 if ((vport->fc_rscn_id_cnt == 0) &&
5071 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5072 spin_lock_irq(shost->host_lock);
5073 vport->fc_flag &= ~FC_RSCN_MODE;
5074 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005075 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005076 } else
James Smart2e0fef82007-06-17 19:56:36 -05005077 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005078 }
5079 }
5080 return;
5081}
5082
5083/*
5084 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5085 * ring the match the sppecified nodelist.
5086 */
5087static void
James Smart2e0fef82007-06-17 19:56:36 -05005088lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005089{
James Smart2534ba72007-04-25 09:52:20 -04005090 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005091 struct lpfc_sli *psli;
5092 IOCB_t *icmd;
5093 struct lpfc_iocbq *iocb, *next_iocb;
5094 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005095
5096 psli = &phba->sli;
5097 pring = &psli->ring[LPFC_ELS_RING];
5098
5099 /* Error matching iocb on txq or txcmplq
5100 * First check the txq.
5101 */
James Smart2e0fef82007-06-17 19:56:36 -05005102 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005103 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5104 if (iocb->context1 != ndlp) {
5105 continue;
5106 }
5107 icmd = &iocb->iocb;
5108 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5109 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5110
James Smart2534ba72007-04-25 09:52:20 -04005111 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005112 }
5113 }
5114
5115 /* Next check the txcmplq */
5116 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5117 if (iocb->context1 != ndlp) {
5118 continue;
5119 }
5120 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005121 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5122 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005123 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005124 }
5125 }
James Smart2e0fef82007-06-17 19:56:36 -05005126 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005127
James Smarta257bf92009-04-06 18:48:10 -04005128 /* Cancel all the IOCBs from the completions list */
5129 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5130 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005131}
5132
Adrian Bunka6ababd2007-11-05 18:07:33 +01005133static void
James Smart2e0fef82007-06-17 19:56:36 -05005134lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005135{
5136 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005137 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005138
James Smart2e0fef82007-06-17 19:56:36 -05005139 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5140 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005141 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005142 if (!NLP_CHK_NODE_ACT(ndlp))
5143 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005144 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5145 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5146 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005147 }
dea31012005-04-17 16:05:31 -05005148 }
5149 }
dea31012005-04-17 16:05:31 -05005150}
5151
James Smart92d7f7b2007-06-17 19:56:38 -05005152void
5153lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5154{
5155 lpfc_els_flush_rscn(vport);
5156 lpfc_els_flush_cmd(vport);
5157 lpfc_disc_flush_list(vport);
5158}
5159
dea31012005-04-17 16:05:31 -05005160/*****************************************************************************/
5161/*
5162 * NAME: lpfc_disc_timeout
5163 *
5164 * FUNCTION: Fibre Channel driver discovery timeout routine.
5165 *
5166 * EXECUTION ENVIRONMENT: interrupt only
5167 *
5168 * CALLED FROM:
5169 * Timer function
5170 *
5171 * RETURNS:
5172 * none
5173 */
5174/*****************************************************************************/
5175void
5176lpfc_disc_timeout(unsigned long ptr)
5177{
James Smart2e0fef82007-06-17 19:56:36 -05005178 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5179 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005180 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005181 unsigned long flags = 0;
5182
5183 if (unlikely(!phba))
5184 return;
5185
James Smart5e9d9b82008-06-14 22:52:53 -04005186 spin_lock_irqsave(&vport->work_port_lock, flags);
5187 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5188 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005189 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005190 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005191
James Smart5e9d9b82008-06-14 22:52:53 -04005192 if (!tmo_posted)
5193 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005194 return;
5195}
5196
5197static void
James Smart2e0fef82007-06-17 19:56:36 -05005198lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005199{
James Smart2e0fef82007-06-17 19:56:36 -05005200 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5201 struct lpfc_hba *phba = vport->phba;
5202 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005203 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005204 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005205 int rc, clrlaerr = 0;
5206
James Smart2e0fef82007-06-17 19:56:36 -05005207 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005208 return;
5209
James Smart2e0fef82007-06-17 19:56:36 -05005210 spin_lock_irq(shost->host_lock);
5211 vport->fc_flag &= ~FC_DISC_TMO;
5212 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005213
James Smart858c9f62007-06-17 19:56:39 -05005214 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5215 "disc timeout: state:x%x rtry:x%x flg:x%x",
5216 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5217
James Smart2e0fef82007-06-17 19:56:36 -05005218 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005219
5220 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005221 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5222 * FAN
5223 */
5224 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005225 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5226 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005227 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005228 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005229 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005230 if (!NLP_CHK_NODE_ACT(ndlp))
5231 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005232 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5233 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005234 if (ndlp->nlp_type & NLP_FABRIC) {
5235 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005236 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005237
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005238 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005239 /* Fail outstanding IO now since device
5240 * is marked for PLOGI.
5241 */
James Smart2e0fef82007-06-17 19:56:36 -05005242 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005243 }
5244 }
James Smart92d7f7b2007-06-17 19:56:38 -05005245 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005246 if (phba->sli_rev <= LPFC_SLI_REV3)
5247 lpfc_initial_flogi(vport);
5248 else
5249 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005250 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005251 }
dea31012005-04-17 16:05:31 -05005252 break;
5253
James Smart92d7f7b2007-06-17 19:56:38 -05005254 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005255 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005256 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005257 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005258 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5259 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005260 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005261
5262 /* Assume no Fabric and go on with discovery.
5263 * Check for outstanding ELS FLOGI to abort.
5264 */
5265
5266 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005267 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005268
5269 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005270 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005271 break;
5272
5273 case LPFC_FABRIC_CFG_LINK:
5274 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5275 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005276 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5277 "0223 Timeout while waiting for "
5278 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005279 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005280 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005281 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005282 lpfc_els_abort(phba, ndlp);
5283
5284 /* ReStart discovery */
5285 goto restart_disc;
dea31012005-04-17 16:05:31 -05005286
5287 case LPFC_NS_QRY:
5288 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005289 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5290 "0224 NameServer Query timeout "
5291 "Data: x%x x%x\n",
5292 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005293
James Smart92d7f7b2007-06-17 19:56:38 -05005294 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5295 /* Try it one more time */
5296 vport->fc_ns_retry++;
5297 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5298 vport->fc_ns_retry, 0);
5299 if (rc == 0)
5300 break;
dea31012005-04-17 16:05:31 -05005301 }
James Smart92d7f7b2007-06-17 19:56:38 -05005302 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005303
James Smart87af33f2007-10-27 13:37:43 -04005304restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005305 /*
5306 * Discovery is over.
5307 * set port_state to PORT_READY if SLI2.
5308 * cmpl_reg_vpi will set port_state to READY for SLI3.
5309 */
James Smart3772a992009-05-22 14:50:54 -04005310 if (phba->sli_rev < LPFC_SLI_REV4) {
5311 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5312 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005313 else {
James Smart3772a992009-05-22 14:50:54 -04005314 lpfc_issue_clear_la(phba, vport);
5315 vport->port_state = LPFC_VPORT_READY;
5316 }
dea31012005-04-17 16:05:31 -05005317 }
5318
5319 /* Setup and issue mailbox INITIALIZE LINK command */
5320 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5321 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005322 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5323 "0206 Device Discovery "
5324 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005325 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005326 break;
5327 }
5328
5329 lpfc_linkdown(phba);
5330 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5331 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005332 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005333 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005334 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005335 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005336 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005337 if (rc == MBX_NOT_FINISHED)
5338 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5339
5340 break;
5341
5342 case LPFC_DISC_AUTH:
5343 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005344 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5345 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005346 lpfc_disc_flush_list(vport);
5347
James Smart92d7f7b2007-06-17 19:56:38 -05005348 /*
5349 * set port_state to PORT_READY if SLI2.
5350 * cmpl_reg_vpi will set port_state to READY for SLI3.
5351 */
James Smart3772a992009-05-22 14:50:54 -04005352 if (phba->sli_rev < LPFC_SLI_REV4) {
5353 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5354 lpfc_issue_reg_vpi(phba, vport);
5355 else { /* NPIV Not enabled */
5356 lpfc_issue_clear_la(phba, vport);
5357 vport->port_state = LPFC_VPORT_READY;
5358 }
dea31012005-04-17 16:05:31 -05005359 }
5360 break;
5361
James Smart2e0fef82007-06-17 19:56:36 -05005362 case LPFC_VPORT_READY:
5363 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005364 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5365 "0231 RSCN timeout Data: x%x "
5366 "x%x\n",
5367 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005368
5369 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005370 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005371
James Smart2e0fef82007-06-17 19:56:36 -05005372 lpfc_els_flush_rscn(vport);
5373 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005374 }
5375 break;
James Smart2e0fef82007-06-17 19:56:36 -05005376
James Smart92d7f7b2007-06-17 19:56:38 -05005377 default:
James Smarte8b62012007-08-02 11:10:09 -04005378 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005379 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005380 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005381 break;
5382 }
5383
5384 switch (phba->link_state) {
5385 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005386 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005387 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5388 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005389 clrlaerr = 1;
5390 break;
5391
James Smart09372822008-01-11 01:52:54 -05005392 case LPFC_LINK_UP:
5393 lpfc_issue_clear_la(phba, vport);
5394 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005395 case LPFC_LINK_UNKNOWN:
5396 case LPFC_WARM_START:
5397 case LPFC_INIT_START:
5398 case LPFC_INIT_MBX_CMDS:
5399 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005400 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005401 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5402 "0230 Unexpected timeout, hba link "
5403 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005404 clrlaerr = 1;
5405 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005406
5407 case LPFC_HBA_READY:
5408 break;
dea31012005-04-17 16:05:31 -05005409 }
5410
5411 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005412 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005413 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005414 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5415 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005416 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005417 }
5418
5419 return;
5420}
5421
dea31012005-04-17 16:05:31 -05005422/*
5423 * This routine handles processing a NameServer REG_LOGIN mailbox
5424 * command upon completion. It is setup in the LPFC_MBOXQ
5425 * as the completion routine when the command is
5426 * handed off to the SLI layer.
5427 */
5428void
James Smart2e0fef82007-06-17 19:56:36 -05005429lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005430{
James Smart04c68492009-05-22 14:52:52 -04005431 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005432 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5433 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5434 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005435
5436 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005437 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005438
James Smart6d368e52011-05-24 11:44:12 -04005439 if (phba->sli_rev < LPFC_SLI_REV4)
5440 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005441 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005442 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005443 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05005444
James Smart2e0fef82007-06-17 19:56:36 -05005445 /*
5446 * Start issuing Fabric-Device Management Interface (FDMI) command to
5447 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5448 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005449 */
James Smart2e0fef82007-06-17 19:56:36 -05005450
James Smart3de2a652007-08-02 11:09:59 -04005451 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05005452 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
5453 else
James Smart256ec0d2013-04-17 20:14:58 -04005454 mod_timer(&vport->fc_fdmitmo,
5455 jiffies + msecs_to_jiffies(1000 * 60));
dea31012005-04-17 16:05:31 -05005456
James Smartfa4066b2008-01-11 01:53:27 -05005457 /* decrement the node reference count held for this callback
5458 * function.
5459 */
James Smart329f9bc2007-04-25 09:53:01 -04005460 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005461 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5462 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005463 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005464
5465 return;
5466}
5467
James Smart685f0bf2007-04-25 09:53:08 -04005468static int
5469lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5470{
5471 uint16_t *rpi = param;
5472
James Smarteff4a012012-01-18 16:25:25 -05005473 /* check for active node */
5474 if (!NLP_CHK_NODE_ACT(ndlp))
5475 return 0;
5476
James Smart685f0bf2007-04-25 09:53:08 -04005477 return ndlp->nlp_rpi == *rpi;
5478}
5479
5480static int
5481lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5482{
5483 return memcmp(&ndlp->nlp_portname, param,
5484 sizeof(ndlp->nlp_portname)) == 0;
5485}
5486
Adrian Bunka6ababd2007-11-05 18:07:33 +01005487static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005488__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005489{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005490 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005491
James Smart2e0fef82007-06-17 19:56:36 -05005492 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005493 if (filter(ndlp, param)) {
5494 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5495 "3185 FIND node filter %p DID "
5496 "Data: x%p x%x x%x\n",
5497 filter, ndlp, ndlp->nlp_DID,
5498 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005499 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005500 }
James Smart685f0bf2007-04-25 09:53:08 -04005501 }
James Smart34f5ad82012-08-03 12:35:03 -04005502 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5503 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005504 return NULL;
dea31012005-04-17 16:05:31 -05005505}
5506
James Smart685f0bf2007-04-25 09:53:08 -04005507/*
5508 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005509 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005510 */
5511struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005512__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005513{
James Smart2e0fef82007-06-17 19:56:36 -05005514 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005515}
5516
James Smart488d1462006-03-07 15:02:37 -05005517/*
James Smart685f0bf2007-04-25 09:53:08 -04005518 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005519 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005520 */
5521struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005522lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005523{
James Smart2e0fef82007-06-17 19:56:36 -05005524 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005525 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005526
James Smart2e0fef82007-06-17 19:56:36 -05005527 spin_lock_irq(shost->host_lock);
5528 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5529 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005530 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005531}
5532
James Smartcb69f7d2011-12-13 13:21:57 -05005533/*
5534 * This routine looks up the ndlp lists for the given RPI. If the rpi
5535 * is found, the routine returns the node element list pointer else
5536 * return NULL.
5537 */
5538struct lpfc_nodelist *
5539lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5540{
5541 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5542 struct lpfc_nodelist *ndlp;
5543
5544 spin_lock_irq(shost->host_lock);
5545 ndlp = __lpfc_findnode_rpi(vport, rpi);
5546 spin_unlock_irq(shost->host_lock);
5547 return ndlp;
5548}
5549
5550/**
5551 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5552 * @phba: pointer to lpfc hba data structure.
5553 * @vpi: the physical host virtual N_Port identifier.
5554 *
5555 * This routine finds a vport on a HBA (referred by @phba) through a
5556 * @vpi. The function walks the HBA's vport list and returns the address
5557 * of the vport with the matching @vpi.
5558 *
5559 * Return code
5560 * NULL - No vport with the matching @vpi found
5561 * Otherwise - Address to the vport with the matching @vpi.
5562 **/
5563struct lpfc_vport *
5564lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5565{
5566 struct lpfc_vport *vport;
5567 unsigned long flags;
5568 int i = 0;
5569
5570 /* The physical ports are always vpi 0 - translate is unnecessary. */
5571 if (vpi > 0) {
5572 /*
5573 * Translate the physical vpi to the logical vpi. The
5574 * vport stores the logical vpi.
5575 */
5576 for (i = 0; i < phba->max_vpi; i++) {
5577 if (vpi == phba->vpi_ids[i])
5578 break;
5579 }
5580
5581 if (i >= phba->max_vpi) {
5582 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5583 "2936 Could not find Vport mapped "
5584 "to vpi %d\n", vpi);
5585 return NULL;
5586 }
5587 }
5588
5589 spin_lock_irqsave(&phba->hbalock, flags);
5590 list_for_each_entry(vport, &phba->port_list, listentry) {
5591 if (vport->vpi == i) {
5592 spin_unlock_irqrestore(&phba->hbalock, flags);
5593 return vport;
5594 }
5595 }
5596 spin_unlock_irqrestore(&phba->hbalock, flags);
5597 return NULL;
5598}
5599
dea31012005-04-17 16:05:31 -05005600void
James Smart2e0fef82007-06-17 19:56:36 -05005601lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5602 uint32_t did)
dea31012005-04-17 16:05:31 -05005603{
5604 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005605
5606 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005607 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05005608
5609 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5610 "node init: did:x%x",
5611 ndlp->nlp_DID, 0, 0);
5612
dea31012005-04-17 16:05:31 -05005613 return;
5614}
James Smart329f9bc2007-04-25 09:53:01 -04005615
James Smart98c9ea52007-10-27 13:37:33 -04005616/* This routine releases all resources associated with a specifc NPort's ndlp
5617 * and mempool_free's the nodelist.
5618 */
James Smart311464e2007-08-02 11:10:37 -04005619static void
James Smart329f9bc2007-04-25 09:53:01 -04005620lpfc_nlp_release(struct kref *kref)
5621{
James Smarte47c9092008-02-08 18:49:26 -05005622 struct lpfc_hba *phba;
5623 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005624 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5625 kref);
James Smart858c9f62007-06-17 19:56:39 -05005626
5627 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5628 "node release: did:x%x flg:x%x type:x%x",
5629 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5630
James Smarte47c9092008-02-08 18:49:26 -05005631 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005632 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smarte47c9092008-02-08 18:49:26 -05005633 "usgmap:x%x refcnt:%d\n",
James Smart939723a2012-05-09 21:19:03 -04005634 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smarte47c9092008-02-08 18:49:26 -05005635 atomic_read(&ndlp->kref.refcount));
5636
5637 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005638 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005639
5640 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005641 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005642 spin_lock_irqsave(&phba->ndlp_lock, flags);
5643 NLP_CLR_NODE_ACT(ndlp);
5644 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005645 if (phba->sli_rev == LPFC_SLI_REV4)
5646 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005647
5648 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005649 if (NLP_CHK_FREE_REQ(ndlp)) {
5650 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04005651 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005652 }
James Smart329f9bc2007-04-25 09:53:01 -04005653}
5654
James Smart98c9ea52007-10-27 13:37:33 -04005655/* This routine bumps the reference count for a ndlp structure to ensure
5656 * that one discovery thread won't free a ndlp while another discovery thread
5657 * is using it.
5658 */
James Smart329f9bc2007-04-25 09:53:01 -04005659struct lpfc_nodelist *
5660lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5661{
James Smarte47c9092008-02-08 18:49:26 -05005662 struct lpfc_hba *phba;
5663 unsigned long flags;
5664
James Smart98c9ea52007-10-27 13:37:33 -04005665 if (ndlp) {
5666 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5667 "node get: did:x%x flg:x%x refcnt:x%x",
5668 ndlp->nlp_DID, ndlp->nlp_flag,
5669 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005670 /* The check of ndlp usage to prevent incrementing the
5671 * ndlp reference count that is in the process of being
5672 * released.
5673 */
James Smarta257bf92009-04-06 18:48:10 -04005674 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005675 spin_lock_irqsave(&phba->ndlp_lock, flags);
5676 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5677 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5678 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5679 "0276 lpfc_nlp_get: ndlp:x%p "
5680 "usgmap:x%x refcnt:%d\n",
5681 (void *)ndlp, ndlp->nlp_usg_map,
5682 atomic_read(&ndlp->kref.refcount));
5683 return NULL;
5684 } else
5685 kref_get(&ndlp->kref);
5686 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005687 }
James Smart329f9bc2007-04-25 09:53:01 -04005688 return ndlp;
5689}
5690
James Smart98c9ea52007-10-27 13:37:33 -04005691/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005692 * count goes to 0, this indicates the the associated nodelist should be
5693 * freed. Returning 1 indicates the ndlp resource has been released; on the
5694 * other hand, returning 0 indicates the ndlp resource has not been released
5695 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005696 */
James Smart329f9bc2007-04-25 09:53:01 -04005697int
5698lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5699{
James Smarte47c9092008-02-08 18:49:26 -05005700 struct lpfc_hba *phba;
5701 unsigned long flags;
5702
5703 if (!ndlp)
5704 return 1;
5705
5706 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5707 "node put: did:x%x flg:x%x refcnt:x%x",
5708 ndlp->nlp_DID, ndlp->nlp_flag,
5709 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005710 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005711 spin_lock_irqsave(&phba->ndlp_lock, flags);
5712 /* Check the ndlp memory free acknowledge flag to avoid the
5713 * possible race condition that kref_put got invoked again
5714 * after previous one has done ndlp memory free.
5715 */
5716 if (NLP_CHK_FREE_ACK(ndlp)) {
5717 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5718 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5719 "0274 lpfc_nlp_put: ndlp:x%p "
5720 "usgmap:x%x refcnt:%d\n",
5721 (void *)ndlp, ndlp->nlp_usg_map,
5722 atomic_read(&ndlp->kref.refcount));
5723 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005724 }
James Smarte47c9092008-02-08 18:49:26 -05005725 /* Check the ndlp inactivate log flag to avoid the possible
5726 * race condition that kref_put got invoked again after ndlp
5727 * is already in inactivating state.
5728 */
5729 if (NLP_CHK_IACT_REQ(ndlp)) {
5730 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5731 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5732 "0275 lpfc_nlp_put: ndlp:x%p "
5733 "usgmap:x%x refcnt:%d\n",
5734 (void *)ndlp, ndlp->nlp_usg_map,
5735 atomic_read(&ndlp->kref.refcount));
5736 return 1;
5737 }
5738 /* For last put, mark the ndlp usage flags to make sure no
5739 * other kref_get and kref_put on the same ndlp shall get
5740 * in between the process when the final kref_put has been
5741 * invoked on this ndlp.
5742 */
5743 if (atomic_read(&ndlp->kref.refcount) == 1) {
5744 /* Indicate ndlp is put to inactive state. */
5745 NLP_SET_IACT_REQ(ndlp);
5746 /* Acknowledge ndlp memory free has been seen. */
5747 if (NLP_CHK_FREE_REQ(ndlp))
5748 NLP_SET_FREE_ACK(ndlp);
5749 }
5750 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5751 /* Note, the kref_put returns 1 when decrementing a reference
5752 * count that was 1, it invokes the release callback function,
5753 * but it still left the reference count as 1 (not actually
5754 * performs the last decrementation). Otherwise, it actually
5755 * decrements the reference count and returns 0.
5756 */
5757 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005758}
James Smart98c9ea52007-10-27 13:37:33 -04005759
5760/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005761 * by any other discovery thread. This routine returns 1 if the
5762 * ndlp has been freed. A return value of 0 indicates the ndlp is
5763 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005764 */
5765int
5766lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5767{
5768 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5769 "node not used: did:x%x flg:x%x refcnt:x%x",
5770 ndlp->nlp_DID, ndlp->nlp_flag,
5771 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005772 if (atomic_read(&ndlp->kref.refcount) == 1)
5773 if (lpfc_nlp_put(ndlp))
5774 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005775 return 0;
5776}
James Smart6fb120a2009-05-22 14:52:59 -04005777
5778/**
5779 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5780 * @phba: Pointer to hba context object.
5781 *
5782 * This function iterate through all FC nodes associated
5783 * will all vports to check if there is any node with
5784 * fc_rports associated with it. If there is an fc_rport
5785 * associated with the node, then the node is either in
5786 * discovered state or its devloss_timer is pending.
5787 */
5788static int
5789lpfc_fcf_inuse(struct lpfc_hba *phba)
5790{
5791 struct lpfc_vport **vports;
5792 int i, ret = 0;
5793 struct lpfc_nodelist *ndlp;
5794 struct Scsi_Host *shost;
5795
5796 vports = lpfc_create_vport_work_array(phba);
5797
James Smart63e801c2010-11-20 23:14:19 -05005798 /* If driver cannot allocate memory, indicate fcf is in use */
5799 if (!vports)
5800 return 1;
5801
James Smart6fb120a2009-05-22 14:52:59 -04005802 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5803 shost = lpfc_shost_from_vport(vports[i]);
5804 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005805 /*
5806 * IF the CVL_RCVD bit is not set then we have sent the
5807 * flogi.
5808 * If dev_loss fires while we are waiting we do not want to
5809 * unreg the fcf.
5810 */
5811 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5812 spin_unlock_irq(shost->host_lock);
5813 ret = 1;
5814 goto out;
5815 }
James Smart6fb120a2009-05-22 14:52:59 -04005816 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5817 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5818 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5819 ret = 1;
5820 spin_unlock_irq(shost->host_lock);
5821 goto out;
James Smart80c17842012-03-01 22:35:45 -05005822 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5823 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005824 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005825 "2624 RPI %x DID %x flag %x "
5826 "still logged in\n",
5827 ndlp->nlp_rpi, ndlp->nlp_DID,
5828 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005829 }
5830 }
5831 spin_unlock_irq(shost->host_lock);
5832 }
5833out:
5834 lpfc_destroy_vport_work_array(phba, vports);
5835 return ret;
5836}
5837
5838/**
5839 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5840 * @phba: Pointer to hba context object.
5841 * @mboxq: Pointer to mailbox object.
5842 *
5843 * This function frees memory associated with the mailbox command.
5844 */
James Smart1b511972011-12-13 13:23:09 -05005845void
James Smart6fb120a2009-05-22 14:52:59 -04005846lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5847{
5848 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005849 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005850
5851 if (mboxq->u.mb.mbxStatus) {
5852 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5853 "2555 UNREG_VFI mbxStatus error x%x "
5854 "HBA state x%x\n",
5855 mboxq->u.mb.mbxStatus, vport->port_state);
5856 }
James Smart38b92ef2010-08-04 16:11:39 -04005857 spin_lock_irq(shost->host_lock);
5858 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5859 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005860 mempool_free(mboxq, phba->mbox_mem_pool);
5861 return;
5862}
5863
5864/**
5865 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5866 * @phba: Pointer to hba context object.
5867 * @mboxq: Pointer to mailbox object.
5868 *
5869 * This function frees memory associated with the mailbox command.
5870 */
5871static void
5872lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5873{
5874 struct lpfc_vport *vport = mboxq->vport;
5875
5876 if (mboxq->u.mb.mbxStatus) {
5877 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5878 "2550 UNREG_FCFI mbxStatus error x%x "
5879 "HBA state x%x\n",
5880 mboxq->u.mb.mbxStatus, vport->port_state);
5881 }
5882 mempool_free(mboxq, phba->mbox_mem_pool);
5883 return;
5884}
5885
5886/**
James Smartecfd03c2010-02-12 14:41:27 -05005887 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005888 * @phba: Pointer to hba context object.
5889 *
James Smartecfd03c2010-02-12 14:41:27 -05005890 * This function prepare the HBA for unregistering the currently registered
5891 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5892 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005893 */
James Smartecfd03c2010-02-12 14:41:27 -05005894int
5895lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005896{
James Smart6fb120a2009-05-22 14:52:59 -04005897 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005898 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005899 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04005900 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005901
James Smartecfd03c2010-02-12 14:41:27 -05005902 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005903 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005904 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005905
James Smart4d9ab992009-10-02 15:16:39 -04005906 /* At this point, all discovery is aborted */
5907 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005908
5909 /* Unregister VPIs */
5910 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005911 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005912 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005913 /* Stop FLOGI/FDISC retries */
5914 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5915 if (ndlp)
5916 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005917 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04005918 if (phba->sli_rev == LPFC_SLI_REV4)
5919 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005920 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005921 shost = lpfc_shost_from_vport(vports[i]);
5922 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005923 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005924 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005925 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005926 }
5927 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04005928 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
5929 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
5930 if (ndlp)
5931 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
5932 lpfc_cleanup_pending_mbox(phba->pport);
5933 if (phba->sli_rev == LPFC_SLI_REV4)
5934 lpfc_sli4_unreg_all_rpis(phba->pport);
5935 lpfc_mbx_unreg_vpi(phba->pport);
5936 shost = lpfc_shost_from_vport(phba->pport);
5937 spin_lock_irq(shost->host_lock);
5938 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
5939 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
5940 spin_unlock_irq(shost->host_lock);
5941 }
James Smart6fb120a2009-05-22 14:52:59 -04005942
James Smart695a8142010-01-26 23:08:03 -05005943 /* Cleanup any outstanding ELS commands */
5944 lpfc_els_flush_all_cmd(phba);
5945
James Smart1b511972011-12-13 13:23:09 -05005946 /* Unregister the physical port VFI */
5947 rc = lpfc_issue_unreg_vfi(phba->pport);
5948 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05005949}
5950
5951/**
5952 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5953 * @phba: Pointer to hba context object.
5954 *
5955 * This function issues synchronous unregister FCF mailbox command to HBA to
5956 * unregister the currently registered FCF record. The driver does not reset
5957 * the driver FCF usage state flags.
5958 *
5959 * Return 0 if successfully issued, none-zero otherwise.
5960 */
5961int
5962lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5963{
5964 LPFC_MBOXQ_t *mbox;
5965 int rc;
5966
James Smart6fb120a2009-05-22 14:52:59 -04005967 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5968 if (!mbox) {
5969 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005970 "2551 UNREG_FCFI mbox allocation failed"
5971 "HBA state x%x\n", phba->pport->port_state);
5972 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005973 }
James Smart6fb120a2009-05-22 14:52:59 -04005974 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5975 mbox->vport = phba->pport;
5976 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5977 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5978
5979 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005980 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5981 "2552 Unregister FCFI command failed rc x%x "
5982 "HBA state x%x\n",
5983 rc, phba->pport->port_state);
5984 return -EINVAL;
5985 }
5986 return 0;
5987}
5988
5989/**
5990 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5991 * @phba: Pointer to hba context object.
5992 *
5993 * This function unregisters the currently reigstered FCF. This function
5994 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5995 */
5996void
5997lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5998{
5999 int rc;
6000
6001 /* Preparation for unregistering fcf */
6002 rc = lpfc_unregister_fcf_prep(phba);
6003 if (rc) {
6004 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6005 "2748 Failed to prepare for unregistering "
6006 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006007 return;
6008 }
6009
James Smartecfd03c2010-02-12 14:41:27 -05006010 /* Now, unregister FCF record and reset HBA FCF state */
6011 rc = lpfc_sli4_unregister_fcf(phba);
6012 if (rc)
6013 return;
6014 /* Reset HBA FCF states after successful unregister FCF */
6015 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006016 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006017
6018 /*
6019 * If driver is not unloading, check if there is any other
6020 * FCF record that can be used for discovery.
6021 */
6022 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006023 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006024 return;
6025
James Smart0c9ab6f2010-02-26 14:15:57 -05006026 /* This is considered as the initial FCF discovery scan */
6027 spin_lock_irq(&phba->hbalock);
6028 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6029 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006030
6031 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006032 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006033
James Smart0c9ab6f2010-02-26 14:15:57 -05006034 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006035
James Smart0c9ab6f2010-02-26 14:15:57 -05006036 if (rc) {
6037 spin_lock_irq(&phba->hbalock);
6038 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6039 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006040 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006041 "2553 lpfc_unregister_unused_fcf failed "
6042 "to read FCF record HBA state x%x\n",
6043 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006044 }
James Smartecfd03c2010-02-12 14:41:27 -05006045}
6046
6047/**
6048 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6049 * @phba: Pointer to hba context object.
6050 *
6051 * This function just unregisters the currently reigstered FCF. It does not
6052 * try to find another FCF for discovery.
6053 */
6054void
6055lpfc_unregister_fcf(struct lpfc_hba *phba)
6056{
6057 int rc;
6058
6059 /* Preparation for unregistering fcf */
6060 rc = lpfc_unregister_fcf_prep(phba);
6061 if (rc) {
6062 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6063 "2749 Failed to prepare for unregistering "
6064 "HBA's FCF record: rc=%d\n", rc);
6065 return;
6066 }
6067
6068 /* Now, unregister FCF record and reset HBA FCF state */
6069 rc = lpfc_sli4_unregister_fcf(phba);
6070 if (rc)
6071 return;
6072 /* Set proper HBA FCF states after successful unregister FCF */
6073 spin_lock_irq(&phba->hbalock);
6074 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6075 spin_unlock_irq(&phba->hbalock);
6076}
6077
6078/**
6079 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6080 * @phba: Pointer to hba context object.
6081 *
6082 * This function check if there are any connected remote port for the FCF and
6083 * if all the devices are disconnected, this function unregister FCFI.
6084 * This function also tries to use another FCF for discovery.
6085 */
6086void
6087lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6088{
6089 /*
James Smart3804dc82010-07-14 15:31:37 -04006090 * If HBA is not running in FIP mode, if HBA does not support
6091 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6092 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006093 */
6094 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006095 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006096 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006097 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006098 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006099 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006100 spin_unlock_irq(&phba->hbalock);
6101 return;
6102 }
6103 spin_unlock_irq(&phba->hbalock);
6104
6105 if (lpfc_fcf_inuse(phba))
6106 return;
6107
6108 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006109}
6110
6111/**
6112 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6113 * @phba: Pointer to hba context object.
6114 * @buff: Buffer containing the FCF connection table as in the config
6115 * region.
6116 * This function create driver data structure for the FCF connection
6117 * record table read from config region 23.
6118 */
6119static void
6120lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6121 uint8_t *buff)
6122{
6123 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6124 struct lpfc_fcf_conn_hdr *conn_hdr;
6125 struct lpfc_fcf_conn_rec *conn_rec;
6126 uint32_t record_count;
6127 int i;
6128
6129 /* Free the current connect table */
6130 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006131 &phba->fcf_conn_rec_list, list) {
6132 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006133 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006134 }
James Smart6fb120a2009-05-22 14:52:59 -04006135
6136 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6137 record_count = conn_hdr->length * sizeof(uint32_t)/
6138 sizeof(struct lpfc_fcf_conn_rec);
6139
6140 conn_rec = (struct lpfc_fcf_conn_rec *)
6141 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6142
6143 for (i = 0; i < record_count; i++) {
6144 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6145 continue;
6146 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6147 GFP_KERNEL);
6148 if (!conn_entry) {
6149 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6150 "2566 Failed to allocate connection"
6151 " table entry\n");
6152 return;
6153 }
6154
6155 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6156 sizeof(struct lpfc_fcf_conn_rec));
6157 conn_entry->conn_rec.vlan_tag =
6158 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
6159 conn_entry->conn_rec.flags =
6160 le16_to_cpu(conn_entry->conn_rec.flags);
6161 list_add_tail(&conn_entry->list,
6162 &phba->fcf_conn_rec_list);
6163 }
6164}
6165
6166/**
6167 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6168 * @phba: Pointer to hba context object.
6169 * @buff: Buffer containing the FCoE parameter data structure.
6170 *
6171 * This function update driver data structure with config
6172 * parameters read from config region 23.
6173 */
6174static void
6175lpfc_read_fcoe_param(struct lpfc_hba *phba,
6176 uint8_t *buff)
6177{
6178 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6179 struct lpfc_fcoe_params *fcoe_param;
6180
6181 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6182 buff;
6183 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006184 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006185
6186 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6187 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6188 return;
6189
James Smart6fb120a2009-05-22 14:52:59 -04006190 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6191 phba->valid_vlan = 1;
6192 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6193 0xFFF;
6194 }
6195
6196 phba->fc_map[0] = fcoe_param->fc_map[0];
6197 phba->fc_map[1] = fcoe_param->fc_map[1];
6198 phba->fc_map[2] = fcoe_param->fc_map[2];
6199 return;
6200}
6201
6202/**
6203 * lpfc_get_rec_conf23 - Get a record type in config region data.
6204 * @buff: Buffer containing config region 23 data.
6205 * @size: Size of the data buffer.
6206 * @rec_type: Record type to be searched.
6207 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006208 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006209 * of the record specified by record_type. If record found, this
6210 * function return pointer to the record else return NULL.
6211 */
6212static uint8_t *
6213lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6214{
6215 uint32_t offset = 0, rec_length;
6216
6217 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6218 (size < sizeof(uint32_t)))
6219 return NULL;
6220
6221 rec_length = buff[offset + 1];
6222
6223 /*
6224 * One TLV record has one word header and number of data words
6225 * specified in the rec_length field of the record header.
6226 */
6227 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6228 <= size) {
6229 if (buff[offset] == rec_type)
6230 return &buff[offset];
6231
6232 if (buff[offset] == LPFC_REGION23_LAST_REC)
6233 return NULL;
6234
6235 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6236 rec_length = buff[offset + 1];
6237 }
6238 return NULL;
6239}
6240
6241/**
6242 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6243 * @phba: Pointer to lpfc_hba data structure.
6244 * @buff: Buffer containing config region 23 data.
6245 * @size: Size of the data buffer.
6246 *
Stefan Weileef35c22010-08-06 21:11:15 +02006247 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006248 * populate driver data structure with the parameters.
6249 */
6250void
6251lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6252 uint8_t *buff,
6253 uint32_t size)
6254{
6255 uint32_t offset = 0, rec_length;
6256 uint8_t *rec_ptr;
6257
6258 /*
6259 * If data size is less than 2 words signature and version cannot be
6260 * verified.
6261 */
6262 if (size < 2*sizeof(uint32_t))
6263 return;
6264
6265 /* Check the region signature first */
6266 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6268 "2567 Config region 23 has bad signature\n");
6269 return;
6270 }
6271
6272 offset += 4;
6273
6274 /* Check the data structure version */
6275 if (buff[offset] != LPFC_REGION23_VERSION) {
6276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6277 "2568 Config region 23 has bad version\n");
6278 return;
6279 }
6280 offset += 4;
6281
6282 rec_length = buff[offset + 1];
6283
6284 /* Read FCoE param record */
6285 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6286 size - offset, FCOE_PARAM_TYPE);
6287 if (rec_ptr)
6288 lpfc_read_fcoe_param(phba, rec_ptr);
6289
6290 /* Read FCF connection table */
6291 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6292 size - offset, FCOE_CONN_TBL_TYPE);
6293 if (rec_ptr)
6294 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6295
6296}