blob: d489a0da3c9d8afebec45824a386959fec6172aa [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 Smart58da1ff2008-04-07 10:15:56 -0400694 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500695 lpfc_sli_handle_slow_ring_event(phba, pring,
696 (status &
697 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500698 }
James Smart0e9bb8d2013-03-01 16:35:12 -0500699 if ((phba->sli_rev == LPFC_SLI_REV4) &
700 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400701 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500702 /*
703 * Turn on Ring interrupts
704 */
James Smart3772a992009-05-22 14:50:54 -0400705 if (phba->sli_rev <= LPFC_SLI_REV3) {
706 spin_lock_irq(&phba->hbalock);
707 control = readl(phba->HCregaddr);
708 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
709 lpfc_debugfs_slow_ring_trc(phba,
710 "WRK Enable ring: cntl:x%x hacopy:x%x",
711 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400712
James Smart3772a992009-05-22 14:50:54 -0400713 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
714 writel(control, phba->HCregaddr);
715 readl(phba->HCregaddr); /* flush */
716 } else {
717 lpfc_debugfs_slow_ring_trc(phba,
718 "WRK Ring ok: cntl:x%x hacopy:x%x",
719 control, ha_copy, 0);
720 }
721 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500722 }
723 }
James Smart2e0fef82007-06-17 19:56:36 -0500724 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500725}
726
dea31012005-04-17 16:05:31 -0500727int
728lpfc_do_work(void *p)
729{
730 struct lpfc_hba *phba = p;
731 int rc;
dea31012005-04-17 16:05:31 -0500732
733 set_user_nice(current, -20);
James Smart043c9562012-05-09 21:17:43 -0400734 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400735 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500736
James Smart3a55b532008-12-04 22:38:54 -0500737 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400738 /* wait and check worker queue activities */
739 rc = wait_event_interruptible(phba->work_waitq,
740 (test_and_clear_bit(LPFC_DATA_READY,
741 &phba->data_flags)
742 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500743 /* Signal wakeup shall terminate the worker thread */
744 if (rc) {
745 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
746 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500747 break;
James Smart3a55b532008-12-04 22:38:54 -0500748 }
dea31012005-04-17 16:05:31 -0500749
James Smart5e9d9b82008-06-14 22:52:53 -0400750 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500751 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500752 }
James Smart3a55b532008-12-04 22:38:54 -0500753 phba->worker_thread = NULL;
754 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
755 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500756 return 0;
757}
758
759/*
760 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300761 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500762 * embedding it in the IOCB.
763 */
764int
James Smart2e0fef82007-06-17 19:56:36 -0500765lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500766 uint32_t evt)
767{
768 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500769 unsigned long flags;
dea31012005-04-17 16:05:31 -0500770
771 /*
772 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
773 * be queued to worker thread for processing
774 */
James Smart92d7f7b2007-06-17 19:56:38 -0500775 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500776 if (!evtp)
777 return 0;
778
779 evtp->evt_arg1 = arg1;
780 evtp->evt_arg2 = arg2;
781 evtp->evt = evt;
782
James Smarted957682007-06-17 19:56:37 -0500783 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400784 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500785 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500786
James Smart5e9d9b82008-06-14 22:52:53 -0400787 lpfc_worker_wake_up(phba);
788
dea31012005-04-17 16:05:31 -0500789 return 1;
790}
791
James Smart92d7f7b2007-06-17 19:56:38 -0500792void
793lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
794{
James Smart09372822008-01-11 01:52:54 -0500795 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500796 struct lpfc_hba *phba = vport->phba;
797 struct lpfc_nodelist *ndlp, *next_ndlp;
798 int rc;
799
800 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500801 if (!NLP_CHK_NODE_ACT(ndlp))
802 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500803 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
804 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400805 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
806 ((vport->port_type == LPFC_NPIV_PORT) &&
807 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500808 lpfc_unreg_rpi(vport, ndlp);
809
810 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400811 if ((phba->sli_rev < LPFC_SLI_REV4) &&
812 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500813 continue;
814 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
815 remove
816 ? NLP_EVT_DEVICE_RM
817 : NLP_EVT_DEVICE_RECOVERY);
818 }
819 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400820 if (phba->sli_rev == LPFC_SLI_REV4)
821 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500822 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500823 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500824 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500825 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500826 }
827}
828
James Smart87af33f2007-10-27 13:37:43 -0400829void
James Smart98c9ea52007-10-27 13:37:33 -0400830lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500831{
James Smart695a8142010-01-26 23:08:03 -0500832 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
833
James Smart45ed1192009-10-02 15:17:02 -0400834 /* Cleanup any outstanding received buffers */
835 lpfc_cleanup_rcv_buffers(vport);
836
James Smart92d7f7b2007-06-17 19:56:38 -0500837 /* Cleanup any outstanding RSCN activity */
838 lpfc_els_flush_rscn(vport);
839
840 /* Cleanup any outstanding ELS commands */
841 lpfc_els_flush_cmd(vport);
842
843 lpfc_cleanup_rpis(vport, 0);
844
James Smart92d7f7b2007-06-17 19:56:38 -0500845 /* Turn off discovery timer if its running */
846 lpfc_can_disctmo(vport);
847}
848
James Smart3772a992009-05-22 14:50:54 -0400849void
James Smart98c9ea52007-10-27 13:37:33 -0400850lpfc_linkdown_port(struct lpfc_vport *vport)
851{
852 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
853
854 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
855
856 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
857 "Link Down: state:x%x rtry:x%x flg:x%x",
858 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
859
860 lpfc_port_link_failure(vport);
861
James Smart92494142011-02-16 12:39:44 -0500862 /* Stop delayed Nport discovery */
863 spin_lock_irq(shost->host_lock);
864 vport->fc_flag &= ~FC_DISC_DELAYED;
865 spin_unlock_irq(shost->host_lock);
866 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400867}
868
dea31012005-04-17 16:05:31 -0500869int
James Smart685f0bf2007-04-25 09:53:08 -0400870lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500871{
James Smart2e0fef82007-06-17 19:56:36 -0500872 struct lpfc_vport *vport = phba->pport;
873 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400874 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400875 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400876 int i;
dea31012005-04-17 16:05:31 -0500877
James Smart3163f722008-02-08 18:50:25 -0500878 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500879 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500880
881 /* Block all SCSI stack I/Os */
882 lpfc_scsi_dev_block(phba);
883
James Smart2e0fef82007-06-17 19:56:36 -0500884 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500885 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500886 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500887 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500888 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500889 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500890 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500891 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500892 }
James Smart549e55c2007-08-02 11:09:51 -0400893 vports = lpfc_create_vport_work_array(phba);
894 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400895 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400896 /* Issue a LINK DOWN event to all nodes */
897 lpfc_linkdown_port(vports[i]);
898 }
James Smart09372822008-01-11 01:52:54 -0500899 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500900 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500901 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
902 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400903 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500904 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500905 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400906 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500907 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500908 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500909 }
910 }
911
dea31012005-04-17 16:05:31 -0500912 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500913 if (phba->pport->fc_flag & FC_PT2PT) {
914 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500915 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
916 if (mb) {
dea31012005-04-17 16:05:31 -0500917 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500918 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500919 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400920 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500921 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500922 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500923 }
924 }
James Smart2e0fef82007-06-17 19:56:36 -0500925 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500926 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500927 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500928 }
James Smart2e0fef82007-06-17 19:56:36 -0500929
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500930 return 0;
dea31012005-04-17 16:05:31 -0500931}
932
James Smart92d7f7b2007-06-17 19:56:38 -0500933static void
934lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500935{
James Smart92d7f7b2007-06-17 19:56:38 -0500936 struct lpfc_nodelist *ndlp;
937
938 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500939 if (!NLP_CHK_NODE_ACT(ndlp))
940 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500941 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
942 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500943 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500944 /* On Linkup its safe to clean up the ndlp
945 * from Fabric connections.
946 */
James Smart92d7f7b2007-06-17 19:56:38 -0500947 if (ndlp->nlp_DID != Fabric_DID)
948 lpfc_unreg_rpi(vport, ndlp);
949 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
950 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500951 /* Fail outstanding IO now since device is
952 * marked for PLOGI.
953 */
James Smart92d7f7b2007-06-17 19:56:38 -0500954 lpfc_unreg_rpi(vport, ndlp);
955 }
956 }
957}
958
959static void
960lpfc_linkup_port(struct lpfc_vport *vport)
961{
962 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500963 struct lpfc_hba *phba = vport->phba;
964
965 if ((vport->load_flag & FC_UNLOADING) != 0)
966 return;
967
James Smart858c9f62007-06-17 19:56:39 -0500968 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
969 "Link Up: top:x%x speed:x%x flg:x%x",
970 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
971
James Smart92d7f7b2007-06-17 19:56:38 -0500972 /* If NPIV is not enabled, only bring the physical port up */
973 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
974 (vport != phba->pport))
975 return;
dea31012005-04-17 16:05:31 -0500976
James Smart2e0fef82007-06-17 19:56:36 -0500977 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400978
James Smart2e0fef82007-06-17 19:56:36 -0500979 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500980 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
981 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
982 vport->fc_flag |= FC_NDISC_ACTIVE;
983 vport->fc_ns_retry = 0;
984 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500985
James Smart92d7f7b2007-06-17 19:56:38 -0500986 if (vport->fc_flag & FC_LBIT)
987 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500988
James Smart92d7f7b2007-06-17 19:56:38 -0500989}
990
991static int
992lpfc_linkup(struct lpfc_hba *phba)
993{
James Smart549e55c2007-08-02 11:09:51 -0400994 struct lpfc_vport **vports;
995 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500996
James Smart19ca7602010-11-20 23:11:55 -0500997 lpfc_cleanup_wt_rrqs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500998 phba->link_state = LPFC_LINK_UP;
999
1000 /* Unblock fabric iocbs if they are blocked */
1001 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1002 del_timer_sync(&phba->fabric_block_timer);
1003
James Smart549e55c2007-08-02 11:09:51 -04001004 vports = lpfc_create_vport_work_array(phba);
1005 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001006 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001007 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001008 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -04001009 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1010 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05001011 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -05001012
1013 return 0;
1014}
1015
1016/*
1017 * This routine handles processing a CLEAR_LA mailbox
1018 * command upon completion. It is setup in the LPFC_MBOXQ
1019 * as the completion routine when the command is
1020 * handed off to the SLI layer.
1021 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001022static void
James Smart2e0fef82007-06-17 19:56:36 -05001023lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001024{
James Smart2e0fef82007-06-17 19:56:36 -05001025 struct lpfc_vport *vport = pmb->vport;
1026 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1027 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001028 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001029 uint32_t control;
1030
dea31012005-04-17 16:05:31 -05001031 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001032 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001033 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1034 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1035
1036 /* Check for error */
1037 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001038 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001039 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1040 "0320 CLEAR_LA mbxStatus error x%x hba "
1041 "state x%x\n",
1042 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001043 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001044 goto out;
1045 }
1046
James Smart92d7f7b2007-06-17 19:56:38 -05001047 if (vport->port_type == LPFC_PHYSICAL_PORT)
1048 phba->link_state = LPFC_HBA_READY;
1049
1050 spin_lock_irq(&phba->hbalock);
1051 psli->sli_flag |= LPFC_PROCESS_LA;
1052 control = readl(phba->HCregaddr);
1053 control |= HC_LAINT_ENA;
1054 writel(control, phba->HCregaddr);
1055 readl(phba->HCregaddr); /* flush */
1056 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001057 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001058 return;
dea31012005-04-17 16:05:31 -05001059
dea31012005-04-17 16:05:31 -05001060out:
1061 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001062 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1063 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001064 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001065
James Smart2e0fef82007-06-17 19:56:36 -05001066 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001067 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001068 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001069
James Smart2e0fef82007-06-17 19:56:36 -05001070 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001071
1072 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001073
1074 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001075 psli->sli_flag |= LPFC_PROCESS_LA;
1076 control = readl(phba->HCregaddr);
1077 control |= HC_LAINT_ENA;
1078 writel(control, phba->HCregaddr);
1079 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001080 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001081
1082 return;
1083}
1084
James Smart2e0fef82007-06-17 19:56:36 -05001085
dea31012005-04-17 16:05:31 -05001086static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001087lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001088{
James Smart2e0fef82007-06-17 19:56:36 -05001089 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001090
James Smart04c68492009-05-22 14:52:52 -04001091 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001092 goto out;
dea31012005-04-17 16:05:31 -05001093
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001094 mempool_free(pmb, phba->mbox_mem_pool);
1095
James Smart1b511972011-12-13 13:23:09 -05001096 /* don't perform discovery for SLI4 loopback diagnostic test */
1097 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1098 !(phba->hba_flag & HBA_FCOE_MODE) &&
1099 (phba->link_flag & LS_LOOPBACK_MODE))
1100 return;
1101
James Smart76a95d72010-11-20 23:11:48 -05001102 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001103 vport->fc_flag & FC_PUBLIC_LOOP &&
1104 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001105 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001106 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001107 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1108 */
James Smart2e0fef82007-06-17 19:56:36 -05001109 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001110 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001111 }
dea31012005-04-17 16:05:31 -05001112
James Smart2e0fef82007-06-17 19:56:36 -05001113 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001114 * LPFC_FLOGI while waiting for FLOGI cmpl
1115 */
James Smarte6446432012-05-09 21:16:42 -04001116 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001117 lpfc_initial_flogi(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001118 return;
dea31012005-04-17 16:05:31 -05001119
1120out:
James Smarte8b62012007-08-02 11:10:09 -04001121 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1122 "0306 CONFIG_LINK mbxStatus error x%x "
1123 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001124 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001125 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001126
1127 lpfc_linkdown(phba);
1128
James Smarte8b62012007-08-02 11:10:09 -04001129 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1130 "0200 CONFIG_LINK bad hba state x%x\n",
1131 vport->port_state);
dea31012005-04-17 16:05:31 -05001132
James Smart92d7f7b2007-06-17 19:56:38 -05001133 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001134 return;
1135}
1136
James Smart7d791df2011-07-22 18:37:52 -04001137/**
1138 * lpfc_sli4_clear_fcf_rr_bmask
1139 * @phba pointer to the struct lpfc_hba for this port.
1140 * This fucnction resets the round robin bit mask and clears the
1141 * fcf priority list. The list deletions are done while holding the
1142 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1143 * from the lpfc_fcf_pri record.
1144 **/
1145void
1146lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1147{
1148 struct lpfc_fcf_pri *fcf_pri;
1149 struct lpfc_fcf_pri *next_fcf_pri;
1150 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1151 spin_lock_irq(&phba->hbalock);
1152 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1153 &phba->fcf.fcf_pri_list, list) {
1154 list_del_init(&fcf_pri->list);
1155 fcf_pri->fcf_rec.flag = 0;
1156 }
1157 spin_unlock_irq(&phba->hbalock);
1158}
dea31012005-04-17 16:05:31 -05001159static void
James Smart6fb120a2009-05-22 14:52:59 -04001160lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1161{
1162 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001163
1164 if (mboxq->u.mb.mbxStatus) {
1165 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1166 "2017 REG_FCFI mbxStatus error x%x "
1167 "HBA state x%x\n",
1168 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001169 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001170 }
1171
1172 /* Start FCoE discovery by sending a FLOGI. */
1173 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1174 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001175 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001176 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001177 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001178
James Smart32b97932009-07-19 10:01:21 -04001179 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001180 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1181 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001182 goto fail_out;
1183
1184 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001185 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001186 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001187 phba->hba_flag &= ~FCF_TS_INPROG;
1188 if (vport->port_state != LPFC_FLOGI) {
1189 phba->hba_flag |= FCF_RR_INPROG;
1190 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001191 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001192 goto out;
1193 }
1194 spin_unlock_irq(&phba->hbalock);
1195 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001196
James Smarta93ff372010-10-22 11:06:08 -04001197fail_out:
1198 spin_lock_irq(&phba->hbalock);
1199 phba->hba_flag &= ~FCF_RR_INPROG;
1200 spin_unlock_irq(&phba->hbalock);
1201out:
James Smart6fb120a2009-05-22 14:52:59 -04001202 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001203}
1204
1205/**
1206 * lpfc_fab_name_match - Check if the fcf fabric name match.
1207 * @fab_name: pointer to fabric name.
1208 * @new_fcf_record: pointer to fcf record.
1209 *
1210 * This routine compare the fcf record's fabric name with provided
1211 * fabric name. If the fabric name are identical this function
1212 * returns 1 else return 0.
1213 **/
1214static uint32_t
1215lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1216{
James Smartecfd03c2010-02-12 14:41:27 -05001217 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001218 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001219 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1220 return 0;
1221 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1222 return 0;
1223 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1224 return 0;
1225 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1226 return 0;
1227 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1228 return 0;
1229 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1230 return 0;
1231 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1232 return 0;
1233 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001234}
1235
1236/**
James Smart8fa38512009-07-19 10:01:03 -04001237 * lpfc_sw_name_match - Check if the fcf switch name match.
1238 * @fab_name: pointer to fabric name.
1239 * @new_fcf_record: pointer to fcf record.
1240 *
1241 * This routine compare the fcf record's switch name with provided
1242 * switch name. If the switch name are identical this function
1243 * returns 1 else return 0.
1244 **/
1245static uint32_t
1246lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1247{
James Smartecfd03c2010-02-12 14:41:27 -05001248 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001249 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001250 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1251 return 0;
1252 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1253 return 0;
1254 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1255 return 0;
1256 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1257 return 0;
1258 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1259 return 0;
1260 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1261 return 0;
1262 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1263 return 0;
1264 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001265}
1266
1267/**
James Smart6fb120a2009-05-22 14:52:59 -04001268 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001269 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001270 * @new_fcf_record: pointer to fcf record.
1271 *
1272 * This routine compare the fcf record's mac address with HBA's
1273 * FCF mac address. If the mac addresses are identical this function
1274 * returns 1 else return 0.
1275 **/
1276static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001277lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001278{
James Smartecfd03c2010-02-12 14:41:27 -05001279 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001280 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001281 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1282 return 0;
1283 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1284 return 0;
1285 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1286 return 0;
1287 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1288 return 0;
1289 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1290 return 0;
1291 return 1;
1292}
1293
1294static bool
1295lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1296{
1297 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001298}
1299
1300/**
James Smart7d791df2011-07-22 18:37:52 -04001301 * lpfc_update_fcf_record - Update driver fcf record
1302 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1303 * @phba: pointer to lpfc hba data structure.
1304 * @fcf_index: Index for the lpfc_fcf_record.
1305 * @new_fcf_record: pointer to hba fcf record.
1306 *
1307 * This routine updates the driver FCF priority record from the new HBA FCF
1308 * record. This routine is called with the host lock held.
1309 **/
1310static void
1311__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1312 struct fcf_record *new_fcf_record
1313 )
1314{
1315 struct lpfc_fcf_pri *fcf_pri;
1316
1317 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1318 fcf_pri->fcf_rec.fcf_index = fcf_index;
1319 /* FCF record priority */
1320 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1321
1322}
1323
1324/**
James Smart6fb120a2009-05-22 14:52:59 -04001325 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001326 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001327 * @new_fcf_record: pointer to fcf record.
1328 *
1329 * This routine copies the FCF information from the FCF
1330 * record to lpfc_hba data structure.
1331 **/
1332static void
James Smartecfd03c2010-02-12 14:41:27 -05001333lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1334 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001335{
James Smartecfd03c2010-02-12 14:41:27 -05001336 /* Fabric name */
1337 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001338 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001339 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001340 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001341 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001342 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001343 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001344 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001345 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001346 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001347 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001348 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001349 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001350 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001351 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001352 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001353 /* Mac address */
1354 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1355 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1356 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1357 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1358 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1359 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1360 /* FCF record index */
1361 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1362 /* FCF record priority */
1363 fcf_rec->priority = new_fcf_record->fip_priority;
1364 /* Switch name */
1365 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001366 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001367 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001368 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001369 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001370 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001371 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001372 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001373 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001374 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001375 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001376 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001377 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001378 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001379 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001380 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001381}
1382
1383/**
James Smartecfd03c2010-02-12 14:41:27 -05001384 * lpfc_update_fcf_record - Update driver fcf record
1385 * @phba: pointer to lpfc hba data structure.
1386 * @fcf_rec: pointer to driver fcf record.
1387 * @new_fcf_record: pointer to hba fcf record.
1388 * @addr_mode: address mode to be set to the driver fcf record.
1389 * @vlan_id: vlan tag to be set to the driver fcf record.
1390 * @flag: flag bits to be set to the driver fcf record.
1391 *
1392 * This routine updates the driver FCF record from the new HBA FCF record
1393 * together with the address mode, vlan_id, and other informations. This
1394 * routine is called with the host lock held.
1395 **/
1396static void
1397__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1398 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1399 uint16_t vlan_id, uint32_t flag)
1400{
1401 /* Copy the fields from the HBA's FCF record */
1402 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1403 /* Update other fields of driver FCF record */
1404 fcf_rec->addr_mode = addr_mode;
1405 fcf_rec->vlan_id = vlan_id;
1406 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001407 __lpfc_update_fcf_record_pri(phba,
1408 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1409 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001410}
1411
1412/**
James Smart6fb120a2009-05-22 14:52:59 -04001413 * lpfc_register_fcf - Register the FCF with hba.
1414 * @phba: pointer to lpfc hba data structure.
1415 *
1416 * This routine issues a register fcfi mailbox command to register
1417 * the fcf with HBA.
1418 **/
1419static void
1420lpfc_register_fcf(struct lpfc_hba *phba)
1421{
1422 LPFC_MBOXQ_t *fcf_mbxq;
1423 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001424
James Smartd439d282010-09-29 11:18:45 -04001425 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001426 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001427 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001428 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001429 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001430 return;
1431 }
1432
1433 /* The FCF is already registered, start discovery */
1434 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001435 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001436 phba->hba_flag &= ~FCF_TS_INPROG;
1437 if (phba->pport->port_state != LPFC_FLOGI) {
1438 phba->hba_flag |= FCF_RR_INPROG;
1439 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001440 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001441 return;
1442 }
1443 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001444 return;
1445 }
James Smartd439d282010-09-29 11:18:45 -04001446 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001447
James Smarta93ff372010-10-22 11:06:08 -04001448 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001449 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001450 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001451 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001452 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001453 return;
James Smart4d9ab992009-10-02 15:16:39 -04001454 }
James Smart6fb120a2009-05-22 14:52:59 -04001455
1456 lpfc_reg_fcfi(phba, fcf_mbxq);
1457 fcf_mbxq->vport = phba->pport;
1458 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1459 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001460 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001461 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001462 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001463 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001464 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001465 }
James Smart6fb120a2009-05-22 14:52:59 -04001466
1467 return;
1468}
1469
1470/**
1471 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1472 * @phba: pointer to lpfc hba data structure.
1473 * @new_fcf_record: pointer to fcf record.
1474 * @boot_flag: Indicates if this record used by boot bios.
1475 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001476 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001477 *
1478 * This routine compare the fcf record with connect list obtained from the
1479 * config region to decide if this FCF can be used for SAN discovery. It returns
1480 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1481 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1482 * is used by boot bios and addr_mode will indicate the addressing mode to be
1483 * used for this FCF when the function returns.
1484 * If the FCF record need to be used with a particular vlan id, the vlan is
1485 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001486 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001487 **/
1488static int
1489lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1490 struct fcf_record *new_fcf_record,
1491 uint32_t *boot_flag, uint32_t *addr_mode,
1492 uint16_t *vlan_id)
1493{
1494 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001495 int i, j, fcf_vlan_id = 0;
1496
1497 /* Find the lowest VLAN id in the FCF record */
1498 for (i = 0; i < 512; i++) {
1499 if (new_fcf_record->vlan_bitmap[i]) {
1500 fcf_vlan_id = i * 8;
1501 j = 0;
1502 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1503 j++;
1504 fcf_vlan_id++;
1505 }
1506 break;
1507 }
1508 }
James Smart6fb120a2009-05-22 14:52:59 -04001509
James Smart26979ce2012-09-29 11:31:55 -04001510 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001511 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001512 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1513 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001514 return 0;
1515
James Smart45ed1192009-10-02 15:17:02 -04001516 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001517 *boot_flag = 0;
1518 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1519 new_fcf_record);
1520 if (phba->valid_vlan)
1521 *vlan_id = phba->vlan_id;
1522 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001523 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001524 return 1;
1525 }
1526
1527 /*
1528 * If there are no FCF connection table entry, driver connect to all
1529 * FCFs.
1530 */
1531 if (list_empty(&phba->fcf_conn_rec_list)) {
1532 *boot_flag = 0;
1533 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1534 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001535
1536 /*
1537 * When there are no FCF connect entries, use driver's default
1538 * addressing mode - FPMA.
1539 */
1540 if (*addr_mode & LPFC_FCF_FPMA)
1541 *addr_mode = LPFC_FCF_FPMA;
1542
James Smart4d9ab992009-10-02 15:16:39 -04001543 /* If FCF record report a vlan id use that vlan id */
1544 if (fcf_vlan_id)
1545 *vlan_id = fcf_vlan_id;
1546 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001547 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001548 return 1;
1549 }
1550
James Smartecfd03c2010-02-12 14:41:27 -05001551 list_for_each_entry(conn_entry,
1552 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001553 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1554 continue;
1555
1556 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1557 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001558 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001559 continue;
James Smart8fa38512009-07-19 10:01:03 -04001560 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1561 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1562 new_fcf_record))
1563 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001564 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1565 /*
1566 * If the vlan bit map does not have the bit set for the
1567 * vlan id to be used, then it is not a match.
1568 */
1569 if (!(new_fcf_record->vlan_bitmap
1570 [conn_entry->conn_rec.vlan_tag / 8] &
1571 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1572 continue;
1573 }
1574
1575 /*
James Smart0c287582009-06-10 17:22:56 -04001576 * If connection record does not support any addressing mode,
1577 * skip the FCF record.
1578 */
1579 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1580 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1581 continue;
1582
1583 /*
James Smart6fb120a2009-05-22 14:52:59 -04001584 * Check if the connection record specifies a required
1585 * addressing mode.
1586 */
1587 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1588 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1589
1590 /*
1591 * If SPMA required but FCF not support this continue.
1592 */
1593 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1594 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1595 new_fcf_record) & LPFC_FCF_SPMA))
1596 continue;
1597
1598 /*
1599 * If FPMA required but FCF not support this continue.
1600 */
1601 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1602 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1603 new_fcf_record) & LPFC_FCF_FPMA))
1604 continue;
1605 }
1606
1607 /*
1608 * This fcf record matches filtering criteria.
1609 */
1610 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1611 *boot_flag = 1;
1612 else
1613 *boot_flag = 0;
1614
James Smart0c287582009-06-10 17:22:56 -04001615 /*
1616 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001617 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001618 * by FCF, allow fabric to pick the addressing mode.
1619 */
James Smart6fb120a2009-05-22 14:52:59 -04001620 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1621 new_fcf_record);
1622 /*
1623 * If the user specified a required address mode, assign that
1624 * address mode
1625 */
1626 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1627 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1628 *addr_mode = (conn_entry->conn_rec.flags &
1629 FCFCNCT_AM_SPMA) ?
1630 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1631 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001632 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001633 * addr mode only if FCF support the addr_mode.
1634 */
1635 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1636 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1637 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1638 (*addr_mode & LPFC_FCF_SPMA))
1639 *addr_mode = LPFC_FCF_SPMA;
1640 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1641 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1642 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1643 (*addr_mode & LPFC_FCF_FPMA))
1644 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001645
James Smart4d9ab992009-10-02 15:16:39 -04001646 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001647 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1648 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001649 /*
1650 * If no vlan id is specified in connect list, use the vlan id
1651 * in the FCF record
1652 */
1653 else if (fcf_vlan_id)
1654 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001655 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001656 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001657
1658 return 1;
1659 }
1660
1661 return 0;
1662}
1663
1664/**
James Smart32b97932009-07-19 10:01:21 -04001665 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1666 * @phba: pointer to lpfc hba data structure.
1667 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1668 *
1669 * This function check if there is any fcoe event pending while driver
1670 * scan FCF entries. If there is any pending event, it will restart the
1671 * FCF saning and return 1 else return 0.
1672 */
1673int
1674lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1675{
James Smart32b97932009-07-19 10:01:21 -04001676 /*
1677 * If the Link is up and no FCoE events while in the
1678 * FCF discovery, no need to restart FCF discovery.
1679 */
1680 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001681 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001682 return 0;
1683
James Smart0c9ab6f2010-02-26 14:15:57 -05001684 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1685 "2768 Pending link or FCF event during current "
1686 "handling of the previous event: link_state:x%x, "
1687 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1688 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1689 phba->fcoe_eventtag);
1690
James Smart32b97932009-07-19 10:01:21 -04001691 spin_lock_irq(&phba->hbalock);
1692 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1693 spin_unlock_irq(&phba->hbalock);
1694
James Smart0c9ab6f2010-02-26 14:15:57 -05001695 if (phba->link_state >= LPFC_LINK_UP) {
1696 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1697 "2780 Restart FCF table scan due to "
1698 "pending FCF event:evt_tag_at_scan:x%x, "
1699 "evt_tag_current:x%x\n",
1700 phba->fcoe_eventtag_at_fcf_scan,
1701 phba->fcoe_eventtag);
1702 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1703 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001704 /*
James Smarta93ff372010-10-22 11:06:08 -04001705 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001706 * flag
1707 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001708 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1709 "2833 Stop FCF discovery process due to link "
1710 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001711 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001712 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001713 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001714 spin_unlock_irq(&phba->hbalock);
1715 }
James Smart32b97932009-07-19 10:01:21 -04001716
James Smart0c9ab6f2010-02-26 14:15:57 -05001717 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001718 if (unreg_fcf) {
1719 spin_lock_irq(&phba->hbalock);
1720 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1721 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001722 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001723 }
James Smart32b97932009-07-19 10:01:21 -04001724 return 1;
1725}
1726
1727/**
James Smart999d8132010-03-15 11:24:56 -04001728 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1729 * @phba: pointer to lpfc hba data structure.
1730 * @fcf_cnt: number of eligible fcf record seen so far.
1731 *
1732 * This function makes an running random selection decision on FCF record to
1733 * use through a sequence of @fcf_cnt eligible FCF records with equal
1734 * probability. To perform integer manunipulation of random numbers with
1735 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1736 * from random32() are taken as the random random number generated.
1737 *
1738 * Returns true when outcome is for the newly read FCF record should be
1739 * chosen; otherwise, return false when outcome is for keeping the previously
1740 * chosen FCF record.
1741 **/
1742static bool
1743lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1744{
1745 uint32_t rand_num;
1746
1747 /* Get 16-bit uniform random number */
1748 rand_num = (0xFFFF & random32());
1749
1750 /* Decision with probability 1/fcf_cnt */
1751 if ((fcf_cnt * rand_num) < 0xFFFF)
1752 return true;
1753 else
1754 return false;
1755}
1756
1757/**
James Smart3804dc82010-07-14 15:31:37 -04001758 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001759 * @phba: pointer to lpfc hba data structure.
1760 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001761 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001762 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001763 * This routine parses the non-embedded fcf mailbox command by performing the
1764 * necessarily error checking, non-embedded read FCF record mailbox command
1765 * SGE parsing, and endianness swapping.
1766 *
1767 * Returns the pointer to the new FCF record in the non-embedded mailbox
1768 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001769 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001770static struct fcf_record *
1771lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1772 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001773{
1774 void *virt_addr;
1775 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001776 struct lpfc_mbx_sge sge;
1777 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1778 uint32_t shdr_status, shdr_add_status;
1779 union lpfc_sli4_cfg_shdr *shdr;
1780 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001781
James Smart6fb120a2009-05-22 14:52:59 -04001782 /* Get the first SGE entry from the non-embedded DMA memory. This
1783 * routine only uses a single SGE.
1784 */
1785 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1786 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1787 if (unlikely(!mboxq->sge_array)) {
1788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1789 "2524 Failed to get the non-embedded SGE "
1790 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001791 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001792 }
1793 virt_addr = mboxq->sge_array->addr[0];
1794
1795 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001796 lpfc_sli_pcimem_bcopy(shdr, shdr,
1797 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001798 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001799 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001800 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001801 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1802 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001803 "2726 READ_FCF_RECORD Indicates empty "
1804 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001805 else
1806 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001807 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001808 "with status x%x add_status x%x, "
1809 "mbx\n", shdr_status, shdr_add_status);
1810 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001811 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001812
1813 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001814 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1815 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1816 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001817 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001818 new_fcf_record = (struct fcf_record *)(virt_addr +
1819 sizeof(struct lpfc_mbx_read_fcf_tbl));
1820 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001821 offsetof(struct fcf_record, vlan_bitmap));
1822 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1823 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001824
James Smart0c9ab6f2010-02-26 14:15:57 -05001825 return new_fcf_record;
1826}
1827
1828/**
1829 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1830 * @phba: pointer to lpfc hba data structure.
1831 * @fcf_record: pointer to the fcf record.
1832 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1833 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1834 *
1835 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1836 * enabled.
1837 **/
1838static void
1839lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1840 struct fcf_record *fcf_record,
1841 uint16_t vlan_id,
1842 uint16_t next_fcf_index)
1843{
1844 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1845 "2764 READ_FCF_RECORD:\n"
1846 "\tFCF_Index : x%x\n"
1847 "\tFCF_Avail : x%x\n"
1848 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001849 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001850 "\tFIP_Priority : x%x\n"
1851 "\tMAC_Provider : x%x\n"
1852 "\tLowest VLANID : x%x\n"
1853 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1854 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1855 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1856 "\tNext_FCF_Index: x%x\n",
1857 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1858 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1859 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001860 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001861 fcf_record->fip_priority,
1862 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1863 vlan_id,
1864 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1865 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1866 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1867 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1868 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1869 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1870 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1871 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1872 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1873 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1874 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1875 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1876 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1877 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1878 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1879 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1880 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1881 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1882 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1883 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1884 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1885 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1886 next_fcf_index);
1887}
1888
1889/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001890 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1891 * @phba: pointer to lpfc hba data structure.
1892 * @fcf_rec: pointer to an existing FCF record.
1893 * @new_fcf_record: pointer to a new FCF record.
1894 * @new_vlan_id: vlan id from the new FCF record.
1895 *
1896 * This function performs matching test of a new FCF record against an existing
1897 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1898 * will not be used as part of the FCF record matching criteria.
1899 *
1900 * Returns true if all the fields matching, otherwise returns false.
1901 */
1902static bool
1903lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1904 struct lpfc_fcf_rec *fcf_rec,
1905 struct fcf_record *new_fcf_record,
1906 uint16_t new_vlan_id)
1907{
1908 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1909 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1910 return false;
1911 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1912 return false;
1913 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1914 return false;
1915 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1916 return false;
James Smart7d791df2011-07-22 18:37:52 -04001917 if (fcf_rec->priority != new_fcf_record->fip_priority)
1918 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001919 return true;
1920}
1921
1922/**
James Smarta93ff372010-10-22 11:06:08 -04001923 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1924 * @vport: Pointer to vport object.
1925 * @fcf_index: index to next fcf.
1926 *
1927 * This function processing the roundrobin fcf failover to next fcf index.
1928 * When this function is invoked, there will be a current fcf registered
1929 * for flogi.
1930 * Return: 0 for continue retrying flogi on currently registered fcf;
1931 * 1 for stop flogi on currently registered fcf;
1932 */
1933int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1934{
1935 struct lpfc_hba *phba = vport->phba;
1936 int rc;
1937
1938 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1939 spin_lock_irq(&phba->hbalock);
1940 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1941 spin_unlock_irq(&phba->hbalock);
1942 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1943 "2872 Devloss tmo with no eligible "
1944 "FCF, unregister in-use FCF (x%x) "
1945 "and rescan FCF table\n",
1946 phba->fcf.current_rec.fcf_indx);
1947 lpfc_unregister_fcf_rescan(phba);
1948 goto stop_flogi_current_fcf;
1949 }
1950 /* Mark the end to FLOGI roundrobin failover */
1951 phba->hba_flag &= ~FCF_RR_INPROG;
1952 /* Allow action to new fcf asynchronous event */
1953 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1954 spin_unlock_irq(&phba->hbalock);
1955 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1956 "2865 No FCF available, stop roundrobin FCF "
1957 "failover and change port state:x%x/x%x\n",
1958 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1959 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1960 goto stop_flogi_current_fcf;
1961 } else {
1962 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1963 "2794 Try FLOGI roundrobin FCF failover to "
1964 "(x%x)\n", fcf_index);
1965 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1966 if (rc)
1967 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1968 "2761 FLOGI roundrobin FCF failover "
1969 "failed (rc:x%x) to read FCF (x%x)\n",
1970 rc, phba->fcf.current_rec.fcf_indx);
1971 else
1972 goto stop_flogi_current_fcf;
1973 }
1974 return 0;
1975
1976stop_flogi_current_fcf:
1977 lpfc_can_disctmo(vport);
1978 return 1;
1979}
1980
1981/**
James Smart7d791df2011-07-22 18:37:52 -04001982 * lpfc_sli4_fcf_pri_list_del
1983 * @phba: pointer to lpfc hba data structure.
1984 * @fcf_index the index of the fcf record to delete
1985 * This routine checks the on list flag of the fcf_index to be deleted.
1986 * If it is one the list then it is removed from the list, and the flag
1987 * is cleared. This routine grab the hbalock before removing the fcf
1988 * record from the list.
1989 **/
1990static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1991 uint16_t fcf_index)
1992{
1993 struct lpfc_fcf_pri *new_fcf_pri;
1994
1995 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1996 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1997 "3058 deleting idx x%x pri x%x flg x%x\n",
1998 fcf_index, new_fcf_pri->fcf_rec.priority,
1999 new_fcf_pri->fcf_rec.flag);
2000 spin_lock_irq(&phba->hbalock);
2001 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2002 if (phba->fcf.current_rec.priority ==
2003 new_fcf_pri->fcf_rec.priority)
2004 phba->fcf.eligible_fcf_cnt--;
2005 list_del_init(&new_fcf_pri->list);
2006 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2007 }
2008 spin_unlock_irq(&phba->hbalock);
2009}
2010
2011/**
2012 * lpfc_sli4_set_fcf_flogi_fail
2013 * @phba: pointer to lpfc hba data structure.
2014 * @fcf_index the index of the fcf record to update
2015 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2016 * flag so the the round robin slection for the particular priority level
2017 * will try a different fcf record that does not have this bit set.
2018 * If the fcf record is re-read for any reason this flag is cleared brfore
2019 * adding it to the priority list.
2020 **/
2021void
2022lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2023{
2024 struct lpfc_fcf_pri *new_fcf_pri;
2025 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2026 spin_lock_irq(&phba->hbalock);
2027 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2028 spin_unlock_irq(&phba->hbalock);
2029}
2030
2031/**
2032 * lpfc_sli4_fcf_pri_list_add
2033 * @phba: pointer to lpfc hba data structure.
2034 * @fcf_index the index of the fcf record to add
2035 * This routine checks the priority of the fcf_index to be added.
2036 * If it is a lower priority than the current head of the fcf_pri list
2037 * then it is added to the list in the right order.
2038 * If it is the same priority as the current head of the list then it
2039 * is added to the head of the list and its bit in the rr_bmask is set.
2040 * If the fcf_index to be added is of a higher priority than the current
2041 * head of the list then the rr_bmask is cleared, its bit is set in the
2042 * rr_bmask and it is added to the head of the list.
2043 * returns:
2044 * 0=success 1=failure
2045 **/
2046int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba, uint16_t fcf_index,
2047 struct fcf_record *new_fcf_record)
2048{
2049 uint16_t current_fcf_pri;
2050 uint16_t last_index;
2051 struct lpfc_fcf_pri *fcf_pri;
2052 struct lpfc_fcf_pri *next_fcf_pri;
2053 struct lpfc_fcf_pri *new_fcf_pri;
2054 int ret;
2055
2056 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2057 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2058 "3059 adding idx x%x pri x%x flg x%x\n",
2059 fcf_index, new_fcf_record->fip_priority,
2060 new_fcf_pri->fcf_rec.flag);
2061 spin_lock_irq(&phba->hbalock);
2062 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2063 list_del_init(&new_fcf_pri->list);
2064 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2065 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2066 if (list_empty(&phba->fcf.fcf_pri_list)) {
2067 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2068 ret = lpfc_sli4_fcf_rr_index_set(phba,
2069 new_fcf_pri->fcf_rec.fcf_index);
2070 goto out;
2071 }
2072
2073 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2074 LPFC_SLI4_FCF_TBL_INDX_MAX);
2075 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2076 ret = 0; /* Empty rr list */
2077 goto out;
2078 }
2079 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2080 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2081 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2082 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2083 memset(phba->fcf.fcf_rr_bmask, 0,
2084 sizeof(*phba->fcf.fcf_rr_bmask));
2085 /* fcfs_at_this_priority_level = 1; */
2086 phba->fcf.eligible_fcf_cnt = 1;
2087 } else
2088 /* fcfs_at_this_priority_level++; */
2089 phba->fcf.eligible_fcf_cnt++;
2090 ret = lpfc_sli4_fcf_rr_index_set(phba,
2091 new_fcf_pri->fcf_rec.fcf_index);
2092 goto out;
2093 }
2094
2095 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2096 &phba->fcf.fcf_pri_list, list) {
2097 if (new_fcf_pri->fcf_rec.priority <=
2098 fcf_pri->fcf_rec.priority) {
2099 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2100 list_add(&new_fcf_pri->list,
2101 &phba->fcf.fcf_pri_list);
2102 else
2103 list_add(&new_fcf_pri->list,
2104 &((struct lpfc_fcf_pri *)
2105 fcf_pri->list.prev)->list);
2106 ret = 0;
2107 goto out;
2108 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2109 || new_fcf_pri->fcf_rec.priority <
2110 next_fcf_pri->fcf_rec.priority) {
2111 list_add(&new_fcf_pri->list, &fcf_pri->list);
2112 ret = 0;
2113 goto out;
2114 }
2115 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2116 continue;
2117
2118 }
2119 ret = 1;
2120out:
2121 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2122 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2123 spin_unlock_irq(&phba->hbalock);
2124 return ret;
2125}
2126
2127/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002128 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2129 * @phba: pointer to lpfc hba data structure.
2130 * @mboxq: pointer to mailbox object.
2131 *
2132 * This function iterates through all the fcf records available in
2133 * HBA and chooses the optimal FCF record for discovery. After finding
2134 * the FCF for discovery it registers the FCF record and kicks start
2135 * discovery.
2136 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2137 * use an FCF record which matches fabric name and mac address of the
2138 * currently used FCF record.
2139 * If the driver supports only one FCF, it will try to use the FCF record
2140 * used by BOOT_BIOS.
2141 */
2142void
2143lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2144{
2145 struct fcf_record *new_fcf_record;
2146 uint32_t boot_flag, addr_mode;
2147 uint16_t fcf_index, next_fcf_index;
2148 struct lpfc_fcf_rec *fcf_rec = NULL;
2149 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002150 uint32_t seed;
2151 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002152 int rc;
2153
2154 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002155 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002156 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2157 return;
2158 }
2159
2160 /* Parse the FCF record from the non-embedded mailbox command */
2161 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2162 &next_fcf_index);
2163 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002164 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002165 "2765 Mailbox command READ_FCF_RECORD "
2166 "failed to retrieve a FCF record.\n");
2167 /* Let next new FCF event trigger fast failover */
2168 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002169 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002170 spin_unlock_irq(&phba->hbalock);
2171 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2172 return;
2173 }
2174
2175 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002176 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2177 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002178
2179 /* Log the FCF record information if turned on */
2180 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2181 next_fcf_index);
2182
James Smart6fb120a2009-05-22 14:52:59 -04002183 /*
2184 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002185 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002186 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002187 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002188 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002189 lpfc_sli4_fcf_pri_list_del(phba,
2190 bf_get(lpfc_fcf_record_fcf_index,
2191 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002192 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002193 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002194 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002195 bf_get(lpfc_fcf_record_fcf_index,
2196 new_fcf_record),
2197 bf_get(lpfc_fcf_record_fcf_avail,
2198 new_fcf_record),
2199 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002200 new_fcf_record),
2201 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002202 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002203 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2204 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2205 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002206 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2207 phba->fcf.current_rec.fcf_indx) {
2208 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2209 "2862 FCF (x%x) matches property "
2210 "of in-use FCF (x%x)\n",
2211 bf_get(lpfc_fcf_record_fcf_index,
2212 new_fcf_record),
2213 phba->fcf.current_rec.fcf_indx);
2214 goto read_next_fcf;
2215 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002216 /*
2217 * In case the current in-use FCF record becomes
2218 * invalid/unavailable during FCF discovery that
2219 * was not triggered by fast FCF failover process,
2220 * treat it as fast FCF failover.
2221 */
2222 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2223 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2224 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2225 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002226 "(x%x), enter FCF failover "
2227 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002228 phba->fcf.current_rec.fcf_indx);
2229 spin_lock_irq(&phba->hbalock);
2230 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2231 spin_unlock_irq(&phba->hbalock);
2232 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2233 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2234 LPFC_FCOE_FCF_GET_FIRST);
2235 return;
2236 }
2237 }
James Smart6fb120a2009-05-22 14:52:59 -04002238 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002239 } else {
2240 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002241 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2242 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002243 if (rc)
2244 goto read_next_fcf;
2245 }
2246
James Smart6fb120a2009-05-22 14:52:59 -04002247 /*
2248 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002249 * FCF record for the discovery. The condition that a rescan
2250 * matches the in-use FCF record: fabric name, switch name, mac
2251 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002252 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002253 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002254 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002255 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2256 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002257 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002258 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2259 phba->fcf.current_rec.fcf_indx) {
2260 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2261 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2262 /* Stop FCF redisc wait timer */
2263 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2264 phba);
2265 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2266 /* Fast failover, mark completed */
2267 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2268 spin_unlock_irq(&phba->hbalock);
2269 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2270 "2836 New FCF matches in-use "
2271 "FCF (x%x)\n",
2272 phba->fcf.current_rec.fcf_indx);
2273 goto out;
2274 } else
2275 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2276 "2863 New FCF (x%x) matches "
2277 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002278 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002279 new_fcf_record),
2280 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002281 }
James Smartecfd03c2010-02-12 14:41:27 -05002282 /*
2283 * Read next FCF record from HBA searching for the matching
2284 * with in-use record only if not during the fast failover
2285 * period. In case of fast failover period, it shall try to
2286 * determine whether the FCF record just read should be the
2287 * next candidate.
2288 */
2289 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002290 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002291 goto read_next_fcf;
2292 }
James Smart6fb120a2009-05-22 14:52:59 -04002293 }
James Smartecfd03c2010-02-12 14:41:27 -05002294 /*
2295 * Update on failover FCF record only if it's in FCF fast-failover
2296 * period; otherwise, update on current FCF record.
2297 */
James Smartfc2b9892010-02-26 14:15:29 -05002298 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2299 fcf_rec = &phba->fcf.failover_rec;
2300 else
James Smartecfd03c2010-02-12 14:41:27 -05002301 fcf_rec = &phba->fcf.current_rec;
2302
James Smart6fb120a2009-05-22 14:52:59 -04002303 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2304 /*
James Smartecfd03c2010-02-12 14:41:27 -05002305 * If the driver FCF record does not have boot flag
2306 * set and new hba fcf record has boot flag set, use
2307 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002308 */
James Smartecfd03c2010-02-12 14:41:27 -05002309 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2310 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002311 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2312 "2837 Update current FCF record "
2313 "(x%x) with new FCF record (x%x)\n",
2314 fcf_rec->fcf_indx,
2315 bf_get(lpfc_fcf_record_fcf_index,
2316 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002317 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2318 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002319 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002320 goto read_next_fcf;
2321 }
2322 /*
James Smartecfd03c2010-02-12 14:41:27 -05002323 * If the driver FCF record has boot flag set and the
2324 * new hba FCF record does not have boot flag, read
2325 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002326 */
James Smartecfd03c2010-02-12 14:41:27 -05002327 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002328 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002329 goto read_next_fcf;
2330 }
2331 /*
James Smartecfd03c2010-02-12 14:41:27 -05002332 * If the new hba FCF record has lower priority value
2333 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002334 */
James Smartfc2b9892010-02-26 14:15:29 -05002335 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002336 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002337 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2338 "2838 Update current FCF record "
2339 "(x%x) with new FCF record (x%x)\n",
2340 fcf_rec->fcf_indx,
2341 bf_get(lpfc_fcf_record_fcf_index,
2342 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002343 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2344 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002345 /* Reset running random FCF selection count */
2346 phba->fcf.eligible_fcf_cnt = 1;
2347 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2348 /* Update running random FCF selection count */
2349 phba->fcf.eligible_fcf_cnt++;
2350 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2351 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002352 if (select_new_fcf) {
2353 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2354 "2839 Update current FCF record "
2355 "(x%x) with new FCF record (x%x)\n",
2356 fcf_rec->fcf_indx,
2357 bf_get(lpfc_fcf_record_fcf_index,
2358 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002359 /* Choose the new FCF by random selection */
2360 __lpfc_update_fcf_record(phba, fcf_rec,
2361 new_fcf_record,
2362 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002363 }
James Smart6fb120a2009-05-22 14:52:59 -04002364 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002365 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002366 goto read_next_fcf;
2367 }
2368 /*
James Smartecfd03c2010-02-12 14:41:27 -05002369 * This is the first suitable FCF record, choose this record for
2370 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002371 */
James Smartecfd03c2010-02-12 14:41:27 -05002372 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002373 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002374 "2840 Update initial FCF candidate "
2375 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002376 bf_get(lpfc_fcf_record_fcf_index,
2377 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002378 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2379 addr_mode, vlan_id, (boot_flag ?
2380 BOOT_ENABLE : 0));
2381 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002382 /* Setup initial running random FCF selection count */
2383 phba->fcf.eligible_fcf_cnt = 1;
2384 /* Seeding the random number generator for random selection */
2385 seed = (uint32_t)(0xFFFFFFFF & jiffies);
2386 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04002387 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002388 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002389 goto read_next_fcf;
2390
2391read_next_fcf:
2392 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002393 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2394 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2395 /*
2396 * Case of FCF fast failover scan
2397 */
2398
2399 /*
2400 * It has not found any suitable FCF record, cancel
2401 * FCF scan inprogress, and do nothing
2402 */
2403 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002404 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002405 "2782 No suitable FCF found: "
2406 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002407 phba->fcoe_eventtag_at_fcf_scan,
2408 bf_get(lpfc_fcf_record_fcf_index,
2409 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002410 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002411 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2412 phba->hba_flag &= ~FCF_TS_INPROG;
2413 spin_unlock_irq(&phba->hbalock);
2414 /* Unregister in-use FCF and rescan */
2415 lpfc_printf_log(phba, KERN_INFO,
2416 LOG_FIP,
2417 "2864 On devloss tmo "
2418 "unreg in-use FCF and "
2419 "rescan FCF table\n");
2420 lpfc_unregister_fcf_rescan(phba);
2421 return;
2422 }
2423 /*
2424 * Let next new FCF event trigger fast failover
2425 */
2426 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002427 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002428 return;
2429 }
2430 /*
2431 * It has found a suitable FCF record that is not
2432 * the same as in-use FCF record, unregister the
2433 * in-use FCF record, replace the in-use FCF record
2434 * with the new FCF record, mark FCF fast failover
2435 * completed, and then start register the new FCF
2436 * record.
2437 */
2438
James Smart0c9ab6f2010-02-26 14:15:57 -05002439 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002440 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002441
2442 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002443 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002444 "2842 Replace in-use FCF (x%x) "
2445 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002446 phba->fcf.current_rec.fcf_indx,
2447 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002448 memcpy(&phba->fcf.current_rec,
2449 &phba->fcf.failover_rec,
2450 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002451 /*
2452 * Mark the fast FCF failover rediscovery completed
2453 * and the start of the first round of the roundrobin
2454 * FCF failover.
2455 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002456 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002457 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002458 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002459 /* Register to the new FCF record */
2460 lpfc_register_fcf(phba);
2461 } else {
2462 /*
2463 * In case of transaction period to fast FCF failover,
2464 * do nothing when search to the end of the FCF table.
2465 */
2466 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2467 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2468 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002469
James Smart7d791df2011-07-22 18:37:52 -04002470 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2471 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002472 /*
2473 * In case the current in-use FCF record no
2474 * longer existed during FCF discovery that
2475 * was not triggered by fast FCF failover
2476 * process, treat it as fast FCF failover.
2477 */
2478 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2479 "2841 In-use FCF record (x%x) "
2480 "not reported, entering fast "
2481 "FCF failover mode scanning.\n",
2482 phba->fcf.current_rec.fcf_indx);
2483 spin_lock_irq(&phba->hbalock);
2484 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2485 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002486 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2487 LPFC_FCOE_FCF_GET_FIRST);
2488 return;
2489 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002490 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002491 lpfc_register_fcf(phba);
2492 }
2493 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002494 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002495 return;
2496
2497out:
2498 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2499 lpfc_register_fcf(phba);
2500
2501 return;
2502}
2503
2504/**
James Smarta93ff372010-10-22 11:06:08 -04002505 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002506 * @phba: pointer to lpfc hba data structure.
2507 * @mboxq: pointer to mailbox object.
2508 *
James Smarta93ff372010-10-22 11:06:08 -04002509 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002510 * read FCF record mailbox command from the eligible FCF record bmask for
2511 * performing the failover. If the FCF read back is not valid/available, it
2512 * fails through to retrying FLOGI to the currently registered FCF again.
2513 * Otherwise, if the FCF read back is valid and available, it will set the
2514 * newly read FCF record to the failover FCF record, unregister currently
2515 * registered FCF record, copy the failover FCF record to the current
2516 * FCF record, and then register the current FCF record before proceeding
2517 * to trying FLOGI on the new failover FCF.
2518 */
2519void
2520lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2521{
2522 struct fcf_record *new_fcf_record;
2523 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002524 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002525 uint16_t current_fcf_index;
2526 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002527 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002528
James Smarta93ff372010-10-22 11:06:08 -04002529 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002530 if (phba->link_state < LPFC_LINK_UP) {
2531 spin_lock_irq(&phba->hbalock);
2532 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002533 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002534 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002535 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002536 }
2537
2538 /* Parse the FCF record from the non-embedded mailbox command */
2539 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2540 &next_fcf_index);
2541 if (!new_fcf_record) {
2542 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2543 "2766 Mailbox command READ_FCF_RECORD "
2544 "failed to retrieve a FCF record.\n");
James Smarta93ff372010-10-22 11:06:08 -04002545 goto error_out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002546 }
2547
2548 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002549 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2550 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002551
2552 /* Log the FCF record information if turned on */
2553 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2554 next_fcf_index);
2555
James Smarta93ff372010-10-22 11:06:08 -04002556 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2557 if (!rc) {
2558 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2559 "2848 Remove ineligible FCF (x%x) from "
2560 "from roundrobin bmask\n", fcf_index);
2561 /* Clear roundrobin bmask bit for ineligible FCF */
2562 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2563 /* Perform next round of roundrobin FCF failover */
2564 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2565 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2566 if (rc)
2567 goto out;
2568 goto error_out;
2569 }
2570
2571 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2572 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2573 "2760 Perform FLOGI roundrobin FCF failover: "
2574 "FCF (x%x) back to FCF (x%x)\n",
2575 phba->fcf.current_rec.fcf_indx, fcf_index);
2576 /* Wait 500 ms before retrying FLOGI to current FCF */
2577 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002578 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002579 goto out;
2580 }
2581
James Smart0c9ab6f2010-02-26 14:15:57 -05002582 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002583 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002584 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2585 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002586 spin_lock_irq(&phba->hbalock);
2587 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2588 new_fcf_record, addr_mode, vlan_id,
2589 (boot_flag ? BOOT_ENABLE : 0));
2590 spin_unlock_irq(&phba->hbalock);
2591
2592 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2593
2594 /* Unregister the current in-use FCF record */
2595 lpfc_unregister_fcf(phba);
2596
2597 /* Replace in-use record with the new record */
2598 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2599 sizeof(struct lpfc_fcf_rec));
2600
2601 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002602 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2603 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002604
James Smarta93ff372010-10-22 11:06:08 -04002605error_out:
2606 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002607out:
2608 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002609}
2610
2611/**
2612 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2613 * @phba: pointer to lpfc hba data structure.
2614 * @mboxq: pointer to mailbox object.
2615 *
2616 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002617 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002618 * failover when a new FCF event happened. If the FCF read back is
2619 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002620 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002621 */
2622void
2623lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2624{
2625 struct fcf_record *new_fcf_record;
2626 uint32_t boot_flag, addr_mode;
2627 uint16_t fcf_index, next_fcf_index;
2628 uint16_t vlan_id;
2629 int rc;
2630
2631 /* If link state is not up, no need to proceed */
2632 if (phba->link_state < LPFC_LINK_UP)
2633 goto out;
2634
2635 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002636 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002637 goto out;
2638
2639 /* Parse the FCF record from the non-embedded mailbox command */
2640 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2641 &next_fcf_index);
2642 if (!new_fcf_record) {
2643 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2644 "2767 Mailbox command READ_FCF_RECORD "
2645 "failed to retrieve a FCF record.\n");
2646 goto out;
2647 }
2648
2649 /* Check the connection list for eligibility */
2650 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2651 &addr_mode, &vlan_id);
2652
2653 /* Log the FCF record information if turned on */
2654 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2655 next_fcf_index);
2656
2657 if (!rc)
2658 goto out;
2659
2660 /* Update the eligible FCF record index bmask */
2661 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002662
2663 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002664
2665out:
2666 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2667}
2668
2669/**
James Smart76a95d72010-11-20 23:11:48 -05002670 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2671 * @phba: pointer to lpfc hba data structure.
2672 * @mboxq: pointer to mailbox data structure.
2673 *
2674 * This function handles completion of init vfi mailbox command.
2675 */
2676void
2677lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2678{
2679 struct lpfc_vport *vport = mboxq->vport;
2680
James Smartdf9e1b52011-12-13 13:22:17 -05002681 /*
2682 * VFI not supported on interface type 0, just do the flogi
2683 * Also continue if the VFI is in use - just use the same one.
2684 */
2685 if (mboxq->u.mb.mbxStatus &&
2686 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2687 LPFC_SLI_INTF_IF_TYPE_0) &&
2688 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002689 lpfc_printf_vlog(vport, KERN_ERR,
2690 LOG_MBOX,
2691 "2891 Init VFI mailbox failed 0x%x\n",
2692 mboxq->u.mb.mbxStatus);
2693 mempool_free(mboxq, phba->mbox_mem_pool);
2694 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2695 return;
2696 }
James Smart73d91e52011-10-10 21:32:10 -04002697
James Smart76a95d72010-11-20 23:11:48 -05002698 lpfc_initial_flogi(vport);
2699 mempool_free(mboxq, phba->mbox_mem_pool);
2700 return;
2701}
2702
2703/**
2704 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2705 * @vport: pointer to lpfc_vport data structure.
2706 *
2707 * This function issue a init_vfi mailbox command to initialize the VFI and
2708 * VPI for the physical port.
2709 */
2710void
2711lpfc_issue_init_vfi(struct lpfc_vport *vport)
2712{
2713 LPFC_MBOXQ_t *mboxq;
2714 int rc;
2715 struct lpfc_hba *phba = vport->phba;
2716
2717 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2718 if (!mboxq) {
2719 lpfc_printf_vlog(vport, KERN_ERR,
2720 LOG_MBOX, "2892 Failed to allocate "
2721 "init_vfi mailbox\n");
2722 return;
2723 }
2724 lpfc_init_vfi(mboxq, vport);
2725 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2726 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2727 if (rc == MBX_NOT_FINISHED) {
2728 lpfc_printf_vlog(vport, KERN_ERR,
2729 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2730 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2731 }
2732}
2733
2734/**
James Smart1c6834a2009-07-19 10:01:26 -04002735 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2736 * @phba: pointer to lpfc hba data structure.
2737 * @mboxq: pointer to mailbox data structure.
2738 *
2739 * This function handles completion of init vpi mailbox command.
2740 */
James Smart695a8142010-01-26 23:08:03 -05002741void
James Smart1c6834a2009-07-19 10:01:26 -04002742lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2743{
2744 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002745 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002746 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2747
James Smart1c6834a2009-07-19 10:01:26 -04002748 if (mboxq->u.mb.mbxStatus) {
2749 lpfc_printf_vlog(vport, KERN_ERR,
2750 LOG_MBOX,
2751 "2609 Init VPI mailbox failed 0x%x\n",
2752 mboxq->u.mb.mbxStatus);
2753 mempool_free(mboxq, phba->mbox_mem_pool);
2754 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2755 return;
2756 }
James Smart72100cc2010-02-12 14:43:01 -05002757 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002758 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002759 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002760
James Smart695a8142010-01-26 23:08:03 -05002761 /* If this port is physical port or FDISC is done, do reg_vpi */
2762 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2763 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2764 if (!ndlp)
2765 lpfc_printf_vlog(vport, KERN_ERR,
2766 LOG_DISCOVERY,
2767 "2731 Cannot find fabric "
2768 "controller node\n");
2769 else
2770 lpfc_register_new_vport(phba, vport, ndlp);
2771 mempool_free(mboxq, phba->mbox_mem_pool);
2772 return;
2773 }
2774
James Smart1c6834a2009-07-19 10:01:26 -04002775 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2776 lpfc_initial_fdisc(vport);
2777 else {
2778 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002779 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2780 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002781 }
James Smart695a8142010-01-26 23:08:03 -05002782 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002783 return;
2784}
2785
2786/**
James Smartecfd03c2010-02-12 14:41:27 -05002787 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2788 * @vport: pointer to lpfc_vport data structure.
2789 *
2790 * This function issue a init_vpi mailbox command to initialize
2791 * VPI for the vport.
2792 */
2793void
2794lpfc_issue_init_vpi(struct lpfc_vport *vport)
2795{
2796 LPFC_MBOXQ_t *mboxq;
2797 int rc;
2798
2799 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2800 if (!mboxq) {
2801 lpfc_printf_vlog(vport, KERN_ERR,
2802 LOG_MBOX, "2607 Failed to allocate "
2803 "init_vpi mailbox\n");
2804 return;
2805 }
2806 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2807 mboxq->vport = vport;
2808 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2809 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2810 if (rc == MBX_NOT_FINISHED) {
2811 lpfc_printf_vlog(vport, KERN_ERR,
2812 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2813 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2814 }
2815}
2816
2817/**
James Smart6fb120a2009-05-22 14:52:59 -04002818 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2819 * @phba: pointer to lpfc hba data structure.
2820 *
2821 * This function loops through the list of vports on the @phba and issues an
2822 * FDISC if possible.
2823 */
2824void
2825lpfc_start_fdiscs(struct lpfc_hba *phba)
2826{
2827 struct lpfc_vport **vports;
2828 int i;
2829
2830 vports = lpfc_create_vport_work_array(phba);
2831 if (vports != NULL) {
2832 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2833 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2834 continue;
2835 /* There are no vpi for this vport */
2836 if (vports[i]->vpi > phba->max_vpi) {
2837 lpfc_vport_set_state(vports[i],
2838 FC_VPORT_FAILED);
2839 continue;
2840 }
James Smart76a95d72010-11-20 23:11:48 -05002841 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002842 lpfc_vport_set_state(vports[i],
2843 FC_VPORT_LINKDOWN);
2844 continue;
2845 }
James Smart1c6834a2009-07-19 10:01:26 -04002846 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002847 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002848 continue;
2849 }
James Smart6fb120a2009-05-22 14:52:59 -04002850 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2851 lpfc_initial_fdisc(vports[i]);
2852 else {
2853 lpfc_vport_set_state(vports[i],
2854 FC_VPORT_NO_FABRIC_SUPP);
2855 lpfc_printf_vlog(vports[i], KERN_ERR,
2856 LOG_ELS,
2857 "0259 No NPIV "
2858 "Fabric support\n");
2859 }
2860 }
2861 }
2862 lpfc_destroy_vport_work_array(phba, vports);
2863}
2864
2865void
2866lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2867{
2868 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2869 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002870 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002871
James Smartf5eca9b2012-03-01 22:37:54 -05002872 /*
2873 * VFI not supported for interface type 0, so ignore any mailbox
2874 * error (except VFI in use) and continue with the discovery.
2875 */
2876 if (mboxq->u.mb.mbxStatus &&
2877 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2878 LPFC_SLI_INTF_IF_TYPE_0) &&
2879 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002880 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2881 "2018 REG_VFI mbxStatus error x%x "
2882 "HBA state x%x\n",
2883 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002884 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002885 /* FLOGI failed, use loop map to make discovery list */
2886 lpfc_disc_list_loopmap(vport);
2887 /* Start discovery */
2888 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002889 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002890 }
2891 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002892 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002893 }
James Smartae05ebe2013-03-01 16:35:38 -05002894
2895 /* If the VFI is already registered, there is nothing else to do */
2896 if (vport->fc_flag & FC_VFI_REGISTERED)
2897 goto out_free_mem;
2898
James Smartc8685952009-11-18 15:39:16 -05002899 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002900 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002901 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002902 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002903 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002904 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002905 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002906
James Smart1b511972011-12-13 13:23:09 -05002907 /* In case SLI4 FC loopback test, we are ready */
2908 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2909 (phba->link_flag & LS_LOOPBACK_MODE)) {
2910 phba->link_state = LPFC_HBA_READY;
2911 goto out_free_mem;
2912 }
2913
James Smart6fb120a2009-05-22 14:52:59 -04002914 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002915 /*
2916 * For private loop or for NPort pt2pt,
2917 * just start discovery and we are done.
2918 */
2919 if ((vport->fc_flag & FC_PT2PT) ||
2920 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2921 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2922
James Smart76a95d72010-11-20 23:11:48 -05002923 /* Use loop map to make discovery list */
2924 lpfc_disc_list_loopmap(vport);
2925 /* Start discovery */
2926 lpfc_disc_start(vport);
2927 } else {
2928 lpfc_start_fdiscs(phba);
2929 lpfc_do_scr_ns_plogi(phba, vport);
2930 }
James Smart6fb120a2009-05-22 14:52:59 -04002931 }
2932
James Smart1b511972011-12-13 13:23:09 -05002933out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002934 mempool_free(mboxq, phba->mbox_mem_pool);
2935 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2936 kfree(dmabuf);
2937 return;
2938}
2939
2940static void
James Smart2e0fef82007-06-17 19:56:36 -05002941lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002942{
James Smart6fb120a2009-05-22 14:52:59 -04002943 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002944 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002945 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002946
2947
2948 /* Check for error */
2949 if (mb->mbxStatus) {
2950 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002951 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2952 "0319 READ_SPARAM mbxStatus error x%x "
2953 "hba state x%x>\n",
2954 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002955 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002956 goto out;
2957 }
2958
James Smart2e0fef82007-06-17 19:56:36 -05002959 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002960 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04002961 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002962 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2963 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2964 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2965 }
2966
dea31012005-04-17 16:05:31 -05002967 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2968 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002969 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002970 return;
2971
2972out:
2973 pmb->context1 = NULL;
2974 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2975 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002976 lpfc_issue_clear_la(phba, vport);
2977 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002978 return;
2979}
2980
2981static void
James Smart76a95d72010-11-20 23:11:48 -05002982lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05002983{
James Smart92d7f7b2007-06-17 19:56:38 -05002984 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002985 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05002986 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05002987 int i;
James Smart14691152006-12-02 13:34:28 -05002988 struct lpfc_dmabuf *mp;
2989 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002990 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002991
James Smart92d7f7b2007-06-17 19:56:38 -05002992 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05002993 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
2994 case LPFC_LINK_SPEED_1GHZ:
2995 case LPFC_LINK_SPEED_2GHZ:
2996 case LPFC_LINK_SPEED_4GHZ:
2997 case LPFC_LINK_SPEED_8GHZ:
2998 case LPFC_LINK_SPEED_10GHZ:
2999 case LPFC_LINK_SPEED_16GHZ:
3000 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04003001 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003002 default:
James Smart76a95d72010-11-20 23:11:48 -05003003 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05003004 break;
dea31012005-04-17 16:05:31 -05003005 }
3006
James Smart76a95d72010-11-20 23:11:48 -05003007 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003008 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003009
James Smartdf9e1b52011-12-13 13:22:17 -05003010 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003011 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003012 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003013
James Smart6a9c52c2009-10-02 15:16:51 -04003014 /* if npiv is enabled and this adapter supports npiv log
3015 * a message that npiv is not supported in this topology
3016 */
3017 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003018 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3019 "1309 Link Up Event npiv not supported in loop "
3020 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003021 /* Get Loop Map information */
James Smartdf9e1b52011-12-13 13:22:17 -05003022 if (bf_get(lpfc_mbx_read_top_il, la)) {
James Smart72859902012-01-18 16:25:38 -05003023 spin_lock(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003024 vport->fc_flag |= FC_LBIT;
James Smart72859902012-01-18 16:25:38 -05003025 spin_unlock(shost->host_lock);
James Smartdf9e1b52011-12-13 13:22:17 -05003026 }
dea31012005-04-17 16:05:31 -05003027
James Smart76a95d72010-11-20 23:11:48 -05003028 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3029 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003030
3031 if (i == 0) {
3032 phba->alpa_map[0] = 0;
3033 } else {
James Smarte8b62012007-08-02 11:10:09 -04003034 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003035 int numalpa, j, k;
3036 union {
3037 uint8_t pamap[16];
3038 struct {
3039 uint32_t wd1;
3040 uint32_t wd2;
3041 uint32_t wd3;
3042 uint32_t wd4;
3043 } pa;
3044 } un;
3045 numalpa = phba->alpa_map[0];
3046 j = 0;
3047 while (j < numalpa) {
3048 memset(un.pamap, 0, 16);
3049 for (k = 1; j < numalpa; k++) {
3050 un.pamap[k - 1] =
3051 phba->alpa_map[j + 1];
3052 j++;
3053 if (k == 16)
3054 break;
3055 }
3056 /* Link Up Event ALPA map */
3057 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003058 KERN_WARNING,
3059 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003060 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003061 "ALPA map Data: x%x "
3062 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003063 un.pa.wd1, un.pa.wd2,
3064 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003065 }
3066 }
3067 }
3068 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003069 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003070 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003071 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003072 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3073 }
James Smart2e0fef82007-06-17 19:56:36 -05003074 vport->fc_myDID = phba->fc_pref_DID;
James Smart72859902012-01-18 16:25:38 -05003075 spin_lock(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003076 vport->fc_flag |= FC_LBIT;
James Smart72859902012-01-18 16:25:38 -05003077 spin_unlock(shost->host_lock);
dea31012005-04-17 16:05:31 -05003078 }
James Smart92d7f7b2007-06-17 19:56:38 -05003079 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003080
3081 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003082 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3083 if (!sparam_mbox)
3084 goto out;
3085
3086 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3087 if (rc) {
3088 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3089 goto out;
3090 }
3091 sparam_mbox->vport = vport;
3092 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3093 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3094 if (rc == MBX_NOT_FINISHED) {
3095 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3096 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3097 kfree(mp);
3098 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3099 goto out;
dea31012005-04-17 16:05:31 -05003100 }
3101
James Smart76a95d72010-11-20 23:11:48 -05003102 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003103 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3104 if (!cfglink_mbox)
3105 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003106 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003107 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003108 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003109 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003110 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003111 if (rc == MBX_NOT_FINISHED) {
3112 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3113 goto out;
3114 }
3115 } else {
James Smart32b97932009-07-19 10:01:21 -04003116 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003117 /*
3118 * Add the driver's default FCF record at FCF index 0 now. This
3119 * is phase 1 implementation that support FCF index 0 and driver
3120 * defaults.
3121 */
James Smart45ed1192009-10-02 15:17:02 -04003122 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003123 fcf_record = kzalloc(sizeof(struct fcf_record),
3124 GFP_KERNEL);
3125 if (unlikely(!fcf_record)) {
3126 lpfc_printf_log(phba, KERN_ERR,
3127 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003128 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003129 "fcf record\n");
3130 rc = -ENODEV;
3131 goto out;
3132 }
3133
3134 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3135 LPFC_FCOE_FCF_DEF_INDEX);
3136 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3137 if (unlikely(rc)) {
3138 lpfc_printf_log(phba, KERN_ERR,
3139 LOG_MBOX | LOG_SLI,
3140 "2013 Could not manually add FCF "
3141 "record 0, status %d\n", rc);
3142 rc = -ENODEV;
3143 kfree(fcf_record);
3144 goto out;
3145 }
3146 kfree(fcf_record);
3147 }
3148 /*
3149 * The driver is expected to do FIP/FCF. Call the port
3150 * and get the FCF Table.
3151 */
James Smart32b97932009-07-19 10:01:21 -04003152 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003153 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003154 spin_unlock_irq(&phba->hbalock);
3155 return;
3156 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003157 /* This is the initial FCF discovery scan */
3158 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003159 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003160 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3161 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003162 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3163 LPFC_FCOE_FCF_GET_FIRST);
3164 if (rc) {
3165 spin_lock_irq(&phba->hbalock);
3166 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3167 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003168 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003169 }
James Smart38b92ef2010-08-04 16:11:39 -04003170 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003171 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003172 }
James Smart6fb120a2009-05-22 14:52:59 -04003173
3174 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003175out:
3176 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003177 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3178 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3179 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003180 lpfc_issue_clear_la(phba, vport);
3181 return;
dea31012005-04-17 16:05:31 -05003182}
3183
3184static void
James Smart84774a42008-08-24 21:50:06 -04003185lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003186{
dea31012005-04-17 16:05:31 -05003187 uint32_t control;
3188 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003189 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003190 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003191 if (phba->sli_rev <= LPFC_SLI_REV3) {
3192 control = readl(phba->HCregaddr);
3193 control |= HC_LAINT_ENA;
3194 writel(control, phba->HCregaddr);
3195 readl(phba->HCregaddr); /* flush */
3196 }
James Smart2e0fef82007-06-17 19:56:36 -05003197 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003198}
3199
James Smart84774a42008-08-24 21:50:06 -04003200static void
3201lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3202{
3203 lpfc_linkdown(phba);
3204 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003205 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003206 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3207}
3208
3209
dea31012005-04-17 16:05:31 -05003210/*
James Smart76a95d72010-11-20 23:11:48 -05003211 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003212 * command upon completion. It is setup in the LPFC_MBOXQ
3213 * as the completion routine when the command is
3214 * handed off to the SLI layer.
3215 */
3216void
James Smart76a95d72010-11-20 23:11:48 -05003217lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003218{
James Smart2e0fef82007-06-17 19:56:36 -05003219 struct lpfc_vport *vport = pmb->vport;
3220 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003221 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003222 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003223 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3224
James Smart0d2b6b82008-06-14 22:52:47 -04003225 /* Unblock ELS traffic */
3226 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003227 /* Check for error */
3228 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003229 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003230 "1307 READ_LA mbox error x%x state x%x\n",
3231 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003232 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003233 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003234 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003235 }
3236
James Smart76a95d72010-11-20 23:11:48 -05003237 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003238
3239 memcpy(&phba->alpa_map[0], mp->virt, 128);
3240
James Smart2e0fef82007-06-17 19:56:36 -05003241 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003242 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003243 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003244 else
James Smart2e0fef82007-06-17 19:56:36 -05003245 vport->fc_flag &= ~FC_BYPASSED_MODE;
3246 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003247
James Smart0c287582009-06-10 17:22:56 -04003248 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05003249 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05003250 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003251 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003252 if (phba->fc_eventTag != 0)
3253 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003254 }
dea31012005-04-17 16:05:31 -05003255
3256 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05003257 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003258 if (bf_get(lpfc_mbx_read_top_mm, la))
James Smart84774a42008-08-24 21:50:06 -04003259 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3260 else
3261 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05003262 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003263
James Smart4d9ab992009-10-02 15:16:39 -04003264 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003265 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
3266 (!bf_get(lpfc_mbx_read_top_mm, la))) {
dea31012005-04-17 16:05:31 -05003267 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003268 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003269 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003270 "1306 Link Up Event in loop back mode "
3271 "x%x received Data: x%x x%x x%x x%x\n",
3272 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003273 bf_get(lpfc_mbx_read_top_alpa_granted,
3274 la),
3275 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003276 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003277 } else {
3278 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003279 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003280 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003281 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003282 bf_get(lpfc_mbx_read_top_alpa_granted,
3283 la),
3284 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003285 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003286 bf_get(lpfc_mbx_read_top_mm, la),
3287 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003288 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003289 }
James Smart92d7f7b2007-06-17 19:56:38 -05003290 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003291 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3292 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003293 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003294 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003295 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3296 "1308 Link Down Event in loop back mode "
3297 "x%x received "
3298 "Data: x%x x%x x%x\n",
3299 la->eventTag, phba->fc_eventTag,
3300 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003301 else
James Smart3163f722008-02-08 18:50:25 -05003302 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003303 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003304 "Data: x%x x%x x%x x%x x%x\n",
3305 la->eventTag, phba->fc_eventTag,
3306 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003307 bf_get(lpfc_mbx_read_top_mm, la),
3308 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003309 lpfc_mbx_issue_link_down(phba);
3310 }
James Smart76a95d72010-11-20 23:11:48 -05003311 if ((bf_get(lpfc_mbx_read_top_mm, la)) &&
3312 (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)) {
James Smart84774a42008-08-24 21:50:06 -04003313 if (phba->link_state != LPFC_LINK_DOWN) {
3314 phba->fc_stat.LinkDown++;
3315 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3316 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003317 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003318 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003319 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003320 lpfc_mbx_issue_link_down(phba);
3321 } else
3322 lpfc_enable_la(phba);
3323
3324 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3325 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3326 "Data: x%x x%x x%x\n",
3327 la->eventTag, phba->fc_eventTag,
3328 phba->pport->port_state, vport->fc_flag);
3329 /*
3330 * The cmnd that triggered this will be waiting for this
3331 * signal.
3332 */
3333 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3334 if (phba->wait_4_mlo_maint_flg) {
3335 phba->wait_4_mlo_maint_flg = 0;
3336 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003337 }
James Smart84774a42008-08-24 21:50:06 -04003338 }
3339
James Smart76a95d72010-11-20 23:11:48 -05003340 if (bf_get(lpfc_mbx_read_top_fa, la)) {
3341 if (bf_get(lpfc_mbx_read_top_mm, la))
James Smart84774a42008-08-24 21:50:06 -04003342 lpfc_issue_clear_la(phba, vport);
3343 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003344 "1311 fa %d\n",
3345 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003346 }
3347
James Smart76a95d72010-11-20 23:11:48 -05003348lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003349 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3350 kfree(mp);
3351 mempool_free(pmb, phba->mbox_mem_pool);
3352 return;
3353}
3354
3355/*
3356 * This routine handles processing a REG_LOGIN mailbox
3357 * command upon completion. It is setup in the LPFC_MBOXQ
3358 * as the completion routine when the command is
3359 * handed off to the SLI layer.
3360 */
3361void
James Smart2e0fef82007-06-17 19:56:36 -05003362lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003363{
James Smart2e0fef82007-06-17 19:56:36 -05003364 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003365 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003366 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003368
dea31012005-04-17 16:05:31 -05003369 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003370 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003371
James Smartffc95492010-06-07 15:23:17 -04003372 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3373 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3374
James Smart40426292010-12-15 17:58:10 -05003375 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3376 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003377 /* We rcvd a rscn after issuing this
3378 * mbox reg login, we may have cycled
3379 * back through the state and be
3380 * back at reg login state so this
3381 * mbox needs to be ignored becase
3382 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003383 * process.
James Smartffc95492010-06-07 15:23:17 -04003384 */
3385 spin_lock_irq(shost->host_lock);
3386 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3387 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003388 } else
3389 /* Good status, call state machine */
3390 lpfc_disc_state_machine(vport, ndlp, pmb,
3391 NLP_EVT_CMPL_REG_LOGIN);
3392
dea31012005-04-17 16:05:31 -05003393 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3394 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003395 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003396 /* decrement the node reference count held for this callback
3397 * function.
3398 */
James Smart329f9bc2007-04-25 09:53:01 -04003399 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003400
3401 return;
3402}
3403
James Smart92d7f7b2007-06-17 19:56:38 -05003404static void
3405lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3406{
James Smart04c68492009-05-22 14:52:52 -04003407 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003408 struct lpfc_vport *vport = pmb->vport;
3409 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3410
3411 switch (mb->mbxStatus) {
3412 case 0x0011:
3413 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003414 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3415 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3416 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003417 break;
James Smart78730cf2010-04-06 15:06:30 -04003418 /* If VPI is busy, reset the HBA */
3419 case 0x9700:
3420 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3421 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3422 vport->vpi, mb->mbxStatus);
3423 if (!(phba->pport->load_flag & FC_UNLOADING))
3424 lpfc_workq_post_event(phba, NULL, NULL,
3425 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003426 }
James Smart72100cc2010-02-12 14:43:01 -05003427 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003428 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003429 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003430 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003431 vport->unreg_vpi_cmpl = VPORT_OK;
3432 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003433 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003434 /*
3435 * This shost reference might have been taken at the beginning of
3436 * lpfc_vport_delete()
3437 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003438 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003439 scsi_host_put(shost);
3440}
3441
James Smartd7c255b2008-08-24 21:50:00 -04003442int
James Smart92d7f7b2007-06-17 19:56:38 -05003443lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3444{
3445 struct lpfc_hba *phba = vport->phba;
3446 LPFC_MBOXQ_t *mbox;
3447 int rc;
3448
3449 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3450 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003451 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003452
3453 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3454 mbox->vport = vport;
3455 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003456 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003457 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003458 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3459 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003460 mempool_free(mbox, phba->mbox_mem_pool);
3461 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003462 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003463 }
James Smartd7c255b2008-08-24 21:50:00 -04003464 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003465}
3466
3467static void
3468lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3469{
3470 struct lpfc_vport *vport = pmb->vport;
3471 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003472 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003473
3474 switch (mb->mbxStatus) {
3475 case 0x0011:
3476 case 0x9601:
3477 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003478 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3479 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3480 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003481 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3482 spin_lock_irq(shost->host_lock);
3483 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3484 spin_unlock_irq(shost->host_lock);
3485 vport->fc_myDID = 0;
3486 goto out;
3487 }
James Smart92d7f7b2007-06-17 19:56:38 -05003488
James Smart72100cc2010-02-12 14:43:01 -05003489 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003490 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003491 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003492 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003493 vport->num_disc_nodes = 0;
3494 /* go thru NPR list and issue ELS PLOGIs */
3495 if (vport->fc_npr_cnt)
3496 lpfc_els_disc_plogi(vport);
3497
3498 if (!vport->num_disc_nodes) {
3499 spin_lock_irq(shost->host_lock);
3500 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3501 spin_unlock_irq(shost->host_lock);
3502 lpfc_can_disctmo(vport);
3503 }
3504 vport->port_state = LPFC_VPORT_READY;
3505
3506out:
3507 mempool_free(pmb, phba->mbox_mem_pool);
3508 return;
3509}
3510
James Smart21e9a0a2009-05-22 14:53:21 -04003511/**
3512 * lpfc_create_static_vport - Read HBA config region to create static vports.
3513 * @phba: pointer to lpfc hba data structure.
3514 *
3515 * This routine issue a DUMP mailbox command for config region 22 to get
3516 * the list of static vports to be created. The function create vports
3517 * based on the information returned from the HBA.
3518 **/
3519void
3520lpfc_create_static_vport(struct lpfc_hba *phba)
3521{
3522 LPFC_MBOXQ_t *pmb = NULL;
3523 MAILBOX_t *mb;
3524 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003525 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003526 struct fc_vport_identifiers vport_id;
3527 struct fc_vport *new_fc_vport;
3528 struct Scsi_Host *shost;
3529 struct lpfc_vport *vport;
3530 uint16_t offset = 0;
3531 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003532 struct lpfc_dmabuf *mp;
3533 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003534
3535 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3536 if (!pmb) {
3537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3538 "0542 lpfc_create_static_vport failed to"
3539 " allocate mailbox memory\n");
3540 return;
3541 }
James Smartcdcc2342012-08-14 14:26:35 -04003542 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003543 mb = &pmb->u.mb;
3544
3545 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3546 if (!vport_info) {
3547 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3548 "0543 lpfc_create_static_vport failed to"
3549 " allocate vport_info\n");
3550 mempool_free(pmb, phba->mbox_mem_pool);
3551 return;
3552 }
3553
3554 vport_buff = (uint8_t *) vport_info;
3555 do {
James Smartcdcc2342012-08-14 14:26:35 -04003556 /* free dma buffer from previous round */
3557 if (pmb->context1) {
3558 mp = (struct lpfc_dmabuf *)pmb->context1;
3559 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3560 kfree(mp);
3561 }
James Smart1c6834a2009-07-19 10:01:26 -04003562 if (lpfc_dump_static_vport(phba, pmb, offset))
3563 goto out;
3564
James Smart21e9a0a2009-05-22 14:53:21 -04003565 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003566 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3567 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003568
James Smartcdcc2342012-08-14 14:26:35 -04003569 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003570 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3571 "0544 lpfc_create_static_vport failed to"
3572 " issue dump mailbox command ret 0x%x "
3573 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003574 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003575 goto out;
3576 }
3577
James Smart1c6834a2009-07-19 10:01:26 -04003578 if (phba->sli_rev == LPFC_SLI_REV4) {
3579 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003580 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003581 if (byte_count > sizeof(struct static_vport_info) -
3582 offset)
3583 byte_count = sizeof(struct static_vport_info)
3584 - offset;
3585 memcpy(vport_buff + offset, mp->virt, byte_count);
3586 offset += byte_count;
3587 } else {
3588 if (mb->un.varDmp.word_cnt >
3589 sizeof(struct static_vport_info) - offset)
3590 mb->un.varDmp.word_cnt =
3591 sizeof(struct static_vport_info)
3592 - offset;
3593 byte_count = mb->un.varDmp.word_cnt;
3594 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3595 vport_buff + offset,
3596 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003597
James Smart1c6834a2009-07-19 10:01:26 -04003598 offset += byte_count;
3599 }
James Smart21e9a0a2009-05-22 14:53:21 -04003600
James Smart1c6834a2009-07-19 10:01:26 -04003601 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003602 offset < sizeof(struct static_vport_info));
3603
3604
3605 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3606 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3607 != VPORT_INFO_REV)) {
3608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3609 "0545 lpfc_create_static_vport bad"
3610 " information header 0x%x 0x%x\n",
3611 le32_to_cpu(vport_info->signature),
3612 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3613
3614 goto out;
3615 }
3616
3617 shost = lpfc_shost_from_vport(phba->pport);
3618
3619 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3620 memset(&vport_id, 0, sizeof(vport_id));
3621 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3622 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3623 if (!vport_id.port_name || !vport_id.node_name)
3624 continue;
3625
3626 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3627 vport_id.vport_type = FC_PORTTYPE_NPIV;
3628 vport_id.disable = false;
3629 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3630
3631 if (!new_fc_vport) {
3632 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3633 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003634 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003635 continue;
3636 }
3637
3638 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3639 vport->vport_flag |= STATIC_VPORT;
3640 }
3641
3642out:
James Smart21e9a0a2009-05-22 14:53:21 -04003643 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003644 if (mbx_wait_rc != MBX_TIMEOUT) {
3645 if (pmb->context1) {
3646 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003647 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3648 kfree(mp);
3649 }
James Smart21e9a0a2009-05-22 14:53:21 -04003650 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003651 }
James Smart21e9a0a2009-05-22 14:53:21 -04003652
3653 return;
3654}
3655
dea31012005-04-17 16:05:31 -05003656/*
3657 * This routine handles processing a Fabric REG_LOGIN mailbox
3658 * command upon completion. It is setup in the LPFC_MBOXQ
3659 * as the completion routine when the command is
3660 * handed off to the SLI layer.
3661 */
3662void
James Smart2e0fef82007-06-17 19:56:36 -05003663lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003664{
James Smart92d7f7b2007-06-17 19:56:38 -05003665 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003666 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003667 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003668 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003669 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003670
James Smart549e55c2007-08-02 11:09:51 -04003671 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003672 pmb->context1 = NULL;
3673 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003674
dea31012005-04-17 16:05:31 -05003675 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003676 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3677 "0258 Register Fabric login error: 0x%x\n",
3678 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003679 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3680 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003681 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003682
James Smart76a95d72010-11-20 23:11:48 -05003683 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003684 /* FLOGI failed, use loop map to make discovery list */
3685 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003686
James Smart92d7f7b2007-06-17 19:56:38 -05003687 /* Start discovery */
3688 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003689 /* Decrement the reference count to ndlp after the
3690 * reference to the ndlp are done.
3691 */
3692 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003693 return;
3694 }
3695
3696 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003697 /* Decrement the reference count to ndlp after the reference
3698 * to the ndlp are done.
3699 */
3700 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003701 return;
3702 }
3703
James Smart6d368e52011-05-24 11:44:12 -04003704 if (phba->sli_rev < LPFC_SLI_REV4)
3705 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003706 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003707 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003708 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003709
James Smart2e0fef82007-06-17 19:56:36 -05003710 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003711 /* when physical port receive logo donot start
3712 * vport discovery */
3713 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3714 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003715 else {
3716 shost = lpfc_shost_from_vport(vport);
3717 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003718 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003719 spin_unlock_irq(shost->host_lock);
3720 }
James Smart92d7f7b2007-06-17 19:56:38 -05003721 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003722 }
3723
3724 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3725 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003726 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003727
3728 /* Drop the reference count from the mbox at the end after
3729 * all the current reference to the ndlp have been done.
3730 */
3731 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003732 return;
3733}
3734
3735/*
3736 * This routine handles processing a NameServer REG_LOGIN mailbox
3737 * command upon completion. It is setup in the LPFC_MBOXQ
3738 * as the completion routine when the command is
3739 * handed off to the SLI layer.
3740 */
3741void
James Smart2e0fef82007-06-17 19:56:36 -05003742lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003743{
James Smart21e9a0a2009-05-22 14:53:21 -04003744 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003745 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3746 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3747 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003748
James Smartd439d282010-09-29 11:18:45 -04003749 pmb->context1 = NULL;
3750 pmb->context2 = NULL;
3751
dea31012005-04-17 16:05:31 -05003752 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003753out:
James Smart21e9a0a2009-05-22 14:53:21 -04003754 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3755 "0260 Register NameServer error: 0x%x\n",
3756 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003757 /* decrement the node reference count held for this
3758 * callback function.
3759 */
James Smart329f9bc2007-04-25 09:53:01 -04003760 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003761 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3762 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003763 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003764
3765 /* If no other thread is using the ndlp, free it */
3766 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003767
James Smart76a95d72010-11-20 23:11:48 -05003768 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003769 /*
3770 * RegLogin failed, use loop map to make discovery
3771 * list
3772 */
3773 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003774
James Smart92d7f7b2007-06-17 19:56:38 -05003775 /* Start discovery */
3776 lpfc_disc_start(vport);
3777 return;
3778 }
3779 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003780 return;
3781 }
3782
James Smart6d368e52011-05-24 11:44:12 -04003783 if (phba->sli_rev < LPFC_SLI_REV4)
3784 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003785 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003786 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003787 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003788
James Smart2e0fef82007-06-17 19:56:36 -05003789 if (vport->port_state < LPFC_VPORT_READY) {
3790 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003791 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3792 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3793 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3794 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3795 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3796
3797 /* Issue SCR just before NameServer GID_FT Query */
3798 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003799 }
3800
James Smart2e0fef82007-06-17 19:56:36 -05003801 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003802 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003803 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003804 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003805 goto out;
dea31012005-04-17 16:05:31 -05003806 }
3807
James Smartfa4066b2008-01-11 01:53:27 -05003808 /* decrement the node reference count held for this
3809 * callback function.
3810 */
James Smart329f9bc2007-04-25 09:53:01 -04003811 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003812 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3813 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003814 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003815
3816 return;
3817}
3818
3819static void
James Smart2e0fef82007-06-17 19:56:36 -05003820lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003821{
James Smart2e0fef82007-06-17 19:56:36 -05003822 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3823 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003824 struct lpfc_rport_data *rdata;
3825 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003826 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003827
3828 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003829 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3830 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003831 rport_ids.port_id = ndlp->nlp_DID;
3832 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003833
James Smart329f9bc2007-04-25 09:53:01 -04003834 /*
3835 * We leave our node pointer in rport->dd_data when we unregister a
3836 * FCP target port. But fc_remote_port_add zeros the space to which
3837 * rport->dd_data points. So, if we're reusing a previously
3838 * registered port, drop the reference that we took the last time we
3839 * registered the port.
3840 */
3841 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003842 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003843 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003844
3845 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3846 "rport add: did:x%x flg:x%x type x%x",
3847 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3848
James Smart9589b0622011-04-16 11:03:17 -04003849 /* Don't add the remote port if unloading. */
3850 if (vport->load_flag & FC_UNLOADING)
3851 return;
3852
James Smart2e0fef82007-06-17 19:56:36 -05003853 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003854 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003855 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3856 "Warning: fc_remote_port_add failed\n");
3857 return;
3858 }
3859
3860 /* initialize static port data */
3861 rport->maxframe_size = ndlp->nlp_maxframe;
3862 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003863 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003864 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003865
3866 if (ndlp->nlp_type & NLP_FCP_TARGET)
3867 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3868 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3869 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3870
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003871 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3872 fc_remote_port_rolechg(rport, rport_ids.roles);
3873
James Smart34f5ad82012-08-03 12:35:03 -04003874 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3875 "3183 rport register x%06x, rport %p role x%x\n",
3876 ndlp->nlp_DID, rport, rport_ids.roles);
3877
James Smart071fbd3d2006-04-15 11:53:20 -04003878 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003879 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003880 ndlp->nlp_sid = rport->scsi_target_id;
3881 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003882 return;
3883}
3884
3885static void
James Smart2e0fef82007-06-17 19:56:36 -05003886lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003887{
3888 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003889
James Smart858c9f62007-06-17 19:56:39 -05003890 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3891 "rport delete: did:x%x flg:x%x type x%x",
3892 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3893
James Smart34f5ad82012-08-03 12:35:03 -04003894 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3895 "3184 rport unregister x%06x, rport %p\n",
3896 ndlp->nlp_DID, rport);
3897
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003898 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003899
3900 return;
3901}
3902
James Smartde0c5b32007-04-25 09:52:27 -04003903static void
James Smart2e0fef82007-06-17 19:56:36 -05003904lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003905{
James Smart2e0fef82007-06-17 19:56:36 -05003906 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3907
3908 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003909 switch (state) {
3910 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003911 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003912 break;
3913 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003914 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003915 break;
3916 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003917 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003918 break;
3919 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003920 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003921 break;
3922 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003923 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003924 break;
3925 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003926 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003927 break;
3928 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003929 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003930 break;
3931 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003932 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003933 break;
3934 }
James Smart2e0fef82007-06-17 19:56:36 -05003935 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003936}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003937
James Smartde0c5b32007-04-25 09:52:27 -04003938static void
James Smart2e0fef82007-06-17 19:56:36 -05003939lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003940 int old_state, int new_state)
3941{
James Smart2e0fef82007-06-17 19:56:36 -05003942 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3943
James Smartde0c5b32007-04-25 09:52:27 -04003944 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003945 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3946 ndlp->nlp_type |= NLP_FC_NODE;
3947 }
3948 if (new_state == NLP_STE_MAPPED_NODE)
3949 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3950 if (new_state == NLP_STE_NPR_NODE)
3951 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003952
James Smartde0c5b32007-04-25 09:52:27 -04003953 /* Transport interface */
3954 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3955 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003956 vport->phba->nport_event_cnt++;
3957 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003958 }
dea31012005-04-17 16:05:31 -05003959
James Smartde0c5b32007-04-25 09:52:27 -04003960 if (new_state == NLP_STE_MAPPED_NODE ||
3961 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003962 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003963 /*
3964 * Tell the fc transport about the port, if we haven't
3965 * already. If we have, and it's a scsi entity, be
3966 * sure to unblock any attached scsi devices
3967 */
3968 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003969 }
James Smartea2151b2008-09-07 11:52:10 -04003970 if ((new_state == NLP_STE_MAPPED_NODE) &&
3971 (vport->stat_data_enabled)) {
3972 /*
3973 * A new target is discovered, if there is no buffer for
3974 * statistical data collection allocate buffer.
3975 */
3976 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3977 sizeof(struct lpfc_scsicmd_bkt),
3978 GFP_KERNEL);
3979
3980 if (!ndlp->lat_data)
3981 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3982 "0286 lpfc_nlp_state_cleanup failed to "
3983 "allocate statistical data buffer DID "
3984 "0x%x\n", ndlp->nlp_DID);
3985 }
James Smart858c9f62007-06-17 19:56:39 -05003986 /*
3987 * if we added to Mapped list, but the remote port
3988 * registration failed or assigned a target id outside
3989 * our presentable range - move the node to the
3990 * Unmapped List
3991 */
James Smartde0c5b32007-04-25 09:52:27 -04003992 if (new_state == NLP_STE_MAPPED_NODE &&
3993 (!ndlp->rport ||
3994 ndlp->rport->scsi_target_id == -1 ||
3995 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003996 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003997 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003998 spin_unlock_irq(shost->host_lock);
3999 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004000 }
James Smartde0c5b32007-04-25 09:52:27 -04004001}
4002
James Smart685f0bf2007-04-25 09:53:08 -04004003static char *
4004lpfc_nlp_state_name(char *buffer, size_t size, int state)
4005{
4006 static char *states[] = {
4007 [NLP_STE_UNUSED_NODE] = "UNUSED",
4008 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4009 [NLP_STE_ADISC_ISSUE] = "ADISC",
4010 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4011 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004012 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004013 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4014 [NLP_STE_MAPPED_NODE] = "MAPPED",
4015 [NLP_STE_NPR_NODE] = "NPR",
4016 };
4017
James Smart311464e2007-08-02 11:10:37 -04004018 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004019 strlcpy(buffer, states[state], size);
4020 else
4021 snprintf(buffer, size, "unknown (%d)", state);
4022 return buffer;
4023}
4024
James Smartde0c5b32007-04-25 09:52:27 -04004025void
James Smart2e0fef82007-06-17 19:56:36 -05004026lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4027 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004028{
James Smart2e0fef82007-06-17 19:56:36 -05004029 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004030 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004031 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004032
James Smarte8b62012007-08-02 11:10:09 -04004033 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4034 "0904 NPort state transition x%06x, %s -> %s\n",
4035 ndlp->nlp_DID,
4036 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4037 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004038
4039 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4040 "node statechg did:x%x old:%d ste:%d",
4041 ndlp->nlp_DID, old_state, state);
4042
James Smartde0c5b32007-04-25 09:52:27 -04004043 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004044 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004045 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004046 if (old_state == NLP_STE_UNMAPPED_NODE) {
4047 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4048 ndlp->nlp_type &= ~NLP_FC_NODE;
4049 }
4050
James Smart685f0bf2007-04-25 09:53:08 -04004051 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004052 spin_lock_irq(shost->host_lock);
4053 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4054 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004055 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004056 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004057
4058 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004059 lpfc_nlp_counters(vport, state, 1);
4060 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004061}
4062
4063void
James Smarte47c9092008-02-08 18:49:26 -05004064lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4065{
4066 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4067
4068 if (list_empty(&ndlp->nlp_listp)) {
4069 spin_lock_irq(shost->host_lock);
4070 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4071 spin_unlock_irq(shost->host_lock);
4072 }
4073}
4074
4075void
James Smart2e0fef82007-06-17 19:56:36 -05004076lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004077{
James Smart2e0fef82007-06-17 19:56:36 -05004078 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4079
James Smart0d2b6b82008-06-14 22:52:47 -04004080 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004081 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004082 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4083 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004084 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004085 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004086 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004087 NLP_STE_UNUSED_NODE);
4088}
4089
Adrian Bunk4d9db012008-02-14 23:24:02 +02004090static void
James Smarte47c9092008-02-08 18:49:26 -05004091lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4092{
James Smart0d2b6b82008-06-14 22:52:47 -04004093 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004094 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4095 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4096 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4097 NLP_STE_UNUSED_NODE);
4098}
James Smart109f6ed2008-12-04 22:39:08 -05004099/**
James Smart3621a712009-04-06 18:47:14 -04004100 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004101 * @vport: Pointer to Virtual Port object.
4102 * @ndlp: Pointer to FC node object.
4103 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004104 *
4105 * This function is always called when node object need to be initialized.
4106 * It initializes all the fields of the node object. Although the reference
4107 * to phba from @ndlp can be obtained indirectly through it's reference to
4108 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4109 * to the life-span of the @ndlp might go beyond the existence of @vport as
4110 * the final release of ndlp is determined by its reference count. And, the
4111 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004112 **/
4113static inline void
4114lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4115 uint32_t did)
4116{
4117 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4118 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4119 init_timer(&ndlp->nlp_delayfunc);
4120 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4121 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4122 ndlp->nlp_DID = did;
4123 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004124 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004125 ndlp->nlp_sid = NLP_NO_SID;
4126 kref_init(&ndlp->kref);
4127 NLP_INT_NODE_ACT(ndlp);
4128 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004129 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart40426292010-12-15 17:58:10 -05004130 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4131 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smart109f6ed2008-12-04 22:39:08 -05004132}
James Smarte47c9092008-02-08 18:49:26 -05004133
4134struct lpfc_nodelist *
4135lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4136 int state)
4137{
4138 struct lpfc_hba *phba = vport->phba;
4139 uint32_t did;
4140 unsigned long flags;
4141
4142 if (!ndlp)
4143 return NULL;
4144
4145 spin_lock_irqsave(&phba->ndlp_lock, flags);
4146 /* The ndlp should not be in memory free mode */
4147 if (NLP_CHK_FREE_REQ(ndlp)) {
4148 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4149 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4150 "0277 lpfc_enable_node: ndlp:x%p "
4151 "usgmap:x%x refcnt:%d\n",
4152 (void *)ndlp, ndlp->nlp_usg_map,
4153 atomic_read(&ndlp->kref.refcount));
4154 return NULL;
4155 }
4156 /* The ndlp should not already be in active mode */
4157 if (NLP_CHK_NODE_ACT(ndlp)) {
4158 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4159 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4160 "0278 lpfc_enable_node: ndlp:x%p "
4161 "usgmap:x%x refcnt:%d\n",
4162 (void *)ndlp, ndlp->nlp_usg_map,
4163 atomic_read(&ndlp->kref.refcount));
4164 return NULL;
4165 }
4166
4167 /* Keep the original DID */
4168 did = ndlp->nlp_DID;
4169
4170 /* re-initialize ndlp except of ndlp linked list pointer */
4171 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4172 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004173 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004174
4175 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4176
4177 if (state != NLP_STE_UNUSED_NODE)
4178 lpfc_nlp_set_state(vport, ndlp, state);
4179
4180 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4181 "node enable: did:x%x",
4182 ndlp->nlp_DID, 0, 0);
4183 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004184}
4185
4186void
James Smart2e0fef82007-06-17 19:56:36 -05004187lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004188{
James Smart87af33f2007-10-27 13:37:43 -04004189 /*
James Smartfa4066b2008-01-11 01:53:27 -05004190 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004191 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004192 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4193 * until ALL other outstanding threads have completed. We check
4194 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004195 */
James Smartfa4066b2008-01-11 01:53:27 -05004196 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4197 return;
James Smart51ef4c22007-08-02 11:10:31 -04004198 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart1151e3e2011-02-16 12:39:35 -05004199 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4200 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004201 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004202 return;
dea31012005-04-17 16:05:31 -05004203}
4204
4205/*
4206 * Start / ReStart rescue timer for Discovery / RSCN handling
4207 */
4208void
James Smart2e0fef82007-06-17 19:56:36 -05004209lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004210{
James Smart2e0fef82007-06-17 19:56:36 -05004211 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4212 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004213 uint32_t tmo;
4214
James Smart2e0fef82007-06-17 19:56:36 -05004215 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004216 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004217 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4218 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004219 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004220 * FC spec states we need 3 * ratov for CT requests
4221 */
4222 tmo = ((phba->fc_ratov * 3) + 3);
4223 }
dea31012005-04-17 16:05:31 -05004224
James Smart858c9f62007-06-17 19:56:39 -05004225
4226 if (!timer_pending(&vport->fc_disctmo)) {
4227 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4228 "set disc timer: tmo:x%x state:x%x flg:x%x",
4229 tmo, vport->port_state, vport->fc_flag);
4230 }
4231
James Smart2e0fef82007-06-17 19:56:36 -05004232 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
4233 spin_lock_irq(shost->host_lock);
4234 vport->fc_flag |= FC_DISC_TMO;
4235 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004236
4237 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004238 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4239 "0247 Start Discovery Timer state x%x "
4240 "Data: x%x x%lx x%x x%x\n",
4241 vport->port_state, tmo,
4242 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4243 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004244
4245 return;
4246}
4247
4248/*
4249 * Cancel rescue timer for Discovery / RSCN handling
4250 */
4251int
James Smart2e0fef82007-06-17 19:56:36 -05004252lpfc_can_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);
James Smart2e0fef82007-06-17 19:56:36 -05004255 unsigned long iflags;
4256
James Smart858c9f62007-06-17 19:56:39 -05004257 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4258 "can disc timer: state:x%x rtry:x%x flg:x%x",
4259 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4260
dea31012005-04-17 16:05:31 -05004261 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004262 if (vport->fc_flag & FC_DISC_TMO) {
4263 spin_lock_irqsave(shost->host_lock, iflags);
4264 vport->fc_flag &= ~FC_DISC_TMO;
4265 spin_unlock_irqrestore(shost->host_lock, iflags);
4266 del_timer_sync(&vport->fc_disctmo);
4267 spin_lock_irqsave(&vport->work_port_lock, iflags);
4268 vport->work_port_events &= ~WORKER_DISC_TMO;
4269 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004270 }
4271
4272 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004273 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4274 "0248 Cancel Discovery Timer state x%x "
4275 "Data: x%x x%x x%x\n",
4276 vport->port_state, vport->fc_flag,
4277 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004278 return 0;
dea31012005-04-17 16:05:31 -05004279}
4280
4281/*
4282 * Check specified ring for outstanding IOCB on the SLI queue
4283 * Return true if iocb matches the specified nport
4284 */
4285int
James Smart2e0fef82007-06-17 19:56:36 -05004286lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4287 struct lpfc_sli_ring *pring,
4288 struct lpfc_iocbq *iocb,
4289 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004290{
James Smart2e0fef82007-06-17 19:56:36 -05004291 struct lpfc_sli *psli = &phba->sli;
4292 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004293 struct lpfc_vport *vport = ndlp->vport;
4294
4295 if (iocb->vport != vport)
4296 return 0;
4297
dea31012005-04-17 16:05:31 -05004298 if (pring->ringno == LPFC_ELS_RING) {
4299 switch (icmd->ulpCommand) {
4300 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004301 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004302 return 1;
dea31012005-04-17 16:05:31 -05004303 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004304 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4305 return 1;
dea31012005-04-17 16:05:31 -05004306 case CMD_XMIT_ELS_RSP64_CX:
4307 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004308 return 1;
dea31012005-04-17 16:05:31 -05004309 }
James Smarta4bc3372006-12-02 13:34:16 -05004310 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004311
4312 } else if (pring->ringno == psli->fcp_ring) {
4313 /* Skip match check if waiting to relogin to FCP target */
4314 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004315 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004316 return 0;
dea31012005-04-17 16:05:31 -05004317 }
4318 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004319 return 1;
dea31012005-04-17 16:05:31 -05004320 }
4321 } else if (pring->ringno == psli->next_ring) {
4322
4323 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004324 return 0;
dea31012005-04-17 16:05:31 -05004325}
4326
4327/*
4328 * Free resources / clean up outstanding I/Os
4329 * associated with nlp_rpi in the LPFC_NODELIST entry.
4330 */
4331static int
James Smart2e0fef82007-06-17 19:56:36 -05004332lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004333{
James Smart2534ba72007-04-25 09:52:20 -04004334 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004335 struct lpfc_sli *psli;
4336 struct lpfc_sli_ring *pring;
4337 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004338 uint32_t i;
dea31012005-04-17 16:05:31 -05004339
James Smart92d7f7b2007-06-17 19:56:38 -05004340 lpfc_fabric_abort_nport(ndlp);
4341
dea31012005-04-17 16:05:31 -05004342 /*
4343 * Everything that matches on txcmplq will be returned
4344 * by firmware with a no rpi error.
4345 */
4346 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004347 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004348 /* Now process each ring */
4349 for (i = 0; i < psli->num_rings; i++) {
4350 pring = &psli->ring[i];
4351
James Smart2e0fef82007-06-17 19:56:36 -05004352 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004353 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004354 list) {
dea31012005-04-17 16:05:31 -05004355 /*
4356 * Check to see if iocb matches the nport we are
4357 * looking for
4358 */
James Smart92d7f7b2007-06-17 19:56:38 -05004359 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4360 ndlp))) {
dea31012005-04-17 16:05:31 -05004361 /* It matches, so deque and call compl
4362 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004363 list_move_tail(&iocb->list,
4364 &completions);
dea31012005-04-17 16:05:31 -05004365 }
4366 }
James Smart2e0fef82007-06-17 19:56:36 -05004367 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004368 }
4369 }
James Smart2534ba72007-04-25 09:52:20 -04004370
James Smarta257bf92009-04-06 18:48:10 -04004371 /* Cancel all the IOCBs from the completions list */
4372 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4373 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004374
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004375 return 0;
dea31012005-04-17 16:05:31 -05004376}
4377
James Smart086a3452012-08-14 14:25:21 -04004378/**
4379 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4380 * @phba: Pointer to HBA context object.
4381 * @pmb: Pointer to mailbox object.
4382 *
4383 * This function will issue an ELS LOGO command after completing
4384 * the UNREG_RPI.
4385 **/
4386void
4387lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4388{
4389 struct lpfc_vport *vport = pmb->vport;
4390 struct lpfc_nodelist *ndlp;
4391
4392 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4393 if (!ndlp)
4394 return;
4395 lpfc_issue_els_logo(vport, ndlp, 0);
4396}
4397
dea31012005-04-17 16:05:31 -05004398/*
4399 * Free rpi associated with LPFC_NODELIST entry.
4400 * This routine is called from lpfc_freenode(), when we are removing
4401 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4402 * LOGO that completes successfully, and we are waiting to PLOGI back
4403 * to the remote NPort. In addition, it is called after we receive
4404 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4405 * we are waiting to PLOGI back to the remote NPort.
4406 */
4407int
James Smart2e0fef82007-06-17 19:56:36 -05004408lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004409{
James Smart2e0fef82007-06-17 19:56:36 -05004410 struct lpfc_hba *phba = vport->phba;
4411 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004412 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004413 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004414
James Smart40426292010-12-15 17:58:10 -05004415 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart2e0fef82007-06-17 19:56:36 -05004416 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4417 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004418 /* SLI4 ports require the physical rpi value. */
4419 rpi = ndlp->nlp_rpi;
4420 if (phba->sli_rev == LPFC_SLI_REV4)
4421 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004422
James Smart6d368e52011-05-24 11:44:12 -04004423 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004424 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004425 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4426 mbox->context1 = ndlp;
4427 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4428 } else {
4429 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4430 }
4431
James Smart0b727fe2007-10-27 13:37:25 -04004432 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004433 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004434 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004435 }
dea31012005-04-17 16:05:31 -05004436 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004437
James Smart40426292010-12-15 17:58:10 -05004438 if (phba->sli_rev != LPFC_SLI_REV4)
4439 ndlp->nlp_rpi = 0;
4440 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004441 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004442 return 1;
4443 }
4444 return 0;
4445}
4446
James Smartecfd03c2010-02-12 14:41:27 -05004447/**
4448 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4449 * @phba: pointer to lpfc hba data structure.
4450 *
4451 * This routine is invoked to unregister all the currently registered RPIs
4452 * to the HBA.
4453 **/
4454void
4455lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4456{
4457 struct lpfc_vport **vports;
4458 struct lpfc_nodelist *ndlp;
4459 struct Scsi_Host *shost;
4460 int i;
4461
4462 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004463 if (!vports) {
4464 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4465 "2884 Vport array allocation failed \n");
4466 return;
4467 }
James Smartecfd03c2010-02-12 14:41:27 -05004468 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4469 shost = lpfc_shost_from_vport(vports[i]);
4470 spin_lock_irq(shost->host_lock);
4471 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004472 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004473 /* The mempool_alloc might sleep */
4474 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004475 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004476 spin_lock_irq(shost->host_lock);
4477 }
James Smartecfd03c2010-02-12 14:41:27 -05004478 }
4479 spin_unlock_irq(shost->host_lock);
4480 }
4481 lpfc_destroy_vport_work_array(phba, vports);
4482}
4483
James Smart92d7f7b2007-06-17 19:56:38 -05004484void
4485lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4486{
4487 struct lpfc_hba *phba = vport->phba;
4488 LPFC_MBOXQ_t *mbox;
4489 int rc;
4490
James Smart5af5eee2010-10-22 11:06:38 -04004491 if (phba->sli_rev == LPFC_SLI_REV4) {
4492 lpfc_sli4_unreg_all_rpis(vport);
4493 return;
4494 }
4495
James Smart92d7f7b2007-06-17 19:56:38 -05004496 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4497 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004498 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4499 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004500 mbox->vport = vport;
4501 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004502 mbox->context1 = NULL;
4503 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004504 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004505 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004506
4507 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4508 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4509 "1836 Could not issue "
4510 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004511 }
4512}
4513
4514void
4515lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4516{
4517 struct lpfc_hba *phba = vport->phba;
4518 LPFC_MBOXQ_t *mbox;
4519 int rc;
4520
4521 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4522 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004523 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4524 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004525 mbox->vport = vport;
4526 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004527 mbox->context1 = NULL;
4528 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004529 if (rc != MBX_TIMEOUT)
4530 mempool_free(mbox, phba->mbox_mem_pool);
4531
4532 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004533 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4534 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004535 "unreg_did (default rpis) status %d\n",
4536 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004537 }
4538}
4539
dea31012005-04-17 16:05:31 -05004540/*
4541 * Free resources associated with LPFC_NODELIST entry
4542 * so it can be freed.
4543 */
4544static int
James Smart2e0fef82007-06-17 19:56:36 -05004545lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004546{
James Smart2e0fef82007-06-17 19:56:36 -05004547 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4548 struct lpfc_hba *phba = vport->phba;
4549 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004550 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004551
4552 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004553 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4554 "0900 Cleanup node for NPort x%x "
4555 "Data: x%x x%x x%x\n",
4556 ndlp->nlp_DID, ndlp->nlp_flag,
4557 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004558 if (NLP_CHK_FREE_REQ(ndlp)) {
4559 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4560 "0280 lpfc_cleanup_node: ndlp:x%p "
4561 "usgmap:x%x refcnt:%d\n",
4562 (void *)ndlp, ndlp->nlp_usg_map,
4563 atomic_read(&ndlp->kref.refcount));
4564 lpfc_dequeue_node(vport, ndlp);
4565 } else {
4566 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4567 "0281 lpfc_cleanup_node: ndlp:x%p "
4568 "usgmap:x%x refcnt:%d\n",
4569 (void *)ndlp, ndlp->nlp_usg_map,
4570 atomic_read(&ndlp->kref.refcount));
4571 lpfc_disable_node(vport, ndlp);
4572 }
dea31012005-04-17 16:05:31 -05004573
James Smart086a3452012-08-14 14:25:21 -04004574
4575 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4576
dea31012005-04-17 16:05:31 -05004577 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4578 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004579 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004580 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004581 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4582 mb->context2 = NULL;
4583 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4584 }
4585 }
James Smart33ccf8d2006-08-17 11:57:58 -04004586
James Smart2e0fef82007-06-17 19:56:36 -05004587 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004588 /* Cleanup REG_LOGIN completions which are not yet processed */
4589 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4590 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004591 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004592 (ndlp != (struct lpfc_nodelist *) mb->context2))
4593 continue;
4594
4595 mb->context2 = NULL;
4596 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4597 }
4598
dea31012005-04-17 16:05:31 -05004599 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004600 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004601 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004602 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004603 mp = (struct lpfc_dmabuf *) (mb->context1);
4604 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004605 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004606 kfree(mp);
4607 }
4608 list_del(&mb->list);
4609 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004610 /* We shall not invoke the lpfc_nlp_put to decrement
4611 * the ndlp reference count as we are in the process
4612 * of lpfc_nlp_release.
4613 */
dea31012005-04-17 16:05:31 -05004614 }
4615 }
James Smart2e0fef82007-06-17 19:56:36 -05004616 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004617
James Smarte47c9092008-02-08 18:49:26 -05004618 lpfc_els_abort(phba, ndlp);
4619
James Smart2e0fef82007-06-17 19:56:36 -05004620 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004621 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004622 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004623
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004624 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004625 del_timer_sync(&ndlp->nlp_delayfunc);
4626
James Smart0d2b6b82008-06-14 22:52:47 -04004627 list_del_init(&ndlp->els_retry_evt.evt_listp);
4628 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004629 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004630 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004631
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004632 return 0;
dea31012005-04-17 16:05:31 -05004633}
4634
4635/*
4636 * Check to see if we can free the nlp back to the freelist.
4637 * If we are in the middle of using the nlp in the discovery state
4638 * machine, defer the free till we reach the end of the state machine.
4639 */
James Smart329f9bc2007-04-25 09:53:01 -04004640static void
James Smart2e0fef82007-06-17 19:56:36 -05004641lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004642{
James Smarta8adb832007-10-27 13:37:53 -04004643 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004644 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004645 LPFC_MBOXQ_t *mbox;
4646 int rc;
dea31012005-04-17 16:05:31 -05004647
James Smart0d2b6b82008-06-14 22:52:47 -04004648 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004649 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004650 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4651 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004652 /* For this case we need to cleanup the default rpi
4653 * allocated by the firmware.
4654 */
4655 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4656 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004657 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004658 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004659 if (rc) {
4660 mempool_free(mbox, phba->mbox_mem_pool);
4661 }
4662 else {
4663 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4664 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4665 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004666 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004667 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4668 if (rc == MBX_NOT_FINISHED) {
4669 mempool_free(mbox, phba->mbox_mem_pool);
4670 }
4671 }
4672 }
4673 }
James Smart2e0fef82007-06-17 19:56:36 -05004674 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004675
James Smart2e0fef82007-06-17 19:56:36 -05004676 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004677 * We can get here with a non-NULL ndlp->rport because when we
4678 * unregister a rport we don't break the rport/node linkage. So if we
4679 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004680 */
James Smart92d7f7b2007-06-17 19:56:38 -05004681 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004682 rdata = ndlp->rport->dd_data;
4683 rdata->pnode = NULL;
4684 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004685 }
dea31012005-04-17 16:05:31 -05004686}
4687
4688static int
James Smart2e0fef82007-06-17 19:56:36 -05004689lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4690 uint32_t did)
dea31012005-04-17 16:05:31 -05004691{
James Smart2e0fef82007-06-17 19:56:36 -05004692 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004693
4694 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004695 return 0;
dea31012005-04-17 16:05:31 -05004696
dea31012005-04-17 16:05:31 -05004697 /* First check for Direct match */
4698 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004699 return 1;
dea31012005-04-17 16:05:31 -05004700
4701 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004702 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004703 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004704 return 0;
dea31012005-04-17 16:05:31 -05004705 }
4706
4707 matchdid.un.word = did;
4708 ndlpdid.un.word = ndlp->nlp_DID;
4709 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4710 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4711 (mydid.un.b.area == matchdid.un.b.area)) {
4712 if ((ndlpdid.un.b.domain == 0) &&
4713 (ndlpdid.un.b.area == 0)) {
4714 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004715 return 1;
dea31012005-04-17 16:05:31 -05004716 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004717 return 0;
dea31012005-04-17 16:05:31 -05004718 }
4719
4720 matchdid.un.word = ndlp->nlp_DID;
4721 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4722 (mydid.un.b.area == ndlpdid.un.b.area)) {
4723 if ((matchdid.un.b.domain == 0) &&
4724 (matchdid.un.b.area == 0)) {
4725 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004726 return 1;
dea31012005-04-17 16:05:31 -05004727 }
4728 }
4729 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004730 return 0;
dea31012005-04-17 16:05:31 -05004731}
4732
James Smart685f0bf2007-04-25 09:53:08 -04004733/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004734static struct lpfc_nodelist *
4735__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004736{
James Smart2fb9bd82006-12-02 13:33:57 -05004737 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004738 uint32_t data1;
4739
James Smart2e0fef82007-06-17 19:56:36 -05004740 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4741 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004742 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4743 ((uint32_t) ndlp->nlp_xri << 16) |
4744 ((uint32_t) ndlp->nlp_type << 8) |
4745 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004746 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4747 "0929 FIND node DID "
4748 "Data: x%p x%x x%x x%x\n",
4749 ndlp, ndlp->nlp_DID,
4750 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004751 return ndlp;
dea31012005-04-17 16:05:31 -05004752 }
4753 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004754
dea31012005-04-17 16:05:31 -05004755 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004756 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4757 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004758 return NULL;
4759}
4760
4761struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004762lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004763{
James Smart2e0fef82007-06-17 19:56:36 -05004764 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004765 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004766 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004767
James Smartfedd3b72011-02-16 12:39:24 -05004768 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004769 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004770 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004771 return ndlp;
4772}
4773
4774struct lpfc_nodelist *
4775lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4776{
4777 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4778 struct lpfc_nodelist *ndlp;
4779
4780 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004781 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004782 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4783 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004784 return NULL;
4785 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004786 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004787 if (!ndlp)
4788 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004789 lpfc_nlp_init(vport, ndlp, did);
4790 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4791 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004792 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004793 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004794 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004795 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4796 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4797 if (!ndlp)
4798 return NULL;
4799 spin_lock_irq(shost->host_lock);
4800 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4801 spin_unlock_irq(shost->host_lock);
4802 return ndlp;
dea31012005-04-17 16:05:31 -05004803 }
James Smarte47c9092008-02-08 18:49:26 -05004804
James Smart58da1ff2008-04-07 10:15:56 -04004805 if ((vport->fc_flag & FC_RSCN_MODE) &&
4806 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004807 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004808 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004809 * we don't need to try to discover it again.
4810 */
4811 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4812 return NULL;
4813
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004814 /* Since this node is marked for discovery,
4815 * delay timeout is not needed.
4816 */
James Smart0d2b6b82008-06-14 22:52:47 -04004817 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004818 spin_lock_irq(shost->host_lock);
4819 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4820 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004821 } else
dea31012005-04-17 16:05:31 -05004822 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004823 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004824 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004825 * or we are already in the process of discovery on it,
4826 * we don't need to try to discover it again.
4827 */
James Smart685f0bf2007-04-25 09:53:08 -04004828 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004829 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4830 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004831 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004832 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4833 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004834 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004835 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004836 }
4837 return ndlp;
4838}
4839
4840/* Build a list of nodes to discover based on the loopmap */
4841void
James Smart2e0fef82007-06-17 19:56:36 -05004842lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004843{
James Smart2e0fef82007-06-17 19:56:36 -05004844 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004845 int j;
4846 uint32_t alpa, index;
4847
James Smart2e0fef82007-06-17 19:56:36 -05004848 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004849 return;
James Smart2e0fef82007-06-17 19:56:36 -05004850
James Smart76a95d72010-11-20 23:11:48 -05004851 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004852 return;
dea31012005-04-17 16:05:31 -05004853
4854 /* Check for loop map present or not */
4855 if (phba->alpa_map[0]) {
4856 for (j = 1; j <= phba->alpa_map[0]; j++) {
4857 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004858 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004859 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004860 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004861 }
4862 } else {
4863 /* No alpamap, so try all alpa's */
4864 for (j = 0; j < FC_MAXLOOP; j++) {
4865 /* If cfg_scan_down is set, start from highest
4866 * ALPA (0xef) to lowest (0x1).
4867 */
James Smart3de2a652007-08-02 11:09:59 -04004868 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004869 index = j;
4870 else
4871 index = FC_MAXLOOP - j - 1;
4872 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004873 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004874 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004875 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004876 }
4877 }
4878 return;
4879}
4880
dea31012005-04-17 16:05:31 -05004881void
James Smart2e0fef82007-06-17 19:56:36 -05004882lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004883{
dea31012005-04-17 16:05:31 -05004884 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004885 struct lpfc_sli *psli = &phba->sli;
4886 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4887 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4888 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4889 int rc;
4890
James Smart92d7f7b2007-06-17 19:56:38 -05004891 /*
4892 * if it's not a physical port or if we already send
4893 * clear_la then don't send it.
4894 */
4895 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004896 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4897 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004898 return;
4899
James Smart2e0fef82007-06-17 19:56:36 -05004900 /* Link up discovery */
4901 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4902 phba->link_state = LPFC_CLEAR_LA;
4903 lpfc_clear_la(phba, mbox);
4904 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4905 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004906 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004907 if (rc == MBX_NOT_FINISHED) {
4908 mempool_free(mbox, phba->mbox_mem_pool);
4909 lpfc_disc_flush_list(vport);
4910 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4911 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4912 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004913 phba->link_state = LPFC_HBA_ERROR;
4914 }
4915 }
4916}
4917
4918/* Reg_vpi to tell firmware to resume normal operations */
4919void
4920lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4921{
4922 LPFC_MBOXQ_t *regvpimbox;
4923
4924 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4925 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004926 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004927 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4928 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004929 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004930 == MBX_NOT_FINISHED) {
4931 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004932 }
4933 }
4934}
4935
4936/* Start Link up / RSCN discovery on NPR nodes */
4937void
4938lpfc_disc_start(struct lpfc_vport *vport)
4939{
4940 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4941 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004942 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004943 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004944 int did_changed;
dea31012005-04-17 16:05:31 -05004945
James Smart2e0fef82007-06-17 19:56:36 -05004946 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004947 return;
James Smart2e0fef82007-06-17 19:56:36 -05004948
4949 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004950 clear_la_pending = 1;
4951 else
4952 clear_la_pending = 0;
4953
James Smart2e0fef82007-06-17 19:56:36 -05004954 if (vport->port_state < LPFC_VPORT_READY)
4955 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004956
James Smart2e0fef82007-06-17 19:56:36 -05004957 lpfc_set_disctmo(vport);
4958
4959 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004960 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004961 else
dea31012005-04-17 16:05:31 -05004962 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004963
4964 vport->fc_prevDID = vport->fc_myDID;
4965 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004966
4967 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004968 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4969 "0202 Start Discovery hba state x%x "
4970 "Data: x%x x%x x%x\n",
4971 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4972 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004973
4974 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004975 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004976
4977 if (num_sent)
4978 return;
4979
James Smart6d368e52011-05-24 11:44:12 -04004980 /* Register the VPI for SLI3, NON-NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05004981 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004982 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004983 !(vport->fc_flag & FC_RSCN_MODE) &&
4984 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004985 lpfc_issue_reg_vpi(phba, vport);
4986 return;
4987 }
James Smart2e0fef82007-06-17 19:56:36 -05004988
James Smart92d7f7b2007-06-17 19:56:38 -05004989 /*
4990 * For SLI2, we need to set port_state to READY and continue
4991 * discovery.
4992 */
4993 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4994 /* If we get here, there is nothing to ADISC */
4995 if (vport->port_type == LPFC_PHYSICAL_PORT)
4996 lpfc_issue_clear_la(phba, vport);
4997
4998 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004999 vport->num_disc_nodes = 0;
5000 /* go thru NPR nodes and issue ELS PLOGIs */
5001 if (vport->fc_npr_cnt)
5002 lpfc_els_disc_plogi(vport);
5003
5004 if (!vport->num_disc_nodes) {
5005 spin_lock_irq(shost->host_lock);
5006 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5007 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005008 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005009 }
5010 }
James Smart92d7f7b2007-06-17 19:56:38 -05005011 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005012 } else {
5013 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005014 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005015
5016 if (num_sent)
5017 return;
5018
James Smart2e0fef82007-06-17 19:56:36 -05005019 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005020 /* Check to see if more RSCNs came in while we
5021 * were processing this one.
5022 */
James Smart2e0fef82007-06-17 19:56:36 -05005023 if ((vport->fc_rscn_id_cnt == 0) &&
5024 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5025 spin_lock_irq(shost->host_lock);
5026 vport->fc_flag &= ~FC_RSCN_MODE;
5027 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005028 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005029 } else
James Smart2e0fef82007-06-17 19:56:36 -05005030 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005031 }
5032 }
5033 return;
5034}
5035
5036/*
5037 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5038 * ring the match the sppecified nodelist.
5039 */
5040static void
James Smart2e0fef82007-06-17 19:56:36 -05005041lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005042{
James Smart2534ba72007-04-25 09:52:20 -04005043 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005044 struct lpfc_sli *psli;
5045 IOCB_t *icmd;
5046 struct lpfc_iocbq *iocb, *next_iocb;
5047 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005048
5049 psli = &phba->sli;
5050 pring = &psli->ring[LPFC_ELS_RING];
5051
5052 /* Error matching iocb on txq or txcmplq
5053 * First check the txq.
5054 */
James Smart2e0fef82007-06-17 19:56:36 -05005055 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005056 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5057 if (iocb->context1 != ndlp) {
5058 continue;
5059 }
5060 icmd = &iocb->iocb;
5061 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5062 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5063
James Smart2534ba72007-04-25 09:52:20 -04005064 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005065 }
5066 }
5067
5068 /* Next check the txcmplq */
5069 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5070 if (iocb->context1 != ndlp) {
5071 continue;
5072 }
5073 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005074 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5075 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005076 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005077 }
5078 }
James Smart2e0fef82007-06-17 19:56:36 -05005079 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005080
James Smarta257bf92009-04-06 18:48:10 -04005081 /* Cancel all the IOCBs from the completions list */
5082 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5083 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005084}
5085
Adrian Bunka6ababd2007-11-05 18:07:33 +01005086static void
James Smart2e0fef82007-06-17 19:56:36 -05005087lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005088{
5089 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005090 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005091
James Smart2e0fef82007-06-17 19:56:36 -05005092 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5093 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005094 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005095 if (!NLP_CHK_NODE_ACT(ndlp))
5096 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005097 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5098 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5099 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005100 }
dea31012005-04-17 16:05:31 -05005101 }
5102 }
dea31012005-04-17 16:05:31 -05005103}
5104
James Smart92d7f7b2007-06-17 19:56:38 -05005105void
5106lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5107{
5108 lpfc_els_flush_rscn(vport);
5109 lpfc_els_flush_cmd(vport);
5110 lpfc_disc_flush_list(vport);
5111}
5112
dea31012005-04-17 16:05:31 -05005113/*****************************************************************************/
5114/*
5115 * NAME: lpfc_disc_timeout
5116 *
5117 * FUNCTION: Fibre Channel driver discovery timeout routine.
5118 *
5119 * EXECUTION ENVIRONMENT: interrupt only
5120 *
5121 * CALLED FROM:
5122 * Timer function
5123 *
5124 * RETURNS:
5125 * none
5126 */
5127/*****************************************************************************/
5128void
5129lpfc_disc_timeout(unsigned long ptr)
5130{
James Smart2e0fef82007-06-17 19:56:36 -05005131 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5132 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005133 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005134 unsigned long flags = 0;
5135
5136 if (unlikely(!phba))
5137 return;
5138
James Smart5e9d9b82008-06-14 22:52:53 -04005139 spin_lock_irqsave(&vport->work_port_lock, flags);
5140 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5141 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005142 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005143 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005144
James Smart5e9d9b82008-06-14 22:52:53 -04005145 if (!tmo_posted)
5146 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005147 return;
5148}
5149
5150static void
James Smart2e0fef82007-06-17 19:56:36 -05005151lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005152{
James Smart2e0fef82007-06-17 19:56:36 -05005153 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5154 struct lpfc_hba *phba = vport->phba;
5155 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005156 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005157 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005158 int rc, clrlaerr = 0;
5159
James Smart2e0fef82007-06-17 19:56:36 -05005160 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005161 return;
5162
James Smart2e0fef82007-06-17 19:56:36 -05005163 spin_lock_irq(shost->host_lock);
5164 vport->fc_flag &= ~FC_DISC_TMO;
5165 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005166
James Smart858c9f62007-06-17 19:56:39 -05005167 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5168 "disc timeout: state:x%x rtry:x%x flg:x%x",
5169 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5170
James Smart2e0fef82007-06-17 19:56:36 -05005171 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005172
5173 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005174 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5175 * FAN
5176 */
5177 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005178 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5179 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005180 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005181 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005182 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005183 if (!NLP_CHK_NODE_ACT(ndlp))
5184 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005185 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5186 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005187 if (ndlp->nlp_type & NLP_FABRIC) {
5188 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005189 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005190
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005191 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005192 /* Fail outstanding IO now since device
5193 * is marked for PLOGI.
5194 */
James Smart2e0fef82007-06-17 19:56:36 -05005195 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005196 }
5197 }
James Smart92d7f7b2007-06-17 19:56:38 -05005198 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005199 if (phba->sli_rev <= LPFC_SLI_REV3)
5200 lpfc_initial_flogi(vport);
5201 else
5202 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005203 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005204 }
dea31012005-04-17 16:05:31 -05005205 break;
5206
James Smart92d7f7b2007-06-17 19:56:38 -05005207 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005208 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005209 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005210 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005211 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5212 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005213 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005214
5215 /* Assume no Fabric and go on with discovery.
5216 * Check for outstanding ELS FLOGI to abort.
5217 */
5218
5219 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005220 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005221
5222 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005223 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005224 break;
5225
5226 case LPFC_FABRIC_CFG_LINK:
5227 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5228 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005229 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5230 "0223 Timeout while waiting for "
5231 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005232 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005233 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005234 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005235 lpfc_els_abort(phba, ndlp);
5236
5237 /* ReStart discovery */
5238 goto restart_disc;
dea31012005-04-17 16:05:31 -05005239
5240 case LPFC_NS_QRY:
5241 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005242 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5243 "0224 NameServer Query timeout "
5244 "Data: x%x x%x\n",
5245 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005246
James Smart92d7f7b2007-06-17 19:56:38 -05005247 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5248 /* Try it one more time */
5249 vport->fc_ns_retry++;
5250 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5251 vport->fc_ns_retry, 0);
5252 if (rc == 0)
5253 break;
dea31012005-04-17 16:05:31 -05005254 }
James Smart92d7f7b2007-06-17 19:56:38 -05005255 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005256
James Smart87af33f2007-10-27 13:37:43 -04005257restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005258 /*
5259 * Discovery is over.
5260 * set port_state to PORT_READY if SLI2.
5261 * cmpl_reg_vpi will set port_state to READY for SLI3.
5262 */
James Smart3772a992009-05-22 14:50:54 -04005263 if (phba->sli_rev < LPFC_SLI_REV4) {
5264 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5265 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005266 else {
James Smart3772a992009-05-22 14:50:54 -04005267 lpfc_issue_clear_la(phba, vport);
5268 vport->port_state = LPFC_VPORT_READY;
5269 }
dea31012005-04-17 16:05:31 -05005270 }
5271
5272 /* Setup and issue mailbox INITIALIZE LINK command */
5273 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5274 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005275 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5276 "0206 Device Discovery "
5277 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005278 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005279 break;
5280 }
5281
5282 lpfc_linkdown(phba);
5283 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5284 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005285 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005286 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005287 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005288 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005289 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005290 if (rc == MBX_NOT_FINISHED)
5291 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5292
5293 break;
5294
5295 case LPFC_DISC_AUTH:
5296 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005297 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5298 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005299 lpfc_disc_flush_list(vport);
5300
James Smart92d7f7b2007-06-17 19:56:38 -05005301 /*
5302 * set port_state to PORT_READY if SLI2.
5303 * cmpl_reg_vpi will set port_state to READY for SLI3.
5304 */
James Smart3772a992009-05-22 14:50:54 -04005305 if (phba->sli_rev < LPFC_SLI_REV4) {
5306 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5307 lpfc_issue_reg_vpi(phba, vport);
5308 else { /* NPIV Not enabled */
5309 lpfc_issue_clear_la(phba, vport);
5310 vport->port_state = LPFC_VPORT_READY;
5311 }
dea31012005-04-17 16:05:31 -05005312 }
5313 break;
5314
James Smart2e0fef82007-06-17 19:56:36 -05005315 case LPFC_VPORT_READY:
5316 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005317 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5318 "0231 RSCN timeout Data: x%x "
5319 "x%x\n",
5320 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005321
5322 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005323 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005324
James Smart2e0fef82007-06-17 19:56:36 -05005325 lpfc_els_flush_rscn(vport);
5326 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005327 }
5328 break;
James Smart2e0fef82007-06-17 19:56:36 -05005329
James Smart92d7f7b2007-06-17 19:56:38 -05005330 default:
James Smarte8b62012007-08-02 11:10:09 -04005331 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005332 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005333 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005334 break;
5335 }
5336
5337 switch (phba->link_state) {
5338 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005339 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005340 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5341 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005342 clrlaerr = 1;
5343 break;
5344
James Smart09372822008-01-11 01:52:54 -05005345 case LPFC_LINK_UP:
5346 lpfc_issue_clear_la(phba, vport);
5347 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005348 case LPFC_LINK_UNKNOWN:
5349 case LPFC_WARM_START:
5350 case LPFC_INIT_START:
5351 case LPFC_INIT_MBX_CMDS:
5352 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005353 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005354 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5355 "0230 Unexpected timeout, hba link "
5356 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005357 clrlaerr = 1;
5358 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005359
5360 case LPFC_HBA_READY:
5361 break;
dea31012005-04-17 16:05:31 -05005362 }
5363
5364 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005365 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005366 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005367 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5368 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005369 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005370 }
5371
5372 return;
5373}
5374
dea31012005-04-17 16:05:31 -05005375/*
5376 * This routine handles processing a NameServer REG_LOGIN mailbox
5377 * command upon completion. It is setup in the LPFC_MBOXQ
5378 * as the completion routine when the command is
5379 * handed off to the SLI layer.
5380 */
5381void
James Smart2e0fef82007-06-17 19:56:36 -05005382lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005383{
James Smart04c68492009-05-22 14:52:52 -04005384 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005385 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5386 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5387 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005388
5389 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005390 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005391
James Smart6d368e52011-05-24 11:44:12 -04005392 if (phba->sli_rev < LPFC_SLI_REV4)
5393 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005394 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005395 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005396 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05005397
James Smart2e0fef82007-06-17 19:56:36 -05005398 /*
5399 * Start issuing Fabric-Device Management Interface (FDMI) command to
5400 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5401 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005402 */
James Smart2e0fef82007-06-17 19:56:36 -05005403
James Smart3de2a652007-08-02 11:09:59 -04005404 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05005405 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
5406 else
5407 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05005408
James Smartfa4066b2008-01-11 01:53:27 -05005409 /* decrement the node reference count held for this callback
5410 * function.
5411 */
James Smart329f9bc2007-04-25 09:53:01 -04005412 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005413 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5414 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005415 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005416
5417 return;
5418}
5419
James Smart685f0bf2007-04-25 09:53:08 -04005420static int
5421lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5422{
5423 uint16_t *rpi = param;
5424
James Smarteff4a012012-01-18 16:25:25 -05005425 /* check for active node */
5426 if (!NLP_CHK_NODE_ACT(ndlp))
5427 return 0;
5428
James Smart685f0bf2007-04-25 09:53:08 -04005429 return ndlp->nlp_rpi == *rpi;
5430}
5431
5432static int
5433lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5434{
5435 return memcmp(&ndlp->nlp_portname, param,
5436 sizeof(ndlp->nlp_portname)) == 0;
5437}
5438
Adrian Bunka6ababd2007-11-05 18:07:33 +01005439static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005440__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005441{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005442 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005443
James Smart2e0fef82007-06-17 19:56:36 -05005444 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005445 if (filter(ndlp, param)) {
5446 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5447 "3185 FIND node filter %p DID "
5448 "Data: x%p x%x x%x\n",
5449 filter, ndlp, ndlp->nlp_DID,
5450 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005451 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005452 }
James Smart685f0bf2007-04-25 09:53:08 -04005453 }
James Smart34f5ad82012-08-03 12:35:03 -04005454 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5455 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005456 return NULL;
dea31012005-04-17 16:05:31 -05005457}
5458
James Smart685f0bf2007-04-25 09:53:08 -04005459/*
5460 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005461 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005462 */
5463struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005464__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005465{
James Smart2e0fef82007-06-17 19:56:36 -05005466 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005467}
5468
James Smart488d1462006-03-07 15:02:37 -05005469/*
James Smart685f0bf2007-04-25 09:53:08 -04005470 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005471 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005472 */
5473struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005474lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005475{
James Smart2e0fef82007-06-17 19:56:36 -05005476 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005477 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005478
James Smart2e0fef82007-06-17 19:56:36 -05005479 spin_lock_irq(shost->host_lock);
5480 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5481 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005482 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005483}
5484
James Smartcb69f7d2011-12-13 13:21:57 -05005485/*
5486 * This routine looks up the ndlp lists for the given RPI. If the rpi
5487 * is found, the routine returns the node element list pointer else
5488 * return NULL.
5489 */
5490struct lpfc_nodelist *
5491lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5492{
5493 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5494 struct lpfc_nodelist *ndlp;
5495
5496 spin_lock_irq(shost->host_lock);
5497 ndlp = __lpfc_findnode_rpi(vport, rpi);
5498 spin_unlock_irq(shost->host_lock);
5499 return ndlp;
5500}
5501
5502/**
5503 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5504 * @phba: pointer to lpfc hba data structure.
5505 * @vpi: the physical host virtual N_Port identifier.
5506 *
5507 * This routine finds a vport on a HBA (referred by @phba) through a
5508 * @vpi. The function walks the HBA's vport list and returns the address
5509 * of the vport with the matching @vpi.
5510 *
5511 * Return code
5512 * NULL - No vport with the matching @vpi found
5513 * Otherwise - Address to the vport with the matching @vpi.
5514 **/
5515struct lpfc_vport *
5516lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5517{
5518 struct lpfc_vport *vport;
5519 unsigned long flags;
5520 int i = 0;
5521
5522 /* The physical ports are always vpi 0 - translate is unnecessary. */
5523 if (vpi > 0) {
5524 /*
5525 * Translate the physical vpi to the logical vpi. The
5526 * vport stores the logical vpi.
5527 */
5528 for (i = 0; i < phba->max_vpi; i++) {
5529 if (vpi == phba->vpi_ids[i])
5530 break;
5531 }
5532
5533 if (i >= phba->max_vpi) {
5534 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5535 "2936 Could not find Vport mapped "
5536 "to vpi %d\n", vpi);
5537 return NULL;
5538 }
5539 }
5540
5541 spin_lock_irqsave(&phba->hbalock, flags);
5542 list_for_each_entry(vport, &phba->port_list, listentry) {
5543 if (vport->vpi == i) {
5544 spin_unlock_irqrestore(&phba->hbalock, flags);
5545 return vport;
5546 }
5547 }
5548 spin_unlock_irqrestore(&phba->hbalock, flags);
5549 return NULL;
5550}
5551
dea31012005-04-17 16:05:31 -05005552void
James Smart2e0fef82007-06-17 19:56:36 -05005553lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5554 uint32_t did)
dea31012005-04-17 16:05:31 -05005555{
5556 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005557
5558 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005559 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05005560
5561 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5562 "node init: did:x%x",
5563 ndlp->nlp_DID, 0, 0);
5564
dea31012005-04-17 16:05:31 -05005565 return;
5566}
James Smart329f9bc2007-04-25 09:53:01 -04005567
James Smart98c9ea52007-10-27 13:37:33 -04005568/* This routine releases all resources associated with a specifc NPort's ndlp
5569 * and mempool_free's the nodelist.
5570 */
James Smart311464e2007-08-02 11:10:37 -04005571static void
James Smart329f9bc2007-04-25 09:53:01 -04005572lpfc_nlp_release(struct kref *kref)
5573{
James Smarte47c9092008-02-08 18:49:26 -05005574 struct lpfc_hba *phba;
5575 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005576 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5577 kref);
James Smart858c9f62007-06-17 19:56:39 -05005578
5579 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5580 "node release: did:x%x flg:x%x type:x%x",
5581 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5582
James Smarte47c9092008-02-08 18:49:26 -05005583 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005584 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smarte47c9092008-02-08 18:49:26 -05005585 "usgmap:x%x refcnt:%d\n",
James Smart939723a2012-05-09 21:19:03 -04005586 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smarte47c9092008-02-08 18:49:26 -05005587 atomic_read(&ndlp->kref.refcount));
5588
5589 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005590 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005591
5592 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005593 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005594 spin_lock_irqsave(&phba->ndlp_lock, flags);
5595 NLP_CLR_NODE_ACT(ndlp);
5596 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005597 if (phba->sli_rev == LPFC_SLI_REV4)
5598 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005599
5600 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005601 if (NLP_CHK_FREE_REQ(ndlp)) {
5602 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04005603 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005604 }
James Smart329f9bc2007-04-25 09:53:01 -04005605}
5606
James Smart98c9ea52007-10-27 13:37:33 -04005607/* This routine bumps the reference count for a ndlp structure to ensure
5608 * that one discovery thread won't free a ndlp while another discovery thread
5609 * is using it.
5610 */
James Smart329f9bc2007-04-25 09:53:01 -04005611struct lpfc_nodelist *
5612lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5613{
James Smarte47c9092008-02-08 18:49:26 -05005614 struct lpfc_hba *phba;
5615 unsigned long flags;
5616
James Smart98c9ea52007-10-27 13:37:33 -04005617 if (ndlp) {
5618 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5619 "node get: did:x%x flg:x%x refcnt:x%x",
5620 ndlp->nlp_DID, ndlp->nlp_flag,
5621 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005622 /* The check of ndlp usage to prevent incrementing the
5623 * ndlp reference count that is in the process of being
5624 * released.
5625 */
James Smarta257bf92009-04-06 18:48:10 -04005626 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005627 spin_lock_irqsave(&phba->ndlp_lock, flags);
5628 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5629 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5630 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5631 "0276 lpfc_nlp_get: ndlp:x%p "
5632 "usgmap:x%x refcnt:%d\n",
5633 (void *)ndlp, ndlp->nlp_usg_map,
5634 atomic_read(&ndlp->kref.refcount));
5635 return NULL;
5636 } else
5637 kref_get(&ndlp->kref);
5638 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005639 }
James Smart329f9bc2007-04-25 09:53:01 -04005640 return ndlp;
5641}
5642
James Smart98c9ea52007-10-27 13:37:33 -04005643/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005644 * count goes to 0, this indicates the the associated nodelist should be
5645 * freed. Returning 1 indicates the ndlp resource has been released; on the
5646 * other hand, returning 0 indicates the ndlp resource has not been released
5647 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005648 */
James Smart329f9bc2007-04-25 09:53:01 -04005649int
5650lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5651{
James Smarte47c9092008-02-08 18:49:26 -05005652 struct lpfc_hba *phba;
5653 unsigned long flags;
5654
5655 if (!ndlp)
5656 return 1;
5657
5658 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5659 "node put: did:x%x flg:x%x refcnt:x%x",
5660 ndlp->nlp_DID, ndlp->nlp_flag,
5661 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005662 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005663 spin_lock_irqsave(&phba->ndlp_lock, flags);
5664 /* Check the ndlp memory free acknowledge flag to avoid the
5665 * possible race condition that kref_put got invoked again
5666 * after previous one has done ndlp memory free.
5667 */
5668 if (NLP_CHK_FREE_ACK(ndlp)) {
5669 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5670 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5671 "0274 lpfc_nlp_put: ndlp:x%p "
5672 "usgmap:x%x refcnt:%d\n",
5673 (void *)ndlp, ndlp->nlp_usg_map,
5674 atomic_read(&ndlp->kref.refcount));
5675 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005676 }
James Smarte47c9092008-02-08 18:49:26 -05005677 /* Check the ndlp inactivate log flag to avoid the possible
5678 * race condition that kref_put got invoked again after ndlp
5679 * is already in inactivating state.
5680 */
5681 if (NLP_CHK_IACT_REQ(ndlp)) {
5682 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5683 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5684 "0275 lpfc_nlp_put: ndlp:x%p "
5685 "usgmap:x%x refcnt:%d\n",
5686 (void *)ndlp, ndlp->nlp_usg_map,
5687 atomic_read(&ndlp->kref.refcount));
5688 return 1;
5689 }
5690 /* For last put, mark the ndlp usage flags to make sure no
5691 * other kref_get and kref_put on the same ndlp shall get
5692 * in between the process when the final kref_put has been
5693 * invoked on this ndlp.
5694 */
5695 if (atomic_read(&ndlp->kref.refcount) == 1) {
5696 /* Indicate ndlp is put to inactive state. */
5697 NLP_SET_IACT_REQ(ndlp);
5698 /* Acknowledge ndlp memory free has been seen. */
5699 if (NLP_CHK_FREE_REQ(ndlp))
5700 NLP_SET_FREE_ACK(ndlp);
5701 }
5702 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5703 /* Note, the kref_put returns 1 when decrementing a reference
5704 * count that was 1, it invokes the release callback function,
5705 * but it still left the reference count as 1 (not actually
5706 * performs the last decrementation). Otherwise, it actually
5707 * decrements the reference count and returns 0.
5708 */
5709 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005710}
James Smart98c9ea52007-10-27 13:37:33 -04005711
5712/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005713 * by any other discovery thread. This routine returns 1 if the
5714 * ndlp has been freed. A return value of 0 indicates the ndlp is
5715 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005716 */
5717int
5718lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5719{
5720 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5721 "node not used: did:x%x flg:x%x refcnt:x%x",
5722 ndlp->nlp_DID, ndlp->nlp_flag,
5723 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005724 if (atomic_read(&ndlp->kref.refcount) == 1)
5725 if (lpfc_nlp_put(ndlp))
5726 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005727 return 0;
5728}
James Smart6fb120a2009-05-22 14:52:59 -04005729
5730/**
5731 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5732 * @phba: Pointer to hba context object.
5733 *
5734 * This function iterate through all FC nodes associated
5735 * will all vports to check if there is any node with
5736 * fc_rports associated with it. If there is an fc_rport
5737 * associated with the node, then the node is either in
5738 * discovered state or its devloss_timer is pending.
5739 */
5740static int
5741lpfc_fcf_inuse(struct lpfc_hba *phba)
5742{
5743 struct lpfc_vport **vports;
5744 int i, ret = 0;
5745 struct lpfc_nodelist *ndlp;
5746 struct Scsi_Host *shost;
5747
5748 vports = lpfc_create_vport_work_array(phba);
5749
James Smart63e801c2010-11-20 23:14:19 -05005750 /* If driver cannot allocate memory, indicate fcf is in use */
5751 if (!vports)
5752 return 1;
5753
James Smart6fb120a2009-05-22 14:52:59 -04005754 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5755 shost = lpfc_shost_from_vport(vports[i]);
5756 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005757 /*
5758 * IF the CVL_RCVD bit is not set then we have sent the
5759 * flogi.
5760 * If dev_loss fires while we are waiting we do not want to
5761 * unreg the fcf.
5762 */
5763 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5764 spin_unlock_irq(shost->host_lock);
5765 ret = 1;
5766 goto out;
5767 }
James Smart6fb120a2009-05-22 14:52:59 -04005768 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5769 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5770 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5771 ret = 1;
5772 spin_unlock_irq(shost->host_lock);
5773 goto out;
James Smart80c17842012-03-01 22:35:45 -05005774 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5775 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005776 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005777 "2624 RPI %x DID %x flag %x "
5778 "still logged in\n",
5779 ndlp->nlp_rpi, ndlp->nlp_DID,
5780 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005781 }
5782 }
5783 spin_unlock_irq(shost->host_lock);
5784 }
5785out:
5786 lpfc_destroy_vport_work_array(phba, vports);
5787 return ret;
5788}
5789
5790/**
5791 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5792 * @phba: Pointer to hba context object.
5793 * @mboxq: Pointer to mailbox object.
5794 *
5795 * This function frees memory associated with the mailbox command.
5796 */
James Smart1b511972011-12-13 13:23:09 -05005797void
James Smart6fb120a2009-05-22 14:52:59 -04005798lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5799{
5800 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005801 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005802
5803 if (mboxq->u.mb.mbxStatus) {
5804 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5805 "2555 UNREG_VFI mbxStatus error x%x "
5806 "HBA state x%x\n",
5807 mboxq->u.mb.mbxStatus, vport->port_state);
5808 }
James Smart38b92ef2010-08-04 16:11:39 -04005809 spin_lock_irq(shost->host_lock);
5810 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5811 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005812 mempool_free(mboxq, phba->mbox_mem_pool);
5813 return;
5814}
5815
5816/**
5817 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5818 * @phba: Pointer to hba context object.
5819 * @mboxq: Pointer to mailbox object.
5820 *
5821 * This function frees memory associated with the mailbox command.
5822 */
5823static void
5824lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5825{
5826 struct lpfc_vport *vport = mboxq->vport;
5827
5828 if (mboxq->u.mb.mbxStatus) {
5829 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5830 "2550 UNREG_FCFI mbxStatus error x%x "
5831 "HBA state x%x\n",
5832 mboxq->u.mb.mbxStatus, vport->port_state);
5833 }
5834 mempool_free(mboxq, phba->mbox_mem_pool);
5835 return;
5836}
5837
5838/**
James Smartecfd03c2010-02-12 14:41:27 -05005839 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005840 * @phba: Pointer to hba context object.
5841 *
James Smartecfd03c2010-02-12 14:41:27 -05005842 * This function prepare the HBA for unregistering the currently registered
5843 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5844 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005845 */
James Smartecfd03c2010-02-12 14:41:27 -05005846int
5847lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005848{
James Smart6fb120a2009-05-22 14:52:59 -04005849 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005850 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005851 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005852 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005853
James Smartecfd03c2010-02-12 14:41:27 -05005854 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005855 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005856 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005857
James Smart4d9ab992009-10-02 15:16:39 -04005858 /* At this point, all discovery is aborted */
5859 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005860
5861 /* Unregister VPIs */
5862 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005863 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005864 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005865 /* Stop FLOGI/FDISC retries */
5866 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5867 if (ndlp)
5868 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005869 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04005870 if (phba->sli_rev == LPFC_SLI_REV4)
5871 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005872 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005873 shost = lpfc_shost_from_vport(vports[i]);
5874 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005875 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005876 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005877 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005878 }
5879 lpfc_destroy_vport_work_array(phba, vports);
5880
James Smart695a8142010-01-26 23:08:03 -05005881 /* Cleanup any outstanding ELS commands */
5882 lpfc_els_flush_all_cmd(phba);
5883
James Smart1b511972011-12-13 13:23:09 -05005884 /* Unregister the physical port VFI */
5885 rc = lpfc_issue_unreg_vfi(phba->pport);
5886 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05005887}
5888
5889/**
5890 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5891 * @phba: Pointer to hba context object.
5892 *
5893 * This function issues synchronous unregister FCF mailbox command to HBA to
5894 * unregister the currently registered FCF record. The driver does not reset
5895 * the driver FCF usage state flags.
5896 *
5897 * Return 0 if successfully issued, none-zero otherwise.
5898 */
5899int
5900lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5901{
5902 LPFC_MBOXQ_t *mbox;
5903 int rc;
5904
James Smart6fb120a2009-05-22 14:52:59 -04005905 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5906 if (!mbox) {
5907 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005908 "2551 UNREG_FCFI mbox allocation failed"
5909 "HBA state x%x\n", phba->pport->port_state);
5910 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005911 }
James Smart6fb120a2009-05-22 14:52:59 -04005912 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5913 mbox->vport = phba->pport;
5914 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5915 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5916
5917 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005918 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5919 "2552 Unregister FCFI command failed rc x%x "
5920 "HBA state x%x\n",
5921 rc, phba->pport->port_state);
5922 return -EINVAL;
5923 }
5924 return 0;
5925}
5926
5927/**
5928 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5929 * @phba: Pointer to hba context object.
5930 *
5931 * This function unregisters the currently reigstered FCF. This function
5932 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5933 */
5934void
5935lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5936{
5937 int rc;
5938
5939 /* Preparation for unregistering fcf */
5940 rc = lpfc_unregister_fcf_prep(phba);
5941 if (rc) {
5942 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5943 "2748 Failed to prepare for unregistering "
5944 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005945 return;
5946 }
5947
James Smartecfd03c2010-02-12 14:41:27 -05005948 /* Now, unregister FCF record and reset HBA FCF state */
5949 rc = lpfc_sli4_unregister_fcf(phba);
5950 if (rc)
5951 return;
5952 /* Reset HBA FCF states after successful unregister FCF */
5953 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005954 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005955
5956 /*
5957 * If driver is not unloading, check if there is any other
5958 * FCF record that can be used for discovery.
5959 */
5960 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005961 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005962 return;
5963
James Smart0c9ab6f2010-02-26 14:15:57 -05005964 /* This is considered as the initial FCF discovery scan */
5965 spin_lock_irq(&phba->hbalock);
5966 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5967 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005968
5969 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04005970 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005971
James Smart0c9ab6f2010-02-26 14:15:57 -05005972 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005973
James Smart0c9ab6f2010-02-26 14:15:57 -05005974 if (rc) {
5975 spin_lock_irq(&phba->hbalock);
5976 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5977 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005978 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005979 "2553 lpfc_unregister_unused_fcf failed "
5980 "to read FCF record HBA state x%x\n",
5981 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005982 }
James Smartecfd03c2010-02-12 14:41:27 -05005983}
5984
5985/**
5986 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5987 * @phba: Pointer to hba context object.
5988 *
5989 * This function just unregisters the currently reigstered FCF. It does not
5990 * try to find another FCF for discovery.
5991 */
5992void
5993lpfc_unregister_fcf(struct lpfc_hba *phba)
5994{
5995 int rc;
5996
5997 /* Preparation for unregistering fcf */
5998 rc = lpfc_unregister_fcf_prep(phba);
5999 if (rc) {
6000 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6001 "2749 Failed to prepare for unregistering "
6002 "HBA's FCF record: rc=%d\n", rc);
6003 return;
6004 }
6005
6006 /* Now, unregister FCF record and reset HBA FCF state */
6007 rc = lpfc_sli4_unregister_fcf(phba);
6008 if (rc)
6009 return;
6010 /* Set proper HBA FCF states after successful unregister FCF */
6011 spin_lock_irq(&phba->hbalock);
6012 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6013 spin_unlock_irq(&phba->hbalock);
6014}
6015
6016/**
6017 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6018 * @phba: Pointer to hba context object.
6019 *
6020 * This function check if there are any connected remote port for the FCF and
6021 * if all the devices are disconnected, this function unregister FCFI.
6022 * This function also tries to use another FCF for discovery.
6023 */
6024void
6025lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6026{
6027 /*
James Smart3804dc82010-07-14 15:31:37 -04006028 * If HBA is not running in FIP mode, if HBA does not support
6029 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6030 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006031 */
6032 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006033 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006034 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006035 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006036 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006037 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006038 spin_unlock_irq(&phba->hbalock);
6039 return;
6040 }
6041 spin_unlock_irq(&phba->hbalock);
6042
6043 if (lpfc_fcf_inuse(phba))
6044 return;
6045
6046 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006047}
6048
6049/**
6050 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6051 * @phba: Pointer to hba context object.
6052 * @buff: Buffer containing the FCF connection table as in the config
6053 * region.
6054 * This function create driver data structure for the FCF connection
6055 * record table read from config region 23.
6056 */
6057static void
6058lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6059 uint8_t *buff)
6060{
6061 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6062 struct lpfc_fcf_conn_hdr *conn_hdr;
6063 struct lpfc_fcf_conn_rec *conn_rec;
6064 uint32_t record_count;
6065 int i;
6066
6067 /* Free the current connect table */
6068 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006069 &phba->fcf_conn_rec_list, list) {
6070 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006071 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006072 }
James Smart6fb120a2009-05-22 14:52:59 -04006073
6074 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6075 record_count = conn_hdr->length * sizeof(uint32_t)/
6076 sizeof(struct lpfc_fcf_conn_rec);
6077
6078 conn_rec = (struct lpfc_fcf_conn_rec *)
6079 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6080
6081 for (i = 0; i < record_count; i++) {
6082 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6083 continue;
6084 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6085 GFP_KERNEL);
6086 if (!conn_entry) {
6087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6088 "2566 Failed to allocate connection"
6089 " table entry\n");
6090 return;
6091 }
6092
6093 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6094 sizeof(struct lpfc_fcf_conn_rec));
6095 conn_entry->conn_rec.vlan_tag =
6096 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
6097 conn_entry->conn_rec.flags =
6098 le16_to_cpu(conn_entry->conn_rec.flags);
6099 list_add_tail(&conn_entry->list,
6100 &phba->fcf_conn_rec_list);
6101 }
6102}
6103
6104/**
6105 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6106 * @phba: Pointer to hba context object.
6107 * @buff: Buffer containing the FCoE parameter data structure.
6108 *
6109 * This function update driver data structure with config
6110 * parameters read from config region 23.
6111 */
6112static void
6113lpfc_read_fcoe_param(struct lpfc_hba *phba,
6114 uint8_t *buff)
6115{
6116 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6117 struct lpfc_fcoe_params *fcoe_param;
6118
6119 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6120 buff;
6121 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006122 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006123
6124 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6125 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6126 return;
6127
James Smart6fb120a2009-05-22 14:52:59 -04006128 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6129 phba->valid_vlan = 1;
6130 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6131 0xFFF;
6132 }
6133
6134 phba->fc_map[0] = fcoe_param->fc_map[0];
6135 phba->fc_map[1] = fcoe_param->fc_map[1];
6136 phba->fc_map[2] = fcoe_param->fc_map[2];
6137 return;
6138}
6139
6140/**
6141 * lpfc_get_rec_conf23 - Get a record type in config region data.
6142 * @buff: Buffer containing config region 23 data.
6143 * @size: Size of the data buffer.
6144 * @rec_type: Record type to be searched.
6145 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006146 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006147 * of the record specified by record_type. If record found, this
6148 * function return pointer to the record else return NULL.
6149 */
6150static uint8_t *
6151lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6152{
6153 uint32_t offset = 0, rec_length;
6154
6155 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6156 (size < sizeof(uint32_t)))
6157 return NULL;
6158
6159 rec_length = buff[offset + 1];
6160
6161 /*
6162 * One TLV record has one word header and number of data words
6163 * specified in the rec_length field of the record header.
6164 */
6165 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6166 <= size) {
6167 if (buff[offset] == rec_type)
6168 return &buff[offset];
6169
6170 if (buff[offset] == LPFC_REGION23_LAST_REC)
6171 return NULL;
6172
6173 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6174 rec_length = buff[offset + 1];
6175 }
6176 return NULL;
6177}
6178
6179/**
6180 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6181 * @phba: Pointer to lpfc_hba data structure.
6182 * @buff: Buffer containing config region 23 data.
6183 * @size: Size of the data buffer.
6184 *
Stefan Weileef35c22010-08-06 21:11:15 +02006185 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006186 * populate driver data structure with the parameters.
6187 */
6188void
6189lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6190 uint8_t *buff,
6191 uint32_t size)
6192{
6193 uint32_t offset = 0, rec_length;
6194 uint8_t *rec_ptr;
6195
6196 /*
6197 * If data size is less than 2 words signature and version cannot be
6198 * verified.
6199 */
6200 if (size < 2*sizeof(uint32_t))
6201 return;
6202
6203 /* Check the region signature first */
6204 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6205 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6206 "2567 Config region 23 has bad signature\n");
6207 return;
6208 }
6209
6210 offset += 4;
6211
6212 /* Check the data structure version */
6213 if (buff[offset] != LPFC_REGION23_VERSION) {
6214 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6215 "2568 Config region 23 has bad version\n");
6216 return;
6217 }
6218 offset += 4;
6219
6220 rec_length = buff[offset + 1];
6221
6222 /* Read FCoE param record */
6223 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6224 size - offset, FCOE_PARAM_TYPE);
6225 if (rec_ptr)
6226 lpfc_read_fcoe_param(phba, rec_ptr);
6227
6228 /* Read FCF connection table */
6229 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6230 size - offset, FCOE_CONN_TBL_TYPE);
6231 if (rec_ptr)
6232 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6233
6234}