blob: a5d1695dac3dcd73b1e5cae6651c46c602d57d20 [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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 Smarta8adb832007-10-27 13:37:53 -0400126 /* Don't defer this if we are in the process of deleting the vport
127 * or unloading the driver. The unload will cleanup the node
128 * appropriately we just need to cleanup the ndlp rport info here.
129 */
130 if (vport->load_flag & FC_UNLOADING) {
131 put_node = rdata->pnode != NULL;
132 put_rport = ndlp->rport != NULL;
133 rdata->pnode = NULL;
134 ndlp->rport = NULL;
135 if (put_node)
136 lpfc_nlp_put(ndlp);
137 if (put_rport)
138 put_device(&rport->dev);
139 return;
140 }
141
142 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
143 return;
144
James Smart858c9f62007-06-17 19:56:39 -0500145 evtp = &ndlp->dev_loss_evt;
146
147 if (!list_empty(&evtp->evt_listp))
148 return;
149
150 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500151 /* We need to hold the node by incrementing the reference
152 * count until this queued work is done
153 */
154 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400155 if (evtp->evt_arg1) {
156 evtp->evt = LPFC_EVT_DEV_LOSS;
157 list_add_tail(&evtp->evt_listp, &phba->work_list);
158 lpfc_worker_wake_up(phba);
159 }
James Smart858c9f62007-06-17 19:56:39 -0500160 spin_unlock_irq(&phba->hbalock);
161
James Smart858c9f62007-06-17 19:56:39 -0500162 return;
163}
164
James Smarta93ff372010-10-22 11:06:08 -0400165/**
166 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
167 * @ndlp: Pointer to remote node object.
168 *
169 * This function is called from the worker thread when devloss timeout timer
170 * expires. For SLI4 host, this routine shall return 1 when at lease one
171 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
172 * routine shall return 0 when there is no remote node is still in use of FCF
173 * when devloss timeout happened to this @ndlp.
174 **/
175static int
James Smart858c9f62007-06-17 19:56:39 -0500176lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
177{
178 struct lpfc_rport_data *rdata;
179 struct fc_rport *rport;
180 struct lpfc_vport *vport;
181 struct lpfc_hba *phba;
182 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400183 int put_node;
184 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500185 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400186 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500187
188 rport = ndlp->rport;
189
190 if (!rport)
James Smarta93ff372010-10-22 11:06:08 -0400191 return fcf_inuse;
James Smart858c9f62007-06-17 19:56:39 -0500192
193 rdata = rport->dd_data;
194 name = (uint8_t *) &ndlp->nlp_portname;
195 vport = ndlp->vport;
196 phba = vport->phba;
197
James Smarta93ff372010-10-22 11:06:08 -0400198 if (phba->sli_rev == LPFC_SLI_REV4)
199 fcf_inuse = lpfc_fcf_inuse(phba);
200
James Smart858c9f62007-06-17 19:56:39 -0500201 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
202 "rport devlosstmo:did:x%x type:x%x id:x%x",
203 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
204
James Smarta8adb832007-10-27 13:37:53 -0400205 /* Don't defer this if we are in the process of deleting the vport
206 * or unloading the driver. The unload will cleanup the node
207 * appropriately we just need to cleanup the ndlp rport info here.
208 */
209 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500210 if (ndlp->nlp_sid != NLP_NO_SID) {
211 /* flush the target */
212 lpfc_sli_abort_iocb(vport,
213 &phba->sli.ring[phba->sli.fcp_ring],
214 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
215 }
James Smarta8adb832007-10-27 13:37:53 -0400216 put_node = rdata->pnode != NULL;
217 put_rport = ndlp->rport != NULL;
218 rdata->pnode = NULL;
219 ndlp->rport = NULL;
220 if (put_node)
221 lpfc_nlp_put(ndlp);
222 if (put_rport)
223 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400224 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400225 }
226
James Smartd7c255b2008-08-24 21:50:00 -0400227 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
228 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
229 "0284 Devloss timeout Ignored on "
230 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
231 "NPort x%x\n",
232 *name, *(name+1), *(name+2), *(name+3),
233 *(name+4), *(name+5), *(name+6), *(name+7),
234 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400235 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400236 }
James Smart858c9f62007-06-17 19:56:39 -0500237
James Smart92d7f7b2007-06-17 19:56:38 -0500238 if (ndlp->nlp_type & NLP_FABRIC) {
239 /* We will clean up these Nodes in linkup */
240 put_node = rdata->pnode != NULL;
241 put_rport = ndlp->rport != NULL;
242 rdata->pnode = NULL;
243 ndlp->rport = NULL;
244 if (put_node)
245 lpfc_nlp_put(ndlp);
246 if (put_rport)
247 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400248 return fcf_inuse;
James Smart92d7f7b2007-06-17 19:56:38 -0500249 }
James Smart82085712007-04-25 09:52:41 -0400250
dea31012005-04-17 16:05:31 -0500251 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400252 warn_on = 1;
dea31012005-04-17 16:05:31 -0500253 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400254 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
255 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500256 }
James Smartc01f3202006-08-18 17:47:08 -0400257
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400258 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400259 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
260 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400261 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
262 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400263 *name, *(name+1), *(name+2), *(name+3),
264 *(name+4), *(name+5), *(name+6), *(name+7),
265 ndlp->nlp_DID, ndlp->nlp_flag,
266 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400267 } else {
James Smarte8b62012007-08-02 11:10:09 -0400268 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
269 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400270 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
271 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400272 *name, *(name+1), *(name+2), *(name+3),
273 *(name+4), *(name+5), *(name+6), *(name+7),
274 ndlp->nlp_DID, ndlp->nlp_flag,
275 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400276 }
277
James Smart87af33f2007-10-27 13:37:43 -0400278 put_node = rdata->pnode != NULL;
279 put_rport = ndlp->rport != NULL;
280 rdata->pnode = NULL;
281 ndlp->rport = NULL;
282 if (put_node)
283 lpfc_nlp_put(ndlp);
284 if (put_rport)
285 put_device(&rport->dev);
286
James Smart2e0fef82007-06-17 19:56:36 -0500287 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400288 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400289 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400290 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400291 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
292 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500293 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400294
James Smarta93ff372010-10-22 11:06:08 -0400295 return fcf_inuse;
296}
297
298/**
299 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
300 * @phba: Pointer to hba context object.
301 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
302 * @nlp_did: remote node identifer with devloss timeout.
303 *
304 * This function is called from the worker thread after invoking devloss
305 * timeout handler and releasing the reference count for the ndlp with
306 * which the devloss timeout was handled for SLI4 host. For the devloss
307 * timeout of the last remote node which had been in use of FCF, when this
308 * routine is invoked, it shall be guaranteed that none of the remote are
309 * in-use of FCF. When devloss timeout to the last remote using the FCF,
310 * if the FIP engine is neither in FCF table scan process nor roundrobin
311 * failover process, the in-use FCF shall be unregistered. If the FIP
312 * engine is in FCF discovery process, the devloss timeout state shall
313 * be set for either the FCF table scan process or roundrobin failover
314 * process to unregister the in-use FCF.
315 **/
316static void
317lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
318 uint32_t nlp_did)
319{
320 /* If devloss timeout happened to a remote node when FCF had no
321 * longer been in-use, do nothing.
322 */
323 if (!fcf_inuse)
324 return;
325
326 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
327 spin_lock_irq(&phba->hbalock);
328 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
329 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
330 spin_unlock_irq(&phba->hbalock);
331 return;
332 }
333 phba->hba_flag |= HBA_DEVLOSS_TMO;
334 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
335 "2847 Last remote node (x%x) using "
336 "FCF devloss tmo\n", nlp_did);
337 }
338 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
339 spin_unlock_irq(&phba->hbalock);
340 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
341 "2868 Devloss tmo to FCF rediscovery "
342 "in progress\n");
343 return;
344 }
345 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
346 spin_unlock_irq(&phba->hbalock);
347 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
348 "2869 Devloss tmo to idle FIP engine, "
349 "unreg in-use FCF and rescan.\n");
350 /* Unregister in-use FCF and rescan */
351 lpfc_unregister_fcf_rescan(phba);
352 return;
353 }
354 spin_unlock_irq(&phba->hbalock);
355 if (phba->hba_flag & FCF_TS_INPROG)
356 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
357 "2870 FCF table scan in progress\n");
358 if (phba->hba_flag & FCF_RR_INPROG)
359 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
360 "2871 FLOGI roundrobin FCF failover "
361 "in progress\n");
362 }
James Smart6fb120a2009-05-22 14:52:59 -0400363 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500364}
James Smartc01f3202006-08-18 17:47:08 -0400365
James Smartea2151b2008-09-07 11:52:10 -0400366/**
James Smart3621a712009-04-06 18:47:14 -0400367 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400368 * @phba: Pointer to hba context object.
369 *
370 * This function is called from the functions which need to post
371 * events from interrupt context. This function allocates data
372 * structure required for posting event. It also keeps track of
373 * number of events pending and prevent event storm when there are
374 * too many events.
375 **/
376struct lpfc_fast_path_event *
377lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
378 struct lpfc_fast_path_event *ret;
379
380 /* If there are lot of fast event do not exhaust memory due to this */
381 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
382 return NULL;
383
384 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
385 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400386 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400387 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400388 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
389 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
390 }
James Smartea2151b2008-09-07 11:52:10 -0400391 return ret;
392}
393
394/**
James Smart3621a712009-04-06 18:47:14 -0400395 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400396 * @phba: Pointer to hba context object.
397 * @evt: Event object which need to be freed.
398 *
399 * This function frees the data structure required for posting
400 * events.
401 **/
402void
403lpfc_free_fast_evt(struct lpfc_hba *phba,
404 struct lpfc_fast_path_event *evt) {
405
406 atomic_dec(&phba->fast_event_count);
407 kfree(evt);
408}
409
410/**
James Smart3621a712009-04-06 18:47:14 -0400411 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400412 * @phba: Pointer to hba context object.
413 * @evtp: Event data structure.
414 *
415 * This function is called from worker thread, when the interrupt
416 * context need to post an event. This function posts the event
417 * to fc transport netlink interface.
418 **/
419static void
420lpfc_send_fastpath_evt(struct lpfc_hba *phba,
421 struct lpfc_work_evt *evtp)
422{
423 unsigned long evt_category, evt_sub_category;
424 struct lpfc_fast_path_event *fast_evt_data;
425 char *evt_data;
426 uint32_t evt_data_size;
427 struct Scsi_Host *shost;
428
429 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
430 work_evt);
431
432 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
433 evt_sub_category = (unsigned long) fast_evt_data->un.
434 fabric_evt.subcategory;
435 shost = lpfc_shost_from_vport(fast_evt_data->vport);
436 if (evt_category == FC_REG_FABRIC_EVENT) {
437 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
438 evt_data = (char *) &fast_evt_data->un.read_check_error;
439 evt_data_size = sizeof(fast_evt_data->un.
440 read_check_error);
441 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500442 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400443 evt_data = (char *) &fast_evt_data->un.fabric_evt;
444 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
445 } else {
446 lpfc_free_fast_evt(phba, fast_evt_data);
447 return;
448 }
449 } else if (evt_category == FC_REG_SCSI_EVENT) {
450 switch (evt_sub_category) {
451 case LPFC_EVENT_QFULL:
452 case LPFC_EVENT_DEVBSY:
453 evt_data = (char *) &fast_evt_data->un.scsi_evt;
454 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
455 break;
456 case LPFC_EVENT_CHECK_COND:
457 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
458 evt_data_size = sizeof(fast_evt_data->un.
459 check_cond_evt);
460 break;
461 case LPFC_EVENT_VARQUEDEPTH:
462 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
463 evt_data_size = sizeof(fast_evt_data->un.
464 queue_depth_evt);
465 break;
466 default:
467 lpfc_free_fast_evt(phba, fast_evt_data);
468 return;
469 }
470 } else {
471 lpfc_free_fast_evt(phba, fast_evt_data);
472 return;
473 }
474
475 fc_host_post_vendor_event(shost,
476 fc_get_event_number(),
477 evt_data_size,
478 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500479 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400480
481 lpfc_free_fast_evt(phba, fast_evt_data);
482 return;
483}
484
dea31012005-04-17 16:05:31 -0500485static void
James Smart2e0fef82007-06-17 19:56:36 -0500486lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500487{
488 struct lpfc_work_evt *evtp = NULL;
489 struct lpfc_nodelist *ndlp;
490 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400491 int fcf_inuse;
492 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500493
James Smart2e0fef82007-06-17 19:56:36 -0500494 spin_lock_irq(&phba->hbalock);
495 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500496 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
497 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500498 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500499 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500500 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500501 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500502 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500503 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500504 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500505 /* decrement the node reference count held
506 * for this queued work
507 */
508 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500509 break;
James Smart858c9f62007-06-17 19:56:39 -0500510 case LPFC_EVT_DEV_LOSS:
511 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400512 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500513 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500514 /* decrement the node reference count held for
515 * this queued work
516 */
James Smarta93ff372010-10-22 11:06:08 -0400517 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500518 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400519 if (phba->sli_rev == LPFC_SLI_REV4)
520 lpfc_sli4_post_dev_loss_tmo_handler(phba,
521 fcf_inuse,
522 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500523 break;
dea31012005-04-17 16:05:31 -0500524 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500525 if (phba->link_state < LPFC_LINK_DOWN)
526 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500527 else
James Smart2e0fef82007-06-17 19:56:36 -0500528 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500529 complete((struct completion *)(evtp->evt_arg2));
530 break;
James Smart46fa3112007-04-25 09:51:45 -0400531 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500532 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart46fa3112007-04-25 09:51:45 -0400533 lpfc_offline_prep(phba);
534 *(int *)(evtp->evt_arg1) = 0;
535 complete((struct completion *)(evtp->evt_arg2));
536 break;
537 case LPFC_EVT_OFFLINE:
538 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500539 lpfc_sli_brdrestart(phba);
540 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400541 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
542 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500543 complete((struct completion *)(evtp->evt_arg2));
544 break;
545 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400546 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500547 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500548 lpfc_sli_brdreset(phba);
549 lpfc_hba_down_post(phba);
550 *(int *)(evtp->evt_arg1) =
551 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400552 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500553 complete((struct completion *)(evtp->evt_arg2));
554 break;
555 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400556 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500557 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500558 = (phba->pport->stopped)
559 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400560 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500561 complete((struct completion *)(evtp->evt_arg2));
562 break;
James Smartea2151b2008-09-07 11:52:10 -0400563 case LPFC_EVT_FASTPATH_MGMT_EVT:
564 lpfc_send_fastpath_evt(phba, evtp);
565 free_evt = 0;
566 break;
James Smart78730cf2010-04-06 15:06:30 -0400567 case LPFC_EVT_RESET_HBA:
568 if (!(phba->pport->load_flag & FC_UNLOADING))
569 lpfc_reset_hba(phba);
570 break;
dea31012005-04-17 16:05:31 -0500571 }
572 if (free_evt)
573 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500574 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500575 }
James Smart2e0fef82007-06-17 19:56:36 -0500576 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500577
578}
579
James Smart311464e2007-08-02 11:10:37 -0400580static void
James Smart2e0fef82007-06-17 19:56:36 -0500581lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500582{
583 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500584 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400585 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400586 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400587 int i;
dea31012005-04-17 16:05:31 -0500588
James Smart2e0fef82007-06-17 19:56:36 -0500589 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500590 ha_copy = phba->work_ha;
591 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500592 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500593
James Smartda0436e2009-05-22 14:51:39 -0400594 /* First, try to post the next mailbox command to SLI4 device */
595 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
596 lpfc_sli4_post_async_mbox(phba);
597
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500598 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400599 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500600 lpfc_handle_eratt(phba);
601
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500602 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500603 lpfc_sli_handle_mb_event(phba);
604
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500605 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500606 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400607
James Smartda0436e2009-05-22 14:51:39 -0400608 /* Process SLI4 events */
609 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
610 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
611 lpfc_sli4_fcp_xri_abort_event_proc(phba);
612 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
613 lpfc_sli4_els_xri_abort_event_proc(phba);
614 if (phba->hba_flag & ASYNC_EVENT)
615 lpfc_sli4_async_event_proc(phba);
616 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
617 spin_lock_irq(&phba->hbalock);
618 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
619 spin_unlock_irq(&phba->hbalock);
620 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
621 }
James Smartecfd03c2010-02-12 14:41:27 -0500622 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
623 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400624 }
625
James Smart549e55c2007-08-02 11:09:51 -0400626 vports = lpfc_create_vport_work_array(phba);
627 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400628 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400629 /*
630 * We could have no vports in array if unloading, so if
631 * this happens then just use the pport
632 */
633 if (vports[i] == NULL && i == 0)
634 vport = phba->pport;
635 else
636 vport = vports[i];
637 if (vport == NULL)
638 break;
James Smart58da1ff2008-04-07 10:15:56 -0400639 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400640 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400641 vport->work_port_events &= ~work_port_events;
642 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400643 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400644 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400645 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400646 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400647 if (work_port_events & WORKER_HB_TMO)
648 lpfc_hb_timeout_handler(phba);
649 if (work_port_events & WORKER_MBOX_TMO)
650 lpfc_mbox_timeout_handler(phba);
651 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
652 lpfc_unblock_fabric_iocbs(phba);
653 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400654 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400655 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
656 lpfc_ramp_down_queue_handler(phba);
657 if (work_port_events & WORKER_RAMP_UP_QUEUE)
658 lpfc_ramp_up_queue_handler(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500659 }
James Smart09372822008-01-11 01:52:54 -0500660 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500661
James Smart858c9f62007-06-17 19:56:39 -0500662 pring = &phba->sli.ring[LPFC_ELS_RING];
663 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
664 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400665 if ((status & HA_RXMASK) ||
666 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400667 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400668 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500669 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400670 /* Set the lpfc data pending flag */
671 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500672 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400673 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500674 lpfc_sli_handle_slow_ring_event(phba, pring,
675 (status &
676 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500677 }
James Smartbc739052010-08-04 16:11:18 -0400678 if ((phba->sli_rev == LPFC_SLI_REV4) && pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400679 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500680 /*
681 * Turn on Ring interrupts
682 */
James Smart3772a992009-05-22 14:50:54 -0400683 if (phba->sli_rev <= LPFC_SLI_REV3) {
684 spin_lock_irq(&phba->hbalock);
685 control = readl(phba->HCregaddr);
686 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
687 lpfc_debugfs_slow_ring_trc(phba,
688 "WRK Enable ring: cntl:x%x hacopy:x%x",
689 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400690
James Smart3772a992009-05-22 14:50:54 -0400691 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
692 writel(control, phba->HCregaddr);
693 readl(phba->HCregaddr); /* flush */
694 } else {
695 lpfc_debugfs_slow_ring_trc(phba,
696 "WRK Ring ok: cntl:x%x hacopy:x%x",
697 control, ha_copy, 0);
698 }
699 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500700 }
701 }
James Smart2e0fef82007-06-17 19:56:36 -0500702 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500703}
704
dea31012005-04-17 16:05:31 -0500705int
706lpfc_do_work(void *p)
707{
708 struct lpfc_hba *phba = p;
709 int rc;
dea31012005-04-17 16:05:31 -0500710
711 set_user_nice(current, -20);
James Smart5e9d9b82008-06-14 22:52:53 -0400712 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500713
James Smart3a55b532008-12-04 22:38:54 -0500714 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400715 /* wait and check worker queue activities */
716 rc = wait_event_interruptible(phba->work_waitq,
717 (test_and_clear_bit(LPFC_DATA_READY,
718 &phba->data_flags)
719 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500720 /* Signal wakeup shall terminate the worker thread */
721 if (rc) {
722 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
723 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500724 break;
James Smart3a55b532008-12-04 22:38:54 -0500725 }
dea31012005-04-17 16:05:31 -0500726
James Smart5e9d9b82008-06-14 22:52:53 -0400727 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500728 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500729 }
James Smart3a55b532008-12-04 22:38:54 -0500730 phba->worker_thread = NULL;
731 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
732 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500733 return 0;
734}
735
736/*
737 * This is only called to handle FC worker events. Since this a rare
738 * occurance, we allocate a struct lpfc_work_evt structure here instead of
739 * embedding it in the IOCB.
740 */
741int
James Smart2e0fef82007-06-17 19:56:36 -0500742lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500743 uint32_t evt)
744{
745 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500746 unsigned long flags;
dea31012005-04-17 16:05:31 -0500747
748 /*
749 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
750 * be queued to worker thread for processing
751 */
James Smart92d7f7b2007-06-17 19:56:38 -0500752 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500753 if (!evtp)
754 return 0;
755
756 evtp->evt_arg1 = arg1;
757 evtp->evt_arg2 = arg2;
758 evtp->evt = evt;
759
James Smarted957682007-06-17 19:56:37 -0500760 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400761 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500762 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500763
James Smart5e9d9b82008-06-14 22:52:53 -0400764 lpfc_worker_wake_up(phba);
765
dea31012005-04-17 16:05:31 -0500766 return 1;
767}
768
James Smart92d7f7b2007-06-17 19:56:38 -0500769void
770lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
771{
James Smart09372822008-01-11 01:52:54 -0500772 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500773 struct lpfc_hba *phba = vport->phba;
774 struct lpfc_nodelist *ndlp, *next_ndlp;
775 int rc;
776
777 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500778 if (!NLP_CHK_NODE_ACT(ndlp))
779 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500780 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
781 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400782 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
783 ((vport->port_type == LPFC_NPIV_PORT) &&
784 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500785 lpfc_unreg_rpi(vport, ndlp);
786
787 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400788 if ((phba->sli_rev < LPFC_SLI_REV4) &&
789 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500790 continue;
791 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
792 remove
793 ? NLP_EVT_DEVICE_RM
794 : NLP_EVT_DEVICE_RECOVERY);
795 }
796 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400797 if (phba->sli_rev == LPFC_SLI_REV4)
798 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500799 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500800 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500801 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500802 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500803 }
804}
805
James Smart87af33f2007-10-27 13:37:43 -0400806void
James Smart98c9ea52007-10-27 13:37:33 -0400807lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500808{
James Smart695a8142010-01-26 23:08:03 -0500809 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
810
James Smart45ed1192009-10-02 15:17:02 -0400811 /* Cleanup any outstanding received buffers */
812 lpfc_cleanup_rcv_buffers(vport);
813
James Smart92d7f7b2007-06-17 19:56:38 -0500814 /* Cleanup any outstanding RSCN activity */
815 lpfc_els_flush_rscn(vport);
816
817 /* Cleanup any outstanding ELS commands */
818 lpfc_els_flush_cmd(vport);
819
820 lpfc_cleanup_rpis(vport, 0);
821
James Smart92d7f7b2007-06-17 19:56:38 -0500822 /* Turn off discovery timer if its running */
823 lpfc_can_disctmo(vport);
824}
825
James Smart3772a992009-05-22 14:50:54 -0400826void
James Smart98c9ea52007-10-27 13:37:33 -0400827lpfc_linkdown_port(struct lpfc_vport *vport)
828{
829 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
830
831 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
832
833 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
834 "Link Down: state:x%x rtry:x%x flg:x%x",
835 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
836
837 lpfc_port_link_failure(vport);
838
839}
840
dea31012005-04-17 16:05:31 -0500841int
James Smart685f0bf2007-04-25 09:53:08 -0400842lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500843{
James Smart2e0fef82007-06-17 19:56:36 -0500844 struct lpfc_vport *vport = phba->pport;
845 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400846 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400847 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400848 int i;
dea31012005-04-17 16:05:31 -0500849
James Smart3163f722008-02-08 18:50:25 -0500850 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500851 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500852
853 /* Block all SCSI stack I/Os */
854 lpfc_scsi_dev_block(phba);
855
James Smart2e0fef82007-06-17 19:56:36 -0500856 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500857 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500858 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500859 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500860 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500861 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500862 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500863 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500864 }
James Smart549e55c2007-08-02 11:09:51 -0400865 vports = lpfc_create_vport_work_array(phba);
866 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400867 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400868 /* Issue a LINK DOWN event to all nodes */
869 lpfc_linkdown_port(vports[i]);
870 }
James Smart09372822008-01-11 01:52:54 -0500871 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500872 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500873 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
874 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500875 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500876 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500877 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400878 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500879 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500880 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500881 }
882 }
883
dea31012005-04-17 16:05:31 -0500884 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500885 if (phba->pport->fc_flag & FC_PT2PT) {
886 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500887 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
888 if (mb) {
dea31012005-04-17 16:05:31 -0500889 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500890 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500891 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400892 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500893 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500894 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500895 }
896 }
James Smart2e0fef82007-06-17 19:56:36 -0500897 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500898 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500899 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500900 }
James Smart2e0fef82007-06-17 19:56:36 -0500901
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500902 return 0;
dea31012005-04-17 16:05:31 -0500903}
904
James Smart92d7f7b2007-06-17 19:56:38 -0500905static void
906lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500907{
James Smart92d7f7b2007-06-17 19:56:38 -0500908 struct lpfc_nodelist *ndlp;
909
910 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500911 if (!NLP_CHK_NODE_ACT(ndlp))
912 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500913 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
914 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500915 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500916 /* On Linkup its safe to clean up the ndlp
917 * from Fabric connections.
918 */
James Smart92d7f7b2007-06-17 19:56:38 -0500919 if (ndlp->nlp_DID != Fabric_DID)
920 lpfc_unreg_rpi(vport, ndlp);
921 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
922 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500923 /* Fail outstanding IO now since device is
924 * marked for PLOGI.
925 */
James Smart92d7f7b2007-06-17 19:56:38 -0500926 lpfc_unreg_rpi(vport, ndlp);
927 }
928 }
929}
930
931static void
932lpfc_linkup_port(struct lpfc_vport *vport)
933{
934 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500935 struct lpfc_hba *phba = vport->phba;
936
937 if ((vport->load_flag & FC_UNLOADING) != 0)
938 return;
939
James Smart858c9f62007-06-17 19:56:39 -0500940 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
941 "Link Up: top:x%x speed:x%x flg:x%x",
942 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
943
James Smart92d7f7b2007-06-17 19:56:38 -0500944 /* If NPIV is not enabled, only bring the physical port up */
945 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
946 (vport != phba->pport))
947 return;
dea31012005-04-17 16:05:31 -0500948
James Smart2e0fef82007-06-17 19:56:36 -0500949 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400950
James Smart2e0fef82007-06-17 19:56:36 -0500951 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500952 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
953 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
954 vport->fc_flag |= FC_NDISC_ACTIVE;
955 vport->fc_ns_retry = 0;
956 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500957
James Smart92d7f7b2007-06-17 19:56:38 -0500958 if (vport->fc_flag & FC_LBIT)
959 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500960
James Smart92d7f7b2007-06-17 19:56:38 -0500961}
962
963static int
964lpfc_linkup(struct lpfc_hba *phba)
965{
James Smart549e55c2007-08-02 11:09:51 -0400966 struct lpfc_vport **vports;
967 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500968
969 phba->link_state = LPFC_LINK_UP;
970
971 /* Unblock fabric iocbs if they are blocked */
972 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
973 del_timer_sync(&phba->fabric_block_timer);
974
James Smart549e55c2007-08-02 11:09:51 -0400975 vports = lpfc_create_vport_work_array(phba);
976 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400977 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400978 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500979 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400980 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
981 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500982 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500983
984 return 0;
985}
986
987/*
988 * This routine handles processing a CLEAR_LA mailbox
989 * command upon completion. It is setup in the LPFC_MBOXQ
990 * as the completion routine when the command is
991 * handed off to the SLI layer.
992 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100993static void
James Smart2e0fef82007-06-17 19:56:36 -0500994lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500995{
James Smart2e0fef82007-06-17 19:56:36 -0500996 struct lpfc_vport *vport = pmb->vport;
997 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
998 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400999 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001000 uint32_t control;
1001
dea31012005-04-17 16:05:31 -05001002 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001003 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001004 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1005 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1006
1007 /* Check for error */
1008 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001009 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001010 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1011 "0320 CLEAR_LA mbxStatus error x%x hba "
1012 "state x%x\n",
1013 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001014 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001015 goto out;
1016 }
1017
James Smart92d7f7b2007-06-17 19:56:38 -05001018 if (vport->port_type == LPFC_PHYSICAL_PORT)
1019 phba->link_state = LPFC_HBA_READY;
1020
1021 spin_lock_irq(&phba->hbalock);
1022 psli->sli_flag |= LPFC_PROCESS_LA;
1023 control = readl(phba->HCregaddr);
1024 control |= HC_LAINT_ENA;
1025 writel(control, phba->HCregaddr);
1026 readl(phba->HCregaddr); /* flush */
1027 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001028 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001029 return;
dea31012005-04-17 16:05:31 -05001030
dea31012005-04-17 16:05:31 -05001031out:
1032 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001033 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1034 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001035 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001036
James Smart2e0fef82007-06-17 19:56:36 -05001037 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001038 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001039 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001040
James Smart2e0fef82007-06-17 19:56:36 -05001041 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001042
1043 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001044
1045 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001046 psli->sli_flag |= LPFC_PROCESS_LA;
1047 control = readl(phba->HCregaddr);
1048 control |= HC_LAINT_ENA;
1049 writel(control, phba->HCregaddr);
1050 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001051 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001052
1053 return;
1054}
1055
James Smart2e0fef82007-06-17 19:56:36 -05001056
dea31012005-04-17 16:05:31 -05001057static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001058lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001059{
James Smart2e0fef82007-06-17 19:56:36 -05001060 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001061
James Smart04c68492009-05-22 14:52:52 -04001062 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001063 goto out;
dea31012005-04-17 16:05:31 -05001064
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001065 mempool_free(pmb, phba->mbox_mem_pool);
1066
1067 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001068 vport->fc_flag & FC_PUBLIC_LOOP &&
1069 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001070 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001071 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001072 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1073 */
James Smart2e0fef82007-06-17 19:56:36 -05001074 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001075 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001076 }
dea31012005-04-17 16:05:31 -05001077
James Smart2e0fef82007-06-17 19:56:36 -05001078 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001079 * LPFC_FLOGI while waiting for FLOGI cmpl
1080 */
James Smart92d7f7b2007-06-17 19:56:38 -05001081 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05001082 lpfc_initial_flogi(vport);
1083 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001084 return;
dea31012005-04-17 16:05:31 -05001085
1086out:
James Smarte8b62012007-08-02 11:10:09 -04001087 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1088 "0306 CONFIG_LINK mbxStatus error x%x "
1089 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001090 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001091 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001092
1093 lpfc_linkdown(phba);
1094
James Smarte8b62012007-08-02 11:10:09 -04001095 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1096 "0200 CONFIG_LINK bad hba state x%x\n",
1097 vport->port_state);
dea31012005-04-17 16:05:31 -05001098
James Smart92d7f7b2007-06-17 19:56:38 -05001099 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001100 return;
1101}
1102
1103static void
James Smart6fb120a2009-05-22 14:52:59 -04001104lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1105{
1106 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001107
1108 if (mboxq->u.mb.mbxStatus) {
1109 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1110 "2017 REG_FCFI mbxStatus error x%x "
1111 "HBA state x%x\n",
1112 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001113 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001114 }
1115
1116 /* Start FCoE discovery by sending a FLOGI. */
1117 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1118 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001119 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001120 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001121 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001122
James Smart32b97932009-07-19 10:01:21 -04001123 /* If there is a pending FCoE event, restart FCF table scan. */
James Smarta93ff372010-10-22 11:06:08 -04001124 if (lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1125 goto fail_out;
1126
1127 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001128 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001129 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001130 phba->hba_flag &= ~FCF_TS_INPROG;
1131 if (vport->port_state != LPFC_FLOGI) {
1132 phba->hba_flag |= FCF_RR_INPROG;
1133 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001134 lpfc_initial_flogi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001135 goto out;
1136 }
1137 spin_unlock_irq(&phba->hbalock);
1138 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001139
James Smarta93ff372010-10-22 11:06:08 -04001140fail_out:
1141 spin_lock_irq(&phba->hbalock);
1142 phba->hba_flag &= ~FCF_RR_INPROG;
1143 spin_unlock_irq(&phba->hbalock);
1144out:
James Smart6fb120a2009-05-22 14:52:59 -04001145 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001146}
1147
1148/**
1149 * lpfc_fab_name_match - Check if the fcf fabric name match.
1150 * @fab_name: pointer to fabric name.
1151 * @new_fcf_record: pointer to fcf record.
1152 *
1153 * This routine compare the fcf record's fabric name with provided
1154 * fabric name. If the fabric name are identical this function
1155 * returns 1 else return 0.
1156 **/
1157static uint32_t
1158lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1159{
James Smartecfd03c2010-02-12 14:41:27 -05001160 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001161 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001162 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1163 return 0;
1164 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1165 return 0;
1166 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1167 return 0;
1168 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1169 return 0;
1170 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1171 return 0;
1172 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1173 return 0;
1174 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1175 return 0;
1176 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001177}
1178
1179/**
James Smart8fa38512009-07-19 10:01:03 -04001180 * lpfc_sw_name_match - Check if the fcf switch name match.
1181 * @fab_name: pointer to fabric name.
1182 * @new_fcf_record: pointer to fcf record.
1183 *
1184 * This routine compare the fcf record's switch name with provided
1185 * switch name. If the switch name are identical this function
1186 * returns 1 else return 0.
1187 **/
1188static uint32_t
1189lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1190{
James Smartecfd03c2010-02-12 14:41:27 -05001191 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001192 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001193 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1194 return 0;
1195 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1196 return 0;
1197 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1198 return 0;
1199 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1200 return 0;
1201 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1202 return 0;
1203 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1204 return 0;
1205 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1206 return 0;
1207 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001208}
1209
1210/**
James Smart6fb120a2009-05-22 14:52:59 -04001211 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001212 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001213 * @new_fcf_record: pointer to fcf record.
1214 *
1215 * This routine compare the fcf record's mac address with HBA's
1216 * FCF mac address. If the mac addresses are identical this function
1217 * returns 1 else return 0.
1218 **/
1219static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001220lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001221{
James Smartecfd03c2010-02-12 14:41:27 -05001222 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001223 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001224 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1225 return 0;
1226 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1227 return 0;
1228 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1229 return 0;
1230 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1231 return 0;
1232 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1233 return 0;
1234 return 1;
1235}
1236
1237static bool
1238lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1239{
1240 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001241}
1242
1243/**
1244 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001245 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001246 * @new_fcf_record: pointer to fcf record.
1247 *
1248 * This routine copies the FCF information from the FCF
1249 * record to lpfc_hba data structure.
1250 **/
1251static void
James Smartecfd03c2010-02-12 14:41:27 -05001252lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1253 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001254{
James Smartecfd03c2010-02-12 14:41:27 -05001255 /* Fabric name */
1256 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001257 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001258 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001259 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001260 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001261 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001262 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001263 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001264 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001265 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001266 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001267 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001268 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001269 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001270 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001271 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001272 /* Mac address */
1273 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1274 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1275 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1276 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1277 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1278 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1279 /* FCF record index */
1280 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1281 /* FCF record priority */
1282 fcf_rec->priority = new_fcf_record->fip_priority;
1283 /* Switch name */
1284 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001285 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001286 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001287 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001288 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001289 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001290 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001291 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001292 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001293 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001294 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001295 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001296 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001297 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001298 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001299 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001300}
1301
1302/**
James Smartecfd03c2010-02-12 14:41:27 -05001303 * lpfc_update_fcf_record - Update driver fcf record
1304 * @phba: pointer to lpfc hba data structure.
1305 * @fcf_rec: pointer to driver fcf record.
1306 * @new_fcf_record: pointer to hba fcf record.
1307 * @addr_mode: address mode to be set to the driver fcf record.
1308 * @vlan_id: vlan tag to be set to the driver fcf record.
1309 * @flag: flag bits to be set to the driver fcf record.
1310 *
1311 * This routine updates the driver FCF record from the new HBA FCF record
1312 * together with the address mode, vlan_id, and other informations. This
1313 * routine is called with the host lock held.
1314 **/
1315static void
1316__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1317 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1318 uint16_t vlan_id, uint32_t flag)
1319{
1320 /* Copy the fields from the HBA's FCF record */
1321 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1322 /* Update other fields of driver FCF record */
1323 fcf_rec->addr_mode = addr_mode;
1324 fcf_rec->vlan_id = vlan_id;
1325 fcf_rec->flag |= (flag | RECORD_VALID);
1326}
1327
1328/**
James Smart6fb120a2009-05-22 14:52:59 -04001329 * lpfc_register_fcf - Register the FCF with hba.
1330 * @phba: pointer to lpfc hba data structure.
1331 *
1332 * This routine issues a register fcfi mailbox command to register
1333 * the fcf with HBA.
1334 **/
1335static void
1336lpfc_register_fcf(struct lpfc_hba *phba)
1337{
1338 LPFC_MBOXQ_t *fcf_mbxq;
1339 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001340
James Smartd439d282010-09-29 11:18:45 -04001341 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001342 /* If the FCF is not availabe do nothing. */
1343 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001344 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001345 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001346 return;
1347 }
1348
1349 /* The FCF is already registered, start discovery */
1350 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001351 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001352 phba->hba_flag &= ~FCF_TS_INPROG;
1353 if (phba->pport->port_state != LPFC_FLOGI) {
1354 phba->hba_flag |= FCF_RR_INPROG;
1355 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001356 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001357 return;
1358 }
1359 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001360 return;
1361 }
James Smartd439d282010-09-29 11:18:45 -04001362 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001363
James Smarta93ff372010-10-22 11:06:08 -04001364 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001365 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001366 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001367 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001368 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001369 return;
James Smart4d9ab992009-10-02 15:16:39 -04001370 }
James Smart6fb120a2009-05-22 14:52:59 -04001371
1372 lpfc_reg_fcfi(phba, fcf_mbxq);
1373 fcf_mbxq->vport = phba->pport;
1374 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1375 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001376 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001377 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001378 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001379 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001380 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001381 }
James Smart6fb120a2009-05-22 14:52:59 -04001382
1383 return;
1384}
1385
1386/**
1387 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1388 * @phba: pointer to lpfc hba data structure.
1389 * @new_fcf_record: pointer to fcf record.
1390 * @boot_flag: Indicates if this record used by boot bios.
1391 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001392 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001393 *
1394 * This routine compare the fcf record with connect list obtained from the
1395 * config region to decide if this FCF can be used for SAN discovery. It returns
1396 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1397 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1398 * is used by boot bios and addr_mode will indicate the addressing mode to be
1399 * used for this FCF when the function returns.
1400 * If the FCF record need to be used with a particular vlan id, the vlan is
1401 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001402 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001403 **/
1404static int
1405lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1406 struct fcf_record *new_fcf_record,
1407 uint32_t *boot_flag, uint32_t *addr_mode,
1408 uint16_t *vlan_id)
1409{
1410 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001411 int i, j, fcf_vlan_id = 0;
1412
1413 /* Find the lowest VLAN id in the FCF record */
1414 for (i = 0; i < 512; i++) {
1415 if (new_fcf_record->vlan_bitmap[i]) {
1416 fcf_vlan_id = i * 8;
1417 j = 0;
1418 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1419 j++;
1420 fcf_vlan_id++;
1421 }
1422 break;
1423 }
1424 }
James Smart6fb120a2009-05-22 14:52:59 -04001425
James Smart0c287582009-06-10 17:22:56 -04001426 /* If FCF not available return 0 */
1427 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1428 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1429 return 0;
1430
James Smart45ed1192009-10-02 15:17:02 -04001431 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001432 *boot_flag = 0;
1433 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1434 new_fcf_record);
1435 if (phba->valid_vlan)
1436 *vlan_id = phba->vlan_id;
1437 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001438 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001439 return 1;
1440 }
1441
1442 /*
1443 * If there are no FCF connection table entry, driver connect to all
1444 * FCFs.
1445 */
1446 if (list_empty(&phba->fcf_conn_rec_list)) {
1447 *boot_flag = 0;
1448 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1449 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001450
1451 /*
1452 * When there are no FCF connect entries, use driver's default
1453 * addressing mode - FPMA.
1454 */
1455 if (*addr_mode & LPFC_FCF_FPMA)
1456 *addr_mode = LPFC_FCF_FPMA;
1457
James Smart4d9ab992009-10-02 15:16:39 -04001458 /* If FCF record report a vlan id use that vlan id */
1459 if (fcf_vlan_id)
1460 *vlan_id = fcf_vlan_id;
1461 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001462 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001463 return 1;
1464 }
1465
James Smartecfd03c2010-02-12 14:41:27 -05001466 list_for_each_entry(conn_entry,
1467 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001468 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1469 continue;
1470
1471 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1472 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001473 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001474 continue;
James Smart8fa38512009-07-19 10:01:03 -04001475 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1476 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1477 new_fcf_record))
1478 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001479 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1480 /*
1481 * If the vlan bit map does not have the bit set for the
1482 * vlan id to be used, then it is not a match.
1483 */
1484 if (!(new_fcf_record->vlan_bitmap
1485 [conn_entry->conn_rec.vlan_tag / 8] &
1486 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1487 continue;
1488 }
1489
1490 /*
James Smart0c287582009-06-10 17:22:56 -04001491 * If connection record does not support any addressing mode,
1492 * skip the FCF record.
1493 */
1494 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1495 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1496 continue;
1497
1498 /*
James Smart6fb120a2009-05-22 14:52:59 -04001499 * Check if the connection record specifies a required
1500 * addressing mode.
1501 */
1502 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1503 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1504
1505 /*
1506 * If SPMA required but FCF not support this continue.
1507 */
1508 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1509 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1510 new_fcf_record) & LPFC_FCF_SPMA))
1511 continue;
1512
1513 /*
1514 * If FPMA required but FCF not support this continue.
1515 */
1516 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1517 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1518 new_fcf_record) & LPFC_FCF_FPMA))
1519 continue;
1520 }
1521
1522 /*
1523 * This fcf record matches filtering criteria.
1524 */
1525 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1526 *boot_flag = 1;
1527 else
1528 *boot_flag = 0;
1529
James Smart0c287582009-06-10 17:22:56 -04001530 /*
1531 * If user did not specify any addressing mode, or if the
1532 * prefered addressing mode specified by user is not supported
1533 * by FCF, allow fabric to pick the addressing mode.
1534 */
James Smart6fb120a2009-05-22 14:52:59 -04001535 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1536 new_fcf_record);
1537 /*
1538 * If the user specified a required address mode, assign that
1539 * address mode
1540 */
1541 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1542 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1543 *addr_mode = (conn_entry->conn_rec.flags &
1544 FCFCNCT_AM_SPMA) ?
1545 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1546 /*
1547 * If the user specified a prefered address mode, use the
1548 * addr mode only if FCF support the addr_mode.
1549 */
1550 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1551 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1552 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1553 (*addr_mode & LPFC_FCF_SPMA))
1554 *addr_mode = LPFC_FCF_SPMA;
1555 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1556 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1557 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1558 (*addr_mode & LPFC_FCF_FPMA))
1559 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001560
James Smart4d9ab992009-10-02 15:16:39 -04001561 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001562 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1563 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001564 /*
1565 * If no vlan id is specified in connect list, use the vlan id
1566 * in the FCF record
1567 */
1568 else if (fcf_vlan_id)
1569 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001570 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001571 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001572
1573 return 1;
1574 }
1575
1576 return 0;
1577}
1578
1579/**
James Smart32b97932009-07-19 10:01:21 -04001580 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1581 * @phba: pointer to lpfc hba data structure.
1582 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1583 *
1584 * This function check if there is any fcoe event pending while driver
1585 * scan FCF entries. If there is any pending event, it will restart the
1586 * FCF saning and return 1 else return 0.
1587 */
1588int
1589lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1590{
James Smart32b97932009-07-19 10:01:21 -04001591 /*
1592 * If the Link is up and no FCoE events while in the
1593 * FCF discovery, no need to restart FCF discovery.
1594 */
1595 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001596 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001597 return 0;
1598
James Smart0c9ab6f2010-02-26 14:15:57 -05001599 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1600 "2768 Pending link or FCF event during current "
1601 "handling of the previous event: link_state:x%x, "
1602 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1603 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1604 phba->fcoe_eventtag);
1605
James Smart32b97932009-07-19 10:01:21 -04001606 spin_lock_irq(&phba->hbalock);
1607 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1608 spin_unlock_irq(&phba->hbalock);
1609
James Smart0c9ab6f2010-02-26 14:15:57 -05001610 if (phba->link_state >= LPFC_LINK_UP) {
1611 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1612 "2780 Restart FCF table scan due to "
1613 "pending FCF event:evt_tag_at_scan:x%x, "
1614 "evt_tag_current:x%x\n",
1615 phba->fcoe_eventtag_at_fcf_scan,
1616 phba->fcoe_eventtag);
1617 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1618 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001619 /*
James Smarta93ff372010-10-22 11:06:08 -04001620 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001621 * flag
1622 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001623 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1624 "2833 Stop FCF discovery process due to link "
1625 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001626 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001627 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001628 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001629 spin_unlock_irq(&phba->hbalock);
1630 }
James Smart32b97932009-07-19 10:01:21 -04001631
James Smart0c9ab6f2010-02-26 14:15:57 -05001632 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001633 if (unreg_fcf) {
1634 spin_lock_irq(&phba->hbalock);
1635 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1636 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001637 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001638 }
James Smart32b97932009-07-19 10:01:21 -04001639 return 1;
1640}
1641
1642/**
James Smart999d8132010-03-15 11:24:56 -04001643 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1644 * @phba: pointer to lpfc hba data structure.
1645 * @fcf_cnt: number of eligible fcf record seen so far.
1646 *
1647 * This function makes an running random selection decision on FCF record to
1648 * use through a sequence of @fcf_cnt eligible FCF records with equal
1649 * probability. To perform integer manunipulation of random numbers with
1650 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1651 * from random32() are taken as the random random number generated.
1652 *
1653 * Returns true when outcome is for the newly read FCF record should be
1654 * chosen; otherwise, return false when outcome is for keeping the previously
1655 * chosen FCF record.
1656 **/
1657static bool
1658lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1659{
1660 uint32_t rand_num;
1661
1662 /* Get 16-bit uniform random number */
1663 rand_num = (0xFFFF & random32());
1664
1665 /* Decision with probability 1/fcf_cnt */
1666 if ((fcf_cnt * rand_num) < 0xFFFF)
1667 return true;
1668 else
1669 return false;
1670}
1671
1672/**
James Smart3804dc82010-07-14 15:31:37 -04001673 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001674 * @phba: pointer to lpfc hba data structure.
1675 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001676 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001677 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001678 * This routine parses the non-embedded fcf mailbox command by performing the
1679 * necessarily error checking, non-embedded read FCF record mailbox command
1680 * SGE parsing, and endianness swapping.
1681 *
1682 * Returns the pointer to the new FCF record in the non-embedded mailbox
1683 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001684 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001685static struct fcf_record *
1686lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1687 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001688{
1689 void *virt_addr;
1690 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001691 struct lpfc_mbx_sge sge;
1692 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1693 uint32_t shdr_status, shdr_add_status;
1694 union lpfc_sli4_cfg_shdr *shdr;
1695 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001696
James Smart6fb120a2009-05-22 14:52:59 -04001697 /* Get the first SGE entry from the non-embedded DMA memory. This
1698 * routine only uses a single SGE.
1699 */
1700 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1701 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1702 if (unlikely(!mboxq->sge_array)) {
1703 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1704 "2524 Failed to get the non-embedded SGE "
1705 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001706 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001707 }
1708 virt_addr = mboxq->sge_array->addr[0];
1709
1710 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1711 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001712 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001713 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001714 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1715 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001716 "2726 READ_FCF_RECORD Indicates empty "
1717 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001718 else
1719 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001720 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001721 "with status x%x add_status x%x, "
1722 "mbx\n", shdr_status, shdr_add_status);
1723 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001724 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001725
1726 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001727 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1728 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1729 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001730 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001731 new_fcf_record = (struct fcf_record *)(virt_addr +
1732 sizeof(struct lpfc_mbx_read_fcf_tbl));
1733 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001734 offsetof(struct fcf_record, vlan_bitmap));
1735 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1736 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001737
James Smart0c9ab6f2010-02-26 14:15:57 -05001738 return new_fcf_record;
1739}
1740
1741/**
1742 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1743 * @phba: pointer to lpfc hba data structure.
1744 * @fcf_record: pointer to the fcf record.
1745 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1746 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1747 *
1748 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1749 * enabled.
1750 **/
1751static void
1752lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1753 struct fcf_record *fcf_record,
1754 uint16_t vlan_id,
1755 uint16_t next_fcf_index)
1756{
1757 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1758 "2764 READ_FCF_RECORD:\n"
1759 "\tFCF_Index : x%x\n"
1760 "\tFCF_Avail : x%x\n"
1761 "\tFCF_Valid : x%x\n"
1762 "\tFIP_Priority : x%x\n"
1763 "\tMAC_Provider : x%x\n"
1764 "\tLowest VLANID : x%x\n"
1765 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1766 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1767 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1768 "\tNext_FCF_Index: x%x\n",
1769 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1770 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1771 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1772 fcf_record->fip_priority,
1773 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1774 vlan_id,
1775 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1776 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1777 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1778 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1779 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1780 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1781 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1782 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1783 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1784 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1785 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1786 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1787 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1788 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1789 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1790 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1791 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1792 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1793 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1794 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1795 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1796 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1797 next_fcf_index);
1798}
1799
1800/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001801 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1802 * @phba: pointer to lpfc hba data structure.
1803 * @fcf_rec: pointer to an existing FCF record.
1804 * @new_fcf_record: pointer to a new FCF record.
1805 * @new_vlan_id: vlan id from the new FCF record.
1806 *
1807 * This function performs matching test of a new FCF record against an existing
1808 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1809 * will not be used as part of the FCF record matching criteria.
1810 *
1811 * Returns true if all the fields matching, otherwise returns false.
1812 */
1813static bool
1814lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1815 struct lpfc_fcf_rec *fcf_rec,
1816 struct fcf_record *new_fcf_record,
1817 uint16_t new_vlan_id)
1818{
1819 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1820 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1821 return false;
1822 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1823 return false;
1824 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1825 return false;
1826 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1827 return false;
1828 return true;
1829}
1830
1831/**
James Smarta93ff372010-10-22 11:06:08 -04001832 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1833 * @vport: Pointer to vport object.
1834 * @fcf_index: index to next fcf.
1835 *
1836 * This function processing the roundrobin fcf failover to next fcf index.
1837 * When this function is invoked, there will be a current fcf registered
1838 * for flogi.
1839 * Return: 0 for continue retrying flogi on currently registered fcf;
1840 * 1 for stop flogi on currently registered fcf;
1841 */
1842int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1843{
1844 struct lpfc_hba *phba = vport->phba;
1845 int rc;
1846
1847 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1848 spin_lock_irq(&phba->hbalock);
1849 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1850 spin_unlock_irq(&phba->hbalock);
1851 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1852 "2872 Devloss tmo with no eligible "
1853 "FCF, unregister in-use FCF (x%x) "
1854 "and rescan FCF table\n",
1855 phba->fcf.current_rec.fcf_indx);
1856 lpfc_unregister_fcf_rescan(phba);
1857 goto stop_flogi_current_fcf;
1858 }
1859 /* Mark the end to FLOGI roundrobin failover */
1860 phba->hba_flag &= ~FCF_RR_INPROG;
1861 /* Allow action to new fcf asynchronous event */
1862 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1863 spin_unlock_irq(&phba->hbalock);
1864 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1865 "2865 No FCF available, stop roundrobin FCF "
1866 "failover and change port state:x%x/x%x\n",
1867 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1868 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1869 goto stop_flogi_current_fcf;
1870 } else {
1871 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1872 "2794 Try FLOGI roundrobin FCF failover to "
1873 "(x%x)\n", fcf_index);
1874 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1875 if (rc)
1876 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1877 "2761 FLOGI roundrobin FCF failover "
1878 "failed (rc:x%x) to read FCF (x%x)\n",
1879 rc, phba->fcf.current_rec.fcf_indx);
1880 else
1881 goto stop_flogi_current_fcf;
1882 }
1883 return 0;
1884
1885stop_flogi_current_fcf:
1886 lpfc_can_disctmo(vport);
1887 return 1;
1888}
1889
1890/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001891 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1892 * @phba: pointer to lpfc hba data structure.
1893 * @mboxq: pointer to mailbox object.
1894 *
1895 * This function iterates through all the fcf records available in
1896 * HBA and chooses the optimal FCF record for discovery. After finding
1897 * the FCF for discovery it registers the FCF record and kicks start
1898 * discovery.
1899 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1900 * use an FCF record which matches fabric name and mac address of the
1901 * currently used FCF record.
1902 * If the driver supports only one FCF, it will try to use the FCF record
1903 * used by BOOT_BIOS.
1904 */
1905void
1906lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1907{
1908 struct fcf_record *new_fcf_record;
1909 uint32_t boot_flag, addr_mode;
1910 uint16_t fcf_index, next_fcf_index;
1911 struct lpfc_fcf_rec *fcf_rec = NULL;
1912 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001913 uint32_t seed;
1914 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001915 int rc;
1916
1917 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04001918 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001919 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1920 return;
1921 }
1922
1923 /* Parse the FCF record from the non-embedded mailbox command */
1924 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1925 &next_fcf_index);
1926 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04001927 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05001928 "2765 Mailbox command READ_FCF_RECORD "
1929 "failed to retrieve a FCF record.\n");
1930 /* Let next new FCF event trigger fast failover */
1931 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001932 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05001933 spin_unlock_irq(&phba->hbalock);
1934 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1935 return;
1936 }
1937
1938 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001939 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1940 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001941
1942 /* Log the FCF record information if turned on */
1943 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1944 next_fcf_index);
1945
James Smart6fb120a2009-05-22 14:52:59 -04001946 /*
1947 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001948 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04001949 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001950 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001951 if (!rc) {
1952 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04001953 "2781 FCF (x%x) failed connection "
1954 "list check: (x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05001955 bf_get(lpfc_fcf_record_fcf_index,
1956 new_fcf_record),
1957 bf_get(lpfc_fcf_record_fcf_avail,
1958 new_fcf_record),
1959 bf_get(lpfc_fcf_record_fcf_valid,
1960 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04001961 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
1962 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
1963 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04001964 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
1965 phba->fcf.current_rec.fcf_indx) {
1966 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
1967 "2862 FCF (x%x) matches property "
1968 "of in-use FCF (x%x)\n",
1969 bf_get(lpfc_fcf_record_fcf_index,
1970 new_fcf_record),
1971 phba->fcf.current_rec.fcf_indx);
1972 goto read_next_fcf;
1973 }
James Smartdbb6b3a2010-06-08 18:31:37 -04001974 /*
1975 * In case the current in-use FCF record becomes
1976 * invalid/unavailable during FCF discovery that
1977 * was not triggered by fast FCF failover process,
1978 * treat it as fast FCF failover.
1979 */
1980 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
1981 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
1982 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1983 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04001984 "(x%x), enter FCF failover "
1985 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04001986 phba->fcf.current_rec.fcf_indx);
1987 spin_lock_irq(&phba->hbalock);
1988 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
1989 spin_unlock_irq(&phba->hbalock);
1990 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1991 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
1992 LPFC_FCOE_FCF_GET_FIRST);
1993 return;
1994 }
1995 }
James Smart6fb120a2009-05-22 14:52:59 -04001996 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001997 } else {
1998 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1999 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2000 if (rc)
2001 goto read_next_fcf;
2002 }
2003
James Smart6fb120a2009-05-22 14:52:59 -04002004 /*
2005 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002006 * FCF record for the discovery. The condition that a rescan
2007 * matches the in-use FCF record: fabric name, switch name, mac
2008 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002009 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002010 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002011 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002012 if (lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2013 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002014 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2015 phba->fcf.current_rec.fcf_indx) {
2016 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2017 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2018 /* Stop FCF redisc wait timer */
2019 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2020 phba);
2021 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2022 /* Fast failover, mark completed */
2023 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2024 spin_unlock_irq(&phba->hbalock);
2025 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2026 "2836 New FCF matches in-use "
2027 "FCF (x%x)\n",
2028 phba->fcf.current_rec.fcf_indx);
2029 goto out;
2030 } else
2031 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2032 "2863 New FCF (x%x) matches "
2033 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002034 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002035 new_fcf_record),
2036 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002037 }
James Smartecfd03c2010-02-12 14:41:27 -05002038 /*
2039 * Read next FCF record from HBA searching for the matching
2040 * with in-use record only if not during the fast failover
2041 * period. In case of fast failover period, it shall try to
2042 * determine whether the FCF record just read should be the
2043 * next candidate.
2044 */
2045 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002046 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002047 goto read_next_fcf;
2048 }
James Smart6fb120a2009-05-22 14:52:59 -04002049 }
James Smartecfd03c2010-02-12 14:41:27 -05002050 /*
2051 * Update on failover FCF record only if it's in FCF fast-failover
2052 * period; otherwise, update on current FCF record.
2053 */
James Smartfc2b9892010-02-26 14:15:29 -05002054 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2055 fcf_rec = &phba->fcf.failover_rec;
2056 else
James Smartecfd03c2010-02-12 14:41:27 -05002057 fcf_rec = &phba->fcf.current_rec;
2058
James Smart6fb120a2009-05-22 14:52:59 -04002059 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2060 /*
James Smartecfd03c2010-02-12 14:41:27 -05002061 * If the driver FCF record does not have boot flag
2062 * set and new hba fcf record has boot flag set, use
2063 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002064 */
James Smartecfd03c2010-02-12 14:41:27 -05002065 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2066 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002067 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2068 "2837 Update current FCF record "
2069 "(x%x) with new FCF record (x%x)\n",
2070 fcf_rec->fcf_indx,
2071 bf_get(lpfc_fcf_record_fcf_index,
2072 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002073 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2074 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002075 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002076 goto read_next_fcf;
2077 }
2078 /*
James Smartecfd03c2010-02-12 14:41:27 -05002079 * If the driver FCF record has boot flag set and the
2080 * new hba FCF record does not have boot flag, read
2081 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002082 */
James Smartecfd03c2010-02-12 14:41:27 -05002083 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002084 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002085 goto read_next_fcf;
2086 }
2087 /*
James Smartecfd03c2010-02-12 14:41:27 -05002088 * If the new hba FCF record has lower priority value
2089 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002090 */
James Smartfc2b9892010-02-26 14:15:29 -05002091 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002092 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002093 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2094 "2838 Update current FCF record "
2095 "(x%x) with new FCF record (x%x)\n",
2096 fcf_rec->fcf_indx,
2097 bf_get(lpfc_fcf_record_fcf_index,
2098 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002099 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2100 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002101 /* Reset running random FCF selection count */
2102 phba->fcf.eligible_fcf_cnt = 1;
2103 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2104 /* Update running random FCF selection count */
2105 phba->fcf.eligible_fcf_cnt++;
2106 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2107 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002108 if (select_new_fcf) {
2109 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2110 "2839 Update current FCF record "
2111 "(x%x) with new FCF record (x%x)\n",
2112 fcf_rec->fcf_indx,
2113 bf_get(lpfc_fcf_record_fcf_index,
2114 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002115 /* Choose the new FCF by random selection */
2116 __lpfc_update_fcf_record(phba, fcf_rec,
2117 new_fcf_record,
2118 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002119 }
James Smart6fb120a2009-05-22 14:52:59 -04002120 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002121 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002122 goto read_next_fcf;
2123 }
2124 /*
James Smartecfd03c2010-02-12 14:41:27 -05002125 * This is the first suitable FCF record, choose this record for
2126 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002127 */
James Smartecfd03c2010-02-12 14:41:27 -05002128 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002129 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002130 "2840 Update initial FCF candidate "
2131 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002132 bf_get(lpfc_fcf_record_fcf_index,
2133 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002134 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2135 addr_mode, vlan_id, (boot_flag ?
2136 BOOT_ENABLE : 0));
2137 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002138 /* Setup initial running random FCF selection count */
2139 phba->fcf.eligible_fcf_cnt = 1;
2140 /* Seeding the random number generator for random selection */
2141 seed = (uint32_t)(0xFFFFFFFF & jiffies);
2142 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04002143 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002144 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002145 goto read_next_fcf;
2146
2147read_next_fcf:
2148 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002149 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2150 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2151 /*
2152 * Case of FCF fast failover scan
2153 */
2154
2155 /*
2156 * It has not found any suitable FCF record, cancel
2157 * FCF scan inprogress, and do nothing
2158 */
2159 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002160 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002161 "2782 No suitable FCF found: "
2162 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002163 phba->fcoe_eventtag_at_fcf_scan,
2164 bf_get(lpfc_fcf_record_fcf_index,
2165 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002166 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002167 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2168 phba->hba_flag &= ~FCF_TS_INPROG;
2169 spin_unlock_irq(&phba->hbalock);
2170 /* Unregister in-use FCF and rescan */
2171 lpfc_printf_log(phba, KERN_INFO,
2172 LOG_FIP,
2173 "2864 On devloss tmo "
2174 "unreg in-use FCF and "
2175 "rescan FCF table\n");
2176 lpfc_unregister_fcf_rescan(phba);
2177 return;
2178 }
2179 /*
2180 * Let next new FCF event trigger fast failover
2181 */
2182 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002183 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002184 return;
2185 }
2186 /*
2187 * It has found a suitable FCF record that is not
2188 * the same as in-use FCF record, unregister the
2189 * in-use FCF record, replace the in-use FCF record
2190 * with the new FCF record, mark FCF fast failover
2191 * completed, and then start register the new FCF
2192 * record.
2193 */
2194
James Smart0c9ab6f2010-02-26 14:15:57 -05002195 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002196 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002197
2198 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002199 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002200 "2842 Replace in-use FCF (x%x) "
2201 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002202 phba->fcf.current_rec.fcf_indx,
2203 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002204 memcpy(&phba->fcf.current_rec,
2205 &phba->fcf.failover_rec,
2206 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002207 /*
2208 * Mark the fast FCF failover rediscovery completed
2209 * and the start of the first round of the roundrobin
2210 * FCF failover.
2211 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002213 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002214 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002215 /* Register to the new FCF record */
2216 lpfc_register_fcf(phba);
2217 } else {
2218 /*
2219 * In case of transaction period to fast FCF failover,
2220 * do nothing when search to the end of the FCF table.
2221 */
2222 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2223 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2224 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002225
2226 if (phba->fcf.fcf_flag & FCF_IN_USE) {
2227 /*
2228 * In case the current in-use FCF record no
2229 * longer existed during FCF discovery that
2230 * was not triggered by fast FCF failover
2231 * process, treat it as fast FCF failover.
2232 */
2233 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2234 "2841 In-use FCF record (x%x) "
2235 "not reported, entering fast "
2236 "FCF failover mode scanning.\n",
2237 phba->fcf.current_rec.fcf_indx);
2238 spin_lock_irq(&phba->hbalock);
2239 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2240 spin_unlock_irq(&phba->hbalock);
2241 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2242 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2243 LPFC_FCOE_FCF_GET_FIRST);
2244 return;
2245 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002246 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002247 lpfc_register_fcf(phba);
2248 }
2249 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002250 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002251 return;
2252
2253out:
2254 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2255 lpfc_register_fcf(phba);
2256
2257 return;
2258}
2259
2260/**
James Smarta93ff372010-10-22 11:06:08 -04002261 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002262 * @phba: pointer to lpfc hba data structure.
2263 * @mboxq: pointer to mailbox object.
2264 *
James Smarta93ff372010-10-22 11:06:08 -04002265 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002266 * read FCF record mailbox command from the eligible FCF record bmask for
2267 * performing the failover. If the FCF read back is not valid/available, it
2268 * fails through to retrying FLOGI to the currently registered FCF again.
2269 * Otherwise, if the FCF read back is valid and available, it will set the
2270 * newly read FCF record to the failover FCF record, unregister currently
2271 * registered FCF record, copy the failover FCF record to the current
2272 * FCF record, and then register the current FCF record before proceeding
2273 * to trying FLOGI on the new failover FCF.
2274 */
2275void
2276lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2277{
2278 struct fcf_record *new_fcf_record;
2279 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002280 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002281 uint16_t current_fcf_index;
2282 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002283 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002284
James Smarta93ff372010-10-22 11:06:08 -04002285 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002286 if (phba->link_state < LPFC_LINK_UP) {
2287 spin_lock_irq(&phba->hbalock);
2288 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002289 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002290 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002291 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002292 }
2293
2294 /* Parse the FCF record from the non-embedded mailbox command */
2295 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2296 &next_fcf_index);
2297 if (!new_fcf_record) {
2298 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2299 "2766 Mailbox command READ_FCF_RECORD "
2300 "failed to retrieve a FCF record.\n");
James Smarta93ff372010-10-22 11:06:08 -04002301 goto error_out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002302 }
2303
2304 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002305 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2306 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002307
2308 /* Log the FCF record information if turned on */
2309 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2310 next_fcf_index);
2311
James Smarta93ff372010-10-22 11:06:08 -04002312 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2313 if (!rc) {
2314 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2315 "2848 Remove ineligible FCF (x%x) from "
2316 "from roundrobin bmask\n", fcf_index);
2317 /* Clear roundrobin bmask bit for ineligible FCF */
2318 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2319 /* Perform next round of roundrobin FCF failover */
2320 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2321 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2322 if (rc)
2323 goto out;
2324 goto error_out;
2325 }
2326
2327 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2328 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2329 "2760 Perform FLOGI roundrobin FCF failover: "
2330 "FCF (x%x) back to FCF (x%x)\n",
2331 phba->fcf.current_rec.fcf_indx, fcf_index);
2332 /* Wait 500 ms before retrying FLOGI to current FCF */
2333 msleep(500);
2334 lpfc_initial_flogi(phba->pport);
2335 goto out;
2336 }
2337
James Smart0c9ab6f2010-02-26 14:15:57 -05002338 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002339 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002340 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2341 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002342 spin_lock_irq(&phba->hbalock);
2343 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2344 new_fcf_record, addr_mode, vlan_id,
2345 (boot_flag ? BOOT_ENABLE : 0));
2346 spin_unlock_irq(&phba->hbalock);
2347
2348 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2349
2350 /* Unregister the current in-use FCF record */
2351 lpfc_unregister_fcf(phba);
2352
2353 /* Replace in-use record with the new record */
2354 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2355 sizeof(struct lpfc_fcf_rec));
2356
2357 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002358 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2359 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002360
James Smarta93ff372010-10-22 11:06:08 -04002361error_out:
2362 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002363out:
2364 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002365}
2366
2367/**
2368 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2369 * @phba: pointer to lpfc hba data structure.
2370 * @mboxq: pointer to mailbox object.
2371 *
2372 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002373 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002374 * failover when a new FCF event happened. If the FCF read back is
2375 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002376 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002377 */
2378void
2379lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2380{
2381 struct fcf_record *new_fcf_record;
2382 uint32_t boot_flag, addr_mode;
2383 uint16_t fcf_index, next_fcf_index;
2384 uint16_t vlan_id;
2385 int rc;
2386
2387 /* If link state is not up, no need to proceed */
2388 if (phba->link_state < LPFC_LINK_UP)
2389 goto out;
2390
2391 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002392 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002393 goto out;
2394
2395 /* Parse the FCF record from the non-embedded mailbox command */
2396 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2397 &next_fcf_index);
2398 if (!new_fcf_record) {
2399 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2400 "2767 Mailbox command READ_FCF_RECORD "
2401 "failed to retrieve a FCF record.\n");
2402 goto out;
2403 }
2404
2405 /* Check the connection list for eligibility */
2406 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2407 &addr_mode, &vlan_id);
2408
2409 /* Log the FCF record information if turned on */
2410 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2411 next_fcf_index);
2412
2413 if (!rc)
2414 goto out;
2415
2416 /* Update the eligible FCF record index bmask */
2417 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2418 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2419
2420out:
2421 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2422}
2423
2424/**
James Smart1c6834a2009-07-19 10:01:26 -04002425 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2426 * @phba: pointer to lpfc hba data structure.
2427 * @mboxq: pointer to mailbox data structure.
2428 *
2429 * This function handles completion of init vpi mailbox command.
2430 */
James Smart695a8142010-01-26 23:08:03 -05002431void
James Smart1c6834a2009-07-19 10:01:26 -04002432lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2433{
2434 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002435 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002436 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2437
James Smart1c6834a2009-07-19 10:01:26 -04002438 if (mboxq->u.mb.mbxStatus) {
2439 lpfc_printf_vlog(vport, KERN_ERR,
2440 LOG_MBOX,
2441 "2609 Init VPI mailbox failed 0x%x\n",
2442 mboxq->u.mb.mbxStatus);
2443 mempool_free(mboxq, phba->mbox_mem_pool);
2444 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2445 return;
2446 }
James Smart72100cc2010-02-12 14:43:01 -05002447 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002448 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002449 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002450
James Smart695a8142010-01-26 23:08:03 -05002451 /* If this port is physical port or FDISC is done, do reg_vpi */
2452 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2453 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2454 if (!ndlp)
2455 lpfc_printf_vlog(vport, KERN_ERR,
2456 LOG_DISCOVERY,
2457 "2731 Cannot find fabric "
2458 "controller node\n");
2459 else
2460 lpfc_register_new_vport(phba, vport, ndlp);
2461 mempool_free(mboxq, phba->mbox_mem_pool);
2462 return;
2463 }
2464
James Smart1c6834a2009-07-19 10:01:26 -04002465 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2466 lpfc_initial_fdisc(vport);
2467 else {
2468 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002469 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2470 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002471 }
James Smart695a8142010-01-26 23:08:03 -05002472 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002473 return;
2474}
2475
2476/**
James Smartecfd03c2010-02-12 14:41:27 -05002477 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2478 * @vport: pointer to lpfc_vport data structure.
2479 *
2480 * This function issue a init_vpi mailbox command to initialize
2481 * VPI for the vport.
2482 */
2483void
2484lpfc_issue_init_vpi(struct lpfc_vport *vport)
2485{
2486 LPFC_MBOXQ_t *mboxq;
2487 int rc;
2488
2489 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2490 if (!mboxq) {
2491 lpfc_printf_vlog(vport, KERN_ERR,
2492 LOG_MBOX, "2607 Failed to allocate "
2493 "init_vpi mailbox\n");
2494 return;
2495 }
2496 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2497 mboxq->vport = vport;
2498 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2499 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2500 if (rc == MBX_NOT_FINISHED) {
2501 lpfc_printf_vlog(vport, KERN_ERR,
2502 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2503 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2504 }
2505}
2506
2507/**
James Smart6fb120a2009-05-22 14:52:59 -04002508 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2509 * @phba: pointer to lpfc hba data structure.
2510 *
2511 * This function loops through the list of vports on the @phba and issues an
2512 * FDISC if possible.
2513 */
2514void
2515lpfc_start_fdiscs(struct lpfc_hba *phba)
2516{
2517 struct lpfc_vport **vports;
2518 int i;
2519
2520 vports = lpfc_create_vport_work_array(phba);
2521 if (vports != NULL) {
2522 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2523 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2524 continue;
2525 /* There are no vpi for this vport */
2526 if (vports[i]->vpi > phba->max_vpi) {
2527 lpfc_vport_set_state(vports[i],
2528 FC_VPORT_FAILED);
2529 continue;
2530 }
2531 if (phba->fc_topology == TOPOLOGY_LOOP) {
2532 lpfc_vport_set_state(vports[i],
2533 FC_VPORT_LINKDOWN);
2534 continue;
2535 }
James Smart1c6834a2009-07-19 10:01:26 -04002536 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002537 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002538 continue;
2539 }
James Smart6fb120a2009-05-22 14:52:59 -04002540 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2541 lpfc_initial_fdisc(vports[i]);
2542 else {
2543 lpfc_vport_set_state(vports[i],
2544 FC_VPORT_NO_FABRIC_SUPP);
2545 lpfc_printf_vlog(vports[i], KERN_ERR,
2546 LOG_ELS,
2547 "0259 No NPIV "
2548 "Fabric support\n");
2549 }
2550 }
2551 }
2552 lpfc_destroy_vport_work_array(phba, vports);
2553}
2554
2555void
2556lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2557{
2558 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2559 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002560 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002561
2562 if (mboxq->u.mb.mbxStatus) {
2563 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2564 "2018 REG_VFI mbxStatus error x%x "
2565 "HBA state x%x\n",
2566 mboxq->u.mb.mbxStatus, vport->port_state);
2567 if (phba->fc_topology == TOPOLOGY_LOOP) {
2568 /* FLOGI failed, use loop map to make discovery list */
2569 lpfc_disc_list_loopmap(vport);
2570 /* Start discovery */
2571 lpfc_disc_start(vport);
2572 goto fail_free_mem;
2573 }
2574 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2575 goto fail_free_mem;
2576 }
James Smartc8685952009-11-18 15:39:16 -05002577 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002578 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002579 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002580 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002581 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002582 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002583
2584 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2585 lpfc_start_fdiscs(phba);
2586 lpfc_do_scr_ns_plogi(phba, vport);
2587 }
2588
2589fail_free_mem:
2590 mempool_free(mboxq, phba->mbox_mem_pool);
2591 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2592 kfree(dmabuf);
2593 return;
2594}
2595
2596static void
James Smart2e0fef82007-06-17 19:56:36 -05002597lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002598{
James Smart6fb120a2009-05-22 14:52:59 -04002599 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002600 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002601 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002602
2603
2604 /* Check for error */
2605 if (mb->mbxStatus) {
2606 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002607 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2608 "0319 READ_SPARAM mbxStatus error x%x "
2609 "hba state x%x>\n",
2610 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002611 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002612 goto out;
2613 }
2614
James Smart2e0fef82007-06-17 19:56:36 -05002615 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002616 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002617 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002618 u64_to_wwn(phba->cfg_soft_wwnn,
2619 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002620 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002621 u64_to_wwn(phba->cfg_soft_wwpn,
2622 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002623 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2624 sizeof(vport->fc_nodename));
2625 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2626 sizeof(vport->fc_portname));
2627 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2628 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2629 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2630 }
2631
dea31012005-04-17 16:05:31 -05002632 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2633 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002634 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002635 return;
2636
2637out:
2638 pmb->context1 = NULL;
2639 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2640 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002641 lpfc_issue_clear_la(phba, vport);
2642 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002643 return;
2644}
2645
2646static void
James Smart92d7f7b2007-06-17 19:56:38 -05002647lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002648{
James Smart92d7f7b2007-06-17 19:56:38 -05002649 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002650 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002651 int i;
James Smart14691152006-12-02 13:34:28 -05002652 struct lpfc_dmabuf *mp;
2653 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002654 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002655
James Smart92d7f7b2007-06-17 19:56:38 -05002656 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002657 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002658 case LA_1GHZ_LINK:
2659 phba->fc_linkspeed = LA_1GHZ_LINK;
2660 break;
2661 case LA_2GHZ_LINK:
2662 phba->fc_linkspeed = LA_2GHZ_LINK;
2663 break;
2664 case LA_4GHZ_LINK:
2665 phba->fc_linkspeed = LA_4GHZ_LINK;
2666 break;
2667 case LA_8GHZ_LINK:
2668 phba->fc_linkspeed = LA_8GHZ_LINK;
2669 break;
James Smartf4b4c682009-05-22 14:53:12 -04002670 case LA_10GHZ_LINK:
2671 phba->fc_linkspeed = LA_10GHZ_LINK;
2672 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002673 default:
2674 phba->fc_linkspeed = LA_UNKNW_LINK;
2675 break;
dea31012005-04-17 16:05:31 -05002676 }
2677
2678 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002679 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002680
2681 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002682 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002683
James Smart6a9c52c2009-10-02 15:16:51 -04002684 /* if npiv is enabled and this adapter supports npiv log
2685 * a message that npiv is not supported in this topology
2686 */
2687 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002688 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2689 "1309 Link Up Event npiv not supported in loop "
2690 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002691 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002692 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002693 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002694
James Smart2e0fef82007-06-17 19:56:36 -05002695 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002696 i = la->un.lilpBde64.tus.f.bdeSize;
2697
2698 if (i == 0) {
2699 phba->alpa_map[0] = 0;
2700 } else {
James Smarte8b62012007-08-02 11:10:09 -04002701 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002702 int numalpa, j, k;
2703 union {
2704 uint8_t pamap[16];
2705 struct {
2706 uint32_t wd1;
2707 uint32_t wd2;
2708 uint32_t wd3;
2709 uint32_t wd4;
2710 } pa;
2711 } un;
2712 numalpa = phba->alpa_map[0];
2713 j = 0;
2714 while (j < numalpa) {
2715 memset(un.pamap, 0, 16);
2716 for (k = 1; j < numalpa; k++) {
2717 un.pamap[k - 1] =
2718 phba->alpa_map[j + 1];
2719 j++;
2720 if (k == 16)
2721 break;
2722 }
2723 /* Link Up Event ALPA map */
2724 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002725 KERN_WARNING,
2726 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002727 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002728 "ALPA map Data: x%x "
2729 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002730 un.pa.wd1, un.pa.wd2,
2731 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002732 }
2733 }
2734 }
2735 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002736 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002737 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002738 (phba->sli_rev == 3))
2739 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2740 }
James Smart2e0fef82007-06-17 19:56:36 -05002741 vport->fc_myDID = phba->fc_pref_DID;
2742 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002743 }
James Smart92d7f7b2007-06-17 19:56:38 -05002744 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002745
2746 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002747 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2748 if (!sparam_mbox)
2749 goto out;
2750
2751 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2752 if (rc) {
2753 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2754 goto out;
2755 }
2756 sparam_mbox->vport = vport;
2757 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2758 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2759 if (rc == MBX_NOT_FINISHED) {
2760 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2761 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2762 kfree(mp);
2763 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2764 goto out;
dea31012005-04-17 16:05:31 -05002765 }
2766
James Smart6fb120a2009-05-22 14:52:59 -04002767 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2768 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2769 if (!cfglink_mbox)
2770 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002771 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002772 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002773 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002774 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002775 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002776 if (rc == MBX_NOT_FINISHED) {
2777 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2778 goto out;
2779 }
2780 } else {
James Smart32b97932009-07-19 10:01:21 -04002781 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002782 /*
2783 * Add the driver's default FCF record at FCF index 0 now. This
2784 * is phase 1 implementation that support FCF index 0 and driver
2785 * defaults.
2786 */
James Smart45ed1192009-10-02 15:17:02 -04002787 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002788 fcf_record = kzalloc(sizeof(struct fcf_record),
2789 GFP_KERNEL);
2790 if (unlikely(!fcf_record)) {
2791 lpfc_printf_log(phba, KERN_ERR,
2792 LOG_MBOX | LOG_SLI,
2793 "2554 Could not allocate memmory for "
2794 "fcf record\n");
2795 rc = -ENODEV;
2796 goto out;
2797 }
2798
2799 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2800 LPFC_FCOE_FCF_DEF_INDEX);
2801 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2802 if (unlikely(rc)) {
2803 lpfc_printf_log(phba, KERN_ERR,
2804 LOG_MBOX | LOG_SLI,
2805 "2013 Could not manually add FCF "
2806 "record 0, status %d\n", rc);
2807 rc = -ENODEV;
2808 kfree(fcf_record);
2809 goto out;
2810 }
2811 kfree(fcf_record);
2812 }
2813 /*
2814 * The driver is expected to do FIP/FCF. Call the port
2815 * and get the FCF Table.
2816 */
James Smart32b97932009-07-19 10:01:21 -04002817 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002818 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04002819 spin_unlock_irq(&phba->hbalock);
2820 return;
2821 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002822 /* This is the initial FCF discovery scan */
2823 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002824 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002825 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2826 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05002827 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2828 LPFC_FCOE_FCF_GET_FIRST);
2829 if (rc) {
2830 spin_lock_irq(&phba->hbalock);
2831 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2832 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002833 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002834 }
James Smart38b92ef2010-08-04 16:11:39 -04002835 /* Reset FCF roundrobin bmask for new discovery */
2836 memset(phba->fcf.fcf_rr_bmask, 0,
2837 sizeof(*phba->fcf.fcf_rr_bmask));
dea31012005-04-17 16:05:31 -05002838 }
James Smart6fb120a2009-05-22 14:52:59 -04002839
2840 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002841out:
2842 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002843 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2844 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2845 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002846 lpfc_issue_clear_la(phba, vport);
2847 return;
dea31012005-04-17 16:05:31 -05002848}
2849
2850static void
James Smart84774a42008-08-24 21:50:06 -04002851lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002852{
dea31012005-04-17 16:05:31 -05002853 uint32_t control;
2854 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002855 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002856 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002857 if (phba->sli_rev <= LPFC_SLI_REV3) {
2858 control = readl(phba->HCregaddr);
2859 control |= HC_LAINT_ENA;
2860 writel(control, phba->HCregaddr);
2861 readl(phba->HCregaddr); /* flush */
2862 }
James Smart2e0fef82007-06-17 19:56:36 -05002863 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002864}
2865
James Smart84774a42008-08-24 21:50:06 -04002866static void
2867lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2868{
2869 lpfc_linkdown(phba);
2870 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002871 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002872 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2873}
2874
2875
dea31012005-04-17 16:05:31 -05002876/*
2877 * This routine handles processing a READ_LA mailbox
2878 * command upon completion. It is setup in the LPFC_MBOXQ
2879 * as the completion routine when the command is
2880 * handed off to the SLI layer.
2881 */
2882void
James Smart2e0fef82007-06-17 19:56:36 -05002883lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002884{
James Smart2e0fef82007-06-17 19:56:36 -05002885 struct lpfc_vport *vport = pmb->vport;
2886 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002887 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002888 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002889 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2890
James Smart0d2b6b82008-06-14 22:52:47 -04002891 /* Unblock ELS traffic */
2892 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002893 /* Check for error */
2894 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002895 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002896 "1307 READ_LA mbox error x%x state x%x\n",
2897 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002898 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002899 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002900 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2901 }
2902
James Smart04c68492009-05-22 14:52:52 -04002903 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002904
2905 memcpy(&phba->alpa_map[0], mp->virt, 128);
2906
James Smart2e0fef82007-06-17 19:56:36 -05002907 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002908 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002909 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002910 else
James Smart2e0fef82007-06-17 19:56:36 -05002911 vport->fc_flag &= ~FC_BYPASSED_MODE;
2912 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002913
James Smart0c287582009-06-10 17:22:56 -04002914 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002915 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002916 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002917 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002918 if (phba->fc_eventTag != 0)
2919 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002920 }
dea31012005-04-17 16:05:31 -05002921
2922 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002923 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002924 if (la->mm)
2925 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2926 else
2927 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002928 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002929
James Smart4d9ab992009-10-02 15:16:39 -04002930 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002931 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002932 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002933 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002934 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002935 "1306 Link Up Event in loop back mode "
2936 "x%x received Data: x%x x%x x%x x%x\n",
2937 la->eventTag, phba->fc_eventTag,
2938 la->granted_AL_PA, la->UlnkSpeed,
2939 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002940 } else {
2941 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002942 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002943 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002944 la->eventTag, phba->fc_eventTag,
2945 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002946 phba->alpa_map[0],
2947 la->mm, la->fa,
2948 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002949 }
James Smart92d7f7b2007-06-17 19:56:38 -05002950 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002951 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002952 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002953 if (phba->link_flag & LS_LOOPBACK_MODE) {
2954 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2955 "1308 Link Down Event in loop back mode "
2956 "x%x received "
2957 "Data: x%x x%x x%x\n",
2958 la->eventTag, phba->fc_eventTag,
2959 phba->pport->port_state, vport->fc_flag);
2960 }
2961 else {
2962 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002963 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002964 "Data: x%x x%x x%x x%x x%x\n",
2965 la->eventTag, phba->fc_eventTag,
2966 phba->pport->port_state, vport->fc_flag,
2967 la->mm, la->fa);
2968 }
2969 lpfc_mbx_issue_link_down(phba);
2970 }
2971 if (la->mm && la->attType == AT_LINK_UP) {
2972 if (phba->link_state != LPFC_LINK_DOWN) {
2973 phba->fc_stat.LinkDown++;
2974 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2975 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002976 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002977 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002978 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002979 lpfc_mbx_issue_link_down(phba);
2980 } else
2981 lpfc_enable_la(phba);
2982
2983 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2984 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2985 "Data: x%x x%x x%x\n",
2986 la->eventTag, phba->fc_eventTag,
2987 phba->pport->port_state, vport->fc_flag);
2988 /*
2989 * The cmnd that triggered this will be waiting for this
2990 * signal.
2991 */
2992 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2993 if (phba->wait_4_mlo_maint_flg) {
2994 phba->wait_4_mlo_maint_flg = 0;
2995 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002996 }
James Smart84774a42008-08-24 21:50:06 -04002997 }
2998
2999 if (la->fa) {
3000 if (la->mm)
3001 lpfc_issue_clear_la(phba, vport);
3002 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3003 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05003004 }
3005
3006lpfc_mbx_cmpl_read_la_free_mbuf:
3007 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3008 kfree(mp);
3009 mempool_free(pmb, phba->mbox_mem_pool);
3010 return;
3011}
3012
3013/*
3014 * This routine handles processing a REG_LOGIN mailbox
3015 * command upon completion. It is setup in the LPFC_MBOXQ
3016 * as the completion routine when the command is
3017 * handed off to the SLI layer.
3018 */
3019void
James Smart2e0fef82007-06-17 19:56:36 -05003020lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003021{
James Smart2e0fef82007-06-17 19:56:36 -05003022 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003023 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003024 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003025 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003026
dea31012005-04-17 16:05:31 -05003027 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003028 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003029
James Smartffc95492010-06-07 15:23:17 -04003030 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3031 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3032
3033 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3034 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3035 /* We rcvd a rscn after issuing this
3036 * mbox reg login, we may have cycled
3037 * back through the state and be
3038 * back at reg login state so this
3039 * mbox needs to be ignored becase
3040 * there is another reg login in
3041 * proccess.
3042 */
3043 spin_lock_irq(shost->host_lock);
3044 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3045 spin_unlock_irq(shost->host_lock);
3046 if (phba->sli_rev == LPFC_SLI_REV4)
3047 lpfc_sli4_free_rpi(phba,
3048 pmb->u.mb.un.varRegLogin.rpi);
3049
3050 } else
3051 /* Good status, call state machine */
3052 lpfc_disc_state_machine(vport, ndlp, pmb,
3053 NLP_EVT_CMPL_REG_LOGIN);
3054
dea31012005-04-17 16:05:31 -05003055 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3056 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003057 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003058 /* decrement the node reference count held for this callback
3059 * function.
3060 */
James Smart329f9bc2007-04-25 09:53:01 -04003061 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003062
3063 return;
3064}
3065
James Smart92d7f7b2007-06-17 19:56:38 -05003066static void
3067lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3068{
James Smart04c68492009-05-22 14:52:52 -04003069 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003070 struct lpfc_vport *vport = pmb->vport;
3071 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3072
3073 switch (mb->mbxStatus) {
3074 case 0x0011:
3075 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003076 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3077 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3078 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003079 break;
James Smart78730cf2010-04-06 15:06:30 -04003080 /* If VPI is busy, reset the HBA */
3081 case 0x9700:
3082 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3083 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3084 vport->vpi, mb->mbxStatus);
3085 if (!(phba->pport->load_flag & FC_UNLOADING))
3086 lpfc_workq_post_event(phba, NULL, NULL,
3087 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003088 }
James Smart72100cc2010-02-12 14:43:01 -05003089 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003090 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003091 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003092 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003093 vport->unreg_vpi_cmpl = VPORT_OK;
3094 mempool_free(pmb, phba->mbox_mem_pool);
3095 /*
3096 * This shost reference might have been taken at the beginning of
3097 * lpfc_vport_delete()
3098 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003099 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003100 scsi_host_put(shost);
3101}
3102
James Smartd7c255b2008-08-24 21:50:00 -04003103int
James Smart92d7f7b2007-06-17 19:56:38 -05003104lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3105{
3106 struct lpfc_hba *phba = vport->phba;
3107 LPFC_MBOXQ_t *mbox;
3108 int rc;
3109
3110 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3111 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003112 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003113
3114 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3115 mbox->vport = vport;
3116 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003118 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003119 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3120 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003121 mempool_free(mbox, phba->mbox_mem_pool);
3122 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003123 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003124 }
James Smartd7c255b2008-08-24 21:50:00 -04003125 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003126}
3127
3128static void
3129lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3130{
3131 struct lpfc_vport *vport = pmb->vport;
3132 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003133 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003134
3135 switch (mb->mbxStatus) {
3136 case 0x0011:
3137 case 0x9601:
3138 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003139 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3140 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3141 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003142 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3143 spin_lock_irq(shost->host_lock);
3144 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3145 spin_unlock_irq(shost->host_lock);
3146 vport->fc_myDID = 0;
3147 goto out;
3148 }
James Smart92d7f7b2007-06-17 19:56:38 -05003149
James Smart72100cc2010-02-12 14:43:01 -05003150 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003151 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003152 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003153 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003154 vport->num_disc_nodes = 0;
3155 /* go thru NPR list and issue ELS PLOGIs */
3156 if (vport->fc_npr_cnt)
3157 lpfc_els_disc_plogi(vport);
3158
3159 if (!vport->num_disc_nodes) {
3160 spin_lock_irq(shost->host_lock);
3161 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3162 spin_unlock_irq(shost->host_lock);
3163 lpfc_can_disctmo(vport);
3164 }
3165 vport->port_state = LPFC_VPORT_READY;
3166
3167out:
3168 mempool_free(pmb, phba->mbox_mem_pool);
3169 return;
3170}
3171
James Smart21e9a0a2009-05-22 14:53:21 -04003172/**
3173 * lpfc_create_static_vport - Read HBA config region to create static vports.
3174 * @phba: pointer to lpfc hba data structure.
3175 *
3176 * This routine issue a DUMP mailbox command for config region 22 to get
3177 * the list of static vports to be created. The function create vports
3178 * based on the information returned from the HBA.
3179 **/
3180void
3181lpfc_create_static_vport(struct lpfc_hba *phba)
3182{
3183 LPFC_MBOXQ_t *pmb = NULL;
3184 MAILBOX_t *mb;
3185 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04003186 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003187 struct fc_vport_identifiers vport_id;
3188 struct fc_vport *new_fc_vport;
3189 struct Scsi_Host *shost;
3190 struct lpfc_vport *vport;
3191 uint16_t offset = 0;
3192 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003193 struct lpfc_dmabuf *mp;
3194 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003195
3196 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3197 if (!pmb) {
3198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3199 "0542 lpfc_create_static_vport failed to"
3200 " allocate mailbox memory\n");
3201 return;
3202 }
3203
3204 mb = &pmb->u.mb;
3205
3206 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3207 if (!vport_info) {
3208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3209 "0543 lpfc_create_static_vport failed to"
3210 " allocate vport_info\n");
3211 mempool_free(pmb, phba->mbox_mem_pool);
3212 return;
3213 }
3214
3215 vport_buff = (uint8_t *) vport_info;
3216 do {
James Smart1c6834a2009-07-19 10:01:26 -04003217 if (lpfc_dump_static_vport(phba, pmb, offset))
3218 goto out;
3219
James Smart21e9a0a2009-05-22 14:53:21 -04003220 pmb->vport = phba->pport;
3221 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
3222
3223 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
3224 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3225 "0544 lpfc_create_static_vport failed to"
3226 " issue dump mailbox command ret 0x%x "
3227 "status 0x%x\n",
3228 rc, mb->mbxStatus);
3229 goto out;
3230 }
3231
James Smart1c6834a2009-07-19 10:01:26 -04003232 if (phba->sli_rev == LPFC_SLI_REV4) {
3233 byte_count = pmb->u.mqe.un.mb_words[5];
3234 mp = (struct lpfc_dmabuf *) pmb->context2;
3235 if (byte_count > sizeof(struct static_vport_info) -
3236 offset)
3237 byte_count = sizeof(struct static_vport_info)
3238 - offset;
3239 memcpy(vport_buff + offset, mp->virt, byte_count);
3240 offset += byte_count;
3241 } else {
3242 if (mb->un.varDmp.word_cnt >
3243 sizeof(struct static_vport_info) - offset)
3244 mb->un.varDmp.word_cnt =
3245 sizeof(struct static_vport_info)
3246 - offset;
3247 byte_count = mb->un.varDmp.word_cnt;
3248 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3249 vport_buff + offset,
3250 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003251
James Smart1c6834a2009-07-19 10:01:26 -04003252 offset += byte_count;
3253 }
James Smart21e9a0a2009-05-22 14:53:21 -04003254
James Smart1c6834a2009-07-19 10:01:26 -04003255 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003256 offset < sizeof(struct static_vport_info));
3257
3258
3259 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3260 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3261 != VPORT_INFO_REV)) {
3262 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3263 "0545 lpfc_create_static_vport bad"
3264 " information header 0x%x 0x%x\n",
3265 le32_to_cpu(vport_info->signature),
3266 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3267
3268 goto out;
3269 }
3270
3271 shost = lpfc_shost_from_vport(phba->pport);
3272
3273 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3274 memset(&vport_id, 0, sizeof(vport_id));
3275 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3276 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3277 if (!vport_id.port_name || !vport_id.node_name)
3278 continue;
3279
3280 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3281 vport_id.vport_type = FC_PORTTYPE_NPIV;
3282 vport_id.disable = false;
3283 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3284
3285 if (!new_fc_vport) {
3286 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3287 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003288 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003289 continue;
3290 }
3291
3292 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3293 vport->vport_flag |= STATIC_VPORT;
3294 }
3295
3296out:
James Smart21e9a0a2009-05-22 14:53:21 -04003297 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04003298 if (rc != MBX_TIMEOUT) {
3299 if (pmb->context2) {
3300 mp = (struct lpfc_dmabuf *) pmb->context2;
3301 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3302 kfree(mp);
3303 }
James Smart21e9a0a2009-05-22 14:53:21 -04003304 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003305 }
James Smart21e9a0a2009-05-22 14:53:21 -04003306
3307 return;
3308}
3309
dea31012005-04-17 16:05:31 -05003310/*
3311 * This routine handles processing a Fabric REG_LOGIN mailbox
3312 * command upon completion. It is setup in the LPFC_MBOXQ
3313 * as the completion routine when the command is
3314 * handed off to the SLI layer.
3315 */
3316void
James Smart2e0fef82007-06-17 19:56:36 -05003317lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003318{
James Smart92d7f7b2007-06-17 19:56:38 -05003319 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003320 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003321 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003322 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003323
James Smart549e55c2007-08-02 11:09:51 -04003324 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003325 pmb->context1 = NULL;
3326 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003327
dea31012005-04-17 16:05:31 -05003328 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003329 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3330 "0258 Register Fabric login error: 0x%x\n",
3331 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003332 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3333 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003334 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003335
James Smart92d7f7b2007-06-17 19:56:38 -05003336 if (phba->fc_topology == TOPOLOGY_LOOP) {
3337 /* FLOGI failed, use loop map to make discovery list */
3338 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003339
James Smart92d7f7b2007-06-17 19:56:38 -05003340 /* Start discovery */
3341 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003342 /* Decrement the reference count to ndlp after the
3343 * reference to the ndlp are done.
3344 */
3345 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003346 return;
3347 }
3348
3349 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003350 /* Decrement the reference count to ndlp after the reference
3351 * to the ndlp are done.
3352 */
3353 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003354 return;
3355 }
3356
dea31012005-04-17 16:05:31 -05003357 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003358 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003359 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003360 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003361
James Smart2e0fef82007-06-17 19:56:36 -05003362 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003363 /* when physical port receive logo donot start
3364 * vport discovery */
3365 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3366 lpfc_start_fdiscs(phba);
3367 else
3368 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003369 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003370 }
3371
3372 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3373 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003374 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003375
3376 /* Drop the reference count from the mbox at the end after
3377 * all the current reference to the ndlp have been done.
3378 */
3379 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003380 return;
3381}
3382
3383/*
3384 * This routine handles processing a NameServer REG_LOGIN mailbox
3385 * command upon completion. It is setup in the LPFC_MBOXQ
3386 * as the completion routine when the command is
3387 * handed off to the SLI layer.
3388 */
3389void
James Smart2e0fef82007-06-17 19:56:36 -05003390lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003391{
James Smart21e9a0a2009-05-22 14:53:21 -04003392 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003393 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3394 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3395 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003396
James Smartd439d282010-09-29 11:18:45 -04003397 pmb->context1 = NULL;
3398 pmb->context2 = NULL;
3399
dea31012005-04-17 16:05:31 -05003400 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003401out:
James Smart21e9a0a2009-05-22 14:53:21 -04003402 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3403 "0260 Register NameServer error: 0x%x\n",
3404 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003405 /* decrement the node reference count held for this
3406 * callback function.
3407 */
James Smart329f9bc2007-04-25 09:53:01 -04003408 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003409 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3410 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003411 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003412
3413 /* If no other thread is using the ndlp, free it */
3414 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003415
James Smart92d7f7b2007-06-17 19:56:38 -05003416 if (phba->fc_topology == TOPOLOGY_LOOP) {
3417 /*
3418 * RegLogin failed, use loop map to make discovery
3419 * list
3420 */
3421 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003422
James Smart92d7f7b2007-06-17 19:56:38 -05003423 /* Start discovery */
3424 lpfc_disc_start(vport);
3425 return;
3426 }
3427 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003428 return;
3429 }
3430
dea31012005-04-17 16:05:31 -05003431 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003432 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003433 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003434 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003435
James Smart2e0fef82007-06-17 19:56:36 -05003436 if (vport->port_state < LPFC_VPORT_READY) {
3437 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003438 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3439 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3440 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3441 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3442 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3443
3444 /* Issue SCR just before NameServer GID_FT Query */
3445 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003446 }
3447
James Smart2e0fef82007-06-17 19:56:36 -05003448 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003449 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003450 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003451 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003452 goto out;
dea31012005-04-17 16:05:31 -05003453 }
3454
James Smartfa4066b2008-01-11 01:53:27 -05003455 /* decrement the node reference count held for this
3456 * callback function.
3457 */
James Smart329f9bc2007-04-25 09:53:01 -04003458 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003459 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3460 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003461 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003462
3463 return;
3464}
3465
3466static void
James Smart2e0fef82007-06-17 19:56:36 -05003467lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003468{
James Smart2e0fef82007-06-17 19:56:36 -05003469 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3470 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003471 struct lpfc_rport_data *rdata;
3472 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003473 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003474
3475 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003476 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3477 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003478 rport_ids.port_id = ndlp->nlp_DID;
3479 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003480
James Smart329f9bc2007-04-25 09:53:01 -04003481 /*
3482 * We leave our node pointer in rport->dd_data when we unregister a
3483 * FCP target port. But fc_remote_port_add zeros the space to which
3484 * rport->dd_data points. So, if we're reusing a previously
3485 * registered port, drop the reference that we took the last time we
3486 * registered the port.
3487 */
3488 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003489 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003490 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003491
3492 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3493 "rport add: did:x%x flg:x%x type x%x",
3494 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3495
James Smart2e0fef82007-06-17 19:56:36 -05003496 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003497 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003498 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3499 "Warning: fc_remote_port_add failed\n");
3500 return;
3501 }
3502
3503 /* initialize static port data */
3504 rport->maxframe_size = ndlp->nlp_maxframe;
3505 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003506 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003507 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003508
3509 if (ndlp->nlp_type & NLP_FCP_TARGET)
3510 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3511 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3512 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3513
3514
3515 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3516 fc_remote_port_rolechg(rport, rport_ids.roles);
3517
James Smart071fbd3d2006-04-15 11:53:20 -04003518 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003519 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003520 ndlp->nlp_sid = rport->scsi_target_id;
3521 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003522 return;
3523}
3524
3525static void
James Smart2e0fef82007-06-17 19:56:36 -05003526lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003527{
3528 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003529
James Smart858c9f62007-06-17 19:56:39 -05003530 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3531 "rport delete: did:x%x flg:x%x type x%x",
3532 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3533
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003534 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003535
3536 return;
3537}
3538
James Smartde0c5b32007-04-25 09:52:27 -04003539static void
James Smart2e0fef82007-06-17 19:56:36 -05003540lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003541{
James Smart2e0fef82007-06-17 19:56:36 -05003542 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3543
3544 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003545 switch (state) {
3546 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003547 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003548 break;
3549 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003550 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003551 break;
3552 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003553 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003554 break;
3555 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003556 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003557 break;
3558 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003559 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003560 break;
3561 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003562 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003563 break;
3564 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003565 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003566 break;
3567 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003568 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003569 break;
3570 }
James Smart2e0fef82007-06-17 19:56:36 -05003571 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003572}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003573
James Smartde0c5b32007-04-25 09:52:27 -04003574static void
James Smart2e0fef82007-06-17 19:56:36 -05003575lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003576 int old_state, int new_state)
3577{
James Smart2e0fef82007-06-17 19:56:36 -05003578 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3579
James Smartde0c5b32007-04-25 09:52:27 -04003580 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003581 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3582 ndlp->nlp_type |= NLP_FC_NODE;
3583 }
3584 if (new_state == NLP_STE_MAPPED_NODE)
3585 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3586 if (new_state == NLP_STE_NPR_NODE)
3587 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003588
James Smartde0c5b32007-04-25 09:52:27 -04003589 /* Transport interface */
3590 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3591 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003592 vport->phba->nport_event_cnt++;
3593 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003594 }
dea31012005-04-17 16:05:31 -05003595
James Smartde0c5b32007-04-25 09:52:27 -04003596 if (new_state == NLP_STE_MAPPED_NODE ||
3597 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003598 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003599 /*
3600 * Tell the fc transport about the port, if we haven't
3601 * already. If we have, and it's a scsi entity, be
3602 * sure to unblock any attached scsi devices
3603 */
3604 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003605 }
James Smartea2151b2008-09-07 11:52:10 -04003606 if ((new_state == NLP_STE_MAPPED_NODE) &&
3607 (vport->stat_data_enabled)) {
3608 /*
3609 * A new target is discovered, if there is no buffer for
3610 * statistical data collection allocate buffer.
3611 */
3612 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3613 sizeof(struct lpfc_scsicmd_bkt),
3614 GFP_KERNEL);
3615
3616 if (!ndlp->lat_data)
3617 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3618 "0286 lpfc_nlp_state_cleanup failed to "
3619 "allocate statistical data buffer DID "
3620 "0x%x\n", ndlp->nlp_DID);
3621 }
James Smart858c9f62007-06-17 19:56:39 -05003622 /*
3623 * if we added to Mapped list, but the remote port
3624 * registration failed or assigned a target id outside
3625 * our presentable range - move the node to the
3626 * Unmapped List
3627 */
James Smartde0c5b32007-04-25 09:52:27 -04003628 if (new_state == NLP_STE_MAPPED_NODE &&
3629 (!ndlp->rport ||
3630 ndlp->rport->scsi_target_id == -1 ||
3631 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003632 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003633 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003634 spin_unlock_irq(shost->host_lock);
3635 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003636 }
James Smartde0c5b32007-04-25 09:52:27 -04003637}
3638
James Smart685f0bf2007-04-25 09:53:08 -04003639static char *
3640lpfc_nlp_state_name(char *buffer, size_t size, int state)
3641{
3642 static char *states[] = {
3643 [NLP_STE_UNUSED_NODE] = "UNUSED",
3644 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3645 [NLP_STE_ADISC_ISSUE] = "ADISC",
3646 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3647 [NLP_STE_PRLI_ISSUE] = "PRLI",
3648 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3649 [NLP_STE_MAPPED_NODE] = "MAPPED",
3650 [NLP_STE_NPR_NODE] = "NPR",
3651 };
3652
James Smart311464e2007-08-02 11:10:37 -04003653 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003654 strlcpy(buffer, states[state], size);
3655 else
3656 snprintf(buffer, size, "unknown (%d)", state);
3657 return buffer;
3658}
3659
James Smartde0c5b32007-04-25 09:52:27 -04003660void
James Smart2e0fef82007-06-17 19:56:36 -05003661lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3662 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003663{
James Smart2e0fef82007-06-17 19:56:36 -05003664 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003665 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003666 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003667
James Smarte8b62012007-08-02 11:10:09 -04003668 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3669 "0904 NPort state transition x%06x, %s -> %s\n",
3670 ndlp->nlp_DID,
3671 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3672 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003673
3674 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3675 "node statechg did:x%x old:%d ste:%d",
3676 ndlp->nlp_DID, old_state, state);
3677
James Smartde0c5b32007-04-25 09:52:27 -04003678 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003679 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003680 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003681 if (old_state == NLP_STE_UNMAPPED_NODE) {
3682 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3683 ndlp->nlp_type &= ~NLP_FC_NODE;
3684 }
3685
James Smart685f0bf2007-04-25 09:53:08 -04003686 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003687 spin_lock_irq(shost->host_lock);
3688 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3689 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003690 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003691 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003692
3693 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003694 lpfc_nlp_counters(vport, state, 1);
3695 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003696}
3697
3698void
James Smarte47c9092008-02-08 18:49:26 -05003699lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3700{
3701 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3702
3703 if (list_empty(&ndlp->nlp_listp)) {
3704 spin_lock_irq(shost->host_lock);
3705 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3706 spin_unlock_irq(shost->host_lock);
3707 }
3708}
3709
3710void
James Smart2e0fef82007-06-17 19:56:36 -05003711lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003712{
James Smart2e0fef82007-06-17 19:56:36 -05003713 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3714
James Smart0d2b6b82008-06-14 22:52:47 -04003715 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003716 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003717 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3718 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003719 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003720 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003721 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003722 NLP_STE_UNUSED_NODE);
3723}
3724
Adrian Bunk4d9db012008-02-14 23:24:02 +02003725static void
James Smarte47c9092008-02-08 18:49:26 -05003726lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3727{
James Smart0d2b6b82008-06-14 22:52:47 -04003728 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003729 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3730 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3731 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3732 NLP_STE_UNUSED_NODE);
3733}
James Smart109f6ed2008-12-04 22:39:08 -05003734/**
James Smart3621a712009-04-06 18:47:14 -04003735 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003736 * @vport: Pointer to Virtual Port object.
3737 * @ndlp: Pointer to FC node object.
3738 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003739 *
3740 * This function is always called when node object need to be initialized.
3741 * It initializes all the fields of the node object. Although the reference
3742 * to phba from @ndlp can be obtained indirectly through it's reference to
3743 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3744 * to the life-span of the @ndlp might go beyond the existence of @vport as
3745 * the final release of ndlp is determined by its reference count. And, the
3746 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003747 **/
3748static inline void
3749lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3750 uint32_t did)
3751{
3752 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3753 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3754 init_timer(&ndlp->nlp_delayfunc);
3755 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3756 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3757 ndlp->nlp_DID = did;
3758 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003759 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003760 ndlp->nlp_sid = NLP_NO_SID;
3761 kref_init(&ndlp->kref);
3762 NLP_INT_NODE_ACT(ndlp);
3763 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04003764 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003765}
James Smarte47c9092008-02-08 18:49:26 -05003766
3767struct lpfc_nodelist *
3768lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3769 int state)
3770{
3771 struct lpfc_hba *phba = vport->phba;
3772 uint32_t did;
3773 unsigned long flags;
3774
3775 if (!ndlp)
3776 return NULL;
3777
3778 spin_lock_irqsave(&phba->ndlp_lock, flags);
3779 /* The ndlp should not be in memory free mode */
3780 if (NLP_CHK_FREE_REQ(ndlp)) {
3781 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3782 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3783 "0277 lpfc_enable_node: ndlp:x%p "
3784 "usgmap:x%x refcnt:%d\n",
3785 (void *)ndlp, ndlp->nlp_usg_map,
3786 atomic_read(&ndlp->kref.refcount));
3787 return NULL;
3788 }
3789 /* The ndlp should not already be in active mode */
3790 if (NLP_CHK_NODE_ACT(ndlp)) {
3791 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3792 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3793 "0278 lpfc_enable_node: ndlp:x%p "
3794 "usgmap:x%x refcnt:%d\n",
3795 (void *)ndlp, ndlp->nlp_usg_map,
3796 atomic_read(&ndlp->kref.refcount));
3797 return NULL;
3798 }
3799
3800 /* Keep the original DID */
3801 did = ndlp->nlp_DID;
3802
3803 /* re-initialize ndlp except of ndlp linked list pointer */
3804 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3805 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003806 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003807
3808 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3809
3810 if (state != NLP_STE_UNUSED_NODE)
3811 lpfc_nlp_set_state(vport, ndlp, state);
3812
3813 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3814 "node enable: did:x%x",
3815 ndlp->nlp_DID, 0, 0);
3816 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003817}
3818
3819void
James Smart2e0fef82007-06-17 19:56:36 -05003820lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003821{
James Smart87af33f2007-10-27 13:37:43 -04003822 /*
James Smartfa4066b2008-01-11 01:53:27 -05003823 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003824 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003825 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3826 * until ALL other outstanding threads have completed. We check
3827 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003828 */
James Smartfa4066b2008-01-11 01:53:27 -05003829 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3830 return;
James Smart51ef4c22007-08-02 11:10:31 -04003831 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003832 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003833 return;
dea31012005-04-17 16:05:31 -05003834}
3835
3836/*
3837 * Start / ReStart rescue timer for Discovery / RSCN handling
3838 */
3839void
James Smart2e0fef82007-06-17 19:56:36 -05003840lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003841{
James Smart2e0fef82007-06-17 19:56:36 -05003842 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3843 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003844 uint32_t tmo;
3845
James Smart2e0fef82007-06-17 19:56:36 -05003846 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003847 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003848 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3849 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003850 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003851 * FC spec states we need 3 * ratov for CT requests
3852 */
3853 tmo = ((phba->fc_ratov * 3) + 3);
3854 }
dea31012005-04-17 16:05:31 -05003855
James Smart858c9f62007-06-17 19:56:39 -05003856
3857 if (!timer_pending(&vport->fc_disctmo)) {
3858 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3859 "set disc timer: tmo:x%x state:x%x flg:x%x",
3860 tmo, vport->port_state, vport->fc_flag);
3861 }
3862
James Smart2e0fef82007-06-17 19:56:36 -05003863 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3864 spin_lock_irq(shost->host_lock);
3865 vport->fc_flag |= FC_DISC_TMO;
3866 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003867
3868 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003869 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3870 "0247 Start Discovery Timer state x%x "
3871 "Data: x%x x%lx x%x x%x\n",
3872 vport->port_state, tmo,
3873 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3874 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003875
3876 return;
3877}
3878
3879/*
3880 * Cancel rescue timer for Discovery / RSCN handling
3881 */
3882int
James Smart2e0fef82007-06-17 19:56:36 -05003883lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003884{
James Smart2e0fef82007-06-17 19:56:36 -05003885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003886 unsigned long iflags;
3887
James Smart858c9f62007-06-17 19:56:39 -05003888 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3889 "can disc timer: state:x%x rtry:x%x flg:x%x",
3890 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3891
dea31012005-04-17 16:05:31 -05003892 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003893 if (vport->fc_flag & FC_DISC_TMO) {
3894 spin_lock_irqsave(shost->host_lock, iflags);
3895 vport->fc_flag &= ~FC_DISC_TMO;
3896 spin_unlock_irqrestore(shost->host_lock, iflags);
3897 del_timer_sync(&vport->fc_disctmo);
3898 spin_lock_irqsave(&vport->work_port_lock, iflags);
3899 vport->work_port_events &= ~WORKER_DISC_TMO;
3900 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003901 }
3902
3903 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003904 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3905 "0248 Cancel Discovery Timer state x%x "
3906 "Data: x%x x%x x%x\n",
3907 vport->port_state, vport->fc_flag,
3908 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003909 return 0;
dea31012005-04-17 16:05:31 -05003910}
3911
3912/*
3913 * Check specified ring for outstanding IOCB on the SLI queue
3914 * Return true if iocb matches the specified nport
3915 */
3916int
James Smart2e0fef82007-06-17 19:56:36 -05003917lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3918 struct lpfc_sli_ring *pring,
3919 struct lpfc_iocbq *iocb,
3920 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003921{
James Smart2e0fef82007-06-17 19:56:36 -05003922 struct lpfc_sli *psli = &phba->sli;
3923 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003924 struct lpfc_vport *vport = ndlp->vport;
3925
3926 if (iocb->vport != vport)
3927 return 0;
3928
dea31012005-04-17 16:05:31 -05003929 if (pring->ringno == LPFC_ELS_RING) {
3930 switch (icmd->ulpCommand) {
3931 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003932 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003933 return 1;
dea31012005-04-17 16:05:31 -05003934 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003935 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3936 return 1;
dea31012005-04-17 16:05:31 -05003937 case CMD_XMIT_ELS_RSP64_CX:
3938 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003939 return 1;
dea31012005-04-17 16:05:31 -05003940 }
James Smarta4bc3372006-12-02 13:34:16 -05003941 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003942
3943 } else if (pring->ringno == psli->fcp_ring) {
3944 /* Skip match check if waiting to relogin to FCP target */
3945 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003946 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003947 return 0;
dea31012005-04-17 16:05:31 -05003948 }
3949 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003950 return 1;
dea31012005-04-17 16:05:31 -05003951 }
3952 } else if (pring->ringno == psli->next_ring) {
3953
3954 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003955 return 0;
dea31012005-04-17 16:05:31 -05003956}
3957
3958/*
3959 * Free resources / clean up outstanding I/Os
3960 * associated with nlp_rpi in the LPFC_NODELIST entry.
3961 */
3962static int
James Smart2e0fef82007-06-17 19:56:36 -05003963lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003964{
James Smart2534ba72007-04-25 09:52:20 -04003965 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003966 struct lpfc_sli *psli;
3967 struct lpfc_sli_ring *pring;
3968 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003969 uint32_t i;
dea31012005-04-17 16:05:31 -05003970
James Smart92d7f7b2007-06-17 19:56:38 -05003971 lpfc_fabric_abort_nport(ndlp);
3972
dea31012005-04-17 16:05:31 -05003973 /*
3974 * Everything that matches on txcmplq will be returned
3975 * by firmware with a no rpi error.
3976 */
3977 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003978 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003979 /* Now process each ring */
3980 for (i = 0; i < psli->num_rings; i++) {
3981 pring = &psli->ring[i];
3982
James Smart2e0fef82007-06-17 19:56:36 -05003983 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003984 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003985 list) {
dea31012005-04-17 16:05:31 -05003986 /*
3987 * Check to see if iocb matches the nport we are
3988 * looking for
3989 */
James Smart92d7f7b2007-06-17 19:56:38 -05003990 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3991 ndlp))) {
dea31012005-04-17 16:05:31 -05003992 /* It matches, so deque and call compl
3993 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003994 list_move_tail(&iocb->list,
3995 &completions);
dea31012005-04-17 16:05:31 -05003996 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003997 }
3998 }
James Smart2e0fef82007-06-17 19:56:36 -05003999 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004000 }
4001 }
James Smart2534ba72007-04-25 09:52:20 -04004002
James Smarta257bf92009-04-06 18:48:10 -04004003 /* Cancel all the IOCBs from the completions list */
4004 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4005 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004006
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004007 return 0;
dea31012005-04-17 16:05:31 -05004008}
4009
4010/*
4011 * Free rpi associated with LPFC_NODELIST entry.
4012 * This routine is called from lpfc_freenode(), when we are removing
4013 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4014 * LOGO that completes successfully, and we are waiting to PLOGI back
4015 * to the remote NPort. In addition, it is called after we receive
4016 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4017 * we are waiting to PLOGI back to the remote NPort.
4018 */
4019int
James Smart2e0fef82007-06-17 19:56:36 -05004020lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004021{
James Smart2e0fef82007-06-17 19:56:36 -05004022 struct lpfc_hba *phba = vport->phba;
4023 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004024 int rc;
4025
James Smart21e9a0a2009-05-22 14:53:21 -04004026 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05004027 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4028 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05004029 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004030 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004031 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004032 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004033 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004034 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004035 }
dea31012005-04-17 16:05:31 -05004036 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004037
dea31012005-04-17 16:05:31 -05004038 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04004039 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04004040 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004041 return 1;
4042 }
4043 return 0;
4044}
4045
James Smartecfd03c2010-02-12 14:41:27 -05004046/**
4047 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4048 * @phba: pointer to lpfc hba data structure.
4049 *
4050 * This routine is invoked to unregister all the currently registered RPIs
4051 * to the HBA.
4052 **/
4053void
4054lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4055{
4056 struct lpfc_vport **vports;
4057 struct lpfc_nodelist *ndlp;
4058 struct Scsi_Host *shost;
4059 int i;
4060
4061 vports = lpfc_create_vport_work_array(phba);
4062 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4063 shost = lpfc_shost_from_vport(vports[i]);
4064 spin_lock_irq(shost->host_lock);
4065 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004066 if (ndlp->nlp_flag & NLP_RPI_VALID) {
4067 /* The mempool_alloc might sleep */
4068 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004069 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004070 spin_lock_irq(shost->host_lock);
4071 }
James Smartecfd03c2010-02-12 14:41:27 -05004072 }
4073 spin_unlock_irq(shost->host_lock);
4074 }
4075 lpfc_destroy_vport_work_array(phba, vports);
4076}
4077
James Smart92d7f7b2007-06-17 19:56:38 -05004078void
4079lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4080{
4081 struct lpfc_hba *phba = vport->phba;
4082 LPFC_MBOXQ_t *mbox;
4083 int rc;
4084
James Smart5af5eee2010-10-22 11:06:38 -04004085 if (phba->sli_rev == LPFC_SLI_REV4) {
4086 lpfc_sli4_unreg_all_rpis(vport);
4087 return;
4088 }
4089
James Smart92d7f7b2007-06-17 19:56:38 -05004090 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4091 if (mbox) {
4092 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
4093 mbox->vport = vport;
4094 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004095 mbox->context1 = NULL;
4096 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004097 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004098 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004099
4100 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4101 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4102 "1836 Could not issue "
4103 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004104 }
4105}
4106
4107void
4108lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4109{
4110 struct lpfc_hba *phba = vport->phba;
4111 LPFC_MBOXQ_t *mbox;
4112 int rc;
4113
4114 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4115 if (mbox) {
4116 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
4117 mbox->vport = vport;
4118 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004119 mbox->context1 = NULL;
4120 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004121 if (rc != MBX_TIMEOUT)
4122 mempool_free(mbox, phba->mbox_mem_pool);
4123
4124 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004125 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4126 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004127 "unreg_did (default rpis) status %d\n",
4128 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004129 }
4130}
4131
dea31012005-04-17 16:05:31 -05004132/*
4133 * Free resources associated with LPFC_NODELIST entry
4134 * so it can be freed.
4135 */
4136static int
James Smart2e0fef82007-06-17 19:56:36 -05004137lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004138{
James Smart2e0fef82007-06-17 19:56:36 -05004139 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4140 struct lpfc_hba *phba = vport->phba;
4141 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004142 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004143
4144 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004145 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4146 "0900 Cleanup node for NPort x%x "
4147 "Data: x%x x%x x%x\n",
4148 ndlp->nlp_DID, ndlp->nlp_flag,
4149 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004150 if (NLP_CHK_FREE_REQ(ndlp)) {
4151 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4152 "0280 lpfc_cleanup_node: ndlp:x%p "
4153 "usgmap:x%x refcnt:%d\n",
4154 (void *)ndlp, ndlp->nlp_usg_map,
4155 atomic_read(&ndlp->kref.refcount));
4156 lpfc_dequeue_node(vport, ndlp);
4157 } else {
4158 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4159 "0281 lpfc_cleanup_node: ndlp:x%p "
4160 "usgmap:x%x refcnt:%d\n",
4161 (void *)ndlp, ndlp->nlp_usg_map,
4162 atomic_read(&ndlp->kref.refcount));
4163 lpfc_disable_node(vport, ndlp);
4164 }
dea31012005-04-17 16:05:31 -05004165
dea31012005-04-17 16:05:31 -05004166 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4167 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004168 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05004169 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4170 mb->context2 = NULL;
4171 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4172 }
4173 }
James Smart33ccf8d2006-08-17 11:57:58 -04004174
James Smart2e0fef82007-06-17 19:56:36 -05004175 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004176 /* Cleanup REG_LOGIN completions which are not yet processed */
4177 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4178 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
4179 (ndlp != (struct lpfc_nodelist *) mb->context2))
4180 continue;
4181
4182 mb->context2 = NULL;
4183 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4184 }
4185
dea31012005-04-17 16:05:31 -05004186 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004187 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004188 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004189 mp = (struct lpfc_dmabuf *) (mb->context1);
4190 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004191 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004192 kfree(mp);
4193 }
4194 list_del(&mb->list);
James Smartffc95492010-06-07 15:23:17 -04004195 if (phba->sli_rev == LPFC_SLI_REV4)
4196 lpfc_sli4_free_rpi(phba,
4197 mb->u.mb.un.varRegLogin.rpi);
dea31012005-04-17 16:05:31 -05004198 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004199 /* We shall not invoke the lpfc_nlp_put to decrement
4200 * the ndlp reference count as we are in the process
4201 * of lpfc_nlp_release.
4202 */
dea31012005-04-17 16:05:31 -05004203 }
4204 }
James Smart2e0fef82007-06-17 19:56:36 -05004205 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004206
James Smarte47c9092008-02-08 18:49:26 -05004207 lpfc_els_abort(phba, ndlp);
4208
James Smart2e0fef82007-06-17 19:56:36 -05004209 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004210 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004211 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004212
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004213 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004214 del_timer_sync(&ndlp->nlp_delayfunc);
4215
James Smart0d2b6b82008-06-14 22:52:47 -04004216 list_del_init(&ndlp->els_retry_evt.evt_listp);
4217 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05004218
James Smart2e0fef82007-06-17 19:56:36 -05004219 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004220
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004221 return 0;
dea31012005-04-17 16:05:31 -05004222}
4223
4224/*
4225 * Check to see if we can free the nlp back to the freelist.
4226 * If we are in the middle of using the nlp in the discovery state
4227 * machine, defer the free till we reach the end of the state machine.
4228 */
James Smart329f9bc2007-04-25 09:53:01 -04004229static void
James Smart2e0fef82007-06-17 19:56:36 -05004230lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004231{
James Smarta8adb832007-10-27 13:37:53 -04004232 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004233 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004234 LPFC_MBOXQ_t *mbox;
4235 int rc;
dea31012005-04-17 16:05:31 -05004236
James Smart0d2b6b82008-06-14 22:52:47 -04004237 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004238 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smartffc95492010-06-07 15:23:17 -04004239 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
James Smart21e9a0a2009-05-22 14:53:21 -04004240 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04004241 /* For this case we need to cleanup the default rpi
4242 * allocated by the firmware.
4243 */
4244 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4245 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004246 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04004247 (uint8_t *) &vport->fc_sparam, mbox, 0);
4248 if (rc) {
4249 mempool_free(mbox, phba->mbox_mem_pool);
4250 }
4251 else {
4252 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4253 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4254 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05004255 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04004256 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4257 if (rc == MBX_NOT_FINISHED) {
4258 mempool_free(mbox, phba->mbox_mem_pool);
4259 }
4260 }
4261 }
4262 }
James Smart2e0fef82007-06-17 19:56:36 -05004263 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004264
James Smart2e0fef82007-06-17 19:56:36 -05004265 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004266 * We can get here with a non-NULL ndlp->rport because when we
4267 * unregister a rport we don't break the rport/node linkage. So if we
4268 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004269 */
James Smart92d7f7b2007-06-17 19:56:38 -05004270 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004271 rdata = ndlp->rport->dd_data;
4272 rdata->pnode = NULL;
4273 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004274 }
dea31012005-04-17 16:05:31 -05004275}
4276
4277static int
James Smart2e0fef82007-06-17 19:56:36 -05004278lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4279 uint32_t did)
dea31012005-04-17 16:05:31 -05004280{
James Smart2e0fef82007-06-17 19:56:36 -05004281 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004282
4283 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004284 return 0;
dea31012005-04-17 16:05:31 -05004285
dea31012005-04-17 16:05:31 -05004286 /* First check for Direct match */
4287 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004288 return 1;
dea31012005-04-17 16:05:31 -05004289
4290 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004291 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004292 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004293 return 0;
dea31012005-04-17 16:05:31 -05004294 }
4295
4296 matchdid.un.word = did;
4297 ndlpdid.un.word = ndlp->nlp_DID;
4298 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4299 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4300 (mydid.un.b.area == matchdid.un.b.area)) {
4301 if ((ndlpdid.un.b.domain == 0) &&
4302 (ndlpdid.un.b.area == 0)) {
4303 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004304 return 1;
dea31012005-04-17 16:05:31 -05004305 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004306 return 0;
dea31012005-04-17 16:05:31 -05004307 }
4308
4309 matchdid.un.word = ndlp->nlp_DID;
4310 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4311 (mydid.un.b.area == ndlpdid.un.b.area)) {
4312 if ((matchdid.un.b.domain == 0) &&
4313 (matchdid.un.b.area == 0)) {
4314 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004315 return 1;
dea31012005-04-17 16:05:31 -05004316 }
4317 }
4318 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004319 return 0;
dea31012005-04-17 16:05:31 -05004320}
4321
James Smart685f0bf2007-04-25 09:53:08 -04004322/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004323static struct lpfc_nodelist *
4324__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004325{
James Smart2fb9bd82006-12-02 13:33:57 -05004326 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004327 uint32_t data1;
4328
James Smart2e0fef82007-06-17 19:56:36 -05004329 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4330 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004331 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4332 ((uint32_t) ndlp->nlp_xri << 16) |
4333 ((uint32_t) ndlp->nlp_type << 8) |
4334 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004335 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4336 "0929 FIND node DID "
4337 "Data: x%p x%x x%x x%x\n",
4338 ndlp, ndlp->nlp_DID,
4339 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004340 return ndlp;
dea31012005-04-17 16:05:31 -05004341 }
4342 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004343
dea31012005-04-17 16:05:31 -05004344 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004345 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4346 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004347 return NULL;
4348}
4349
4350struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004351lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004352{
James Smart2e0fef82007-06-17 19:56:36 -05004353 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004354 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004355
James Smart2e0fef82007-06-17 19:56:36 -05004356 spin_lock_irq(shost->host_lock);
4357 ndlp = __lpfc_findnode_did(vport, did);
4358 spin_unlock_irq(shost->host_lock);
4359 return ndlp;
4360}
4361
4362struct lpfc_nodelist *
4363lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4364{
4365 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4366 struct lpfc_nodelist *ndlp;
4367
4368 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004369 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004370 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4371 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004372 return NULL;
4373 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004374 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004375 if (!ndlp)
4376 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004377 lpfc_nlp_init(vport, ndlp, did);
4378 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4379 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004380 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004381 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004382 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004383 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4384 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4385 if (!ndlp)
4386 return NULL;
4387 spin_lock_irq(shost->host_lock);
4388 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4389 spin_unlock_irq(shost->host_lock);
4390 return ndlp;
dea31012005-04-17 16:05:31 -05004391 }
James Smarte47c9092008-02-08 18:49:26 -05004392
James Smart58da1ff2008-04-07 10:15:56 -04004393 if ((vport->fc_flag & FC_RSCN_MODE) &&
4394 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004395 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004396 /* If we've already recieved a PLOGI from this NPort
4397 * we don't need to try to discover it again.
4398 */
4399 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4400 return NULL;
4401
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004402 /* Since this node is marked for discovery,
4403 * delay timeout is not needed.
4404 */
James Smart0d2b6b82008-06-14 22:52:47 -04004405 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004406 spin_lock_irq(shost->host_lock);
4407 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4408 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004409 } else
dea31012005-04-17 16:05:31 -05004410 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004411 } else {
James Smart87af33f2007-10-27 13:37:43 -04004412 /* If we've already recieved a PLOGI from this NPort,
4413 * or we are already in the process of discovery on it,
4414 * we don't need to try to discover it again.
4415 */
James Smart685f0bf2007-04-25 09:53:08 -04004416 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004417 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4418 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004419 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004420 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4421 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004422 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004423 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004424 }
4425 return ndlp;
4426}
4427
4428/* Build a list of nodes to discover based on the loopmap */
4429void
James Smart2e0fef82007-06-17 19:56:36 -05004430lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004431{
James Smart2e0fef82007-06-17 19:56:36 -05004432 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004433 int j;
4434 uint32_t alpa, index;
4435
James Smart2e0fef82007-06-17 19:56:36 -05004436 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004437 return;
James Smart2e0fef82007-06-17 19:56:36 -05004438
4439 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004440 return;
dea31012005-04-17 16:05:31 -05004441
4442 /* Check for loop map present or not */
4443 if (phba->alpa_map[0]) {
4444 for (j = 1; j <= phba->alpa_map[0]; j++) {
4445 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004446 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004447 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004448 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004449 }
4450 } else {
4451 /* No alpamap, so try all alpa's */
4452 for (j = 0; j < FC_MAXLOOP; j++) {
4453 /* If cfg_scan_down is set, start from highest
4454 * ALPA (0xef) to lowest (0x1).
4455 */
James Smart3de2a652007-08-02 11:09:59 -04004456 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004457 index = j;
4458 else
4459 index = FC_MAXLOOP - j - 1;
4460 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004461 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004462 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004463 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004464 }
4465 }
4466 return;
4467}
4468
dea31012005-04-17 16:05:31 -05004469void
James Smart2e0fef82007-06-17 19:56:36 -05004470lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004471{
dea31012005-04-17 16:05:31 -05004472 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004473 struct lpfc_sli *psli = &phba->sli;
4474 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4475 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4476 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4477 int rc;
4478
James Smart92d7f7b2007-06-17 19:56:38 -05004479 /*
4480 * if it's not a physical port or if we already send
4481 * clear_la then don't send it.
4482 */
4483 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004484 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4485 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004486 return;
4487
James Smart2e0fef82007-06-17 19:56:36 -05004488 /* Link up discovery */
4489 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4490 phba->link_state = LPFC_CLEAR_LA;
4491 lpfc_clear_la(phba, mbox);
4492 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4493 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004494 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004495 if (rc == MBX_NOT_FINISHED) {
4496 mempool_free(mbox, phba->mbox_mem_pool);
4497 lpfc_disc_flush_list(vport);
4498 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4499 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4500 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004501 phba->link_state = LPFC_HBA_ERROR;
4502 }
4503 }
4504}
4505
4506/* Reg_vpi to tell firmware to resume normal operations */
4507void
4508lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4509{
4510 LPFC_MBOXQ_t *regvpimbox;
4511
4512 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4513 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004514 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004515 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4516 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004517 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004518 == MBX_NOT_FINISHED) {
4519 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004520 }
4521 }
4522}
4523
4524/* Start Link up / RSCN discovery on NPR nodes */
4525void
4526lpfc_disc_start(struct lpfc_vport *vport)
4527{
4528 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4529 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004530 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004531 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004532 int did_changed;
dea31012005-04-17 16:05:31 -05004533
James Smart2e0fef82007-06-17 19:56:36 -05004534 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004535 return;
James Smart2e0fef82007-06-17 19:56:36 -05004536
4537 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004538 clear_la_pending = 1;
4539 else
4540 clear_la_pending = 0;
4541
James Smart2e0fef82007-06-17 19:56:36 -05004542 if (vport->port_state < LPFC_VPORT_READY)
4543 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004544
James Smart2e0fef82007-06-17 19:56:36 -05004545 lpfc_set_disctmo(vport);
4546
4547 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004548 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004549 else
dea31012005-04-17 16:05:31 -05004550 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004551
4552 vport->fc_prevDID = vport->fc_myDID;
4553 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004554
4555 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004556 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4557 "0202 Start Discovery hba state x%x "
4558 "Data: x%x x%x x%x\n",
4559 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4560 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004561
4562 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004563 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004564
4565 if (num_sent)
4566 return;
4567
James Smart92d7f7b2007-06-17 19:56:38 -05004568 /*
4569 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4570 * continue discovery.
4571 */
4572 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004573 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004574 !(vport->fc_flag & FC_RSCN_MODE) &&
4575 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004576 lpfc_issue_reg_vpi(phba, vport);
4577 return;
4578 }
James Smart2e0fef82007-06-17 19:56:36 -05004579
James Smart92d7f7b2007-06-17 19:56:38 -05004580 /*
4581 * For SLI2, we need to set port_state to READY and continue
4582 * discovery.
4583 */
4584 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4585 /* If we get here, there is nothing to ADISC */
4586 if (vport->port_type == LPFC_PHYSICAL_PORT)
4587 lpfc_issue_clear_la(phba, vport);
4588
4589 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004590 vport->num_disc_nodes = 0;
4591 /* go thru NPR nodes and issue ELS PLOGIs */
4592 if (vport->fc_npr_cnt)
4593 lpfc_els_disc_plogi(vport);
4594
4595 if (!vport->num_disc_nodes) {
4596 spin_lock_irq(shost->host_lock);
4597 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4598 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004599 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004600 }
4601 }
James Smart92d7f7b2007-06-17 19:56:38 -05004602 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004603 } else {
4604 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004605 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004606
4607 if (num_sent)
4608 return;
4609
James Smart2e0fef82007-06-17 19:56:36 -05004610 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004611 /* Check to see if more RSCNs came in while we
4612 * were processing this one.
4613 */
James Smart2e0fef82007-06-17 19:56:36 -05004614 if ((vport->fc_rscn_id_cnt == 0) &&
4615 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4616 spin_lock_irq(shost->host_lock);
4617 vport->fc_flag &= ~FC_RSCN_MODE;
4618 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004619 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004620 } else
James Smart2e0fef82007-06-17 19:56:36 -05004621 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004622 }
4623 }
4624 return;
4625}
4626
4627/*
4628 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4629 * ring the match the sppecified nodelist.
4630 */
4631static void
James Smart2e0fef82007-06-17 19:56:36 -05004632lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004633{
James Smart2534ba72007-04-25 09:52:20 -04004634 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004635 struct lpfc_sli *psli;
4636 IOCB_t *icmd;
4637 struct lpfc_iocbq *iocb, *next_iocb;
4638 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004639
4640 psli = &phba->sli;
4641 pring = &psli->ring[LPFC_ELS_RING];
4642
4643 /* Error matching iocb on txq or txcmplq
4644 * First check the txq.
4645 */
James Smart2e0fef82007-06-17 19:56:36 -05004646 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004647 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4648 if (iocb->context1 != ndlp) {
4649 continue;
4650 }
4651 icmd = &iocb->iocb;
4652 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4653 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4654
James Smart2534ba72007-04-25 09:52:20 -04004655 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004656 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004657 }
4658 }
4659
4660 /* Next check the txcmplq */
4661 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4662 if (iocb->context1 != ndlp) {
4663 continue;
4664 }
4665 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004666 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4667 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004668 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004669 }
4670 }
James Smart2e0fef82007-06-17 19:56:36 -05004671 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004672
James Smarta257bf92009-04-06 18:48:10 -04004673 /* Cancel all the IOCBs from the completions list */
4674 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4675 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004676}
4677
Adrian Bunka6ababd2007-11-05 18:07:33 +01004678static void
James Smart2e0fef82007-06-17 19:56:36 -05004679lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004680{
4681 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004682 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004683
James Smart2e0fef82007-06-17 19:56:36 -05004684 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4685 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004686 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004687 if (!NLP_CHK_NODE_ACT(ndlp))
4688 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004689 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4690 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4691 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004692 }
dea31012005-04-17 16:05:31 -05004693 }
4694 }
dea31012005-04-17 16:05:31 -05004695}
4696
James Smart92d7f7b2007-06-17 19:56:38 -05004697void
4698lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4699{
4700 lpfc_els_flush_rscn(vport);
4701 lpfc_els_flush_cmd(vport);
4702 lpfc_disc_flush_list(vport);
4703}
4704
dea31012005-04-17 16:05:31 -05004705/*****************************************************************************/
4706/*
4707 * NAME: lpfc_disc_timeout
4708 *
4709 * FUNCTION: Fibre Channel driver discovery timeout routine.
4710 *
4711 * EXECUTION ENVIRONMENT: interrupt only
4712 *
4713 * CALLED FROM:
4714 * Timer function
4715 *
4716 * RETURNS:
4717 * none
4718 */
4719/*****************************************************************************/
4720void
4721lpfc_disc_timeout(unsigned long ptr)
4722{
James Smart2e0fef82007-06-17 19:56:36 -05004723 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4724 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004725 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004726 unsigned long flags = 0;
4727
4728 if (unlikely(!phba))
4729 return;
4730
James Smart5e9d9b82008-06-14 22:52:53 -04004731 spin_lock_irqsave(&vport->work_port_lock, flags);
4732 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4733 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004734 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004735 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004736
James Smart5e9d9b82008-06-14 22:52:53 -04004737 if (!tmo_posted)
4738 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004739 return;
4740}
4741
4742static void
James Smart2e0fef82007-06-17 19:56:36 -05004743lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004744{
James Smart2e0fef82007-06-17 19:56:36 -05004745 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4746 struct lpfc_hba *phba = vport->phba;
4747 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004748 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004749 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004750 int rc, clrlaerr = 0;
4751
James Smart2e0fef82007-06-17 19:56:36 -05004752 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004753 return;
4754
James Smart2e0fef82007-06-17 19:56:36 -05004755 spin_lock_irq(shost->host_lock);
4756 vport->fc_flag &= ~FC_DISC_TMO;
4757 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004758
James Smart858c9f62007-06-17 19:56:39 -05004759 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4760 "disc timeout: state:x%x rtry:x%x flg:x%x",
4761 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4762
James Smart2e0fef82007-06-17 19:56:36 -05004763 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004764
4765 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004766 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4767 * FAN
4768 */
4769 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004770 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4771 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004772 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004773 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004774 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004775 if (!NLP_CHK_NODE_ACT(ndlp))
4776 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004777 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4778 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004779 if (ndlp->nlp_type & NLP_FABRIC) {
4780 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004781 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004782
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004783 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004784 /* Fail outstanding IO now since device
4785 * is marked for PLOGI.
4786 */
James Smart2e0fef82007-06-17 19:56:36 -05004787 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004788 }
4789 }
James Smart92d7f7b2007-06-17 19:56:38 -05004790 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004791 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004792 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004793 }
dea31012005-04-17 16:05:31 -05004794 break;
4795
James Smart92d7f7b2007-06-17 19:56:38 -05004796 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004797 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004798 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004799 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004800 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4801 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004802 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004803
4804 /* Assume no Fabric and go on with discovery.
4805 * Check for outstanding ELS FLOGI to abort.
4806 */
4807
4808 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004809 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004810
4811 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004812 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004813 break;
4814
4815 case LPFC_FABRIC_CFG_LINK:
4816 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4817 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004818 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4819 "0223 Timeout while waiting for "
4820 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004821 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004822 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004823 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004824 lpfc_els_abort(phba, ndlp);
4825
4826 /* ReStart discovery */
4827 goto restart_disc;
dea31012005-04-17 16:05:31 -05004828
4829 case LPFC_NS_QRY:
4830 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004831 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4832 "0224 NameServer Query timeout "
4833 "Data: x%x x%x\n",
4834 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004835
James Smart92d7f7b2007-06-17 19:56:38 -05004836 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4837 /* Try it one more time */
4838 vport->fc_ns_retry++;
4839 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4840 vport->fc_ns_retry, 0);
4841 if (rc == 0)
4842 break;
dea31012005-04-17 16:05:31 -05004843 }
James Smart92d7f7b2007-06-17 19:56:38 -05004844 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004845
James Smart87af33f2007-10-27 13:37:43 -04004846restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004847 /*
4848 * Discovery is over.
4849 * set port_state to PORT_READY if SLI2.
4850 * cmpl_reg_vpi will set port_state to READY for SLI3.
4851 */
James Smart3772a992009-05-22 14:50:54 -04004852 if (phba->sli_rev < LPFC_SLI_REV4) {
4853 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4854 lpfc_issue_reg_vpi(phba, vport);
4855 else { /* NPIV Not enabled */
4856 lpfc_issue_clear_la(phba, vport);
4857 vport->port_state = LPFC_VPORT_READY;
4858 }
dea31012005-04-17 16:05:31 -05004859 }
4860
4861 /* Setup and issue mailbox INITIALIZE LINK command */
4862 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4863 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004864 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4865 "0206 Device Discovery "
4866 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004867 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004868 break;
4869 }
4870
4871 lpfc_linkdown(phba);
4872 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4873 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004874 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004875 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004876 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004877 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004878 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004879 if (rc == MBX_NOT_FINISHED)
4880 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4881
4882 break;
4883
4884 case LPFC_DISC_AUTH:
4885 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004886 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4887 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004888 lpfc_disc_flush_list(vport);
4889
James Smart92d7f7b2007-06-17 19:56:38 -05004890 /*
4891 * set port_state to PORT_READY if SLI2.
4892 * cmpl_reg_vpi will set port_state to READY for SLI3.
4893 */
James Smart3772a992009-05-22 14:50:54 -04004894 if (phba->sli_rev < LPFC_SLI_REV4) {
4895 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4896 lpfc_issue_reg_vpi(phba, vport);
4897 else { /* NPIV Not enabled */
4898 lpfc_issue_clear_la(phba, vport);
4899 vport->port_state = LPFC_VPORT_READY;
4900 }
dea31012005-04-17 16:05:31 -05004901 }
4902 break;
4903
James Smart2e0fef82007-06-17 19:56:36 -05004904 case LPFC_VPORT_READY:
4905 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004906 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4907 "0231 RSCN timeout Data: x%x "
4908 "x%x\n",
4909 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004910
4911 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004912 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004913
James Smart2e0fef82007-06-17 19:56:36 -05004914 lpfc_els_flush_rscn(vport);
4915 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004916 }
4917 break;
James Smart2e0fef82007-06-17 19:56:36 -05004918
James Smart92d7f7b2007-06-17 19:56:38 -05004919 default:
James Smarte8b62012007-08-02 11:10:09 -04004920 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004921 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004922 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004923 break;
4924 }
4925
4926 switch (phba->link_state) {
4927 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004928 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004929 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4930 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004931 clrlaerr = 1;
4932 break;
4933
James Smart09372822008-01-11 01:52:54 -05004934 case LPFC_LINK_UP:
4935 lpfc_issue_clear_la(phba, vport);
4936 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004937 case LPFC_LINK_UNKNOWN:
4938 case LPFC_WARM_START:
4939 case LPFC_INIT_START:
4940 case LPFC_INIT_MBX_CMDS:
4941 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004942 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004943 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4944 "0230 Unexpected timeout, hba link "
4945 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004946 clrlaerr = 1;
4947 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004948
4949 case LPFC_HBA_READY:
4950 break;
dea31012005-04-17 16:05:31 -05004951 }
4952
4953 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004954 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004955 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004956 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4957 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004958 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004959 }
4960
4961 return;
4962}
4963
dea31012005-04-17 16:05:31 -05004964/*
4965 * This routine handles processing a NameServer REG_LOGIN mailbox
4966 * command upon completion. It is setup in the LPFC_MBOXQ
4967 * as the completion routine when the command is
4968 * handed off to the SLI layer.
4969 */
4970void
James Smart2e0fef82007-06-17 19:56:36 -05004971lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004972{
James Smart04c68492009-05-22 14:52:52 -04004973 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004974 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4975 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4976 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004977
4978 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04004979 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05004980
dea31012005-04-17 16:05:31 -05004981 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004982 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004983 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004984 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004985
James Smart2e0fef82007-06-17 19:56:36 -05004986 /*
4987 * Start issuing Fabric-Device Management Interface (FDMI) command to
4988 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4989 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004990 */
James Smart2e0fef82007-06-17 19:56:36 -05004991
James Smart3de2a652007-08-02 11:09:59 -04004992 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004993 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4994 else
4995 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004996
James Smartfa4066b2008-01-11 01:53:27 -05004997 /* decrement the node reference count held for this callback
4998 * function.
4999 */
James Smart329f9bc2007-04-25 09:53:01 -04005000 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005001 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5002 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005003 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005004
5005 return;
5006}
5007
James Smart685f0bf2007-04-25 09:53:08 -04005008static int
5009lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5010{
5011 uint16_t *rpi = param;
5012
5013 return ndlp->nlp_rpi == *rpi;
5014}
5015
5016static int
5017lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5018{
5019 return memcmp(&ndlp->nlp_portname, param,
5020 sizeof(ndlp->nlp_portname)) == 0;
5021}
5022
Adrian Bunka6ababd2007-11-05 18:07:33 +01005023static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005024__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005025{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005026 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005027
James Smart2e0fef82007-06-17 19:56:36 -05005028 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04005029 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04005030 return ndlp;
5031 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005032 return NULL;
dea31012005-04-17 16:05:31 -05005033}
5034
James Smart685f0bf2007-04-25 09:53:08 -04005035/*
5036 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005037 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005038 */
5039struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005040__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005041{
James Smart2e0fef82007-06-17 19:56:36 -05005042 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005043}
5044
James Smart488d1462006-03-07 15:02:37 -05005045/*
James Smart685f0bf2007-04-25 09:53:08 -04005046 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005047 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005048 */
5049struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005050lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005051{
James Smart2e0fef82007-06-17 19:56:36 -05005052 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005053 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005054
James Smart2e0fef82007-06-17 19:56:36 -05005055 spin_lock_irq(shost->host_lock);
5056 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5057 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005058 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005059}
5060
dea31012005-04-17 16:05:31 -05005061void
James Smart2e0fef82007-06-17 19:56:36 -05005062lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5063 uint32_t did)
dea31012005-04-17 16:05:31 -05005064{
5065 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005066
5067 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005068 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05005069
5070 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5071 "node init: did:x%x",
5072 ndlp->nlp_DID, 0, 0);
5073
dea31012005-04-17 16:05:31 -05005074 return;
5075}
James Smart329f9bc2007-04-25 09:53:01 -04005076
James Smart98c9ea52007-10-27 13:37:33 -04005077/* This routine releases all resources associated with a specifc NPort's ndlp
5078 * and mempool_free's the nodelist.
5079 */
James Smart311464e2007-08-02 11:10:37 -04005080static void
James Smart329f9bc2007-04-25 09:53:01 -04005081lpfc_nlp_release(struct kref *kref)
5082{
James Smarte47c9092008-02-08 18:49:26 -05005083 struct lpfc_hba *phba;
5084 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005085 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5086 kref);
James Smart858c9f62007-06-17 19:56:39 -05005087
5088 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5089 "node release: did:x%x flg:x%x type:x%x",
5090 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5091
James Smarte47c9092008-02-08 18:49:26 -05005092 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5093 "0279 lpfc_nlp_release: ndlp:x%p "
5094 "usgmap:x%x refcnt:%d\n",
5095 (void *)ndlp, ndlp->nlp_usg_map,
5096 atomic_read(&ndlp->kref.refcount));
5097
5098 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005099 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005100
5101 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005102 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005103 spin_lock_irqsave(&phba->ndlp_lock, flags);
5104 NLP_CLR_NODE_ACT(ndlp);
5105 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5106
5107 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005108 if (NLP_CHK_FREE_REQ(ndlp)) {
5109 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04005110 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005111 }
James Smart329f9bc2007-04-25 09:53:01 -04005112}
5113
James Smart98c9ea52007-10-27 13:37:33 -04005114/* This routine bumps the reference count for a ndlp structure to ensure
5115 * that one discovery thread won't free a ndlp while another discovery thread
5116 * is using it.
5117 */
James Smart329f9bc2007-04-25 09:53:01 -04005118struct lpfc_nodelist *
5119lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5120{
James Smarte47c9092008-02-08 18:49:26 -05005121 struct lpfc_hba *phba;
5122 unsigned long flags;
5123
James Smart98c9ea52007-10-27 13:37:33 -04005124 if (ndlp) {
5125 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5126 "node get: did:x%x flg:x%x refcnt:x%x",
5127 ndlp->nlp_DID, ndlp->nlp_flag,
5128 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005129 /* The check of ndlp usage to prevent incrementing the
5130 * ndlp reference count that is in the process of being
5131 * released.
5132 */
James Smarta257bf92009-04-06 18:48:10 -04005133 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005134 spin_lock_irqsave(&phba->ndlp_lock, flags);
5135 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5136 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5137 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5138 "0276 lpfc_nlp_get: ndlp:x%p "
5139 "usgmap:x%x refcnt:%d\n",
5140 (void *)ndlp, ndlp->nlp_usg_map,
5141 atomic_read(&ndlp->kref.refcount));
5142 return NULL;
5143 } else
5144 kref_get(&ndlp->kref);
5145 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005146 }
James Smart329f9bc2007-04-25 09:53:01 -04005147 return ndlp;
5148}
5149
James Smart98c9ea52007-10-27 13:37:33 -04005150/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005151 * count goes to 0, this indicates the the associated nodelist should be
5152 * freed. Returning 1 indicates the ndlp resource has been released; on the
5153 * other hand, returning 0 indicates the ndlp resource has not been released
5154 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005155 */
James Smart329f9bc2007-04-25 09:53:01 -04005156int
5157lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5158{
James Smarte47c9092008-02-08 18:49:26 -05005159 struct lpfc_hba *phba;
5160 unsigned long flags;
5161
5162 if (!ndlp)
5163 return 1;
5164
5165 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5166 "node put: did:x%x flg:x%x refcnt:x%x",
5167 ndlp->nlp_DID, ndlp->nlp_flag,
5168 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005169 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005170 spin_lock_irqsave(&phba->ndlp_lock, flags);
5171 /* Check the ndlp memory free acknowledge flag to avoid the
5172 * possible race condition that kref_put got invoked again
5173 * after previous one has done ndlp memory free.
5174 */
5175 if (NLP_CHK_FREE_ACK(ndlp)) {
5176 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5177 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5178 "0274 lpfc_nlp_put: ndlp:x%p "
5179 "usgmap:x%x refcnt:%d\n",
5180 (void *)ndlp, ndlp->nlp_usg_map,
5181 atomic_read(&ndlp->kref.refcount));
5182 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005183 }
James Smarte47c9092008-02-08 18:49:26 -05005184 /* Check the ndlp inactivate log flag to avoid the possible
5185 * race condition that kref_put got invoked again after ndlp
5186 * is already in inactivating state.
5187 */
5188 if (NLP_CHK_IACT_REQ(ndlp)) {
5189 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5190 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5191 "0275 lpfc_nlp_put: ndlp:x%p "
5192 "usgmap:x%x refcnt:%d\n",
5193 (void *)ndlp, ndlp->nlp_usg_map,
5194 atomic_read(&ndlp->kref.refcount));
5195 return 1;
5196 }
5197 /* For last put, mark the ndlp usage flags to make sure no
5198 * other kref_get and kref_put on the same ndlp shall get
5199 * in between the process when the final kref_put has been
5200 * invoked on this ndlp.
5201 */
5202 if (atomic_read(&ndlp->kref.refcount) == 1) {
5203 /* Indicate ndlp is put to inactive state. */
5204 NLP_SET_IACT_REQ(ndlp);
5205 /* Acknowledge ndlp memory free has been seen. */
5206 if (NLP_CHK_FREE_REQ(ndlp))
5207 NLP_SET_FREE_ACK(ndlp);
5208 }
5209 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5210 /* Note, the kref_put returns 1 when decrementing a reference
5211 * count that was 1, it invokes the release callback function,
5212 * but it still left the reference count as 1 (not actually
5213 * performs the last decrementation). Otherwise, it actually
5214 * decrements the reference count and returns 0.
5215 */
5216 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005217}
James Smart98c9ea52007-10-27 13:37:33 -04005218
5219/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005220 * by any other discovery thread. This routine returns 1 if the
5221 * ndlp has been freed. A return value of 0 indicates the ndlp is
5222 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005223 */
5224int
5225lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5226{
5227 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5228 "node not used: did:x%x flg:x%x refcnt:x%x",
5229 ndlp->nlp_DID, ndlp->nlp_flag,
5230 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005231 if (atomic_read(&ndlp->kref.refcount) == 1)
5232 if (lpfc_nlp_put(ndlp))
5233 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005234 return 0;
5235}
James Smart6fb120a2009-05-22 14:52:59 -04005236
5237/**
5238 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5239 * @phba: Pointer to hba context object.
5240 *
5241 * This function iterate through all FC nodes associated
5242 * will all vports to check if there is any node with
5243 * fc_rports associated with it. If there is an fc_rport
5244 * associated with the node, then the node is either in
5245 * discovered state or its devloss_timer is pending.
5246 */
5247static int
5248lpfc_fcf_inuse(struct lpfc_hba *phba)
5249{
5250 struct lpfc_vport **vports;
5251 int i, ret = 0;
5252 struct lpfc_nodelist *ndlp;
5253 struct Scsi_Host *shost;
5254
5255 vports = lpfc_create_vport_work_array(phba);
5256
5257 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5258 shost = lpfc_shost_from_vport(vports[i]);
5259 spin_lock_irq(shost->host_lock);
5260 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5261 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5262 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5263 ret = 1;
5264 spin_unlock_irq(shost->host_lock);
5265 goto out;
James Smart891478a2009-11-18 15:40:23 -05005266 } else {
5267 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
5268 "2624 RPI %x DID %x flg %x still "
5269 "logged in\n",
5270 ndlp->nlp_rpi, ndlp->nlp_DID,
5271 ndlp->nlp_flag);
5272 if (ndlp->nlp_flag & NLP_RPI_VALID)
5273 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04005274 }
5275 }
5276 spin_unlock_irq(shost->host_lock);
5277 }
5278out:
5279 lpfc_destroy_vport_work_array(phba, vports);
5280 return ret;
5281}
5282
5283/**
5284 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5285 * @phba: Pointer to hba context object.
5286 * @mboxq: Pointer to mailbox object.
5287 *
5288 * This function frees memory associated with the mailbox command.
5289 */
5290static void
5291lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5292{
5293 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005294 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005295
5296 if (mboxq->u.mb.mbxStatus) {
5297 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5298 "2555 UNREG_VFI mbxStatus error x%x "
5299 "HBA state x%x\n",
5300 mboxq->u.mb.mbxStatus, vport->port_state);
5301 }
James Smart38b92ef2010-08-04 16:11:39 -04005302 spin_lock_irq(shost->host_lock);
5303 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5304 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005305 mempool_free(mboxq, phba->mbox_mem_pool);
5306 return;
5307}
5308
5309/**
5310 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5311 * @phba: Pointer to hba context object.
5312 * @mboxq: Pointer to mailbox object.
5313 *
5314 * This function frees memory associated with the mailbox command.
5315 */
5316static void
5317lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5318{
5319 struct lpfc_vport *vport = mboxq->vport;
5320
5321 if (mboxq->u.mb.mbxStatus) {
5322 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5323 "2550 UNREG_FCFI mbxStatus error x%x "
5324 "HBA state x%x\n",
5325 mboxq->u.mb.mbxStatus, vport->port_state);
5326 }
5327 mempool_free(mboxq, phba->mbox_mem_pool);
5328 return;
5329}
5330
5331/**
James Smartecfd03c2010-02-12 14:41:27 -05005332 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005333 * @phba: Pointer to hba context object.
5334 *
James Smartecfd03c2010-02-12 14:41:27 -05005335 * This function prepare the HBA for unregistering the currently registered
5336 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5337 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005338 */
James Smartecfd03c2010-02-12 14:41:27 -05005339int
5340lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005341{
5342 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04005343 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005344 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005345 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005346 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005347
James Smartecfd03c2010-02-12 14:41:27 -05005348 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005349 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005350 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005351
James Smart4d9ab992009-10-02 15:16:39 -04005352 /* At this point, all discovery is aborted */
5353 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005354
5355 /* Unregister VPIs */
5356 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005357 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005358 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005359 /* Stop FLOGI/FDISC retries */
5360 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5361 if (ndlp)
5362 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005363 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04005364 if (phba->sli_rev == LPFC_SLI_REV4)
5365 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005366 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005367 shost = lpfc_shost_from_vport(vports[i]);
5368 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005369 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005370 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005371 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005372 }
5373 lpfc_destroy_vport_work_array(phba, vports);
5374
James Smart695a8142010-01-26 23:08:03 -05005375 /* Cleanup any outstanding ELS commands */
5376 lpfc_els_flush_all_cmd(phba);
5377
James Smart6fb120a2009-05-22 14:52:59 -04005378 /* Unregister VFI */
5379 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5380 if (!mbox) {
5381 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005382 "2556 UNREG_VFI mbox allocation failed"
5383 "HBA state x%x\n", phba->pport->port_state);
5384 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005385 }
5386
James Smart6669f9b2009-10-02 15:16:45 -04005387 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005388 mbox->vport = phba->pport;
5389 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5390
5391 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5392 if (rc == MBX_NOT_FINISHED) {
5393 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005394 "2557 UNREG_VFI issue mbox failed rc x%x "
5395 "HBA state x%x\n",
5396 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005397 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005398 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005399 }
5400
James Smart72100cc2010-02-12 14:43:01 -05005401 shost = lpfc_shost_from_vport(phba->pport);
5402 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005403 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005404 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005405
James Smartecfd03c2010-02-12 14:41:27 -05005406 return 0;
5407}
5408
5409/**
5410 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5411 * @phba: Pointer to hba context object.
5412 *
5413 * This function issues synchronous unregister FCF mailbox command to HBA to
5414 * unregister the currently registered FCF record. The driver does not reset
5415 * the driver FCF usage state flags.
5416 *
5417 * Return 0 if successfully issued, none-zero otherwise.
5418 */
5419int
5420lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5421{
5422 LPFC_MBOXQ_t *mbox;
5423 int rc;
5424
James Smart6fb120a2009-05-22 14:52:59 -04005425 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5426 if (!mbox) {
5427 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005428 "2551 UNREG_FCFI mbox allocation failed"
5429 "HBA state x%x\n", phba->pport->port_state);
5430 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005431 }
James Smart6fb120a2009-05-22 14:52:59 -04005432 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5433 mbox->vport = phba->pport;
5434 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5435 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5436
5437 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005438 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5439 "2552 Unregister FCFI command failed rc x%x "
5440 "HBA state x%x\n",
5441 rc, phba->pport->port_state);
5442 return -EINVAL;
5443 }
5444 return 0;
5445}
5446
5447/**
5448 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5449 * @phba: Pointer to hba context object.
5450 *
5451 * This function unregisters the currently reigstered FCF. This function
5452 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5453 */
5454void
5455lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5456{
5457 int rc;
5458
5459 /* Preparation for unregistering fcf */
5460 rc = lpfc_unregister_fcf_prep(phba);
5461 if (rc) {
5462 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5463 "2748 Failed to prepare for unregistering "
5464 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005465 return;
5466 }
5467
James Smartecfd03c2010-02-12 14:41:27 -05005468 /* Now, unregister FCF record and reset HBA FCF state */
5469 rc = lpfc_sli4_unregister_fcf(phba);
5470 if (rc)
5471 return;
5472 /* Reset HBA FCF states after successful unregister FCF */
5473 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005474 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005475
5476 /*
5477 * If driver is not unloading, check if there is any other
5478 * FCF record that can be used for discovery.
5479 */
5480 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005481 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005482 return;
5483
James Smart0c9ab6f2010-02-26 14:15:57 -05005484 /* This is considered as the initial FCF discovery scan */
5485 spin_lock_irq(&phba->hbalock);
5486 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5487 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005488
5489 /* Reset FCF roundrobin bmask for new discovery */
5490 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
5491
James Smart0c9ab6f2010-02-26 14:15:57 -05005492 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005493
James Smart0c9ab6f2010-02-26 14:15:57 -05005494 if (rc) {
5495 spin_lock_irq(&phba->hbalock);
5496 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5497 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005498 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005499 "2553 lpfc_unregister_unused_fcf failed "
5500 "to read FCF record HBA state x%x\n",
5501 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005502 }
James Smartecfd03c2010-02-12 14:41:27 -05005503}
5504
5505/**
5506 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5507 * @phba: Pointer to hba context object.
5508 *
5509 * This function just unregisters the currently reigstered FCF. It does not
5510 * try to find another FCF for discovery.
5511 */
5512void
5513lpfc_unregister_fcf(struct lpfc_hba *phba)
5514{
5515 int rc;
5516
5517 /* Preparation for unregistering fcf */
5518 rc = lpfc_unregister_fcf_prep(phba);
5519 if (rc) {
5520 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5521 "2749 Failed to prepare for unregistering "
5522 "HBA's FCF record: rc=%d\n", rc);
5523 return;
5524 }
5525
5526 /* Now, unregister FCF record and reset HBA FCF state */
5527 rc = lpfc_sli4_unregister_fcf(phba);
5528 if (rc)
5529 return;
5530 /* Set proper HBA FCF states after successful unregister FCF */
5531 spin_lock_irq(&phba->hbalock);
5532 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5533 spin_unlock_irq(&phba->hbalock);
5534}
5535
5536/**
5537 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5538 * @phba: Pointer to hba context object.
5539 *
5540 * This function check if there are any connected remote port for the FCF and
5541 * if all the devices are disconnected, this function unregister FCFI.
5542 * This function also tries to use another FCF for discovery.
5543 */
5544void
5545lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5546{
5547 /*
James Smart3804dc82010-07-14 15:31:37 -04005548 * If HBA is not running in FIP mode, if HBA does not support
5549 * FCoE, if FCF discovery is ongoing, or if FCF has not been
5550 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05005551 */
5552 spin_lock_irq(&phba->hbalock);
5553 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5554 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04005555 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04005556 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04005557 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05005558 spin_unlock_irq(&phba->hbalock);
5559 return;
5560 }
5561 spin_unlock_irq(&phba->hbalock);
5562
5563 if (lpfc_fcf_inuse(phba))
5564 return;
5565
5566 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005567}
5568
5569/**
5570 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5571 * @phba: Pointer to hba context object.
5572 * @buff: Buffer containing the FCF connection table as in the config
5573 * region.
5574 * This function create driver data structure for the FCF connection
5575 * record table read from config region 23.
5576 */
5577static void
5578lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5579 uint8_t *buff)
5580{
5581 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5582 struct lpfc_fcf_conn_hdr *conn_hdr;
5583 struct lpfc_fcf_conn_rec *conn_rec;
5584 uint32_t record_count;
5585 int i;
5586
5587 /* Free the current connect table */
5588 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005589 &phba->fcf_conn_rec_list, list) {
5590 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005591 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005592 }
James Smart6fb120a2009-05-22 14:52:59 -04005593
5594 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5595 record_count = conn_hdr->length * sizeof(uint32_t)/
5596 sizeof(struct lpfc_fcf_conn_rec);
5597
5598 conn_rec = (struct lpfc_fcf_conn_rec *)
5599 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5600
5601 for (i = 0; i < record_count; i++) {
5602 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5603 continue;
5604 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5605 GFP_KERNEL);
5606 if (!conn_entry) {
5607 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5608 "2566 Failed to allocate connection"
5609 " table entry\n");
5610 return;
5611 }
5612
5613 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5614 sizeof(struct lpfc_fcf_conn_rec));
5615 conn_entry->conn_rec.vlan_tag =
5616 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5617 conn_entry->conn_rec.flags =
5618 le16_to_cpu(conn_entry->conn_rec.flags);
5619 list_add_tail(&conn_entry->list,
5620 &phba->fcf_conn_rec_list);
5621 }
5622}
5623
5624/**
5625 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5626 * @phba: Pointer to hba context object.
5627 * @buff: Buffer containing the FCoE parameter data structure.
5628 *
5629 * This function update driver data structure with config
5630 * parameters read from config region 23.
5631 */
5632static void
5633lpfc_read_fcoe_param(struct lpfc_hba *phba,
5634 uint8_t *buff)
5635{
5636 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5637 struct lpfc_fcoe_params *fcoe_param;
5638
5639 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5640 buff;
5641 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005642 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005643
5644 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5645 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5646 return;
5647
James Smart6fb120a2009-05-22 14:52:59 -04005648 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5649 phba->valid_vlan = 1;
5650 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5651 0xFFF;
5652 }
5653
5654 phba->fc_map[0] = fcoe_param->fc_map[0];
5655 phba->fc_map[1] = fcoe_param->fc_map[1];
5656 phba->fc_map[2] = fcoe_param->fc_map[2];
5657 return;
5658}
5659
5660/**
5661 * lpfc_get_rec_conf23 - Get a record type in config region data.
5662 * @buff: Buffer containing config region 23 data.
5663 * @size: Size of the data buffer.
5664 * @rec_type: Record type to be searched.
5665 *
5666 * This function searches config region data to find the begining
5667 * of the record specified by record_type. If record found, this
5668 * function return pointer to the record else return NULL.
5669 */
5670static uint8_t *
5671lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5672{
5673 uint32_t offset = 0, rec_length;
5674
5675 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5676 (size < sizeof(uint32_t)))
5677 return NULL;
5678
5679 rec_length = buff[offset + 1];
5680
5681 /*
5682 * One TLV record has one word header and number of data words
5683 * specified in the rec_length field of the record header.
5684 */
5685 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5686 <= size) {
5687 if (buff[offset] == rec_type)
5688 return &buff[offset];
5689
5690 if (buff[offset] == LPFC_REGION23_LAST_REC)
5691 return NULL;
5692
5693 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5694 rec_length = buff[offset + 1];
5695 }
5696 return NULL;
5697}
5698
5699/**
5700 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5701 * @phba: Pointer to lpfc_hba data structure.
5702 * @buff: Buffer containing config region 23 data.
5703 * @size: Size of the data buffer.
5704 *
Stefan Weileef35c22010-08-06 21:11:15 +02005705 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04005706 * populate driver data structure with the parameters.
5707 */
5708void
5709lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5710 uint8_t *buff,
5711 uint32_t size)
5712{
5713 uint32_t offset = 0, rec_length;
5714 uint8_t *rec_ptr;
5715
5716 /*
5717 * If data size is less than 2 words signature and version cannot be
5718 * verified.
5719 */
5720 if (size < 2*sizeof(uint32_t))
5721 return;
5722
5723 /* Check the region signature first */
5724 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5726 "2567 Config region 23 has bad signature\n");
5727 return;
5728 }
5729
5730 offset += 4;
5731
5732 /* Check the data structure version */
5733 if (buff[offset] != LPFC_REGION23_VERSION) {
5734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5735 "2568 Config region 23 has bad version\n");
5736 return;
5737 }
5738 offset += 4;
5739
5740 rec_length = buff[offset + 1];
5741
5742 /* Read FCoE param record */
5743 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5744 size - offset, FCOE_PARAM_TYPE);
5745 if (rec_ptr)
5746 lpfc_read_fcoe_param(phba, rec_ptr);
5747
5748 /* Read FCF connection table */
5749 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5750 size - offset, FCOE_CONN_TBL_TYPE);
5751 if (rec_ptr)
5752 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5753
5754}