blob: 6e74a75fa8fd06a36e2bd5a79f90c3e63577cc24 [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;
1796 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001797 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001798 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001799 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1800 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001801 "2726 READ_FCF_RECORD Indicates empty "
1802 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001803 else
1804 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001805 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001806 "with status x%x add_status x%x, "
1807 "mbx\n", shdr_status, shdr_add_status);
1808 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001809 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001810
1811 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001812 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1813 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1814 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001815 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001816 new_fcf_record = (struct fcf_record *)(virt_addr +
1817 sizeof(struct lpfc_mbx_read_fcf_tbl));
1818 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001819 offsetof(struct fcf_record, vlan_bitmap));
1820 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1821 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001822
James Smart0c9ab6f2010-02-26 14:15:57 -05001823 return new_fcf_record;
1824}
1825
1826/**
1827 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1828 * @phba: pointer to lpfc hba data structure.
1829 * @fcf_record: pointer to the fcf record.
1830 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1831 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1832 *
1833 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1834 * enabled.
1835 **/
1836static void
1837lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1838 struct fcf_record *fcf_record,
1839 uint16_t vlan_id,
1840 uint16_t next_fcf_index)
1841{
1842 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1843 "2764 READ_FCF_RECORD:\n"
1844 "\tFCF_Index : x%x\n"
1845 "\tFCF_Avail : x%x\n"
1846 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001847 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001848 "\tFIP_Priority : x%x\n"
1849 "\tMAC_Provider : x%x\n"
1850 "\tLowest VLANID : x%x\n"
1851 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1852 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1853 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1854 "\tNext_FCF_Index: x%x\n",
1855 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1856 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1857 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001858 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001859 fcf_record->fip_priority,
1860 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1861 vlan_id,
1862 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1863 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1864 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1865 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1866 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1867 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1868 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1869 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1870 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1871 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1872 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1873 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1874 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1875 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1876 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1877 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1878 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1879 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1880 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1881 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1882 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1883 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1884 next_fcf_index);
1885}
1886
1887/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001888 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1889 * @phba: pointer to lpfc hba data structure.
1890 * @fcf_rec: pointer to an existing FCF record.
1891 * @new_fcf_record: pointer to a new FCF record.
1892 * @new_vlan_id: vlan id from the new FCF record.
1893 *
1894 * This function performs matching test of a new FCF record against an existing
1895 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1896 * will not be used as part of the FCF record matching criteria.
1897 *
1898 * Returns true if all the fields matching, otherwise returns false.
1899 */
1900static bool
1901lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1902 struct lpfc_fcf_rec *fcf_rec,
1903 struct fcf_record *new_fcf_record,
1904 uint16_t new_vlan_id)
1905{
1906 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1907 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1908 return false;
1909 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1910 return false;
1911 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1912 return false;
1913 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1914 return false;
James Smart7d791df2011-07-22 18:37:52 -04001915 if (fcf_rec->priority != new_fcf_record->fip_priority)
1916 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001917 return true;
1918}
1919
1920/**
James Smarta93ff372010-10-22 11:06:08 -04001921 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1922 * @vport: Pointer to vport object.
1923 * @fcf_index: index to next fcf.
1924 *
1925 * This function processing the roundrobin fcf failover to next fcf index.
1926 * When this function is invoked, there will be a current fcf registered
1927 * for flogi.
1928 * Return: 0 for continue retrying flogi on currently registered fcf;
1929 * 1 for stop flogi on currently registered fcf;
1930 */
1931int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1932{
1933 struct lpfc_hba *phba = vport->phba;
1934 int rc;
1935
1936 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1937 spin_lock_irq(&phba->hbalock);
1938 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1939 spin_unlock_irq(&phba->hbalock);
1940 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1941 "2872 Devloss tmo with no eligible "
1942 "FCF, unregister in-use FCF (x%x) "
1943 "and rescan FCF table\n",
1944 phba->fcf.current_rec.fcf_indx);
1945 lpfc_unregister_fcf_rescan(phba);
1946 goto stop_flogi_current_fcf;
1947 }
1948 /* Mark the end to FLOGI roundrobin failover */
1949 phba->hba_flag &= ~FCF_RR_INPROG;
1950 /* Allow action to new fcf asynchronous event */
1951 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1952 spin_unlock_irq(&phba->hbalock);
1953 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1954 "2865 No FCF available, stop roundrobin FCF "
1955 "failover and change port state:x%x/x%x\n",
1956 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1957 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1958 goto stop_flogi_current_fcf;
1959 } else {
1960 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1961 "2794 Try FLOGI roundrobin FCF failover to "
1962 "(x%x)\n", fcf_index);
1963 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1964 if (rc)
1965 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1966 "2761 FLOGI roundrobin FCF failover "
1967 "failed (rc:x%x) to read FCF (x%x)\n",
1968 rc, phba->fcf.current_rec.fcf_indx);
1969 else
1970 goto stop_flogi_current_fcf;
1971 }
1972 return 0;
1973
1974stop_flogi_current_fcf:
1975 lpfc_can_disctmo(vport);
1976 return 1;
1977}
1978
1979/**
James Smart7d791df2011-07-22 18:37:52 -04001980 * lpfc_sli4_fcf_pri_list_del
1981 * @phba: pointer to lpfc hba data structure.
1982 * @fcf_index the index of the fcf record to delete
1983 * This routine checks the on list flag of the fcf_index to be deleted.
1984 * If it is one the list then it is removed from the list, and the flag
1985 * is cleared. This routine grab the hbalock before removing the fcf
1986 * record from the list.
1987 **/
1988static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1989 uint16_t fcf_index)
1990{
1991 struct lpfc_fcf_pri *new_fcf_pri;
1992
1993 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1994 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1995 "3058 deleting idx x%x pri x%x flg x%x\n",
1996 fcf_index, new_fcf_pri->fcf_rec.priority,
1997 new_fcf_pri->fcf_rec.flag);
1998 spin_lock_irq(&phba->hbalock);
1999 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2000 if (phba->fcf.current_rec.priority ==
2001 new_fcf_pri->fcf_rec.priority)
2002 phba->fcf.eligible_fcf_cnt--;
2003 list_del_init(&new_fcf_pri->list);
2004 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2005 }
2006 spin_unlock_irq(&phba->hbalock);
2007}
2008
2009/**
2010 * lpfc_sli4_set_fcf_flogi_fail
2011 * @phba: pointer to lpfc hba data structure.
2012 * @fcf_index the index of the fcf record to update
2013 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2014 * flag so the the round robin slection for the particular priority level
2015 * will try a different fcf record that does not have this bit set.
2016 * If the fcf record is re-read for any reason this flag is cleared brfore
2017 * adding it to the priority list.
2018 **/
2019void
2020lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2021{
2022 struct lpfc_fcf_pri *new_fcf_pri;
2023 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2024 spin_lock_irq(&phba->hbalock);
2025 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2026 spin_unlock_irq(&phba->hbalock);
2027}
2028
2029/**
2030 * lpfc_sli4_fcf_pri_list_add
2031 * @phba: pointer to lpfc hba data structure.
2032 * @fcf_index the index of the fcf record to add
2033 * This routine checks the priority of the fcf_index to be added.
2034 * If it is a lower priority than the current head of the fcf_pri list
2035 * then it is added to the list in the right order.
2036 * If it is the same priority as the current head of the list then it
2037 * is added to the head of the list and its bit in the rr_bmask is set.
2038 * If the fcf_index to be added is of a higher priority than the current
2039 * head of the list then the rr_bmask is cleared, its bit is set in the
2040 * rr_bmask and it is added to the head of the list.
2041 * returns:
2042 * 0=success 1=failure
2043 **/
2044int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba, uint16_t fcf_index,
2045 struct fcf_record *new_fcf_record)
2046{
2047 uint16_t current_fcf_pri;
2048 uint16_t last_index;
2049 struct lpfc_fcf_pri *fcf_pri;
2050 struct lpfc_fcf_pri *next_fcf_pri;
2051 struct lpfc_fcf_pri *new_fcf_pri;
2052 int ret;
2053
2054 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2055 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2056 "3059 adding idx x%x pri x%x flg x%x\n",
2057 fcf_index, new_fcf_record->fip_priority,
2058 new_fcf_pri->fcf_rec.flag);
2059 spin_lock_irq(&phba->hbalock);
2060 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2061 list_del_init(&new_fcf_pri->list);
2062 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2063 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2064 if (list_empty(&phba->fcf.fcf_pri_list)) {
2065 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2066 ret = lpfc_sli4_fcf_rr_index_set(phba,
2067 new_fcf_pri->fcf_rec.fcf_index);
2068 goto out;
2069 }
2070
2071 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2072 LPFC_SLI4_FCF_TBL_INDX_MAX);
2073 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2074 ret = 0; /* Empty rr list */
2075 goto out;
2076 }
2077 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2078 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2079 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2080 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2081 memset(phba->fcf.fcf_rr_bmask, 0,
2082 sizeof(*phba->fcf.fcf_rr_bmask));
2083 /* fcfs_at_this_priority_level = 1; */
2084 phba->fcf.eligible_fcf_cnt = 1;
2085 } else
2086 /* fcfs_at_this_priority_level++; */
2087 phba->fcf.eligible_fcf_cnt++;
2088 ret = lpfc_sli4_fcf_rr_index_set(phba,
2089 new_fcf_pri->fcf_rec.fcf_index);
2090 goto out;
2091 }
2092
2093 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2094 &phba->fcf.fcf_pri_list, list) {
2095 if (new_fcf_pri->fcf_rec.priority <=
2096 fcf_pri->fcf_rec.priority) {
2097 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2098 list_add(&new_fcf_pri->list,
2099 &phba->fcf.fcf_pri_list);
2100 else
2101 list_add(&new_fcf_pri->list,
2102 &((struct lpfc_fcf_pri *)
2103 fcf_pri->list.prev)->list);
2104 ret = 0;
2105 goto out;
2106 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2107 || new_fcf_pri->fcf_rec.priority <
2108 next_fcf_pri->fcf_rec.priority) {
2109 list_add(&new_fcf_pri->list, &fcf_pri->list);
2110 ret = 0;
2111 goto out;
2112 }
2113 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2114 continue;
2115
2116 }
2117 ret = 1;
2118out:
2119 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2120 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2121 spin_unlock_irq(&phba->hbalock);
2122 return ret;
2123}
2124
2125/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002126 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2127 * @phba: pointer to lpfc hba data structure.
2128 * @mboxq: pointer to mailbox object.
2129 *
2130 * This function iterates through all the fcf records available in
2131 * HBA and chooses the optimal FCF record for discovery. After finding
2132 * the FCF for discovery it registers the FCF record and kicks start
2133 * discovery.
2134 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2135 * use an FCF record which matches fabric name and mac address of the
2136 * currently used FCF record.
2137 * If the driver supports only one FCF, it will try to use the FCF record
2138 * used by BOOT_BIOS.
2139 */
2140void
2141lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2142{
2143 struct fcf_record *new_fcf_record;
2144 uint32_t boot_flag, addr_mode;
2145 uint16_t fcf_index, next_fcf_index;
2146 struct lpfc_fcf_rec *fcf_rec = NULL;
2147 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002148 uint32_t seed;
2149 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002150 int rc;
2151
2152 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002153 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002154 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2155 return;
2156 }
2157
2158 /* Parse the FCF record from the non-embedded mailbox command */
2159 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2160 &next_fcf_index);
2161 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002162 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002163 "2765 Mailbox command READ_FCF_RECORD "
2164 "failed to retrieve a FCF record.\n");
2165 /* Let next new FCF event trigger fast failover */
2166 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002167 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002168 spin_unlock_irq(&phba->hbalock);
2169 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2170 return;
2171 }
2172
2173 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002174 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2175 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002176
2177 /* Log the FCF record information if turned on */
2178 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2179 next_fcf_index);
2180
James Smart6fb120a2009-05-22 14:52:59 -04002181 /*
2182 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002183 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002184 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002185 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002186 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002187 lpfc_sli4_fcf_pri_list_del(phba,
2188 bf_get(lpfc_fcf_record_fcf_index,
2189 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002190 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002191 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002192 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002193 bf_get(lpfc_fcf_record_fcf_index,
2194 new_fcf_record),
2195 bf_get(lpfc_fcf_record_fcf_avail,
2196 new_fcf_record),
2197 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002198 new_fcf_record),
2199 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002200 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002201 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2202 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2203 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002204 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2205 phba->fcf.current_rec.fcf_indx) {
2206 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2207 "2862 FCF (x%x) matches property "
2208 "of in-use FCF (x%x)\n",
2209 bf_get(lpfc_fcf_record_fcf_index,
2210 new_fcf_record),
2211 phba->fcf.current_rec.fcf_indx);
2212 goto read_next_fcf;
2213 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002214 /*
2215 * In case the current in-use FCF record becomes
2216 * invalid/unavailable during FCF discovery that
2217 * was not triggered by fast FCF failover process,
2218 * treat it as fast FCF failover.
2219 */
2220 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2221 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2222 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2223 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002224 "(x%x), enter FCF failover "
2225 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002226 phba->fcf.current_rec.fcf_indx);
2227 spin_lock_irq(&phba->hbalock);
2228 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2229 spin_unlock_irq(&phba->hbalock);
2230 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2231 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2232 LPFC_FCOE_FCF_GET_FIRST);
2233 return;
2234 }
2235 }
James Smart6fb120a2009-05-22 14:52:59 -04002236 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002237 } else {
2238 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002239 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2240 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002241 if (rc)
2242 goto read_next_fcf;
2243 }
2244
James Smart6fb120a2009-05-22 14:52:59 -04002245 /*
2246 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002247 * FCF record for the discovery. The condition that a rescan
2248 * matches the in-use FCF record: fabric name, switch name, mac
2249 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002250 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002251 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002252 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002253 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2254 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002255 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002256 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2257 phba->fcf.current_rec.fcf_indx) {
2258 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2259 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2260 /* Stop FCF redisc wait timer */
2261 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2262 phba);
2263 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2264 /* Fast failover, mark completed */
2265 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2266 spin_unlock_irq(&phba->hbalock);
2267 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2268 "2836 New FCF matches in-use "
2269 "FCF (x%x)\n",
2270 phba->fcf.current_rec.fcf_indx);
2271 goto out;
2272 } else
2273 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2274 "2863 New FCF (x%x) matches "
2275 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002276 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002277 new_fcf_record),
2278 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002279 }
James Smartecfd03c2010-02-12 14:41:27 -05002280 /*
2281 * Read next FCF record from HBA searching for the matching
2282 * with in-use record only if not during the fast failover
2283 * period. In case of fast failover period, it shall try to
2284 * determine whether the FCF record just read should be the
2285 * next candidate.
2286 */
2287 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002288 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002289 goto read_next_fcf;
2290 }
James Smart6fb120a2009-05-22 14:52:59 -04002291 }
James Smartecfd03c2010-02-12 14:41:27 -05002292 /*
2293 * Update on failover FCF record only if it's in FCF fast-failover
2294 * period; otherwise, update on current FCF record.
2295 */
James Smartfc2b9892010-02-26 14:15:29 -05002296 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2297 fcf_rec = &phba->fcf.failover_rec;
2298 else
James Smartecfd03c2010-02-12 14:41:27 -05002299 fcf_rec = &phba->fcf.current_rec;
2300
James Smart6fb120a2009-05-22 14:52:59 -04002301 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2302 /*
James Smartecfd03c2010-02-12 14:41:27 -05002303 * If the driver FCF record does not have boot flag
2304 * set and new hba fcf record has boot flag set, use
2305 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002306 */
James Smartecfd03c2010-02-12 14:41:27 -05002307 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2308 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002309 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2310 "2837 Update current FCF record "
2311 "(x%x) with new FCF record (x%x)\n",
2312 fcf_rec->fcf_indx,
2313 bf_get(lpfc_fcf_record_fcf_index,
2314 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002315 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2316 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002317 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002318 goto read_next_fcf;
2319 }
2320 /*
James Smartecfd03c2010-02-12 14:41:27 -05002321 * If the driver FCF record has boot flag set and the
2322 * new hba FCF record does not have boot flag, read
2323 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002324 */
James Smartecfd03c2010-02-12 14:41:27 -05002325 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002326 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002327 goto read_next_fcf;
2328 }
2329 /*
James Smartecfd03c2010-02-12 14:41:27 -05002330 * If the new hba FCF record has lower priority value
2331 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002332 */
James Smartfc2b9892010-02-26 14:15:29 -05002333 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002334 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002335 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2336 "2838 Update current FCF record "
2337 "(x%x) with new FCF record (x%x)\n",
2338 fcf_rec->fcf_indx,
2339 bf_get(lpfc_fcf_record_fcf_index,
2340 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002341 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2342 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002343 /* Reset running random FCF selection count */
2344 phba->fcf.eligible_fcf_cnt = 1;
2345 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2346 /* Update running random FCF selection count */
2347 phba->fcf.eligible_fcf_cnt++;
2348 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2349 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002350 if (select_new_fcf) {
2351 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2352 "2839 Update current FCF record "
2353 "(x%x) with new FCF record (x%x)\n",
2354 fcf_rec->fcf_indx,
2355 bf_get(lpfc_fcf_record_fcf_index,
2356 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002357 /* Choose the new FCF by random selection */
2358 __lpfc_update_fcf_record(phba, fcf_rec,
2359 new_fcf_record,
2360 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002361 }
James Smart6fb120a2009-05-22 14:52:59 -04002362 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002363 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002364 goto read_next_fcf;
2365 }
2366 /*
James Smartecfd03c2010-02-12 14:41:27 -05002367 * This is the first suitable FCF record, choose this record for
2368 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002369 */
James Smartecfd03c2010-02-12 14:41:27 -05002370 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002371 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002372 "2840 Update initial FCF candidate "
2373 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002374 bf_get(lpfc_fcf_record_fcf_index,
2375 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002376 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2377 addr_mode, vlan_id, (boot_flag ?
2378 BOOT_ENABLE : 0));
2379 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002380 /* Setup initial running random FCF selection count */
2381 phba->fcf.eligible_fcf_cnt = 1;
2382 /* Seeding the random number generator for random selection */
2383 seed = (uint32_t)(0xFFFFFFFF & jiffies);
2384 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04002385 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002386 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002387 goto read_next_fcf;
2388
2389read_next_fcf:
2390 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002391 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2392 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2393 /*
2394 * Case of FCF fast failover scan
2395 */
2396
2397 /*
2398 * It has not found any suitable FCF record, cancel
2399 * FCF scan inprogress, and do nothing
2400 */
2401 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002402 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002403 "2782 No suitable FCF found: "
2404 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002405 phba->fcoe_eventtag_at_fcf_scan,
2406 bf_get(lpfc_fcf_record_fcf_index,
2407 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002408 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002409 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2410 phba->hba_flag &= ~FCF_TS_INPROG;
2411 spin_unlock_irq(&phba->hbalock);
2412 /* Unregister in-use FCF and rescan */
2413 lpfc_printf_log(phba, KERN_INFO,
2414 LOG_FIP,
2415 "2864 On devloss tmo "
2416 "unreg in-use FCF and "
2417 "rescan FCF table\n");
2418 lpfc_unregister_fcf_rescan(phba);
2419 return;
2420 }
2421 /*
2422 * Let next new FCF event trigger fast failover
2423 */
2424 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002425 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002426 return;
2427 }
2428 /*
2429 * It has found a suitable FCF record that is not
2430 * the same as in-use FCF record, unregister the
2431 * in-use FCF record, replace the in-use FCF record
2432 * with the new FCF record, mark FCF fast failover
2433 * completed, and then start register the new FCF
2434 * record.
2435 */
2436
James Smart0c9ab6f2010-02-26 14:15:57 -05002437 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002438 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002439
2440 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002441 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002442 "2842 Replace in-use FCF (x%x) "
2443 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002444 phba->fcf.current_rec.fcf_indx,
2445 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002446 memcpy(&phba->fcf.current_rec,
2447 &phba->fcf.failover_rec,
2448 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002449 /*
2450 * Mark the fast FCF failover rediscovery completed
2451 * and the start of the first round of the roundrobin
2452 * FCF failover.
2453 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002454 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002455 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002456 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002457 /* Register to the new FCF record */
2458 lpfc_register_fcf(phba);
2459 } else {
2460 /*
2461 * In case of transaction period to fast FCF failover,
2462 * do nothing when search to the end of the FCF table.
2463 */
2464 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2465 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2466 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002467
James Smart7d791df2011-07-22 18:37:52 -04002468 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2469 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002470 /*
2471 * In case the current in-use FCF record no
2472 * longer existed during FCF discovery that
2473 * was not triggered by fast FCF failover
2474 * process, treat it as fast FCF failover.
2475 */
2476 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2477 "2841 In-use FCF record (x%x) "
2478 "not reported, entering fast "
2479 "FCF failover mode scanning.\n",
2480 phba->fcf.current_rec.fcf_indx);
2481 spin_lock_irq(&phba->hbalock);
2482 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2483 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002484 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2485 LPFC_FCOE_FCF_GET_FIRST);
2486 return;
2487 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002488 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002489 lpfc_register_fcf(phba);
2490 }
2491 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002492 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002493 return;
2494
2495out:
2496 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2497 lpfc_register_fcf(phba);
2498
2499 return;
2500}
2501
2502/**
James Smarta93ff372010-10-22 11:06:08 -04002503 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002504 * @phba: pointer to lpfc hba data structure.
2505 * @mboxq: pointer to mailbox object.
2506 *
James Smarta93ff372010-10-22 11:06:08 -04002507 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002508 * read FCF record mailbox command from the eligible FCF record bmask for
2509 * performing the failover. If the FCF read back is not valid/available, it
2510 * fails through to retrying FLOGI to the currently registered FCF again.
2511 * Otherwise, if the FCF read back is valid and available, it will set the
2512 * newly read FCF record to the failover FCF record, unregister currently
2513 * registered FCF record, copy the failover FCF record to the current
2514 * FCF record, and then register the current FCF record before proceeding
2515 * to trying FLOGI on the new failover FCF.
2516 */
2517void
2518lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2519{
2520 struct fcf_record *new_fcf_record;
2521 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002522 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002523 uint16_t current_fcf_index;
2524 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002525 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002526
James Smarta93ff372010-10-22 11:06:08 -04002527 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002528 if (phba->link_state < LPFC_LINK_UP) {
2529 spin_lock_irq(&phba->hbalock);
2530 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002531 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002532 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002533 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002534 }
2535
2536 /* Parse the FCF record from the non-embedded mailbox command */
2537 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2538 &next_fcf_index);
2539 if (!new_fcf_record) {
2540 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2541 "2766 Mailbox command READ_FCF_RECORD "
2542 "failed to retrieve a FCF record.\n");
James Smarta93ff372010-10-22 11:06:08 -04002543 goto error_out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002544 }
2545
2546 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002547 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2548 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002549
2550 /* Log the FCF record information if turned on */
2551 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2552 next_fcf_index);
2553
James Smarta93ff372010-10-22 11:06:08 -04002554 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2555 if (!rc) {
2556 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2557 "2848 Remove ineligible FCF (x%x) from "
2558 "from roundrobin bmask\n", fcf_index);
2559 /* Clear roundrobin bmask bit for ineligible FCF */
2560 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2561 /* Perform next round of roundrobin FCF failover */
2562 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2563 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2564 if (rc)
2565 goto out;
2566 goto error_out;
2567 }
2568
2569 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2570 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2571 "2760 Perform FLOGI roundrobin FCF failover: "
2572 "FCF (x%x) back to FCF (x%x)\n",
2573 phba->fcf.current_rec.fcf_indx, fcf_index);
2574 /* Wait 500 ms before retrying FLOGI to current FCF */
2575 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002576 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002577 goto out;
2578 }
2579
James Smart0c9ab6f2010-02-26 14:15:57 -05002580 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002581 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002582 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2583 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002584 spin_lock_irq(&phba->hbalock);
2585 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2586 new_fcf_record, addr_mode, vlan_id,
2587 (boot_flag ? BOOT_ENABLE : 0));
2588 spin_unlock_irq(&phba->hbalock);
2589
2590 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2591
2592 /* Unregister the current in-use FCF record */
2593 lpfc_unregister_fcf(phba);
2594
2595 /* Replace in-use record with the new record */
2596 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2597 sizeof(struct lpfc_fcf_rec));
2598
2599 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002600 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2601 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002602
James Smarta93ff372010-10-22 11:06:08 -04002603error_out:
2604 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002605out:
2606 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002607}
2608
2609/**
2610 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2611 * @phba: pointer to lpfc hba data structure.
2612 * @mboxq: pointer to mailbox object.
2613 *
2614 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002615 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002616 * failover when a new FCF event happened. If the FCF read back is
2617 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002618 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002619 */
2620void
2621lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2622{
2623 struct fcf_record *new_fcf_record;
2624 uint32_t boot_flag, addr_mode;
2625 uint16_t fcf_index, next_fcf_index;
2626 uint16_t vlan_id;
2627 int rc;
2628
2629 /* If link state is not up, no need to proceed */
2630 if (phba->link_state < LPFC_LINK_UP)
2631 goto out;
2632
2633 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002634 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002635 goto out;
2636
2637 /* Parse the FCF record from the non-embedded mailbox command */
2638 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2639 &next_fcf_index);
2640 if (!new_fcf_record) {
2641 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2642 "2767 Mailbox command READ_FCF_RECORD "
2643 "failed to retrieve a FCF record.\n");
2644 goto out;
2645 }
2646
2647 /* Check the connection list for eligibility */
2648 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2649 &addr_mode, &vlan_id);
2650
2651 /* Log the FCF record information if turned on */
2652 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2653 next_fcf_index);
2654
2655 if (!rc)
2656 goto out;
2657
2658 /* Update the eligible FCF record index bmask */
2659 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002660
2661 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002662
2663out:
2664 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2665}
2666
2667/**
James Smart76a95d72010-11-20 23:11:48 -05002668 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2669 * @phba: pointer to lpfc hba data structure.
2670 * @mboxq: pointer to mailbox data structure.
2671 *
2672 * This function handles completion of init vfi mailbox command.
2673 */
2674void
2675lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2676{
2677 struct lpfc_vport *vport = mboxq->vport;
2678
James Smartdf9e1b52011-12-13 13:22:17 -05002679 /*
2680 * VFI not supported on interface type 0, just do the flogi
2681 * Also continue if the VFI is in use - just use the same one.
2682 */
2683 if (mboxq->u.mb.mbxStatus &&
2684 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2685 LPFC_SLI_INTF_IF_TYPE_0) &&
2686 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002687 lpfc_printf_vlog(vport, KERN_ERR,
2688 LOG_MBOX,
2689 "2891 Init VFI mailbox failed 0x%x\n",
2690 mboxq->u.mb.mbxStatus);
2691 mempool_free(mboxq, phba->mbox_mem_pool);
2692 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2693 return;
2694 }
James Smart73d91e52011-10-10 21:32:10 -04002695
James Smart76a95d72010-11-20 23:11:48 -05002696 lpfc_initial_flogi(vport);
2697 mempool_free(mboxq, phba->mbox_mem_pool);
2698 return;
2699}
2700
2701/**
2702 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2703 * @vport: pointer to lpfc_vport data structure.
2704 *
2705 * This function issue a init_vfi mailbox command to initialize the VFI and
2706 * VPI for the physical port.
2707 */
2708void
2709lpfc_issue_init_vfi(struct lpfc_vport *vport)
2710{
2711 LPFC_MBOXQ_t *mboxq;
2712 int rc;
2713 struct lpfc_hba *phba = vport->phba;
2714
2715 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2716 if (!mboxq) {
2717 lpfc_printf_vlog(vport, KERN_ERR,
2718 LOG_MBOX, "2892 Failed to allocate "
2719 "init_vfi mailbox\n");
2720 return;
2721 }
2722 lpfc_init_vfi(mboxq, vport);
2723 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2724 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2725 if (rc == MBX_NOT_FINISHED) {
2726 lpfc_printf_vlog(vport, KERN_ERR,
2727 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2728 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2729 }
2730}
2731
2732/**
James Smart1c6834a2009-07-19 10:01:26 -04002733 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2734 * @phba: pointer to lpfc hba data structure.
2735 * @mboxq: pointer to mailbox data structure.
2736 *
2737 * This function handles completion of init vpi mailbox command.
2738 */
James Smart695a8142010-01-26 23:08:03 -05002739void
James Smart1c6834a2009-07-19 10:01:26 -04002740lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2741{
2742 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002743 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002744 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2745
James Smart1c6834a2009-07-19 10:01:26 -04002746 if (mboxq->u.mb.mbxStatus) {
2747 lpfc_printf_vlog(vport, KERN_ERR,
2748 LOG_MBOX,
2749 "2609 Init VPI mailbox failed 0x%x\n",
2750 mboxq->u.mb.mbxStatus);
2751 mempool_free(mboxq, phba->mbox_mem_pool);
2752 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2753 return;
2754 }
James Smart72100cc2010-02-12 14:43:01 -05002755 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002756 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002757 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002758
James Smart695a8142010-01-26 23:08:03 -05002759 /* If this port is physical port or FDISC is done, do reg_vpi */
2760 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2761 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2762 if (!ndlp)
2763 lpfc_printf_vlog(vport, KERN_ERR,
2764 LOG_DISCOVERY,
2765 "2731 Cannot find fabric "
2766 "controller node\n");
2767 else
2768 lpfc_register_new_vport(phba, vport, ndlp);
2769 mempool_free(mboxq, phba->mbox_mem_pool);
2770 return;
2771 }
2772
James Smart1c6834a2009-07-19 10:01:26 -04002773 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2774 lpfc_initial_fdisc(vport);
2775 else {
2776 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002777 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2778 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002779 }
James Smart695a8142010-01-26 23:08:03 -05002780 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002781 return;
2782}
2783
2784/**
James Smartecfd03c2010-02-12 14:41:27 -05002785 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2786 * @vport: pointer to lpfc_vport data structure.
2787 *
2788 * This function issue a init_vpi mailbox command to initialize
2789 * VPI for the vport.
2790 */
2791void
2792lpfc_issue_init_vpi(struct lpfc_vport *vport)
2793{
2794 LPFC_MBOXQ_t *mboxq;
2795 int rc;
2796
2797 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2798 if (!mboxq) {
2799 lpfc_printf_vlog(vport, KERN_ERR,
2800 LOG_MBOX, "2607 Failed to allocate "
2801 "init_vpi mailbox\n");
2802 return;
2803 }
2804 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2805 mboxq->vport = vport;
2806 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2807 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2808 if (rc == MBX_NOT_FINISHED) {
2809 lpfc_printf_vlog(vport, KERN_ERR,
2810 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2811 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2812 }
2813}
2814
2815/**
James Smart6fb120a2009-05-22 14:52:59 -04002816 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2817 * @phba: pointer to lpfc hba data structure.
2818 *
2819 * This function loops through the list of vports on the @phba and issues an
2820 * FDISC if possible.
2821 */
2822void
2823lpfc_start_fdiscs(struct lpfc_hba *phba)
2824{
2825 struct lpfc_vport **vports;
2826 int i;
2827
2828 vports = lpfc_create_vport_work_array(phba);
2829 if (vports != NULL) {
2830 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2831 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2832 continue;
2833 /* There are no vpi for this vport */
2834 if (vports[i]->vpi > phba->max_vpi) {
2835 lpfc_vport_set_state(vports[i],
2836 FC_VPORT_FAILED);
2837 continue;
2838 }
James Smart76a95d72010-11-20 23:11:48 -05002839 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002840 lpfc_vport_set_state(vports[i],
2841 FC_VPORT_LINKDOWN);
2842 continue;
2843 }
James Smart1c6834a2009-07-19 10:01:26 -04002844 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002845 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002846 continue;
2847 }
James Smart6fb120a2009-05-22 14:52:59 -04002848 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2849 lpfc_initial_fdisc(vports[i]);
2850 else {
2851 lpfc_vport_set_state(vports[i],
2852 FC_VPORT_NO_FABRIC_SUPP);
2853 lpfc_printf_vlog(vports[i], KERN_ERR,
2854 LOG_ELS,
2855 "0259 No NPIV "
2856 "Fabric support\n");
2857 }
2858 }
2859 }
2860 lpfc_destroy_vport_work_array(phba, vports);
2861}
2862
2863void
2864lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2865{
2866 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2867 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002868 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002869
James Smartf5eca9b2012-03-01 22:37:54 -05002870 /*
2871 * VFI not supported for interface type 0, so ignore any mailbox
2872 * error (except VFI in use) and continue with the discovery.
2873 */
2874 if (mboxq->u.mb.mbxStatus &&
2875 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2876 LPFC_SLI_INTF_IF_TYPE_0) &&
2877 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002878 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2879 "2018 REG_VFI mbxStatus error x%x "
2880 "HBA state x%x\n",
2881 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002882 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002883 /* FLOGI failed, use loop map to make discovery list */
2884 lpfc_disc_list_loopmap(vport);
2885 /* Start discovery */
2886 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002887 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002888 }
2889 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002890 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002891 }
James Smartae05ebe2013-03-01 16:35:38 -05002892
2893 /* If the VFI is already registered, there is nothing else to do */
2894 if (vport->fc_flag & FC_VFI_REGISTERED)
2895 goto out_free_mem;
2896
James Smartc8685952009-11-18 15:39:16 -05002897 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002898 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002899 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002900 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002901 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002902 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002903 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002904
James Smart1b511972011-12-13 13:23:09 -05002905 /* In case SLI4 FC loopback test, we are ready */
2906 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2907 (phba->link_flag & LS_LOOPBACK_MODE)) {
2908 phba->link_state = LPFC_HBA_READY;
2909 goto out_free_mem;
2910 }
2911
James Smart6fb120a2009-05-22 14:52:59 -04002912 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002913 /*
2914 * For private loop or for NPort pt2pt,
2915 * just start discovery and we are done.
2916 */
2917 if ((vport->fc_flag & FC_PT2PT) ||
2918 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2919 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2920
James Smart76a95d72010-11-20 23:11:48 -05002921 /* Use loop map to make discovery list */
2922 lpfc_disc_list_loopmap(vport);
2923 /* Start discovery */
2924 lpfc_disc_start(vport);
2925 } else {
2926 lpfc_start_fdiscs(phba);
2927 lpfc_do_scr_ns_plogi(phba, vport);
2928 }
James Smart6fb120a2009-05-22 14:52:59 -04002929 }
2930
James Smart1b511972011-12-13 13:23:09 -05002931out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002932 mempool_free(mboxq, phba->mbox_mem_pool);
2933 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2934 kfree(dmabuf);
2935 return;
2936}
2937
2938static void
James Smart2e0fef82007-06-17 19:56:36 -05002939lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002940{
James Smart6fb120a2009-05-22 14:52:59 -04002941 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002942 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002943 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002944
2945
2946 /* Check for error */
2947 if (mb->mbxStatus) {
2948 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002949 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2950 "0319 READ_SPARAM mbxStatus error x%x "
2951 "hba state x%x>\n",
2952 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002953 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002954 goto out;
2955 }
2956
James Smart2e0fef82007-06-17 19:56:36 -05002957 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002958 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04002959 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002960 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2961 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2962 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2963 }
2964
dea31012005-04-17 16:05:31 -05002965 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2966 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002967 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002968 return;
2969
2970out:
2971 pmb->context1 = NULL;
2972 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2973 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002974 lpfc_issue_clear_la(phba, vport);
2975 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002976 return;
2977}
2978
2979static void
James Smart76a95d72010-11-20 23:11:48 -05002980lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05002981{
James Smart92d7f7b2007-06-17 19:56:38 -05002982 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002983 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05002984 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05002985 int i;
James Smart14691152006-12-02 13:34:28 -05002986 struct lpfc_dmabuf *mp;
2987 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002988 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002989
James Smart92d7f7b2007-06-17 19:56:38 -05002990 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05002991 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
2992 case LPFC_LINK_SPEED_1GHZ:
2993 case LPFC_LINK_SPEED_2GHZ:
2994 case LPFC_LINK_SPEED_4GHZ:
2995 case LPFC_LINK_SPEED_8GHZ:
2996 case LPFC_LINK_SPEED_10GHZ:
2997 case LPFC_LINK_SPEED_16GHZ:
2998 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04002999 break;
James Smart92d7f7b2007-06-17 19:56:38 -05003000 default:
James Smart76a95d72010-11-20 23:11:48 -05003001 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05003002 break;
dea31012005-04-17 16:05:31 -05003003 }
3004
James Smart76a95d72010-11-20 23:11:48 -05003005 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003006 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003007
James Smartdf9e1b52011-12-13 13:22:17 -05003008 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003009 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003010 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003011
James Smart6a9c52c2009-10-02 15:16:51 -04003012 /* if npiv is enabled and this adapter supports npiv log
3013 * a message that npiv is not supported in this topology
3014 */
3015 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003016 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3017 "1309 Link Up Event npiv not supported in loop "
3018 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003019 /* Get Loop Map information */
James Smartdf9e1b52011-12-13 13:22:17 -05003020 if (bf_get(lpfc_mbx_read_top_il, la)) {
James Smart72859902012-01-18 16:25:38 -05003021 spin_lock(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003022 vport->fc_flag |= FC_LBIT;
James Smart72859902012-01-18 16:25:38 -05003023 spin_unlock(shost->host_lock);
James Smartdf9e1b52011-12-13 13:22:17 -05003024 }
dea31012005-04-17 16:05:31 -05003025
James Smart76a95d72010-11-20 23:11:48 -05003026 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3027 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003028
3029 if (i == 0) {
3030 phba->alpa_map[0] = 0;
3031 } else {
James Smarte8b62012007-08-02 11:10:09 -04003032 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003033 int numalpa, j, k;
3034 union {
3035 uint8_t pamap[16];
3036 struct {
3037 uint32_t wd1;
3038 uint32_t wd2;
3039 uint32_t wd3;
3040 uint32_t wd4;
3041 } pa;
3042 } un;
3043 numalpa = phba->alpa_map[0];
3044 j = 0;
3045 while (j < numalpa) {
3046 memset(un.pamap, 0, 16);
3047 for (k = 1; j < numalpa; k++) {
3048 un.pamap[k - 1] =
3049 phba->alpa_map[j + 1];
3050 j++;
3051 if (k == 16)
3052 break;
3053 }
3054 /* Link Up Event ALPA map */
3055 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003056 KERN_WARNING,
3057 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003058 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003059 "ALPA map Data: x%x "
3060 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003061 un.pa.wd1, un.pa.wd2,
3062 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003063 }
3064 }
3065 }
3066 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003067 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003068 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003069 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003070 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3071 }
James Smart2e0fef82007-06-17 19:56:36 -05003072 vport->fc_myDID = phba->fc_pref_DID;
James Smart72859902012-01-18 16:25:38 -05003073 spin_lock(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003074 vport->fc_flag |= FC_LBIT;
James Smart72859902012-01-18 16:25:38 -05003075 spin_unlock(shost->host_lock);
dea31012005-04-17 16:05:31 -05003076 }
James Smart92d7f7b2007-06-17 19:56:38 -05003077 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003078
3079 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003080 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3081 if (!sparam_mbox)
3082 goto out;
3083
3084 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3085 if (rc) {
3086 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3087 goto out;
3088 }
3089 sparam_mbox->vport = vport;
3090 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3091 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3092 if (rc == MBX_NOT_FINISHED) {
3093 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3094 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3095 kfree(mp);
3096 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3097 goto out;
dea31012005-04-17 16:05:31 -05003098 }
3099
James Smart76a95d72010-11-20 23:11:48 -05003100 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003101 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3102 if (!cfglink_mbox)
3103 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003104 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003105 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003106 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003107 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003108 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003109 if (rc == MBX_NOT_FINISHED) {
3110 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3111 goto out;
3112 }
3113 } else {
James Smart32b97932009-07-19 10:01:21 -04003114 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003115 /*
3116 * Add the driver's default FCF record at FCF index 0 now. This
3117 * is phase 1 implementation that support FCF index 0 and driver
3118 * defaults.
3119 */
James Smart45ed1192009-10-02 15:17:02 -04003120 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003121 fcf_record = kzalloc(sizeof(struct fcf_record),
3122 GFP_KERNEL);
3123 if (unlikely(!fcf_record)) {
3124 lpfc_printf_log(phba, KERN_ERR,
3125 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003126 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003127 "fcf record\n");
3128 rc = -ENODEV;
3129 goto out;
3130 }
3131
3132 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3133 LPFC_FCOE_FCF_DEF_INDEX);
3134 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3135 if (unlikely(rc)) {
3136 lpfc_printf_log(phba, KERN_ERR,
3137 LOG_MBOX | LOG_SLI,
3138 "2013 Could not manually add FCF "
3139 "record 0, status %d\n", rc);
3140 rc = -ENODEV;
3141 kfree(fcf_record);
3142 goto out;
3143 }
3144 kfree(fcf_record);
3145 }
3146 /*
3147 * The driver is expected to do FIP/FCF. Call the port
3148 * and get the FCF Table.
3149 */
James Smart32b97932009-07-19 10:01:21 -04003150 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003151 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003152 spin_unlock_irq(&phba->hbalock);
3153 return;
3154 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003155 /* This is the initial FCF discovery scan */
3156 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003157 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003158 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3159 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003160 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3161 LPFC_FCOE_FCF_GET_FIRST);
3162 if (rc) {
3163 spin_lock_irq(&phba->hbalock);
3164 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3165 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003166 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003167 }
James Smart38b92ef2010-08-04 16:11:39 -04003168 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003169 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003170 }
James Smart6fb120a2009-05-22 14:52:59 -04003171
3172 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003173out:
3174 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003175 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3176 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3177 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003178 lpfc_issue_clear_la(phba, vport);
3179 return;
dea31012005-04-17 16:05:31 -05003180}
3181
3182static void
James Smart84774a42008-08-24 21:50:06 -04003183lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003184{
dea31012005-04-17 16:05:31 -05003185 uint32_t control;
3186 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003187 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003188 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003189 if (phba->sli_rev <= LPFC_SLI_REV3) {
3190 control = readl(phba->HCregaddr);
3191 control |= HC_LAINT_ENA;
3192 writel(control, phba->HCregaddr);
3193 readl(phba->HCregaddr); /* flush */
3194 }
James Smart2e0fef82007-06-17 19:56:36 -05003195 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003196}
3197
James Smart84774a42008-08-24 21:50:06 -04003198static void
3199lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3200{
3201 lpfc_linkdown(phba);
3202 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003203 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003204 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3205}
3206
3207
dea31012005-04-17 16:05:31 -05003208/*
James Smart76a95d72010-11-20 23:11:48 -05003209 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003210 * command upon completion. It is setup in the LPFC_MBOXQ
3211 * as the completion routine when the command is
3212 * handed off to the SLI layer.
3213 */
3214void
James Smart76a95d72010-11-20 23:11:48 -05003215lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003216{
James Smart2e0fef82007-06-17 19:56:36 -05003217 struct lpfc_vport *vport = pmb->vport;
3218 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003219 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003220 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003221 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3222
James Smart0d2b6b82008-06-14 22:52:47 -04003223 /* Unblock ELS traffic */
3224 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003225 /* Check for error */
3226 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003227 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003228 "1307 READ_LA mbox error x%x state x%x\n",
3229 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003230 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003231 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003232 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003233 }
3234
James Smart76a95d72010-11-20 23:11:48 -05003235 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003236
3237 memcpy(&phba->alpa_map[0], mp->virt, 128);
3238
James Smart2e0fef82007-06-17 19:56:36 -05003239 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003240 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003241 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003242 else
James Smart2e0fef82007-06-17 19:56:36 -05003243 vport->fc_flag &= ~FC_BYPASSED_MODE;
3244 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003245
James Smart0c287582009-06-10 17:22:56 -04003246 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05003247 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05003248 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003249 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003250 if (phba->fc_eventTag != 0)
3251 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003252 }
dea31012005-04-17 16:05:31 -05003253
3254 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05003255 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003256 if (bf_get(lpfc_mbx_read_top_mm, la))
James Smart84774a42008-08-24 21:50:06 -04003257 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3258 else
3259 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05003260 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003261
James Smart4d9ab992009-10-02 15:16:39 -04003262 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003263 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
3264 (!bf_get(lpfc_mbx_read_top_mm, la))) {
dea31012005-04-17 16:05:31 -05003265 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003266 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003267 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003268 "1306 Link Up Event in loop back mode "
3269 "x%x received Data: x%x x%x x%x x%x\n",
3270 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003271 bf_get(lpfc_mbx_read_top_alpa_granted,
3272 la),
3273 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003274 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003275 } else {
3276 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003277 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003278 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003279 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003280 bf_get(lpfc_mbx_read_top_alpa_granted,
3281 la),
3282 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003283 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003284 bf_get(lpfc_mbx_read_top_mm, la),
3285 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003286 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003287 }
James Smart92d7f7b2007-06-17 19:56:38 -05003288 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003289 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3290 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003291 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003292 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003293 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3294 "1308 Link Down Event in loop back mode "
3295 "x%x received "
3296 "Data: x%x x%x x%x\n",
3297 la->eventTag, phba->fc_eventTag,
3298 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003299 else
James Smart3163f722008-02-08 18:50:25 -05003300 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003301 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003302 "Data: x%x x%x x%x x%x x%x\n",
3303 la->eventTag, phba->fc_eventTag,
3304 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003305 bf_get(lpfc_mbx_read_top_mm, la),
3306 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003307 lpfc_mbx_issue_link_down(phba);
3308 }
James Smart76a95d72010-11-20 23:11:48 -05003309 if ((bf_get(lpfc_mbx_read_top_mm, la)) &&
3310 (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)) {
James Smart84774a42008-08-24 21:50:06 -04003311 if (phba->link_state != LPFC_LINK_DOWN) {
3312 phba->fc_stat.LinkDown++;
3313 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3314 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003315 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003316 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003317 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003318 lpfc_mbx_issue_link_down(phba);
3319 } else
3320 lpfc_enable_la(phba);
3321
3322 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3323 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3324 "Data: x%x x%x x%x\n",
3325 la->eventTag, phba->fc_eventTag,
3326 phba->pport->port_state, vport->fc_flag);
3327 /*
3328 * The cmnd that triggered this will be waiting for this
3329 * signal.
3330 */
3331 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3332 if (phba->wait_4_mlo_maint_flg) {
3333 phba->wait_4_mlo_maint_flg = 0;
3334 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003335 }
James Smart84774a42008-08-24 21:50:06 -04003336 }
3337
James Smart76a95d72010-11-20 23:11:48 -05003338 if (bf_get(lpfc_mbx_read_top_fa, la)) {
3339 if (bf_get(lpfc_mbx_read_top_mm, la))
James Smart84774a42008-08-24 21:50:06 -04003340 lpfc_issue_clear_la(phba, vport);
3341 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003342 "1311 fa %d\n",
3343 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003344 }
3345
James Smart76a95d72010-11-20 23:11:48 -05003346lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003347 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3348 kfree(mp);
3349 mempool_free(pmb, phba->mbox_mem_pool);
3350 return;
3351}
3352
3353/*
3354 * This routine handles processing a REG_LOGIN mailbox
3355 * command upon completion. It is setup in the LPFC_MBOXQ
3356 * as the completion routine when the command is
3357 * handed off to the SLI layer.
3358 */
3359void
James Smart2e0fef82007-06-17 19:56:36 -05003360lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003361{
James Smart2e0fef82007-06-17 19:56:36 -05003362 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003363 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003364 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003365 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003366
dea31012005-04-17 16:05:31 -05003367 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003368 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003369
James Smartffc95492010-06-07 15:23:17 -04003370 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3371 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3372
James Smart40426292010-12-15 17:58:10 -05003373 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3374 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003375 /* We rcvd a rscn after issuing this
3376 * mbox reg login, we may have cycled
3377 * back through the state and be
3378 * back at reg login state so this
3379 * mbox needs to be ignored becase
3380 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003381 * process.
James Smartffc95492010-06-07 15:23:17 -04003382 */
3383 spin_lock_irq(shost->host_lock);
3384 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3385 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003386 } else
3387 /* Good status, call state machine */
3388 lpfc_disc_state_machine(vport, ndlp, pmb,
3389 NLP_EVT_CMPL_REG_LOGIN);
3390
dea31012005-04-17 16:05:31 -05003391 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3392 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003393 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003394 /* decrement the node reference count held for this callback
3395 * function.
3396 */
James Smart329f9bc2007-04-25 09:53:01 -04003397 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003398
3399 return;
3400}
3401
James Smart92d7f7b2007-06-17 19:56:38 -05003402static void
3403lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3404{
James Smart04c68492009-05-22 14:52:52 -04003405 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003406 struct lpfc_vport *vport = pmb->vport;
3407 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3408
3409 switch (mb->mbxStatus) {
3410 case 0x0011:
3411 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003412 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3413 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3414 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003415 break;
James Smart78730cf2010-04-06 15:06:30 -04003416 /* If VPI is busy, reset the HBA */
3417 case 0x9700:
3418 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3419 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3420 vport->vpi, mb->mbxStatus);
3421 if (!(phba->pport->load_flag & FC_UNLOADING))
3422 lpfc_workq_post_event(phba, NULL, NULL,
3423 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003424 }
James Smart72100cc2010-02-12 14:43:01 -05003425 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003426 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003427 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003428 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003429 vport->unreg_vpi_cmpl = VPORT_OK;
3430 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003431 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003432 /*
3433 * This shost reference might have been taken at the beginning of
3434 * lpfc_vport_delete()
3435 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003436 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003437 scsi_host_put(shost);
3438}
3439
James Smartd7c255b2008-08-24 21:50:00 -04003440int
James Smart92d7f7b2007-06-17 19:56:38 -05003441lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3442{
3443 struct lpfc_hba *phba = vport->phba;
3444 LPFC_MBOXQ_t *mbox;
3445 int rc;
3446
3447 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3448 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003449 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003450
3451 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3452 mbox->vport = vport;
3453 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003454 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003455 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003456 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3457 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003458 mempool_free(mbox, phba->mbox_mem_pool);
3459 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003460 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003461 }
James Smartd7c255b2008-08-24 21:50:00 -04003462 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003463}
3464
3465static void
3466lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3467{
3468 struct lpfc_vport *vport = pmb->vport;
3469 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003470 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003471
3472 switch (mb->mbxStatus) {
3473 case 0x0011:
3474 case 0x9601:
3475 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003476 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3477 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3478 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003479 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3480 spin_lock_irq(shost->host_lock);
3481 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3482 spin_unlock_irq(shost->host_lock);
3483 vport->fc_myDID = 0;
3484 goto out;
3485 }
James Smart92d7f7b2007-06-17 19:56:38 -05003486
James Smart72100cc2010-02-12 14:43:01 -05003487 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003488 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003489 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003490 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003491 vport->num_disc_nodes = 0;
3492 /* go thru NPR list and issue ELS PLOGIs */
3493 if (vport->fc_npr_cnt)
3494 lpfc_els_disc_plogi(vport);
3495
3496 if (!vport->num_disc_nodes) {
3497 spin_lock_irq(shost->host_lock);
3498 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3499 spin_unlock_irq(shost->host_lock);
3500 lpfc_can_disctmo(vport);
3501 }
3502 vport->port_state = LPFC_VPORT_READY;
3503
3504out:
3505 mempool_free(pmb, phba->mbox_mem_pool);
3506 return;
3507}
3508
James Smart21e9a0a2009-05-22 14:53:21 -04003509/**
3510 * lpfc_create_static_vport - Read HBA config region to create static vports.
3511 * @phba: pointer to lpfc hba data structure.
3512 *
3513 * This routine issue a DUMP mailbox command for config region 22 to get
3514 * the list of static vports to be created. The function create vports
3515 * based on the information returned from the HBA.
3516 **/
3517void
3518lpfc_create_static_vport(struct lpfc_hba *phba)
3519{
3520 LPFC_MBOXQ_t *pmb = NULL;
3521 MAILBOX_t *mb;
3522 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003523 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003524 struct fc_vport_identifiers vport_id;
3525 struct fc_vport *new_fc_vport;
3526 struct Scsi_Host *shost;
3527 struct lpfc_vport *vport;
3528 uint16_t offset = 0;
3529 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003530 struct lpfc_dmabuf *mp;
3531 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003532
3533 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3534 if (!pmb) {
3535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3536 "0542 lpfc_create_static_vport failed to"
3537 " allocate mailbox memory\n");
3538 return;
3539 }
James Smartcdcc2342012-08-14 14:26:35 -04003540 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003541 mb = &pmb->u.mb;
3542
3543 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3544 if (!vport_info) {
3545 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3546 "0543 lpfc_create_static_vport failed to"
3547 " allocate vport_info\n");
3548 mempool_free(pmb, phba->mbox_mem_pool);
3549 return;
3550 }
3551
3552 vport_buff = (uint8_t *) vport_info;
3553 do {
James Smartcdcc2342012-08-14 14:26:35 -04003554 /* free dma buffer from previous round */
3555 if (pmb->context1) {
3556 mp = (struct lpfc_dmabuf *)pmb->context1;
3557 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3558 kfree(mp);
3559 }
James Smart1c6834a2009-07-19 10:01:26 -04003560 if (lpfc_dump_static_vport(phba, pmb, offset))
3561 goto out;
3562
James Smart21e9a0a2009-05-22 14:53:21 -04003563 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003564 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3565 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003566
James Smartcdcc2342012-08-14 14:26:35 -04003567 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003568 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3569 "0544 lpfc_create_static_vport failed to"
3570 " issue dump mailbox command ret 0x%x "
3571 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003572 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003573 goto out;
3574 }
3575
James Smart1c6834a2009-07-19 10:01:26 -04003576 if (phba->sli_rev == LPFC_SLI_REV4) {
3577 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003578 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003579 if (byte_count > sizeof(struct static_vport_info) -
3580 offset)
3581 byte_count = sizeof(struct static_vport_info)
3582 - offset;
3583 memcpy(vport_buff + offset, mp->virt, byte_count);
3584 offset += byte_count;
3585 } else {
3586 if (mb->un.varDmp.word_cnt >
3587 sizeof(struct static_vport_info) - offset)
3588 mb->un.varDmp.word_cnt =
3589 sizeof(struct static_vport_info)
3590 - offset;
3591 byte_count = mb->un.varDmp.word_cnt;
3592 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3593 vport_buff + offset,
3594 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003595
James Smart1c6834a2009-07-19 10:01:26 -04003596 offset += byte_count;
3597 }
James Smart21e9a0a2009-05-22 14:53:21 -04003598
James Smart1c6834a2009-07-19 10:01:26 -04003599 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003600 offset < sizeof(struct static_vport_info));
3601
3602
3603 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3604 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3605 != VPORT_INFO_REV)) {
3606 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3607 "0545 lpfc_create_static_vport bad"
3608 " information header 0x%x 0x%x\n",
3609 le32_to_cpu(vport_info->signature),
3610 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3611
3612 goto out;
3613 }
3614
3615 shost = lpfc_shost_from_vport(phba->pport);
3616
3617 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3618 memset(&vport_id, 0, sizeof(vport_id));
3619 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3620 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3621 if (!vport_id.port_name || !vport_id.node_name)
3622 continue;
3623
3624 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3625 vport_id.vport_type = FC_PORTTYPE_NPIV;
3626 vport_id.disable = false;
3627 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3628
3629 if (!new_fc_vport) {
3630 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3631 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003632 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003633 continue;
3634 }
3635
3636 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3637 vport->vport_flag |= STATIC_VPORT;
3638 }
3639
3640out:
James Smart21e9a0a2009-05-22 14:53:21 -04003641 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003642 if (mbx_wait_rc != MBX_TIMEOUT) {
3643 if (pmb->context1) {
3644 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003645 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3646 kfree(mp);
3647 }
James Smart21e9a0a2009-05-22 14:53:21 -04003648 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003649 }
James Smart21e9a0a2009-05-22 14:53:21 -04003650
3651 return;
3652}
3653
dea31012005-04-17 16:05:31 -05003654/*
3655 * This routine handles processing a Fabric REG_LOGIN mailbox
3656 * command upon completion. It is setup in the LPFC_MBOXQ
3657 * as the completion routine when the command is
3658 * handed off to the SLI layer.
3659 */
3660void
James Smart2e0fef82007-06-17 19:56:36 -05003661lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003662{
James Smart92d7f7b2007-06-17 19:56:38 -05003663 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003664 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003665 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003666 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003667 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003668
James Smart549e55c2007-08-02 11:09:51 -04003669 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003670 pmb->context1 = NULL;
3671 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003672
dea31012005-04-17 16:05:31 -05003673 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003674 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3675 "0258 Register Fabric login error: 0x%x\n",
3676 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003677 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3678 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003679 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003680
James Smart76a95d72010-11-20 23:11:48 -05003681 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003682 /* FLOGI failed, use loop map to make discovery list */
3683 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003684
James Smart92d7f7b2007-06-17 19:56:38 -05003685 /* Start discovery */
3686 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003687 /* Decrement the reference count to ndlp after the
3688 * reference to the ndlp are done.
3689 */
3690 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003691 return;
3692 }
3693
3694 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003695 /* Decrement the reference count to ndlp after the reference
3696 * to the ndlp are done.
3697 */
3698 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003699 return;
3700 }
3701
James Smart6d368e52011-05-24 11:44:12 -04003702 if (phba->sli_rev < LPFC_SLI_REV4)
3703 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003704 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003705 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003706 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003707
James Smart2e0fef82007-06-17 19:56:36 -05003708 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003709 /* when physical port receive logo donot start
3710 * vport discovery */
3711 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3712 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003713 else {
3714 shost = lpfc_shost_from_vport(vport);
3715 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003716 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003717 spin_unlock_irq(shost->host_lock);
3718 }
James Smart92d7f7b2007-06-17 19:56:38 -05003719 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003720 }
3721
3722 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3723 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003724 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003725
3726 /* Drop the reference count from the mbox at the end after
3727 * all the current reference to the ndlp have been done.
3728 */
3729 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003730 return;
3731}
3732
3733/*
3734 * This routine handles processing a NameServer REG_LOGIN mailbox
3735 * command upon completion. It is setup in the LPFC_MBOXQ
3736 * as the completion routine when the command is
3737 * handed off to the SLI layer.
3738 */
3739void
James Smart2e0fef82007-06-17 19:56:36 -05003740lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003741{
James Smart21e9a0a2009-05-22 14:53:21 -04003742 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003743 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3744 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3745 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003746
James Smartd439d282010-09-29 11:18:45 -04003747 pmb->context1 = NULL;
3748 pmb->context2 = NULL;
3749
dea31012005-04-17 16:05:31 -05003750 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003751out:
James Smart21e9a0a2009-05-22 14:53:21 -04003752 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3753 "0260 Register NameServer error: 0x%x\n",
3754 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003755 /* decrement the node reference count held for this
3756 * callback function.
3757 */
James Smart329f9bc2007-04-25 09:53:01 -04003758 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003759 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3760 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003761 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003762
3763 /* If no other thread is using the ndlp, free it */
3764 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003765
James Smart76a95d72010-11-20 23:11:48 -05003766 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003767 /*
3768 * RegLogin failed, use loop map to make discovery
3769 * list
3770 */
3771 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003772
James Smart92d7f7b2007-06-17 19:56:38 -05003773 /* Start discovery */
3774 lpfc_disc_start(vport);
3775 return;
3776 }
3777 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003778 return;
3779 }
3780
James Smart6d368e52011-05-24 11:44:12 -04003781 if (phba->sli_rev < LPFC_SLI_REV4)
3782 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003783 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003784 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003785 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003786
James Smart2e0fef82007-06-17 19:56:36 -05003787 if (vport->port_state < LPFC_VPORT_READY) {
3788 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003789 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3790 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3791 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3792 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3793 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3794
3795 /* Issue SCR just before NameServer GID_FT Query */
3796 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003797 }
3798
James Smart2e0fef82007-06-17 19:56:36 -05003799 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003800 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003801 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003802 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003803 goto out;
dea31012005-04-17 16:05:31 -05003804 }
3805
James Smartfa4066b2008-01-11 01:53:27 -05003806 /* decrement the node reference count held for this
3807 * callback function.
3808 */
James Smart329f9bc2007-04-25 09:53:01 -04003809 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003810 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3811 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003812 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003813
3814 return;
3815}
3816
3817static void
James Smart2e0fef82007-06-17 19:56:36 -05003818lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003819{
James Smart2e0fef82007-06-17 19:56:36 -05003820 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3821 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003822 struct lpfc_rport_data *rdata;
3823 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003824 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003825
3826 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003827 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3828 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003829 rport_ids.port_id = ndlp->nlp_DID;
3830 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003831
James Smart329f9bc2007-04-25 09:53:01 -04003832 /*
3833 * We leave our node pointer in rport->dd_data when we unregister a
3834 * FCP target port. But fc_remote_port_add zeros the space to which
3835 * rport->dd_data points. So, if we're reusing a previously
3836 * registered port, drop the reference that we took the last time we
3837 * registered the port.
3838 */
3839 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003840 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003841 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003842
3843 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3844 "rport add: did:x%x flg:x%x type x%x",
3845 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3846
James Smart9589b0622011-04-16 11:03:17 -04003847 /* Don't add the remote port if unloading. */
3848 if (vport->load_flag & FC_UNLOADING)
3849 return;
3850
James Smart2e0fef82007-06-17 19:56:36 -05003851 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003852 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003853 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3854 "Warning: fc_remote_port_add failed\n");
3855 return;
3856 }
3857
3858 /* initialize static port data */
3859 rport->maxframe_size = ndlp->nlp_maxframe;
3860 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003861 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003862 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003863
3864 if (ndlp->nlp_type & NLP_FCP_TARGET)
3865 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3866 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3867 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3868
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003869 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3870 fc_remote_port_rolechg(rport, rport_ids.roles);
3871
James Smart34f5ad82012-08-03 12:35:03 -04003872 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3873 "3183 rport register x%06x, rport %p role x%x\n",
3874 ndlp->nlp_DID, rport, rport_ids.roles);
3875
James Smart071fbd3d2006-04-15 11:53:20 -04003876 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003877 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003878 ndlp->nlp_sid = rport->scsi_target_id;
3879 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003880 return;
3881}
3882
3883static void
James Smart2e0fef82007-06-17 19:56:36 -05003884lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003885{
3886 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003887
James Smart858c9f62007-06-17 19:56:39 -05003888 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3889 "rport delete: did:x%x flg:x%x type x%x",
3890 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3891
James Smart34f5ad82012-08-03 12:35:03 -04003892 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3893 "3184 rport unregister x%06x, rport %p\n",
3894 ndlp->nlp_DID, rport);
3895
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003896 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003897
3898 return;
3899}
3900
James Smartde0c5b32007-04-25 09:52:27 -04003901static void
James Smart2e0fef82007-06-17 19:56:36 -05003902lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003903{
James Smart2e0fef82007-06-17 19:56:36 -05003904 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3905
3906 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003907 switch (state) {
3908 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003909 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003910 break;
3911 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003912 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003913 break;
3914 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003915 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003916 break;
3917 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003918 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003919 break;
3920 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003921 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003922 break;
3923 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003924 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003925 break;
3926 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003927 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003928 break;
3929 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003930 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003931 break;
3932 }
James Smart2e0fef82007-06-17 19:56:36 -05003933 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003934}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003935
James Smartde0c5b32007-04-25 09:52:27 -04003936static void
James Smart2e0fef82007-06-17 19:56:36 -05003937lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003938 int old_state, int new_state)
3939{
James Smart2e0fef82007-06-17 19:56:36 -05003940 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3941
James Smartde0c5b32007-04-25 09:52:27 -04003942 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003943 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3944 ndlp->nlp_type |= NLP_FC_NODE;
3945 }
3946 if (new_state == NLP_STE_MAPPED_NODE)
3947 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3948 if (new_state == NLP_STE_NPR_NODE)
3949 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003950
James Smartde0c5b32007-04-25 09:52:27 -04003951 /* Transport interface */
3952 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3953 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003954 vport->phba->nport_event_cnt++;
3955 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003956 }
dea31012005-04-17 16:05:31 -05003957
James Smartde0c5b32007-04-25 09:52:27 -04003958 if (new_state == NLP_STE_MAPPED_NODE ||
3959 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003960 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003961 /*
3962 * Tell the fc transport about the port, if we haven't
3963 * already. If we have, and it's a scsi entity, be
3964 * sure to unblock any attached scsi devices
3965 */
3966 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003967 }
James Smartea2151b2008-09-07 11:52:10 -04003968 if ((new_state == NLP_STE_MAPPED_NODE) &&
3969 (vport->stat_data_enabled)) {
3970 /*
3971 * A new target is discovered, if there is no buffer for
3972 * statistical data collection allocate buffer.
3973 */
3974 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3975 sizeof(struct lpfc_scsicmd_bkt),
3976 GFP_KERNEL);
3977
3978 if (!ndlp->lat_data)
3979 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3980 "0286 lpfc_nlp_state_cleanup failed to "
3981 "allocate statistical data buffer DID "
3982 "0x%x\n", ndlp->nlp_DID);
3983 }
James Smart858c9f62007-06-17 19:56:39 -05003984 /*
3985 * if we added to Mapped list, but the remote port
3986 * registration failed or assigned a target id outside
3987 * our presentable range - move the node to the
3988 * Unmapped List
3989 */
James Smartde0c5b32007-04-25 09:52:27 -04003990 if (new_state == NLP_STE_MAPPED_NODE &&
3991 (!ndlp->rport ||
3992 ndlp->rport->scsi_target_id == -1 ||
3993 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003994 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003995 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003996 spin_unlock_irq(shost->host_lock);
3997 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003998 }
James Smartde0c5b32007-04-25 09:52:27 -04003999}
4000
James Smart685f0bf2007-04-25 09:53:08 -04004001static char *
4002lpfc_nlp_state_name(char *buffer, size_t size, int state)
4003{
4004 static char *states[] = {
4005 [NLP_STE_UNUSED_NODE] = "UNUSED",
4006 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4007 [NLP_STE_ADISC_ISSUE] = "ADISC",
4008 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4009 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004010 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004011 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4012 [NLP_STE_MAPPED_NODE] = "MAPPED",
4013 [NLP_STE_NPR_NODE] = "NPR",
4014 };
4015
James Smart311464e2007-08-02 11:10:37 -04004016 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004017 strlcpy(buffer, states[state], size);
4018 else
4019 snprintf(buffer, size, "unknown (%d)", state);
4020 return buffer;
4021}
4022
James Smartde0c5b32007-04-25 09:52:27 -04004023void
James Smart2e0fef82007-06-17 19:56:36 -05004024lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4025 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004026{
James Smart2e0fef82007-06-17 19:56:36 -05004027 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004028 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004029 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004030
James Smarte8b62012007-08-02 11:10:09 -04004031 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4032 "0904 NPort state transition x%06x, %s -> %s\n",
4033 ndlp->nlp_DID,
4034 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4035 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004036
4037 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4038 "node statechg did:x%x old:%d ste:%d",
4039 ndlp->nlp_DID, old_state, state);
4040
James Smartde0c5b32007-04-25 09:52:27 -04004041 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004042 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004043 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004044 if (old_state == NLP_STE_UNMAPPED_NODE) {
4045 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4046 ndlp->nlp_type &= ~NLP_FC_NODE;
4047 }
4048
James Smart685f0bf2007-04-25 09:53:08 -04004049 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004050 spin_lock_irq(shost->host_lock);
4051 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4052 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004053 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004054 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004055
4056 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004057 lpfc_nlp_counters(vport, state, 1);
4058 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004059}
4060
4061void
James Smarte47c9092008-02-08 18:49:26 -05004062lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4063{
4064 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4065
4066 if (list_empty(&ndlp->nlp_listp)) {
4067 spin_lock_irq(shost->host_lock);
4068 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4069 spin_unlock_irq(shost->host_lock);
4070 }
4071}
4072
4073void
James Smart2e0fef82007-06-17 19:56:36 -05004074lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004075{
James Smart2e0fef82007-06-17 19:56:36 -05004076 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4077
James Smart0d2b6b82008-06-14 22:52:47 -04004078 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004079 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004080 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4081 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004082 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004083 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004084 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004085 NLP_STE_UNUSED_NODE);
4086}
4087
Adrian Bunk4d9db012008-02-14 23:24:02 +02004088static void
James Smarte47c9092008-02-08 18:49:26 -05004089lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4090{
James Smart0d2b6b82008-06-14 22:52:47 -04004091 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004092 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4093 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4094 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4095 NLP_STE_UNUSED_NODE);
4096}
James Smart109f6ed2008-12-04 22:39:08 -05004097/**
James Smart3621a712009-04-06 18:47:14 -04004098 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004099 * @vport: Pointer to Virtual Port object.
4100 * @ndlp: Pointer to FC node object.
4101 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004102 *
4103 * This function is always called when node object need to be initialized.
4104 * It initializes all the fields of the node object. Although the reference
4105 * to phba from @ndlp can be obtained indirectly through it's reference to
4106 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4107 * to the life-span of the @ndlp might go beyond the existence of @vport as
4108 * the final release of ndlp is determined by its reference count. And, the
4109 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004110 **/
4111static inline void
4112lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4113 uint32_t did)
4114{
4115 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4116 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4117 init_timer(&ndlp->nlp_delayfunc);
4118 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4119 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4120 ndlp->nlp_DID = did;
4121 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004122 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004123 ndlp->nlp_sid = NLP_NO_SID;
4124 kref_init(&ndlp->kref);
4125 NLP_INT_NODE_ACT(ndlp);
4126 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004127 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart40426292010-12-15 17:58:10 -05004128 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4129 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smart109f6ed2008-12-04 22:39:08 -05004130}
James Smarte47c9092008-02-08 18:49:26 -05004131
4132struct lpfc_nodelist *
4133lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4134 int state)
4135{
4136 struct lpfc_hba *phba = vport->phba;
4137 uint32_t did;
4138 unsigned long flags;
4139
4140 if (!ndlp)
4141 return NULL;
4142
4143 spin_lock_irqsave(&phba->ndlp_lock, flags);
4144 /* The ndlp should not be in memory free mode */
4145 if (NLP_CHK_FREE_REQ(ndlp)) {
4146 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4147 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4148 "0277 lpfc_enable_node: ndlp:x%p "
4149 "usgmap:x%x refcnt:%d\n",
4150 (void *)ndlp, ndlp->nlp_usg_map,
4151 atomic_read(&ndlp->kref.refcount));
4152 return NULL;
4153 }
4154 /* The ndlp should not already be in active mode */
4155 if (NLP_CHK_NODE_ACT(ndlp)) {
4156 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4157 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4158 "0278 lpfc_enable_node: ndlp:x%p "
4159 "usgmap:x%x refcnt:%d\n",
4160 (void *)ndlp, ndlp->nlp_usg_map,
4161 atomic_read(&ndlp->kref.refcount));
4162 return NULL;
4163 }
4164
4165 /* Keep the original DID */
4166 did = ndlp->nlp_DID;
4167
4168 /* re-initialize ndlp except of ndlp linked list pointer */
4169 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4170 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004171 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004172
4173 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4174
4175 if (state != NLP_STE_UNUSED_NODE)
4176 lpfc_nlp_set_state(vport, ndlp, state);
4177
4178 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4179 "node enable: did:x%x",
4180 ndlp->nlp_DID, 0, 0);
4181 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004182}
4183
4184void
James Smart2e0fef82007-06-17 19:56:36 -05004185lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004186{
James Smart87af33f2007-10-27 13:37:43 -04004187 /*
James Smartfa4066b2008-01-11 01:53:27 -05004188 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004189 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004190 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4191 * until ALL other outstanding threads have completed. We check
4192 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004193 */
James Smartfa4066b2008-01-11 01:53:27 -05004194 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4195 return;
James Smart51ef4c22007-08-02 11:10:31 -04004196 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart1151e3e2011-02-16 12:39:35 -05004197 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4198 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004199 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004200 return;
dea31012005-04-17 16:05:31 -05004201}
4202
4203/*
4204 * Start / ReStart rescue timer for Discovery / RSCN handling
4205 */
4206void
James Smart2e0fef82007-06-17 19:56:36 -05004207lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004208{
James Smart2e0fef82007-06-17 19:56:36 -05004209 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4210 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004211 uint32_t tmo;
4212
James Smart2e0fef82007-06-17 19:56:36 -05004213 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004214 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004215 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4216 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004217 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004218 * FC spec states we need 3 * ratov for CT requests
4219 */
4220 tmo = ((phba->fc_ratov * 3) + 3);
4221 }
dea31012005-04-17 16:05:31 -05004222
James Smart858c9f62007-06-17 19:56:39 -05004223
4224 if (!timer_pending(&vport->fc_disctmo)) {
4225 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4226 "set disc timer: tmo:x%x state:x%x flg:x%x",
4227 tmo, vport->port_state, vport->fc_flag);
4228 }
4229
James Smart2e0fef82007-06-17 19:56:36 -05004230 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
4231 spin_lock_irq(shost->host_lock);
4232 vport->fc_flag |= FC_DISC_TMO;
4233 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004234
4235 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004236 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4237 "0247 Start Discovery Timer state x%x "
4238 "Data: x%x x%lx x%x x%x\n",
4239 vport->port_state, tmo,
4240 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4241 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004242
4243 return;
4244}
4245
4246/*
4247 * Cancel rescue timer for Discovery / RSCN handling
4248 */
4249int
James Smart2e0fef82007-06-17 19:56:36 -05004250lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004251{
James Smart2e0fef82007-06-17 19:56:36 -05004252 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004253 unsigned long iflags;
4254
James Smart858c9f62007-06-17 19:56:39 -05004255 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4256 "can disc timer: state:x%x rtry:x%x flg:x%x",
4257 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4258
dea31012005-04-17 16:05:31 -05004259 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004260 if (vport->fc_flag & FC_DISC_TMO) {
4261 spin_lock_irqsave(shost->host_lock, iflags);
4262 vport->fc_flag &= ~FC_DISC_TMO;
4263 spin_unlock_irqrestore(shost->host_lock, iflags);
4264 del_timer_sync(&vport->fc_disctmo);
4265 spin_lock_irqsave(&vport->work_port_lock, iflags);
4266 vport->work_port_events &= ~WORKER_DISC_TMO;
4267 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004268 }
4269
4270 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004271 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4272 "0248 Cancel Discovery Timer state x%x "
4273 "Data: x%x x%x x%x\n",
4274 vport->port_state, vport->fc_flag,
4275 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004276 return 0;
dea31012005-04-17 16:05:31 -05004277}
4278
4279/*
4280 * Check specified ring for outstanding IOCB on the SLI queue
4281 * Return true if iocb matches the specified nport
4282 */
4283int
James Smart2e0fef82007-06-17 19:56:36 -05004284lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4285 struct lpfc_sli_ring *pring,
4286 struct lpfc_iocbq *iocb,
4287 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004288{
James Smart2e0fef82007-06-17 19:56:36 -05004289 struct lpfc_sli *psli = &phba->sli;
4290 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004291 struct lpfc_vport *vport = ndlp->vport;
4292
4293 if (iocb->vport != vport)
4294 return 0;
4295
dea31012005-04-17 16:05:31 -05004296 if (pring->ringno == LPFC_ELS_RING) {
4297 switch (icmd->ulpCommand) {
4298 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004299 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004300 return 1;
dea31012005-04-17 16:05:31 -05004301 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004302 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4303 return 1;
dea31012005-04-17 16:05:31 -05004304 case CMD_XMIT_ELS_RSP64_CX:
4305 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004306 return 1;
dea31012005-04-17 16:05:31 -05004307 }
James Smarta4bc3372006-12-02 13:34:16 -05004308 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004309
4310 } else if (pring->ringno == psli->fcp_ring) {
4311 /* Skip match check if waiting to relogin to FCP target */
4312 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004313 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004314 return 0;
dea31012005-04-17 16:05:31 -05004315 }
4316 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004317 return 1;
dea31012005-04-17 16:05:31 -05004318 }
4319 } else if (pring->ringno == psli->next_ring) {
4320
4321 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004322 return 0;
dea31012005-04-17 16:05:31 -05004323}
4324
4325/*
4326 * Free resources / clean up outstanding I/Os
4327 * associated with nlp_rpi in the LPFC_NODELIST entry.
4328 */
4329static int
James Smart2e0fef82007-06-17 19:56:36 -05004330lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004331{
James Smart2534ba72007-04-25 09:52:20 -04004332 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004333 struct lpfc_sli *psli;
4334 struct lpfc_sli_ring *pring;
4335 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004336 uint32_t i;
dea31012005-04-17 16:05:31 -05004337
James Smart92d7f7b2007-06-17 19:56:38 -05004338 lpfc_fabric_abort_nport(ndlp);
4339
dea31012005-04-17 16:05:31 -05004340 /*
4341 * Everything that matches on txcmplq will be returned
4342 * by firmware with a no rpi error.
4343 */
4344 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004345 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004346 /* Now process each ring */
4347 for (i = 0; i < psli->num_rings; i++) {
4348 pring = &psli->ring[i];
4349
James Smart2e0fef82007-06-17 19:56:36 -05004350 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004351 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004352 list) {
dea31012005-04-17 16:05:31 -05004353 /*
4354 * Check to see if iocb matches the nport we are
4355 * looking for
4356 */
James Smart92d7f7b2007-06-17 19:56:38 -05004357 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4358 ndlp))) {
dea31012005-04-17 16:05:31 -05004359 /* It matches, so deque and call compl
4360 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004361 list_move_tail(&iocb->list,
4362 &completions);
dea31012005-04-17 16:05:31 -05004363 }
4364 }
James Smart2e0fef82007-06-17 19:56:36 -05004365 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004366 }
4367 }
James Smart2534ba72007-04-25 09:52:20 -04004368
James Smarta257bf92009-04-06 18:48:10 -04004369 /* Cancel all the IOCBs from the completions list */
4370 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4371 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004372
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004373 return 0;
dea31012005-04-17 16:05:31 -05004374}
4375
James Smart086a3452012-08-14 14:25:21 -04004376/**
4377 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4378 * @phba: Pointer to HBA context object.
4379 * @pmb: Pointer to mailbox object.
4380 *
4381 * This function will issue an ELS LOGO command after completing
4382 * the UNREG_RPI.
4383 **/
4384void
4385lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4386{
4387 struct lpfc_vport *vport = pmb->vport;
4388 struct lpfc_nodelist *ndlp;
4389
4390 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4391 if (!ndlp)
4392 return;
4393 lpfc_issue_els_logo(vport, ndlp, 0);
4394}
4395
dea31012005-04-17 16:05:31 -05004396/*
4397 * Free rpi associated with LPFC_NODELIST entry.
4398 * This routine is called from lpfc_freenode(), when we are removing
4399 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4400 * LOGO that completes successfully, and we are waiting to PLOGI back
4401 * to the remote NPort. In addition, it is called after we receive
4402 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4403 * we are waiting to PLOGI back to the remote NPort.
4404 */
4405int
James Smart2e0fef82007-06-17 19:56:36 -05004406lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004407{
James Smart2e0fef82007-06-17 19:56:36 -05004408 struct lpfc_hba *phba = vport->phba;
4409 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004410 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004411 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004412
James Smart40426292010-12-15 17:58:10 -05004413 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart2e0fef82007-06-17 19:56:36 -05004414 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4415 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004416 /* SLI4 ports require the physical rpi value. */
4417 rpi = ndlp->nlp_rpi;
4418 if (phba->sli_rev == LPFC_SLI_REV4)
4419 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004420
James Smart6d368e52011-05-24 11:44:12 -04004421 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004422 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004423 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4424 mbox->context1 = ndlp;
4425 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4426 } else {
4427 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4428 }
4429
James Smart0b727fe2007-10-27 13:37:25 -04004430 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004431 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004432 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004433 }
dea31012005-04-17 16:05:31 -05004434 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004435
James Smart40426292010-12-15 17:58:10 -05004436 if (phba->sli_rev != LPFC_SLI_REV4)
4437 ndlp->nlp_rpi = 0;
4438 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004439 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004440 return 1;
4441 }
4442 return 0;
4443}
4444
James Smartecfd03c2010-02-12 14:41:27 -05004445/**
4446 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4447 * @phba: pointer to lpfc hba data structure.
4448 *
4449 * This routine is invoked to unregister all the currently registered RPIs
4450 * to the HBA.
4451 **/
4452void
4453lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4454{
4455 struct lpfc_vport **vports;
4456 struct lpfc_nodelist *ndlp;
4457 struct Scsi_Host *shost;
4458 int i;
4459
4460 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004461 if (!vports) {
4462 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4463 "2884 Vport array allocation failed \n");
4464 return;
4465 }
James Smartecfd03c2010-02-12 14:41:27 -05004466 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4467 shost = lpfc_shost_from_vport(vports[i]);
4468 spin_lock_irq(shost->host_lock);
4469 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004470 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004471 /* The mempool_alloc might sleep */
4472 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004473 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004474 spin_lock_irq(shost->host_lock);
4475 }
James Smartecfd03c2010-02-12 14:41:27 -05004476 }
4477 spin_unlock_irq(shost->host_lock);
4478 }
4479 lpfc_destroy_vport_work_array(phba, vports);
4480}
4481
James Smart92d7f7b2007-06-17 19:56:38 -05004482void
4483lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4484{
4485 struct lpfc_hba *phba = vport->phba;
4486 LPFC_MBOXQ_t *mbox;
4487 int rc;
4488
James Smart5af5eee2010-10-22 11:06:38 -04004489 if (phba->sli_rev == LPFC_SLI_REV4) {
4490 lpfc_sli4_unreg_all_rpis(vport);
4491 return;
4492 }
4493
James Smart92d7f7b2007-06-17 19:56:38 -05004494 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4495 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004496 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4497 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004498 mbox->vport = vport;
4499 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004500 mbox->context1 = NULL;
4501 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004502 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004503 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004504
4505 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4506 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4507 "1836 Could not issue "
4508 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004509 }
4510}
4511
4512void
4513lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4514{
4515 struct lpfc_hba *phba = vport->phba;
4516 LPFC_MBOXQ_t *mbox;
4517 int rc;
4518
4519 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4520 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004521 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4522 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004523 mbox->vport = vport;
4524 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004525 mbox->context1 = NULL;
4526 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004527 if (rc != MBX_TIMEOUT)
4528 mempool_free(mbox, phba->mbox_mem_pool);
4529
4530 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004531 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4532 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004533 "unreg_did (default rpis) status %d\n",
4534 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004535 }
4536}
4537
dea31012005-04-17 16:05:31 -05004538/*
4539 * Free resources associated with LPFC_NODELIST entry
4540 * so it can be freed.
4541 */
4542static int
James Smart2e0fef82007-06-17 19:56:36 -05004543lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004544{
James Smart2e0fef82007-06-17 19:56:36 -05004545 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4546 struct lpfc_hba *phba = vport->phba;
4547 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004548 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004549
4550 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004551 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4552 "0900 Cleanup node for NPort x%x "
4553 "Data: x%x x%x x%x\n",
4554 ndlp->nlp_DID, ndlp->nlp_flag,
4555 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004556 if (NLP_CHK_FREE_REQ(ndlp)) {
4557 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4558 "0280 lpfc_cleanup_node: ndlp:x%p "
4559 "usgmap:x%x refcnt:%d\n",
4560 (void *)ndlp, ndlp->nlp_usg_map,
4561 atomic_read(&ndlp->kref.refcount));
4562 lpfc_dequeue_node(vport, ndlp);
4563 } else {
4564 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4565 "0281 lpfc_cleanup_node: ndlp:x%p "
4566 "usgmap:x%x refcnt:%d\n",
4567 (void *)ndlp, ndlp->nlp_usg_map,
4568 atomic_read(&ndlp->kref.refcount));
4569 lpfc_disable_node(vport, ndlp);
4570 }
dea31012005-04-17 16:05:31 -05004571
James Smart086a3452012-08-14 14:25:21 -04004572
4573 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4574
dea31012005-04-17 16:05:31 -05004575 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4576 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004577 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004578 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004579 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4580 mb->context2 = NULL;
4581 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4582 }
4583 }
James Smart33ccf8d2006-08-17 11:57:58 -04004584
James Smart2e0fef82007-06-17 19:56:36 -05004585 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004586 /* Cleanup REG_LOGIN completions which are not yet processed */
4587 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4588 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004589 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004590 (ndlp != (struct lpfc_nodelist *) mb->context2))
4591 continue;
4592
4593 mb->context2 = NULL;
4594 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4595 }
4596
dea31012005-04-17 16:05:31 -05004597 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004598 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004599 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004600 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004601 mp = (struct lpfc_dmabuf *) (mb->context1);
4602 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004603 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004604 kfree(mp);
4605 }
4606 list_del(&mb->list);
4607 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004608 /* We shall not invoke the lpfc_nlp_put to decrement
4609 * the ndlp reference count as we are in the process
4610 * of lpfc_nlp_release.
4611 */
dea31012005-04-17 16:05:31 -05004612 }
4613 }
James Smart2e0fef82007-06-17 19:56:36 -05004614 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004615
James Smarte47c9092008-02-08 18:49:26 -05004616 lpfc_els_abort(phba, ndlp);
4617
James Smart2e0fef82007-06-17 19:56:36 -05004618 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004619 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004620 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004621
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004622 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004623 del_timer_sync(&ndlp->nlp_delayfunc);
4624
James Smart0d2b6b82008-06-14 22:52:47 -04004625 list_del_init(&ndlp->els_retry_evt.evt_listp);
4626 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004627 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004628 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004629
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004630 return 0;
dea31012005-04-17 16:05:31 -05004631}
4632
4633/*
4634 * Check to see if we can free the nlp back to the freelist.
4635 * If we are in the middle of using the nlp in the discovery state
4636 * machine, defer the free till we reach the end of the state machine.
4637 */
James Smart329f9bc2007-04-25 09:53:01 -04004638static void
James Smart2e0fef82007-06-17 19:56:36 -05004639lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004640{
James Smarta8adb832007-10-27 13:37:53 -04004641 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004642 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004643 LPFC_MBOXQ_t *mbox;
4644 int rc;
dea31012005-04-17 16:05:31 -05004645
James Smart0d2b6b82008-06-14 22:52:47 -04004646 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004647 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004648 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4649 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004650 /* For this case we need to cleanup the default rpi
4651 * allocated by the firmware.
4652 */
4653 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4654 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004655 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004656 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004657 if (rc) {
4658 mempool_free(mbox, phba->mbox_mem_pool);
4659 }
4660 else {
4661 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4662 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4663 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004664 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004665 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4666 if (rc == MBX_NOT_FINISHED) {
4667 mempool_free(mbox, phba->mbox_mem_pool);
4668 }
4669 }
4670 }
4671 }
James Smart2e0fef82007-06-17 19:56:36 -05004672 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004673
James Smart2e0fef82007-06-17 19:56:36 -05004674 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004675 * We can get here with a non-NULL ndlp->rport because when we
4676 * unregister a rport we don't break the rport/node linkage. So if we
4677 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004678 */
James Smart92d7f7b2007-06-17 19:56:38 -05004679 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004680 rdata = ndlp->rport->dd_data;
4681 rdata->pnode = NULL;
4682 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004683 }
dea31012005-04-17 16:05:31 -05004684}
4685
4686static int
James Smart2e0fef82007-06-17 19:56:36 -05004687lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4688 uint32_t did)
dea31012005-04-17 16:05:31 -05004689{
James Smart2e0fef82007-06-17 19:56:36 -05004690 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004691
4692 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004693 return 0;
dea31012005-04-17 16:05:31 -05004694
dea31012005-04-17 16:05:31 -05004695 /* First check for Direct match */
4696 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004697 return 1;
dea31012005-04-17 16:05:31 -05004698
4699 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004700 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004701 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004702 return 0;
dea31012005-04-17 16:05:31 -05004703 }
4704
4705 matchdid.un.word = did;
4706 ndlpdid.un.word = ndlp->nlp_DID;
4707 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4708 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4709 (mydid.un.b.area == matchdid.un.b.area)) {
4710 if ((ndlpdid.un.b.domain == 0) &&
4711 (ndlpdid.un.b.area == 0)) {
4712 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004713 return 1;
dea31012005-04-17 16:05:31 -05004714 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004715 return 0;
dea31012005-04-17 16:05:31 -05004716 }
4717
4718 matchdid.un.word = ndlp->nlp_DID;
4719 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4720 (mydid.un.b.area == ndlpdid.un.b.area)) {
4721 if ((matchdid.un.b.domain == 0) &&
4722 (matchdid.un.b.area == 0)) {
4723 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004724 return 1;
dea31012005-04-17 16:05:31 -05004725 }
4726 }
4727 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004728 return 0;
dea31012005-04-17 16:05:31 -05004729}
4730
James Smart685f0bf2007-04-25 09:53:08 -04004731/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004732static struct lpfc_nodelist *
4733__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004734{
James Smart2fb9bd82006-12-02 13:33:57 -05004735 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004736 uint32_t data1;
4737
James Smart2e0fef82007-06-17 19:56:36 -05004738 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4739 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004740 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4741 ((uint32_t) ndlp->nlp_xri << 16) |
4742 ((uint32_t) ndlp->nlp_type << 8) |
4743 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004744 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4745 "0929 FIND node DID "
4746 "Data: x%p x%x x%x x%x\n",
4747 ndlp, ndlp->nlp_DID,
4748 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004749 return ndlp;
dea31012005-04-17 16:05:31 -05004750 }
4751 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004752
dea31012005-04-17 16:05:31 -05004753 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004754 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4755 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004756 return NULL;
4757}
4758
4759struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004760lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004761{
James Smart2e0fef82007-06-17 19:56:36 -05004762 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004763 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004764 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004765
James Smartfedd3b72011-02-16 12:39:24 -05004766 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004767 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004768 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004769 return ndlp;
4770}
4771
4772struct lpfc_nodelist *
4773lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4774{
4775 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4776 struct lpfc_nodelist *ndlp;
4777
4778 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004779 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004780 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4781 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004782 return NULL;
4783 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004784 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004785 if (!ndlp)
4786 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004787 lpfc_nlp_init(vport, ndlp, did);
4788 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4789 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004790 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004791 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004792 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004793 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4794 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4795 if (!ndlp)
4796 return NULL;
4797 spin_lock_irq(shost->host_lock);
4798 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4799 spin_unlock_irq(shost->host_lock);
4800 return ndlp;
dea31012005-04-17 16:05:31 -05004801 }
James Smarte47c9092008-02-08 18:49:26 -05004802
James Smart58da1ff2008-04-07 10:15:56 -04004803 if ((vport->fc_flag & FC_RSCN_MODE) &&
4804 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004805 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004806 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004807 * we don't need to try to discover it again.
4808 */
4809 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4810 return NULL;
4811
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004812 /* Since this node is marked for discovery,
4813 * delay timeout is not needed.
4814 */
James Smart0d2b6b82008-06-14 22:52:47 -04004815 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004816 spin_lock_irq(shost->host_lock);
4817 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4818 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004819 } else
dea31012005-04-17 16:05:31 -05004820 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004821 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004822 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004823 * or we are already in the process of discovery on it,
4824 * we don't need to try to discover it again.
4825 */
James Smart685f0bf2007-04-25 09:53:08 -04004826 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004827 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4828 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004829 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004830 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4831 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004832 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004833 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004834 }
4835 return ndlp;
4836}
4837
4838/* Build a list of nodes to discover based on the loopmap */
4839void
James Smart2e0fef82007-06-17 19:56:36 -05004840lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004841{
James Smart2e0fef82007-06-17 19:56:36 -05004842 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004843 int j;
4844 uint32_t alpa, index;
4845
James Smart2e0fef82007-06-17 19:56:36 -05004846 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004847 return;
James Smart2e0fef82007-06-17 19:56:36 -05004848
James Smart76a95d72010-11-20 23:11:48 -05004849 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004850 return;
dea31012005-04-17 16:05:31 -05004851
4852 /* Check for loop map present or not */
4853 if (phba->alpa_map[0]) {
4854 for (j = 1; j <= phba->alpa_map[0]; j++) {
4855 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004856 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004857 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004858 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004859 }
4860 } else {
4861 /* No alpamap, so try all alpa's */
4862 for (j = 0; j < FC_MAXLOOP; j++) {
4863 /* If cfg_scan_down is set, start from highest
4864 * ALPA (0xef) to lowest (0x1).
4865 */
James Smart3de2a652007-08-02 11:09:59 -04004866 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004867 index = j;
4868 else
4869 index = FC_MAXLOOP - j - 1;
4870 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004871 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004872 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004873 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004874 }
4875 }
4876 return;
4877}
4878
dea31012005-04-17 16:05:31 -05004879void
James Smart2e0fef82007-06-17 19:56:36 -05004880lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004881{
dea31012005-04-17 16:05:31 -05004882 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004883 struct lpfc_sli *psli = &phba->sli;
4884 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4885 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4886 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4887 int rc;
4888
James Smart92d7f7b2007-06-17 19:56:38 -05004889 /*
4890 * if it's not a physical port or if we already send
4891 * clear_la then don't send it.
4892 */
4893 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004894 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4895 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004896 return;
4897
James Smart2e0fef82007-06-17 19:56:36 -05004898 /* Link up discovery */
4899 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4900 phba->link_state = LPFC_CLEAR_LA;
4901 lpfc_clear_la(phba, mbox);
4902 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4903 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004904 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004905 if (rc == MBX_NOT_FINISHED) {
4906 mempool_free(mbox, phba->mbox_mem_pool);
4907 lpfc_disc_flush_list(vport);
4908 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4909 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4910 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004911 phba->link_state = LPFC_HBA_ERROR;
4912 }
4913 }
4914}
4915
4916/* Reg_vpi to tell firmware to resume normal operations */
4917void
4918lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4919{
4920 LPFC_MBOXQ_t *regvpimbox;
4921
4922 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4923 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004924 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004925 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4926 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004927 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004928 == MBX_NOT_FINISHED) {
4929 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004930 }
4931 }
4932}
4933
4934/* Start Link up / RSCN discovery on NPR nodes */
4935void
4936lpfc_disc_start(struct lpfc_vport *vport)
4937{
4938 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4939 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004940 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004941 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004942 int did_changed;
dea31012005-04-17 16:05:31 -05004943
James Smart2e0fef82007-06-17 19:56:36 -05004944 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004945 return;
James Smart2e0fef82007-06-17 19:56:36 -05004946
4947 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004948 clear_la_pending = 1;
4949 else
4950 clear_la_pending = 0;
4951
James Smart2e0fef82007-06-17 19:56:36 -05004952 if (vport->port_state < LPFC_VPORT_READY)
4953 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004954
James Smart2e0fef82007-06-17 19:56:36 -05004955 lpfc_set_disctmo(vport);
4956
4957 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004958 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004959 else
dea31012005-04-17 16:05:31 -05004960 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004961
4962 vport->fc_prevDID = vport->fc_myDID;
4963 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004964
4965 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004966 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4967 "0202 Start Discovery hba state x%x "
4968 "Data: x%x x%x x%x\n",
4969 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4970 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004971
4972 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004973 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004974
4975 if (num_sent)
4976 return;
4977
James Smart6d368e52011-05-24 11:44:12 -04004978 /* Register the VPI for SLI3, NON-NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05004979 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004980 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004981 !(vport->fc_flag & FC_RSCN_MODE) &&
4982 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004983 lpfc_issue_reg_vpi(phba, vport);
4984 return;
4985 }
James Smart2e0fef82007-06-17 19:56:36 -05004986
James Smart92d7f7b2007-06-17 19:56:38 -05004987 /*
4988 * For SLI2, we need to set port_state to READY and continue
4989 * discovery.
4990 */
4991 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4992 /* If we get here, there is nothing to ADISC */
4993 if (vport->port_type == LPFC_PHYSICAL_PORT)
4994 lpfc_issue_clear_la(phba, vport);
4995
4996 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004997 vport->num_disc_nodes = 0;
4998 /* go thru NPR nodes and issue ELS PLOGIs */
4999 if (vport->fc_npr_cnt)
5000 lpfc_els_disc_plogi(vport);
5001
5002 if (!vport->num_disc_nodes) {
5003 spin_lock_irq(shost->host_lock);
5004 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5005 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005006 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005007 }
5008 }
James Smart92d7f7b2007-06-17 19:56:38 -05005009 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005010 } else {
5011 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005012 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005013
5014 if (num_sent)
5015 return;
5016
James Smart2e0fef82007-06-17 19:56:36 -05005017 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005018 /* Check to see if more RSCNs came in while we
5019 * were processing this one.
5020 */
James Smart2e0fef82007-06-17 19:56:36 -05005021 if ((vport->fc_rscn_id_cnt == 0) &&
5022 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5023 spin_lock_irq(shost->host_lock);
5024 vport->fc_flag &= ~FC_RSCN_MODE;
5025 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005026 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005027 } else
James Smart2e0fef82007-06-17 19:56:36 -05005028 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005029 }
5030 }
5031 return;
5032}
5033
5034/*
5035 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5036 * ring the match the sppecified nodelist.
5037 */
5038static void
James Smart2e0fef82007-06-17 19:56:36 -05005039lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005040{
James Smart2534ba72007-04-25 09:52:20 -04005041 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005042 struct lpfc_sli *psli;
5043 IOCB_t *icmd;
5044 struct lpfc_iocbq *iocb, *next_iocb;
5045 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005046
5047 psli = &phba->sli;
5048 pring = &psli->ring[LPFC_ELS_RING];
5049
5050 /* Error matching iocb on txq or txcmplq
5051 * First check the txq.
5052 */
James Smart2e0fef82007-06-17 19:56:36 -05005053 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005054 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5055 if (iocb->context1 != ndlp) {
5056 continue;
5057 }
5058 icmd = &iocb->iocb;
5059 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5060 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5061
James Smart2534ba72007-04-25 09:52:20 -04005062 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005063 }
5064 }
5065
5066 /* Next check the txcmplq */
5067 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5068 if (iocb->context1 != ndlp) {
5069 continue;
5070 }
5071 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005072 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5073 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005074 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005075 }
5076 }
James Smart2e0fef82007-06-17 19:56:36 -05005077 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005078
James Smarta257bf92009-04-06 18:48:10 -04005079 /* Cancel all the IOCBs from the completions list */
5080 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5081 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005082}
5083
Adrian Bunka6ababd2007-11-05 18:07:33 +01005084static void
James Smart2e0fef82007-06-17 19:56:36 -05005085lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005086{
5087 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005088 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005089
James Smart2e0fef82007-06-17 19:56:36 -05005090 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5091 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005092 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005093 if (!NLP_CHK_NODE_ACT(ndlp))
5094 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005095 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5096 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5097 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005098 }
dea31012005-04-17 16:05:31 -05005099 }
5100 }
dea31012005-04-17 16:05:31 -05005101}
5102
James Smart92d7f7b2007-06-17 19:56:38 -05005103void
5104lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5105{
5106 lpfc_els_flush_rscn(vport);
5107 lpfc_els_flush_cmd(vport);
5108 lpfc_disc_flush_list(vport);
5109}
5110
dea31012005-04-17 16:05:31 -05005111/*****************************************************************************/
5112/*
5113 * NAME: lpfc_disc_timeout
5114 *
5115 * FUNCTION: Fibre Channel driver discovery timeout routine.
5116 *
5117 * EXECUTION ENVIRONMENT: interrupt only
5118 *
5119 * CALLED FROM:
5120 * Timer function
5121 *
5122 * RETURNS:
5123 * none
5124 */
5125/*****************************************************************************/
5126void
5127lpfc_disc_timeout(unsigned long ptr)
5128{
James Smart2e0fef82007-06-17 19:56:36 -05005129 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5130 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005131 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005132 unsigned long flags = 0;
5133
5134 if (unlikely(!phba))
5135 return;
5136
James Smart5e9d9b82008-06-14 22:52:53 -04005137 spin_lock_irqsave(&vport->work_port_lock, flags);
5138 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5139 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005140 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005141 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005142
James Smart5e9d9b82008-06-14 22:52:53 -04005143 if (!tmo_posted)
5144 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005145 return;
5146}
5147
5148static void
James Smart2e0fef82007-06-17 19:56:36 -05005149lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005150{
James Smart2e0fef82007-06-17 19:56:36 -05005151 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5152 struct lpfc_hba *phba = vport->phba;
5153 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005154 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005155 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005156 int rc, clrlaerr = 0;
5157
James Smart2e0fef82007-06-17 19:56:36 -05005158 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005159 return;
5160
James Smart2e0fef82007-06-17 19:56:36 -05005161 spin_lock_irq(shost->host_lock);
5162 vport->fc_flag &= ~FC_DISC_TMO;
5163 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005164
James Smart858c9f62007-06-17 19:56:39 -05005165 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5166 "disc timeout: state:x%x rtry:x%x flg:x%x",
5167 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5168
James Smart2e0fef82007-06-17 19:56:36 -05005169 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005170
5171 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005172 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5173 * FAN
5174 */
5175 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005176 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5177 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005178 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005179 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005180 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005181 if (!NLP_CHK_NODE_ACT(ndlp))
5182 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005183 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5184 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005185 if (ndlp->nlp_type & NLP_FABRIC) {
5186 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005187 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005188
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005189 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005190 /* Fail outstanding IO now since device
5191 * is marked for PLOGI.
5192 */
James Smart2e0fef82007-06-17 19:56:36 -05005193 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005194 }
5195 }
James Smart92d7f7b2007-06-17 19:56:38 -05005196 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005197 if (phba->sli_rev <= LPFC_SLI_REV3)
5198 lpfc_initial_flogi(vport);
5199 else
5200 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005201 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005202 }
dea31012005-04-17 16:05:31 -05005203 break;
5204
James Smart92d7f7b2007-06-17 19:56:38 -05005205 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005206 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005207 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005208 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005209 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5210 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005211 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005212
5213 /* Assume no Fabric and go on with discovery.
5214 * Check for outstanding ELS FLOGI to abort.
5215 */
5216
5217 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005218 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005219
5220 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005221 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005222 break;
5223
5224 case LPFC_FABRIC_CFG_LINK:
5225 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5226 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005227 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5228 "0223 Timeout while waiting for "
5229 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005230 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005231 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005232 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005233 lpfc_els_abort(phba, ndlp);
5234
5235 /* ReStart discovery */
5236 goto restart_disc;
dea31012005-04-17 16:05:31 -05005237
5238 case LPFC_NS_QRY:
5239 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005240 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5241 "0224 NameServer Query timeout "
5242 "Data: x%x x%x\n",
5243 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005244
James Smart92d7f7b2007-06-17 19:56:38 -05005245 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5246 /* Try it one more time */
5247 vport->fc_ns_retry++;
5248 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5249 vport->fc_ns_retry, 0);
5250 if (rc == 0)
5251 break;
dea31012005-04-17 16:05:31 -05005252 }
James Smart92d7f7b2007-06-17 19:56:38 -05005253 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005254
James Smart87af33f2007-10-27 13:37:43 -04005255restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005256 /*
5257 * Discovery is over.
5258 * set port_state to PORT_READY if SLI2.
5259 * cmpl_reg_vpi will set port_state to READY for SLI3.
5260 */
James Smart3772a992009-05-22 14:50:54 -04005261 if (phba->sli_rev < LPFC_SLI_REV4) {
5262 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5263 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005264 else {
James Smart3772a992009-05-22 14:50:54 -04005265 lpfc_issue_clear_la(phba, vport);
5266 vport->port_state = LPFC_VPORT_READY;
5267 }
dea31012005-04-17 16:05:31 -05005268 }
5269
5270 /* Setup and issue mailbox INITIALIZE LINK command */
5271 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5272 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005273 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5274 "0206 Device Discovery "
5275 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005276 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005277 break;
5278 }
5279
5280 lpfc_linkdown(phba);
5281 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5282 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005283 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005284 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005285 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005286 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005287 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005288 if (rc == MBX_NOT_FINISHED)
5289 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5290
5291 break;
5292
5293 case LPFC_DISC_AUTH:
5294 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005295 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5296 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005297 lpfc_disc_flush_list(vport);
5298
James Smart92d7f7b2007-06-17 19:56:38 -05005299 /*
5300 * set port_state to PORT_READY if SLI2.
5301 * cmpl_reg_vpi will set port_state to READY for SLI3.
5302 */
James Smart3772a992009-05-22 14:50:54 -04005303 if (phba->sli_rev < LPFC_SLI_REV4) {
5304 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5305 lpfc_issue_reg_vpi(phba, vport);
5306 else { /* NPIV Not enabled */
5307 lpfc_issue_clear_la(phba, vport);
5308 vport->port_state = LPFC_VPORT_READY;
5309 }
dea31012005-04-17 16:05:31 -05005310 }
5311 break;
5312
James Smart2e0fef82007-06-17 19:56:36 -05005313 case LPFC_VPORT_READY:
5314 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005315 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5316 "0231 RSCN timeout Data: x%x "
5317 "x%x\n",
5318 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005319
5320 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005321 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005322
James Smart2e0fef82007-06-17 19:56:36 -05005323 lpfc_els_flush_rscn(vport);
5324 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005325 }
5326 break;
James Smart2e0fef82007-06-17 19:56:36 -05005327
James Smart92d7f7b2007-06-17 19:56:38 -05005328 default:
James Smarte8b62012007-08-02 11:10:09 -04005329 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005330 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005331 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005332 break;
5333 }
5334
5335 switch (phba->link_state) {
5336 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005337 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005338 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5339 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005340 clrlaerr = 1;
5341 break;
5342
James Smart09372822008-01-11 01:52:54 -05005343 case LPFC_LINK_UP:
5344 lpfc_issue_clear_la(phba, vport);
5345 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005346 case LPFC_LINK_UNKNOWN:
5347 case LPFC_WARM_START:
5348 case LPFC_INIT_START:
5349 case LPFC_INIT_MBX_CMDS:
5350 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005351 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005352 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5353 "0230 Unexpected timeout, hba link "
5354 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005355 clrlaerr = 1;
5356 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005357
5358 case LPFC_HBA_READY:
5359 break;
dea31012005-04-17 16:05:31 -05005360 }
5361
5362 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005363 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005364 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005365 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5366 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005367 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005368 }
5369
5370 return;
5371}
5372
dea31012005-04-17 16:05:31 -05005373/*
5374 * This routine handles processing a NameServer REG_LOGIN mailbox
5375 * command upon completion. It is setup in the LPFC_MBOXQ
5376 * as the completion routine when the command is
5377 * handed off to the SLI layer.
5378 */
5379void
James Smart2e0fef82007-06-17 19:56:36 -05005380lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005381{
James Smart04c68492009-05-22 14:52:52 -04005382 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005383 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5384 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5385 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005386
5387 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005388 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005389
James Smart6d368e52011-05-24 11:44:12 -04005390 if (phba->sli_rev < LPFC_SLI_REV4)
5391 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005392 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005393 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005394 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05005395
James Smart2e0fef82007-06-17 19:56:36 -05005396 /*
5397 * Start issuing Fabric-Device Management Interface (FDMI) command to
5398 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5399 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005400 */
James Smart2e0fef82007-06-17 19:56:36 -05005401
James Smart3de2a652007-08-02 11:09:59 -04005402 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05005403 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
5404 else
5405 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05005406
James Smartfa4066b2008-01-11 01:53:27 -05005407 /* decrement the node reference count held for this callback
5408 * function.
5409 */
James Smart329f9bc2007-04-25 09:53:01 -04005410 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005411 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5412 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005413 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005414
5415 return;
5416}
5417
James Smart685f0bf2007-04-25 09:53:08 -04005418static int
5419lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5420{
5421 uint16_t *rpi = param;
5422
James Smarteff4a012012-01-18 16:25:25 -05005423 /* check for active node */
5424 if (!NLP_CHK_NODE_ACT(ndlp))
5425 return 0;
5426
James Smart685f0bf2007-04-25 09:53:08 -04005427 return ndlp->nlp_rpi == *rpi;
5428}
5429
5430static int
5431lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5432{
5433 return memcmp(&ndlp->nlp_portname, param,
5434 sizeof(ndlp->nlp_portname)) == 0;
5435}
5436
Adrian Bunka6ababd2007-11-05 18:07:33 +01005437static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005438__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005439{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005440 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005441
James Smart2e0fef82007-06-17 19:56:36 -05005442 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005443 if (filter(ndlp, param)) {
5444 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5445 "3185 FIND node filter %p DID "
5446 "Data: x%p x%x x%x\n",
5447 filter, ndlp, ndlp->nlp_DID,
5448 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005449 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005450 }
James Smart685f0bf2007-04-25 09:53:08 -04005451 }
James Smart34f5ad82012-08-03 12:35:03 -04005452 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5453 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005454 return NULL;
dea31012005-04-17 16:05:31 -05005455}
5456
James Smart685f0bf2007-04-25 09:53:08 -04005457/*
5458 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005459 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005460 */
5461struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005462__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005463{
James Smart2e0fef82007-06-17 19:56:36 -05005464 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005465}
5466
James Smart488d1462006-03-07 15:02:37 -05005467/*
James Smart685f0bf2007-04-25 09:53:08 -04005468 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005469 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005470 */
5471struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005472lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005473{
James Smart2e0fef82007-06-17 19:56:36 -05005474 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005475 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005476
James Smart2e0fef82007-06-17 19:56:36 -05005477 spin_lock_irq(shost->host_lock);
5478 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5479 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005480 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005481}
5482
James Smartcb69f7d2011-12-13 13:21:57 -05005483/*
5484 * This routine looks up the ndlp lists for the given RPI. If the rpi
5485 * is found, the routine returns the node element list pointer else
5486 * return NULL.
5487 */
5488struct lpfc_nodelist *
5489lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5490{
5491 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5492 struct lpfc_nodelist *ndlp;
5493
5494 spin_lock_irq(shost->host_lock);
5495 ndlp = __lpfc_findnode_rpi(vport, rpi);
5496 spin_unlock_irq(shost->host_lock);
5497 return ndlp;
5498}
5499
5500/**
5501 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5502 * @phba: pointer to lpfc hba data structure.
5503 * @vpi: the physical host virtual N_Port identifier.
5504 *
5505 * This routine finds a vport on a HBA (referred by @phba) through a
5506 * @vpi. The function walks the HBA's vport list and returns the address
5507 * of the vport with the matching @vpi.
5508 *
5509 * Return code
5510 * NULL - No vport with the matching @vpi found
5511 * Otherwise - Address to the vport with the matching @vpi.
5512 **/
5513struct lpfc_vport *
5514lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5515{
5516 struct lpfc_vport *vport;
5517 unsigned long flags;
5518 int i = 0;
5519
5520 /* The physical ports are always vpi 0 - translate is unnecessary. */
5521 if (vpi > 0) {
5522 /*
5523 * Translate the physical vpi to the logical vpi. The
5524 * vport stores the logical vpi.
5525 */
5526 for (i = 0; i < phba->max_vpi; i++) {
5527 if (vpi == phba->vpi_ids[i])
5528 break;
5529 }
5530
5531 if (i >= phba->max_vpi) {
5532 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5533 "2936 Could not find Vport mapped "
5534 "to vpi %d\n", vpi);
5535 return NULL;
5536 }
5537 }
5538
5539 spin_lock_irqsave(&phba->hbalock, flags);
5540 list_for_each_entry(vport, &phba->port_list, listentry) {
5541 if (vport->vpi == i) {
5542 spin_unlock_irqrestore(&phba->hbalock, flags);
5543 return vport;
5544 }
5545 }
5546 spin_unlock_irqrestore(&phba->hbalock, flags);
5547 return NULL;
5548}
5549
dea31012005-04-17 16:05:31 -05005550void
James Smart2e0fef82007-06-17 19:56:36 -05005551lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5552 uint32_t did)
dea31012005-04-17 16:05:31 -05005553{
5554 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005555
5556 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005557 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05005558
5559 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5560 "node init: did:x%x",
5561 ndlp->nlp_DID, 0, 0);
5562
dea31012005-04-17 16:05:31 -05005563 return;
5564}
James Smart329f9bc2007-04-25 09:53:01 -04005565
James Smart98c9ea52007-10-27 13:37:33 -04005566/* This routine releases all resources associated with a specifc NPort's ndlp
5567 * and mempool_free's the nodelist.
5568 */
James Smart311464e2007-08-02 11:10:37 -04005569static void
James Smart329f9bc2007-04-25 09:53:01 -04005570lpfc_nlp_release(struct kref *kref)
5571{
James Smarte47c9092008-02-08 18:49:26 -05005572 struct lpfc_hba *phba;
5573 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005574 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5575 kref);
James Smart858c9f62007-06-17 19:56:39 -05005576
5577 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5578 "node release: did:x%x flg:x%x type:x%x",
5579 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5580
James Smarte47c9092008-02-08 18:49:26 -05005581 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005582 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smarte47c9092008-02-08 18:49:26 -05005583 "usgmap:x%x refcnt:%d\n",
James Smart939723a2012-05-09 21:19:03 -04005584 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smarte47c9092008-02-08 18:49:26 -05005585 atomic_read(&ndlp->kref.refcount));
5586
5587 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005588 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005589
5590 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005591 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005592 spin_lock_irqsave(&phba->ndlp_lock, flags);
5593 NLP_CLR_NODE_ACT(ndlp);
5594 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005595 if (phba->sli_rev == LPFC_SLI_REV4)
5596 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005597
5598 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005599 if (NLP_CHK_FREE_REQ(ndlp)) {
5600 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04005601 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005602 }
James Smart329f9bc2007-04-25 09:53:01 -04005603}
5604
James Smart98c9ea52007-10-27 13:37:33 -04005605/* This routine bumps the reference count for a ndlp structure to ensure
5606 * that one discovery thread won't free a ndlp while another discovery thread
5607 * is using it.
5608 */
James Smart329f9bc2007-04-25 09:53:01 -04005609struct lpfc_nodelist *
5610lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5611{
James Smarte47c9092008-02-08 18:49:26 -05005612 struct lpfc_hba *phba;
5613 unsigned long flags;
5614
James Smart98c9ea52007-10-27 13:37:33 -04005615 if (ndlp) {
5616 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5617 "node get: did:x%x flg:x%x refcnt:x%x",
5618 ndlp->nlp_DID, ndlp->nlp_flag,
5619 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005620 /* The check of ndlp usage to prevent incrementing the
5621 * ndlp reference count that is in the process of being
5622 * released.
5623 */
James Smarta257bf92009-04-06 18:48:10 -04005624 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005625 spin_lock_irqsave(&phba->ndlp_lock, flags);
5626 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5627 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5628 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5629 "0276 lpfc_nlp_get: ndlp:x%p "
5630 "usgmap:x%x refcnt:%d\n",
5631 (void *)ndlp, ndlp->nlp_usg_map,
5632 atomic_read(&ndlp->kref.refcount));
5633 return NULL;
5634 } else
5635 kref_get(&ndlp->kref);
5636 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005637 }
James Smart329f9bc2007-04-25 09:53:01 -04005638 return ndlp;
5639}
5640
James Smart98c9ea52007-10-27 13:37:33 -04005641/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005642 * count goes to 0, this indicates the the associated nodelist should be
5643 * freed. Returning 1 indicates the ndlp resource has been released; on the
5644 * other hand, returning 0 indicates the ndlp resource has not been released
5645 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005646 */
James Smart329f9bc2007-04-25 09:53:01 -04005647int
5648lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5649{
James Smarte47c9092008-02-08 18:49:26 -05005650 struct lpfc_hba *phba;
5651 unsigned long flags;
5652
5653 if (!ndlp)
5654 return 1;
5655
5656 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5657 "node put: did:x%x flg:x%x refcnt:x%x",
5658 ndlp->nlp_DID, ndlp->nlp_flag,
5659 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005660 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005661 spin_lock_irqsave(&phba->ndlp_lock, flags);
5662 /* Check the ndlp memory free acknowledge flag to avoid the
5663 * possible race condition that kref_put got invoked again
5664 * after previous one has done ndlp memory free.
5665 */
5666 if (NLP_CHK_FREE_ACK(ndlp)) {
5667 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5668 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5669 "0274 lpfc_nlp_put: ndlp:x%p "
5670 "usgmap:x%x refcnt:%d\n",
5671 (void *)ndlp, ndlp->nlp_usg_map,
5672 atomic_read(&ndlp->kref.refcount));
5673 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005674 }
James Smarte47c9092008-02-08 18:49:26 -05005675 /* Check the ndlp inactivate log flag to avoid the possible
5676 * race condition that kref_put got invoked again after ndlp
5677 * is already in inactivating state.
5678 */
5679 if (NLP_CHK_IACT_REQ(ndlp)) {
5680 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5681 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5682 "0275 lpfc_nlp_put: ndlp:x%p "
5683 "usgmap:x%x refcnt:%d\n",
5684 (void *)ndlp, ndlp->nlp_usg_map,
5685 atomic_read(&ndlp->kref.refcount));
5686 return 1;
5687 }
5688 /* For last put, mark the ndlp usage flags to make sure no
5689 * other kref_get and kref_put on the same ndlp shall get
5690 * in between the process when the final kref_put has been
5691 * invoked on this ndlp.
5692 */
5693 if (atomic_read(&ndlp->kref.refcount) == 1) {
5694 /* Indicate ndlp is put to inactive state. */
5695 NLP_SET_IACT_REQ(ndlp);
5696 /* Acknowledge ndlp memory free has been seen. */
5697 if (NLP_CHK_FREE_REQ(ndlp))
5698 NLP_SET_FREE_ACK(ndlp);
5699 }
5700 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5701 /* Note, the kref_put returns 1 when decrementing a reference
5702 * count that was 1, it invokes the release callback function,
5703 * but it still left the reference count as 1 (not actually
5704 * performs the last decrementation). Otherwise, it actually
5705 * decrements the reference count and returns 0.
5706 */
5707 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005708}
James Smart98c9ea52007-10-27 13:37:33 -04005709
5710/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005711 * by any other discovery thread. This routine returns 1 if the
5712 * ndlp has been freed. A return value of 0 indicates the ndlp is
5713 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005714 */
5715int
5716lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5717{
5718 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5719 "node not used: did:x%x flg:x%x refcnt:x%x",
5720 ndlp->nlp_DID, ndlp->nlp_flag,
5721 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005722 if (atomic_read(&ndlp->kref.refcount) == 1)
5723 if (lpfc_nlp_put(ndlp))
5724 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005725 return 0;
5726}
James Smart6fb120a2009-05-22 14:52:59 -04005727
5728/**
5729 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5730 * @phba: Pointer to hba context object.
5731 *
5732 * This function iterate through all FC nodes associated
5733 * will all vports to check if there is any node with
5734 * fc_rports associated with it. If there is an fc_rport
5735 * associated with the node, then the node is either in
5736 * discovered state or its devloss_timer is pending.
5737 */
5738static int
5739lpfc_fcf_inuse(struct lpfc_hba *phba)
5740{
5741 struct lpfc_vport **vports;
5742 int i, ret = 0;
5743 struct lpfc_nodelist *ndlp;
5744 struct Scsi_Host *shost;
5745
5746 vports = lpfc_create_vport_work_array(phba);
5747
James Smart63e801c2010-11-20 23:14:19 -05005748 /* If driver cannot allocate memory, indicate fcf is in use */
5749 if (!vports)
5750 return 1;
5751
James Smart6fb120a2009-05-22 14:52:59 -04005752 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5753 shost = lpfc_shost_from_vport(vports[i]);
5754 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005755 /*
5756 * IF the CVL_RCVD bit is not set then we have sent the
5757 * flogi.
5758 * If dev_loss fires while we are waiting we do not want to
5759 * unreg the fcf.
5760 */
5761 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5762 spin_unlock_irq(shost->host_lock);
5763 ret = 1;
5764 goto out;
5765 }
James Smart6fb120a2009-05-22 14:52:59 -04005766 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5767 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5768 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5769 ret = 1;
5770 spin_unlock_irq(shost->host_lock);
5771 goto out;
James Smart80c17842012-03-01 22:35:45 -05005772 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5773 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005774 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005775 "2624 RPI %x DID %x flag %x "
5776 "still logged in\n",
5777 ndlp->nlp_rpi, ndlp->nlp_DID,
5778 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005779 }
5780 }
5781 spin_unlock_irq(shost->host_lock);
5782 }
5783out:
5784 lpfc_destroy_vport_work_array(phba, vports);
5785 return ret;
5786}
5787
5788/**
5789 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5790 * @phba: Pointer to hba context object.
5791 * @mboxq: Pointer to mailbox object.
5792 *
5793 * This function frees memory associated with the mailbox command.
5794 */
James Smart1b511972011-12-13 13:23:09 -05005795void
James Smart6fb120a2009-05-22 14:52:59 -04005796lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5797{
5798 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005799 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005800
5801 if (mboxq->u.mb.mbxStatus) {
5802 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5803 "2555 UNREG_VFI mbxStatus error x%x "
5804 "HBA state x%x\n",
5805 mboxq->u.mb.mbxStatus, vport->port_state);
5806 }
James Smart38b92ef2010-08-04 16:11:39 -04005807 spin_lock_irq(shost->host_lock);
5808 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5809 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005810 mempool_free(mboxq, phba->mbox_mem_pool);
5811 return;
5812}
5813
5814/**
5815 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5816 * @phba: Pointer to hba context object.
5817 * @mboxq: Pointer to mailbox object.
5818 *
5819 * This function frees memory associated with the mailbox command.
5820 */
5821static void
5822lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5823{
5824 struct lpfc_vport *vport = mboxq->vport;
5825
5826 if (mboxq->u.mb.mbxStatus) {
5827 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5828 "2550 UNREG_FCFI mbxStatus error x%x "
5829 "HBA state x%x\n",
5830 mboxq->u.mb.mbxStatus, vport->port_state);
5831 }
5832 mempool_free(mboxq, phba->mbox_mem_pool);
5833 return;
5834}
5835
5836/**
James Smartecfd03c2010-02-12 14:41:27 -05005837 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005838 * @phba: Pointer to hba context object.
5839 *
James Smartecfd03c2010-02-12 14:41:27 -05005840 * This function prepare the HBA for unregistering the currently registered
5841 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5842 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005843 */
James Smartecfd03c2010-02-12 14:41:27 -05005844int
5845lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005846{
James Smart6fb120a2009-05-22 14:52:59 -04005847 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005848 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005849 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005850 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005851
James Smartecfd03c2010-02-12 14:41:27 -05005852 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005853 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005854 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005855
James Smart4d9ab992009-10-02 15:16:39 -04005856 /* At this point, all discovery is aborted */
5857 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005858
5859 /* Unregister VPIs */
5860 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005861 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005862 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005863 /* Stop FLOGI/FDISC retries */
5864 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5865 if (ndlp)
5866 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005867 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04005868 if (phba->sli_rev == LPFC_SLI_REV4)
5869 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005870 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005871 shost = lpfc_shost_from_vport(vports[i]);
5872 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005873 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005874 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005875 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005876 }
5877 lpfc_destroy_vport_work_array(phba, vports);
5878
James Smart695a8142010-01-26 23:08:03 -05005879 /* Cleanup any outstanding ELS commands */
5880 lpfc_els_flush_all_cmd(phba);
5881
James Smart1b511972011-12-13 13:23:09 -05005882 /* Unregister the physical port VFI */
5883 rc = lpfc_issue_unreg_vfi(phba->pport);
5884 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05005885}
5886
5887/**
5888 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5889 * @phba: Pointer to hba context object.
5890 *
5891 * This function issues synchronous unregister FCF mailbox command to HBA to
5892 * unregister the currently registered FCF record. The driver does not reset
5893 * the driver FCF usage state flags.
5894 *
5895 * Return 0 if successfully issued, none-zero otherwise.
5896 */
5897int
5898lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5899{
5900 LPFC_MBOXQ_t *mbox;
5901 int rc;
5902
James Smart6fb120a2009-05-22 14:52:59 -04005903 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5904 if (!mbox) {
5905 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005906 "2551 UNREG_FCFI mbox allocation failed"
5907 "HBA state x%x\n", phba->pport->port_state);
5908 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005909 }
James Smart6fb120a2009-05-22 14:52:59 -04005910 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5911 mbox->vport = phba->pport;
5912 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5913 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5914
5915 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5917 "2552 Unregister FCFI command failed rc x%x "
5918 "HBA state x%x\n",
5919 rc, phba->pport->port_state);
5920 return -EINVAL;
5921 }
5922 return 0;
5923}
5924
5925/**
5926 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5927 * @phba: Pointer to hba context object.
5928 *
5929 * This function unregisters the currently reigstered FCF. This function
5930 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5931 */
5932void
5933lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5934{
5935 int rc;
5936
5937 /* Preparation for unregistering fcf */
5938 rc = lpfc_unregister_fcf_prep(phba);
5939 if (rc) {
5940 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5941 "2748 Failed to prepare for unregistering "
5942 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005943 return;
5944 }
5945
James Smartecfd03c2010-02-12 14:41:27 -05005946 /* Now, unregister FCF record and reset HBA FCF state */
5947 rc = lpfc_sli4_unregister_fcf(phba);
5948 if (rc)
5949 return;
5950 /* Reset HBA FCF states after successful unregister FCF */
5951 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005952 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005953
5954 /*
5955 * If driver is not unloading, check if there is any other
5956 * FCF record that can be used for discovery.
5957 */
5958 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005959 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005960 return;
5961
James Smart0c9ab6f2010-02-26 14:15:57 -05005962 /* This is considered as the initial FCF discovery scan */
5963 spin_lock_irq(&phba->hbalock);
5964 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5965 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005966
5967 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04005968 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005969
James Smart0c9ab6f2010-02-26 14:15:57 -05005970 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005971
James Smart0c9ab6f2010-02-26 14:15:57 -05005972 if (rc) {
5973 spin_lock_irq(&phba->hbalock);
5974 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5975 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005976 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005977 "2553 lpfc_unregister_unused_fcf failed "
5978 "to read FCF record HBA state x%x\n",
5979 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005980 }
James Smartecfd03c2010-02-12 14:41:27 -05005981}
5982
5983/**
5984 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5985 * @phba: Pointer to hba context object.
5986 *
5987 * This function just unregisters the currently reigstered FCF. It does not
5988 * try to find another FCF for discovery.
5989 */
5990void
5991lpfc_unregister_fcf(struct lpfc_hba *phba)
5992{
5993 int rc;
5994
5995 /* Preparation for unregistering fcf */
5996 rc = lpfc_unregister_fcf_prep(phba);
5997 if (rc) {
5998 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5999 "2749 Failed to prepare for unregistering "
6000 "HBA's FCF record: rc=%d\n", rc);
6001 return;
6002 }
6003
6004 /* Now, unregister FCF record and reset HBA FCF state */
6005 rc = lpfc_sli4_unregister_fcf(phba);
6006 if (rc)
6007 return;
6008 /* Set proper HBA FCF states after successful unregister FCF */
6009 spin_lock_irq(&phba->hbalock);
6010 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6011 spin_unlock_irq(&phba->hbalock);
6012}
6013
6014/**
6015 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6016 * @phba: Pointer to hba context object.
6017 *
6018 * This function check if there are any connected remote port for the FCF and
6019 * if all the devices are disconnected, this function unregister FCFI.
6020 * This function also tries to use another FCF for discovery.
6021 */
6022void
6023lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6024{
6025 /*
James Smart3804dc82010-07-14 15:31:37 -04006026 * If HBA is not running in FIP mode, if HBA does not support
6027 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6028 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006029 */
6030 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006031 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006032 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006033 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006034 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006035 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006036 spin_unlock_irq(&phba->hbalock);
6037 return;
6038 }
6039 spin_unlock_irq(&phba->hbalock);
6040
6041 if (lpfc_fcf_inuse(phba))
6042 return;
6043
6044 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006045}
6046
6047/**
6048 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6049 * @phba: Pointer to hba context object.
6050 * @buff: Buffer containing the FCF connection table as in the config
6051 * region.
6052 * This function create driver data structure for the FCF connection
6053 * record table read from config region 23.
6054 */
6055static void
6056lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6057 uint8_t *buff)
6058{
6059 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6060 struct lpfc_fcf_conn_hdr *conn_hdr;
6061 struct lpfc_fcf_conn_rec *conn_rec;
6062 uint32_t record_count;
6063 int i;
6064
6065 /* Free the current connect table */
6066 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006067 &phba->fcf_conn_rec_list, list) {
6068 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006069 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006070 }
James Smart6fb120a2009-05-22 14:52:59 -04006071
6072 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6073 record_count = conn_hdr->length * sizeof(uint32_t)/
6074 sizeof(struct lpfc_fcf_conn_rec);
6075
6076 conn_rec = (struct lpfc_fcf_conn_rec *)
6077 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6078
6079 for (i = 0; i < record_count; i++) {
6080 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6081 continue;
6082 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6083 GFP_KERNEL);
6084 if (!conn_entry) {
6085 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6086 "2566 Failed to allocate connection"
6087 " table entry\n");
6088 return;
6089 }
6090
6091 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6092 sizeof(struct lpfc_fcf_conn_rec));
6093 conn_entry->conn_rec.vlan_tag =
6094 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
6095 conn_entry->conn_rec.flags =
6096 le16_to_cpu(conn_entry->conn_rec.flags);
6097 list_add_tail(&conn_entry->list,
6098 &phba->fcf_conn_rec_list);
6099 }
6100}
6101
6102/**
6103 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6104 * @phba: Pointer to hba context object.
6105 * @buff: Buffer containing the FCoE parameter data structure.
6106 *
6107 * This function update driver data structure with config
6108 * parameters read from config region 23.
6109 */
6110static void
6111lpfc_read_fcoe_param(struct lpfc_hba *phba,
6112 uint8_t *buff)
6113{
6114 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6115 struct lpfc_fcoe_params *fcoe_param;
6116
6117 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6118 buff;
6119 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006120 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006121
6122 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6123 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6124 return;
6125
James Smart6fb120a2009-05-22 14:52:59 -04006126 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6127 phba->valid_vlan = 1;
6128 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6129 0xFFF;
6130 }
6131
6132 phba->fc_map[0] = fcoe_param->fc_map[0];
6133 phba->fc_map[1] = fcoe_param->fc_map[1];
6134 phba->fc_map[2] = fcoe_param->fc_map[2];
6135 return;
6136}
6137
6138/**
6139 * lpfc_get_rec_conf23 - Get a record type in config region data.
6140 * @buff: Buffer containing config region 23 data.
6141 * @size: Size of the data buffer.
6142 * @rec_type: Record type to be searched.
6143 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006144 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006145 * of the record specified by record_type. If record found, this
6146 * function return pointer to the record else return NULL.
6147 */
6148static uint8_t *
6149lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6150{
6151 uint32_t offset = 0, rec_length;
6152
6153 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6154 (size < sizeof(uint32_t)))
6155 return NULL;
6156
6157 rec_length = buff[offset + 1];
6158
6159 /*
6160 * One TLV record has one word header and number of data words
6161 * specified in the rec_length field of the record header.
6162 */
6163 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6164 <= size) {
6165 if (buff[offset] == rec_type)
6166 return &buff[offset];
6167
6168 if (buff[offset] == LPFC_REGION23_LAST_REC)
6169 return NULL;
6170
6171 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6172 rec_length = buff[offset + 1];
6173 }
6174 return NULL;
6175}
6176
6177/**
6178 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6179 * @phba: Pointer to lpfc_hba data structure.
6180 * @buff: Buffer containing config region 23 data.
6181 * @size: Size of the data buffer.
6182 *
Stefan Weileef35c22010-08-06 21:11:15 +02006183 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006184 * populate driver data structure with the parameters.
6185 */
6186void
6187lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6188 uint8_t *buff,
6189 uint32_t size)
6190{
6191 uint32_t offset = 0, rec_length;
6192 uint8_t *rec_ptr;
6193
6194 /*
6195 * If data size is less than 2 words signature and version cannot be
6196 * verified.
6197 */
6198 if (size < 2*sizeof(uint32_t))
6199 return;
6200
6201 /* Check the region signature first */
6202 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6204 "2567 Config region 23 has bad signature\n");
6205 return;
6206 }
6207
6208 offset += 4;
6209
6210 /* Check the data structure version */
6211 if (buff[offset] != LPFC_REGION23_VERSION) {
6212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6213 "2568 Config region 23 has bad version\n");
6214 return;
6215 }
6216 offset += 4;
6217
6218 rec_length = buff[offset + 1];
6219
6220 /* Read FCoE param record */
6221 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6222 size - offset, FCOE_PARAM_TYPE);
6223 if (rec_ptr)
6224 lpfc_read_fcoe_param(phba, rec_ptr);
6225
6226 /* Read FCF connection table */
6227 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6228 size - offset, FCOE_CONN_TBL_TYPE);
6229 if (rec_ptr)
6230 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6231
6232}