blob: 05c9398a723dfb964f3a538d5fec838e324aa55b [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) {
797 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500798 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500799 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500800 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500801 }
802}
803
James Smart87af33f2007-10-27 13:37:43 -0400804void
James Smart98c9ea52007-10-27 13:37:33 -0400805lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500806{
James Smart695a8142010-01-26 23:08:03 -0500807 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
808
James Smart45ed1192009-10-02 15:17:02 -0400809 /* Cleanup any outstanding received buffers */
810 lpfc_cleanup_rcv_buffers(vport);
811
James Smart92d7f7b2007-06-17 19:56:38 -0500812 /* Cleanup any outstanding RSCN activity */
813 lpfc_els_flush_rscn(vport);
814
815 /* Cleanup any outstanding ELS commands */
816 lpfc_els_flush_cmd(vport);
817
818 lpfc_cleanup_rpis(vport, 0);
819
James Smart92d7f7b2007-06-17 19:56:38 -0500820 /* Turn off discovery timer if its running */
821 lpfc_can_disctmo(vport);
822}
823
James Smart3772a992009-05-22 14:50:54 -0400824void
James Smart98c9ea52007-10-27 13:37:33 -0400825lpfc_linkdown_port(struct lpfc_vport *vport)
826{
827 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
828
829 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
830
831 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
832 "Link Down: state:x%x rtry:x%x flg:x%x",
833 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
834
835 lpfc_port_link_failure(vport);
836
837}
838
dea31012005-04-17 16:05:31 -0500839int
James Smart685f0bf2007-04-25 09:53:08 -0400840lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500841{
James Smart2e0fef82007-06-17 19:56:36 -0500842 struct lpfc_vport *vport = phba->pport;
843 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400844 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400845 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400846 int i;
dea31012005-04-17 16:05:31 -0500847
James Smart3163f722008-02-08 18:50:25 -0500848 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500849 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500850
851 /* Block all SCSI stack I/Os */
852 lpfc_scsi_dev_block(phba);
853
James Smart2e0fef82007-06-17 19:56:36 -0500854 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500855 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500856 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500857 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500858 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500859 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500860 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500861 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500862 }
James Smart549e55c2007-08-02 11:09:51 -0400863 vports = lpfc_create_vport_work_array(phba);
864 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400866 /* Issue a LINK DOWN event to all nodes */
867 lpfc_linkdown_port(vports[i]);
868 }
James Smart09372822008-01-11 01:52:54 -0500869 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500870 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500871 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
872 if (mb) {
James Smart92d7f7b2007-06-17 19:56:38 -0500873 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
James Smarted957682007-06-17 19:56:37 -0500874 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500875 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400876 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500877 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500878 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500879 }
880 }
881
dea31012005-04-17 16:05:31 -0500882 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500883 if (phba->pport->fc_flag & FC_PT2PT) {
884 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500885 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
886 if (mb) {
dea31012005-04-17 16:05:31 -0500887 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500888 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500889 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400890 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500891 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500892 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500893 }
894 }
James Smart2e0fef82007-06-17 19:56:36 -0500895 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500896 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500897 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500898 }
James Smart2e0fef82007-06-17 19:56:36 -0500899
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500900 return 0;
dea31012005-04-17 16:05:31 -0500901}
902
James Smart92d7f7b2007-06-17 19:56:38 -0500903static void
904lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500905{
James Smart92d7f7b2007-06-17 19:56:38 -0500906 struct lpfc_nodelist *ndlp;
907
908 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500909 if (!NLP_CHK_NODE_ACT(ndlp))
910 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500911 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
912 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500913 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500914 /* On Linkup its safe to clean up the ndlp
915 * from Fabric connections.
916 */
James Smart92d7f7b2007-06-17 19:56:38 -0500917 if (ndlp->nlp_DID != Fabric_DID)
918 lpfc_unreg_rpi(vport, ndlp);
919 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
920 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500921 /* Fail outstanding IO now since device is
922 * marked for PLOGI.
923 */
James Smart92d7f7b2007-06-17 19:56:38 -0500924 lpfc_unreg_rpi(vport, ndlp);
925 }
926 }
927}
928
929static void
930lpfc_linkup_port(struct lpfc_vport *vport)
931{
932 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500933 struct lpfc_hba *phba = vport->phba;
934
935 if ((vport->load_flag & FC_UNLOADING) != 0)
936 return;
937
James Smart858c9f62007-06-17 19:56:39 -0500938 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
939 "Link Up: top:x%x speed:x%x flg:x%x",
940 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
941
James Smart92d7f7b2007-06-17 19:56:38 -0500942 /* If NPIV is not enabled, only bring the physical port up */
943 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
944 (vport != phba->pport))
945 return;
dea31012005-04-17 16:05:31 -0500946
James Smart2e0fef82007-06-17 19:56:36 -0500947 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400948
James Smart2e0fef82007-06-17 19:56:36 -0500949 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500950 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
951 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
952 vport->fc_flag |= FC_NDISC_ACTIVE;
953 vport->fc_ns_retry = 0;
954 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500955
James Smart92d7f7b2007-06-17 19:56:38 -0500956 if (vport->fc_flag & FC_LBIT)
957 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500958
James Smart92d7f7b2007-06-17 19:56:38 -0500959}
960
961static int
962lpfc_linkup(struct lpfc_hba *phba)
963{
James Smart549e55c2007-08-02 11:09:51 -0400964 struct lpfc_vport **vports;
965 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500966
967 phba->link_state = LPFC_LINK_UP;
968
969 /* Unblock fabric iocbs if they are blocked */
970 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
971 del_timer_sync(&phba->fabric_block_timer);
972
James Smart549e55c2007-08-02 11:09:51 -0400973 vports = lpfc_create_vport_work_array(phba);
974 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400975 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -0400976 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -0500977 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -0400978 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
979 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -0500980 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -0500981
982 return 0;
983}
984
985/*
986 * This routine handles processing a CLEAR_LA mailbox
987 * command upon completion. It is setup in the LPFC_MBOXQ
988 * as the completion routine when the command is
989 * handed off to the SLI layer.
990 */
Adrian Bunka6ababd2007-11-05 18:07:33 +0100991static void
James Smart2e0fef82007-06-17 19:56:36 -0500992lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -0500993{
James Smart2e0fef82007-06-17 19:56:36 -0500994 struct lpfc_vport *vport = pmb->vport;
995 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
996 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -0400997 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -0500998 uint32_t control;
999
dea31012005-04-17 16:05:31 -05001000 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001001 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001002 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1003 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1004
1005 /* Check for error */
1006 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001007 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001008 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1009 "0320 CLEAR_LA mbxStatus error x%x hba "
1010 "state x%x\n",
1011 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001012 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001013 goto out;
1014 }
1015
James Smart92d7f7b2007-06-17 19:56:38 -05001016 if (vport->port_type == LPFC_PHYSICAL_PORT)
1017 phba->link_state = LPFC_HBA_READY;
1018
1019 spin_lock_irq(&phba->hbalock);
1020 psli->sli_flag |= LPFC_PROCESS_LA;
1021 control = readl(phba->HCregaddr);
1022 control |= HC_LAINT_ENA;
1023 writel(control, phba->HCregaddr);
1024 readl(phba->HCregaddr); /* flush */
1025 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001026 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001027 return;
dea31012005-04-17 16:05:31 -05001028
dea31012005-04-17 16:05:31 -05001029out:
1030 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001031 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1032 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001033 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001034
James Smart2e0fef82007-06-17 19:56:36 -05001035 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001036 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001037 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001038
James Smart2e0fef82007-06-17 19:56:36 -05001039 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001040
1041 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001042
1043 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001044 psli->sli_flag |= LPFC_PROCESS_LA;
1045 control = readl(phba->HCregaddr);
1046 control |= HC_LAINT_ENA;
1047 writel(control, phba->HCregaddr);
1048 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001049 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001050
1051 return;
1052}
1053
James Smart2e0fef82007-06-17 19:56:36 -05001054
dea31012005-04-17 16:05:31 -05001055static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001056lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001057{
James Smart2e0fef82007-06-17 19:56:36 -05001058 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001059
James Smart04c68492009-05-22 14:52:52 -04001060 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001061 goto out;
dea31012005-04-17 16:05:31 -05001062
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001063 mempool_free(pmb, phba->mbox_mem_pool);
1064
1065 if (phba->fc_topology == TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001066 vport->fc_flag & FC_PUBLIC_LOOP &&
1067 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001068 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001069 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001070 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1071 */
James Smart2e0fef82007-06-17 19:56:36 -05001072 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001073 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001074 }
dea31012005-04-17 16:05:31 -05001075
James Smart2e0fef82007-06-17 19:56:36 -05001076 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001077 * LPFC_FLOGI while waiting for FLOGI cmpl
1078 */
James Smart92d7f7b2007-06-17 19:56:38 -05001079 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05001080 lpfc_initial_flogi(vport);
1081 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001082 return;
dea31012005-04-17 16:05:31 -05001083
1084out:
James Smarte8b62012007-08-02 11:10:09 -04001085 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1086 "0306 CONFIG_LINK mbxStatus error x%x "
1087 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001088 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001089 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001090
1091 lpfc_linkdown(phba);
1092
James Smarte8b62012007-08-02 11:10:09 -04001093 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1094 "0200 CONFIG_LINK bad hba state x%x\n",
1095 vport->port_state);
dea31012005-04-17 16:05:31 -05001096
James Smart92d7f7b2007-06-17 19:56:38 -05001097 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001098 return;
1099}
1100
1101static void
James Smart6fb120a2009-05-22 14:52:59 -04001102lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1103{
1104 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001105
1106 if (mboxq->u.mb.mbxStatus) {
1107 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1108 "2017 REG_FCFI mbxStatus error x%x "
1109 "HBA state x%x\n",
1110 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001111 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001112 }
1113
1114 /* Start FCoE discovery by sending a FLOGI. */
1115 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1116 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001117 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001118 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001119 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001120
James Smart32b97932009-07-19 10:01:21 -04001121 /* If there is a pending FCoE event, restart FCF table scan. */
James Smarta93ff372010-10-22 11:06:08 -04001122 if (lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1123 goto fail_out;
1124
1125 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001126 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001127 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001128 phba->hba_flag &= ~FCF_TS_INPROG;
1129 if (vport->port_state != LPFC_FLOGI) {
1130 phba->hba_flag |= FCF_RR_INPROG;
1131 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001132 lpfc_initial_flogi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001133 goto out;
1134 }
1135 spin_unlock_irq(&phba->hbalock);
1136 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001137
James Smarta93ff372010-10-22 11:06:08 -04001138fail_out:
1139 spin_lock_irq(&phba->hbalock);
1140 phba->hba_flag &= ~FCF_RR_INPROG;
1141 spin_unlock_irq(&phba->hbalock);
1142out:
James Smart6fb120a2009-05-22 14:52:59 -04001143 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001144}
1145
1146/**
1147 * lpfc_fab_name_match - Check if the fcf fabric name match.
1148 * @fab_name: pointer to fabric name.
1149 * @new_fcf_record: pointer to fcf record.
1150 *
1151 * This routine compare the fcf record's fabric name with provided
1152 * fabric name. If the fabric name are identical this function
1153 * returns 1 else return 0.
1154 **/
1155static uint32_t
1156lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1157{
James Smartecfd03c2010-02-12 14:41:27 -05001158 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001159 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001160 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1161 return 0;
1162 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1163 return 0;
1164 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1165 return 0;
1166 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1167 return 0;
1168 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1169 return 0;
1170 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1171 return 0;
1172 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1173 return 0;
1174 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001175}
1176
1177/**
James Smart8fa38512009-07-19 10:01:03 -04001178 * lpfc_sw_name_match - Check if the fcf switch name match.
1179 * @fab_name: pointer to fabric name.
1180 * @new_fcf_record: pointer to fcf record.
1181 *
1182 * This routine compare the fcf record's switch name with provided
1183 * switch name. If the switch name are identical this function
1184 * returns 1 else return 0.
1185 **/
1186static uint32_t
1187lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1188{
James Smartecfd03c2010-02-12 14:41:27 -05001189 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001190 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001191 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1192 return 0;
1193 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1194 return 0;
1195 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1196 return 0;
1197 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1198 return 0;
1199 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1200 return 0;
1201 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1202 return 0;
1203 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1204 return 0;
1205 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001206}
1207
1208/**
James Smart6fb120a2009-05-22 14:52:59 -04001209 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001210 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001211 * @new_fcf_record: pointer to fcf record.
1212 *
1213 * This routine compare the fcf record's mac address with HBA's
1214 * FCF mac address. If the mac addresses are identical this function
1215 * returns 1 else return 0.
1216 **/
1217static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001218lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001219{
James Smartecfd03c2010-02-12 14:41:27 -05001220 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001221 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001222 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1223 return 0;
1224 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1225 return 0;
1226 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1227 return 0;
1228 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1229 return 0;
1230 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1231 return 0;
1232 return 1;
1233}
1234
1235static bool
1236lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1237{
1238 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001239}
1240
1241/**
1242 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001243 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001244 * @new_fcf_record: pointer to fcf record.
1245 *
1246 * This routine copies the FCF information from the FCF
1247 * record to lpfc_hba data structure.
1248 **/
1249static void
James Smartecfd03c2010-02-12 14:41:27 -05001250lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1251 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001252{
James Smartecfd03c2010-02-12 14:41:27 -05001253 /* Fabric name */
1254 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001255 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001256 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001257 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001258 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001259 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001260 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001261 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001262 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001263 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001264 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001265 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001266 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001267 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001268 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001269 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001270 /* Mac address */
1271 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1272 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1273 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1274 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1275 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1276 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1277 /* FCF record index */
1278 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1279 /* FCF record priority */
1280 fcf_rec->priority = new_fcf_record->fip_priority;
1281 /* Switch name */
1282 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001283 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001284 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001285 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001286 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001287 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001288 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001289 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001290 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001291 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001292 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001293 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001294 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001295 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001296 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001297 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001298}
1299
1300/**
James Smartecfd03c2010-02-12 14:41:27 -05001301 * lpfc_update_fcf_record - Update driver fcf record
1302 * @phba: pointer to lpfc hba data structure.
1303 * @fcf_rec: pointer to driver fcf record.
1304 * @new_fcf_record: pointer to hba fcf record.
1305 * @addr_mode: address mode to be set to the driver fcf record.
1306 * @vlan_id: vlan tag to be set to the driver fcf record.
1307 * @flag: flag bits to be set to the driver fcf record.
1308 *
1309 * This routine updates the driver FCF record from the new HBA FCF record
1310 * together with the address mode, vlan_id, and other informations. This
1311 * routine is called with the host lock held.
1312 **/
1313static void
1314__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1315 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1316 uint16_t vlan_id, uint32_t flag)
1317{
1318 /* Copy the fields from the HBA's FCF record */
1319 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1320 /* Update other fields of driver FCF record */
1321 fcf_rec->addr_mode = addr_mode;
1322 fcf_rec->vlan_id = vlan_id;
1323 fcf_rec->flag |= (flag | RECORD_VALID);
1324}
1325
1326/**
James Smart6fb120a2009-05-22 14:52:59 -04001327 * lpfc_register_fcf - Register the FCF with hba.
1328 * @phba: pointer to lpfc hba data structure.
1329 *
1330 * This routine issues a register fcfi mailbox command to register
1331 * the fcf with HBA.
1332 **/
1333static void
1334lpfc_register_fcf(struct lpfc_hba *phba)
1335{
1336 LPFC_MBOXQ_t *fcf_mbxq;
1337 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001338
James Smartd439d282010-09-29 11:18:45 -04001339 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001340 /* If the FCF is not availabe do nothing. */
1341 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001342 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001343 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001344 return;
1345 }
1346
1347 /* The FCF is already registered, start discovery */
1348 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001349 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001350 phba->hba_flag &= ~FCF_TS_INPROG;
1351 if (phba->pport->port_state != LPFC_FLOGI) {
1352 phba->hba_flag |= FCF_RR_INPROG;
1353 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001354 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001355 return;
1356 }
1357 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001358 return;
1359 }
James Smartd439d282010-09-29 11:18:45 -04001360 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001361
James Smarta93ff372010-10-22 11:06:08 -04001362 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001363 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001364 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001365 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001366 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001367 return;
James Smart4d9ab992009-10-02 15:16:39 -04001368 }
James Smart6fb120a2009-05-22 14:52:59 -04001369
1370 lpfc_reg_fcfi(phba, fcf_mbxq);
1371 fcf_mbxq->vport = phba->pport;
1372 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1373 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001374 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001375 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001376 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001377 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001378 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001379 }
James Smart6fb120a2009-05-22 14:52:59 -04001380
1381 return;
1382}
1383
1384/**
1385 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1386 * @phba: pointer to lpfc hba data structure.
1387 * @new_fcf_record: pointer to fcf record.
1388 * @boot_flag: Indicates if this record used by boot bios.
1389 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001390 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001391 *
1392 * This routine compare the fcf record with connect list obtained from the
1393 * config region to decide if this FCF can be used for SAN discovery. It returns
1394 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1395 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1396 * is used by boot bios and addr_mode will indicate the addressing mode to be
1397 * used for this FCF when the function returns.
1398 * If the FCF record need to be used with a particular vlan id, the vlan is
1399 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001400 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001401 **/
1402static int
1403lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1404 struct fcf_record *new_fcf_record,
1405 uint32_t *boot_flag, uint32_t *addr_mode,
1406 uint16_t *vlan_id)
1407{
1408 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001409 int i, j, fcf_vlan_id = 0;
1410
1411 /* Find the lowest VLAN id in the FCF record */
1412 for (i = 0; i < 512; i++) {
1413 if (new_fcf_record->vlan_bitmap[i]) {
1414 fcf_vlan_id = i * 8;
1415 j = 0;
1416 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1417 j++;
1418 fcf_vlan_id++;
1419 }
1420 break;
1421 }
1422 }
James Smart6fb120a2009-05-22 14:52:59 -04001423
James Smart0c287582009-06-10 17:22:56 -04001424 /* If FCF not available return 0 */
1425 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1426 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1427 return 0;
1428
James Smart45ed1192009-10-02 15:17:02 -04001429 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001430 *boot_flag = 0;
1431 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1432 new_fcf_record);
1433 if (phba->valid_vlan)
1434 *vlan_id = phba->vlan_id;
1435 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001436 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001437 return 1;
1438 }
1439
1440 /*
1441 * If there are no FCF connection table entry, driver connect to all
1442 * FCFs.
1443 */
1444 if (list_empty(&phba->fcf_conn_rec_list)) {
1445 *boot_flag = 0;
1446 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1447 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001448
1449 /*
1450 * When there are no FCF connect entries, use driver's default
1451 * addressing mode - FPMA.
1452 */
1453 if (*addr_mode & LPFC_FCF_FPMA)
1454 *addr_mode = LPFC_FCF_FPMA;
1455
James Smart4d9ab992009-10-02 15:16:39 -04001456 /* If FCF record report a vlan id use that vlan id */
1457 if (fcf_vlan_id)
1458 *vlan_id = fcf_vlan_id;
1459 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001460 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001461 return 1;
1462 }
1463
James Smartecfd03c2010-02-12 14:41:27 -05001464 list_for_each_entry(conn_entry,
1465 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001466 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1467 continue;
1468
1469 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1470 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001471 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001472 continue;
James Smart8fa38512009-07-19 10:01:03 -04001473 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1474 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1475 new_fcf_record))
1476 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001477 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1478 /*
1479 * If the vlan bit map does not have the bit set for the
1480 * vlan id to be used, then it is not a match.
1481 */
1482 if (!(new_fcf_record->vlan_bitmap
1483 [conn_entry->conn_rec.vlan_tag / 8] &
1484 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1485 continue;
1486 }
1487
1488 /*
James Smart0c287582009-06-10 17:22:56 -04001489 * If connection record does not support any addressing mode,
1490 * skip the FCF record.
1491 */
1492 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1493 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1494 continue;
1495
1496 /*
James Smart6fb120a2009-05-22 14:52:59 -04001497 * Check if the connection record specifies a required
1498 * addressing mode.
1499 */
1500 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1501 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1502
1503 /*
1504 * If SPMA required but FCF not support this continue.
1505 */
1506 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1507 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1508 new_fcf_record) & LPFC_FCF_SPMA))
1509 continue;
1510
1511 /*
1512 * If FPMA required but FCF not support this continue.
1513 */
1514 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1515 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1516 new_fcf_record) & LPFC_FCF_FPMA))
1517 continue;
1518 }
1519
1520 /*
1521 * This fcf record matches filtering criteria.
1522 */
1523 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1524 *boot_flag = 1;
1525 else
1526 *boot_flag = 0;
1527
James Smart0c287582009-06-10 17:22:56 -04001528 /*
1529 * If user did not specify any addressing mode, or if the
1530 * prefered addressing mode specified by user is not supported
1531 * by FCF, allow fabric to pick the addressing mode.
1532 */
James Smart6fb120a2009-05-22 14:52:59 -04001533 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1534 new_fcf_record);
1535 /*
1536 * If the user specified a required address mode, assign that
1537 * address mode
1538 */
1539 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1540 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1541 *addr_mode = (conn_entry->conn_rec.flags &
1542 FCFCNCT_AM_SPMA) ?
1543 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1544 /*
1545 * If the user specified a prefered address mode, use the
1546 * addr mode only if FCF support the addr_mode.
1547 */
1548 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1549 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1550 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1551 (*addr_mode & LPFC_FCF_SPMA))
1552 *addr_mode = LPFC_FCF_SPMA;
1553 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1554 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1555 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1556 (*addr_mode & LPFC_FCF_FPMA))
1557 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001558
James Smart4d9ab992009-10-02 15:16:39 -04001559 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001560 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1561 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001562 /*
1563 * If no vlan id is specified in connect list, use the vlan id
1564 * in the FCF record
1565 */
1566 else if (fcf_vlan_id)
1567 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001568 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001569 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001570
1571 return 1;
1572 }
1573
1574 return 0;
1575}
1576
1577/**
James Smart32b97932009-07-19 10:01:21 -04001578 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1579 * @phba: pointer to lpfc hba data structure.
1580 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1581 *
1582 * This function check if there is any fcoe event pending while driver
1583 * scan FCF entries. If there is any pending event, it will restart the
1584 * FCF saning and return 1 else return 0.
1585 */
1586int
1587lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1588{
James Smart32b97932009-07-19 10:01:21 -04001589 /*
1590 * If the Link is up and no FCoE events while in the
1591 * FCF discovery, no need to restart FCF discovery.
1592 */
1593 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001594 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001595 return 0;
1596
James Smart0c9ab6f2010-02-26 14:15:57 -05001597 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1598 "2768 Pending link or FCF event during current "
1599 "handling of the previous event: link_state:x%x, "
1600 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1601 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1602 phba->fcoe_eventtag);
1603
James Smart32b97932009-07-19 10:01:21 -04001604 spin_lock_irq(&phba->hbalock);
1605 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1606 spin_unlock_irq(&phba->hbalock);
1607
James Smart0c9ab6f2010-02-26 14:15:57 -05001608 if (phba->link_state >= LPFC_LINK_UP) {
1609 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1610 "2780 Restart FCF table scan due to "
1611 "pending FCF event:evt_tag_at_scan:x%x, "
1612 "evt_tag_current:x%x\n",
1613 phba->fcoe_eventtag_at_fcf_scan,
1614 phba->fcoe_eventtag);
1615 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1616 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001617 /*
James Smarta93ff372010-10-22 11:06:08 -04001618 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001619 * flag
1620 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001621 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1622 "2833 Stop FCF discovery process due to link "
1623 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001624 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001625 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001626 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001627 spin_unlock_irq(&phba->hbalock);
1628 }
James Smart32b97932009-07-19 10:01:21 -04001629
James Smart0c9ab6f2010-02-26 14:15:57 -05001630 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001631 if (unreg_fcf) {
1632 spin_lock_irq(&phba->hbalock);
1633 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1634 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001635 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001636 }
James Smart32b97932009-07-19 10:01:21 -04001637 return 1;
1638}
1639
1640/**
James Smart999d8132010-03-15 11:24:56 -04001641 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1642 * @phba: pointer to lpfc hba data structure.
1643 * @fcf_cnt: number of eligible fcf record seen so far.
1644 *
1645 * This function makes an running random selection decision on FCF record to
1646 * use through a sequence of @fcf_cnt eligible FCF records with equal
1647 * probability. To perform integer manunipulation of random numbers with
1648 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1649 * from random32() are taken as the random random number generated.
1650 *
1651 * Returns true when outcome is for the newly read FCF record should be
1652 * chosen; otherwise, return false when outcome is for keeping the previously
1653 * chosen FCF record.
1654 **/
1655static bool
1656lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1657{
1658 uint32_t rand_num;
1659
1660 /* Get 16-bit uniform random number */
1661 rand_num = (0xFFFF & random32());
1662
1663 /* Decision with probability 1/fcf_cnt */
1664 if ((fcf_cnt * rand_num) < 0xFFFF)
1665 return true;
1666 else
1667 return false;
1668}
1669
1670/**
James Smart3804dc82010-07-14 15:31:37 -04001671 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001672 * @phba: pointer to lpfc hba data structure.
1673 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001674 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001675 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001676 * This routine parses the non-embedded fcf mailbox command by performing the
1677 * necessarily error checking, non-embedded read FCF record mailbox command
1678 * SGE parsing, and endianness swapping.
1679 *
1680 * Returns the pointer to the new FCF record in the non-embedded mailbox
1681 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001682 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001683static struct fcf_record *
1684lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1685 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001686{
1687 void *virt_addr;
1688 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001689 struct lpfc_mbx_sge sge;
1690 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1691 uint32_t shdr_status, shdr_add_status;
1692 union lpfc_sli4_cfg_shdr *shdr;
1693 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001694
James Smart6fb120a2009-05-22 14:52:59 -04001695 /* Get the first SGE entry from the non-embedded DMA memory. This
1696 * routine only uses a single SGE.
1697 */
1698 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1699 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1700 if (unlikely(!mboxq->sge_array)) {
1701 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1702 "2524 Failed to get the non-embedded SGE "
1703 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001704 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001705 }
1706 virt_addr = mboxq->sge_array->addr[0];
1707
1708 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1709 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001710 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001711 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001712 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1713 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001714 "2726 READ_FCF_RECORD Indicates empty "
1715 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001716 else
1717 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001718 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001719 "with status x%x add_status x%x, "
1720 "mbx\n", shdr_status, shdr_add_status);
1721 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001722 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001723
1724 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001725 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1726 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1727 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001728 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001729 new_fcf_record = (struct fcf_record *)(virt_addr +
1730 sizeof(struct lpfc_mbx_read_fcf_tbl));
1731 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001732 offsetof(struct fcf_record, vlan_bitmap));
1733 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1734 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001735
James Smart0c9ab6f2010-02-26 14:15:57 -05001736 return new_fcf_record;
1737}
1738
1739/**
1740 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1741 * @phba: pointer to lpfc hba data structure.
1742 * @fcf_record: pointer to the fcf record.
1743 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1744 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1745 *
1746 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1747 * enabled.
1748 **/
1749static void
1750lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1751 struct fcf_record *fcf_record,
1752 uint16_t vlan_id,
1753 uint16_t next_fcf_index)
1754{
1755 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1756 "2764 READ_FCF_RECORD:\n"
1757 "\tFCF_Index : x%x\n"
1758 "\tFCF_Avail : x%x\n"
1759 "\tFCF_Valid : x%x\n"
1760 "\tFIP_Priority : x%x\n"
1761 "\tMAC_Provider : x%x\n"
1762 "\tLowest VLANID : x%x\n"
1763 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1764 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1765 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1766 "\tNext_FCF_Index: x%x\n",
1767 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1768 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1769 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1770 fcf_record->fip_priority,
1771 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1772 vlan_id,
1773 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1774 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1775 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1776 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1777 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1778 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1779 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1780 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1781 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1782 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1783 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1784 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1785 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1786 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1787 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1788 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1789 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1790 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1791 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1792 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1793 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1794 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1795 next_fcf_index);
1796}
1797
1798/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001799 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1800 * @phba: pointer to lpfc hba data structure.
1801 * @fcf_rec: pointer to an existing FCF record.
1802 * @new_fcf_record: pointer to a new FCF record.
1803 * @new_vlan_id: vlan id from the new FCF record.
1804 *
1805 * This function performs matching test of a new FCF record against an existing
1806 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1807 * will not be used as part of the FCF record matching criteria.
1808 *
1809 * Returns true if all the fields matching, otherwise returns false.
1810 */
1811static bool
1812lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1813 struct lpfc_fcf_rec *fcf_rec,
1814 struct fcf_record *new_fcf_record,
1815 uint16_t new_vlan_id)
1816{
1817 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1818 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1819 return false;
1820 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1821 return false;
1822 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1823 return false;
1824 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1825 return false;
1826 return true;
1827}
1828
1829/**
James Smarta93ff372010-10-22 11:06:08 -04001830 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1831 * @vport: Pointer to vport object.
1832 * @fcf_index: index to next fcf.
1833 *
1834 * This function processing the roundrobin fcf failover to next fcf index.
1835 * When this function is invoked, there will be a current fcf registered
1836 * for flogi.
1837 * Return: 0 for continue retrying flogi on currently registered fcf;
1838 * 1 for stop flogi on currently registered fcf;
1839 */
1840int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1841{
1842 struct lpfc_hba *phba = vport->phba;
1843 int rc;
1844
1845 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1846 spin_lock_irq(&phba->hbalock);
1847 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1848 spin_unlock_irq(&phba->hbalock);
1849 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1850 "2872 Devloss tmo with no eligible "
1851 "FCF, unregister in-use FCF (x%x) "
1852 "and rescan FCF table\n",
1853 phba->fcf.current_rec.fcf_indx);
1854 lpfc_unregister_fcf_rescan(phba);
1855 goto stop_flogi_current_fcf;
1856 }
1857 /* Mark the end to FLOGI roundrobin failover */
1858 phba->hba_flag &= ~FCF_RR_INPROG;
1859 /* Allow action to new fcf asynchronous event */
1860 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1861 spin_unlock_irq(&phba->hbalock);
1862 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1863 "2865 No FCF available, stop roundrobin FCF "
1864 "failover and change port state:x%x/x%x\n",
1865 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1866 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1867 goto stop_flogi_current_fcf;
1868 } else {
1869 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1870 "2794 Try FLOGI roundrobin FCF failover to "
1871 "(x%x)\n", fcf_index);
1872 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1873 if (rc)
1874 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1875 "2761 FLOGI roundrobin FCF failover "
1876 "failed (rc:x%x) to read FCF (x%x)\n",
1877 rc, phba->fcf.current_rec.fcf_indx);
1878 else
1879 goto stop_flogi_current_fcf;
1880 }
1881 return 0;
1882
1883stop_flogi_current_fcf:
1884 lpfc_can_disctmo(vport);
1885 return 1;
1886}
1887
1888/**
James Smart0c9ab6f2010-02-26 14:15:57 -05001889 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
1890 * @phba: pointer to lpfc hba data structure.
1891 * @mboxq: pointer to mailbox object.
1892 *
1893 * This function iterates through all the fcf records available in
1894 * HBA and chooses the optimal FCF record for discovery. After finding
1895 * the FCF for discovery it registers the FCF record and kicks start
1896 * discovery.
1897 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
1898 * use an FCF record which matches fabric name and mac address of the
1899 * currently used FCF record.
1900 * If the driver supports only one FCF, it will try to use the FCF record
1901 * used by BOOT_BIOS.
1902 */
1903void
1904lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1905{
1906 struct fcf_record *new_fcf_record;
1907 uint32_t boot_flag, addr_mode;
1908 uint16_t fcf_index, next_fcf_index;
1909 struct lpfc_fcf_rec *fcf_rec = NULL;
1910 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04001911 uint32_t seed;
1912 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001913 int rc;
1914
1915 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04001916 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001917 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1918 return;
1919 }
1920
1921 /* Parse the FCF record from the non-embedded mailbox command */
1922 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
1923 &next_fcf_index);
1924 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04001925 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05001926 "2765 Mailbox command READ_FCF_RECORD "
1927 "failed to retrieve a FCF record.\n");
1928 /* Let next new FCF event trigger fast failover */
1929 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001930 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05001931 spin_unlock_irq(&phba->hbalock);
1932 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1933 return;
1934 }
1935
1936 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05001937 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
1938 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05001939
1940 /* Log the FCF record information if turned on */
1941 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
1942 next_fcf_index);
1943
James Smart6fb120a2009-05-22 14:52:59 -04001944 /*
1945 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05001946 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04001947 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04001948 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001949 if (!rc) {
1950 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04001951 "2781 FCF (x%x) failed connection "
1952 "list check: (x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05001953 bf_get(lpfc_fcf_record_fcf_index,
1954 new_fcf_record),
1955 bf_get(lpfc_fcf_record_fcf_avail,
1956 new_fcf_record),
1957 bf_get(lpfc_fcf_record_fcf_valid,
1958 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04001959 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
1960 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
1961 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04001962 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
1963 phba->fcf.current_rec.fcf_indx) {
1964 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
1965 "2862 FCF (x%x) matches property "
1966 "of in-use FCF (x%x)\n",
1967 bf_get(lpfc_fcf_record_fcf_index,
1968 new_fcf_record),
1969 phba->fcf.current_rec.fcf_indx);
1970 goto read_next_fcf;
1971 }
James Smartdbb6b3a2010-06-08 18:31:37 -04001972 /*
1973 * In case the current in-use FCF record becomes
1974 * invalid/unavailable during FCF discovery that
1975 * was not triggered by fast FCF failover process,
1976 * treat it as fast FCF failover.
1977 */
1978 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
1979 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
1980 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
1981 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04001982 "(x%x), enter FCF failover "
1983 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04001984 phba->fcf.current_rec.fcf_indx);
1985 spin_lock_irq(&phba->hbalock);
1986 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
1987 spin_unlock_irq(&phba->hbalock);
1988 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1989 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
1990 LPFC_FCOE_FCF_GET_FIRST);
1991 return;
1992 }
1993 }
James Smart6fb120a2009-05-22 14:52:59 -04001994 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05001995 } else {
1996 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1997 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
1998 if (rc)
1999 goto read_next_fcf;
2000 }
2001
James Smart6fb120a2009-05-22 14:52:59 -04002002 /*
2003 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002004 * FCF record for the discovery. The condition that a rescan
2005 * matches the in-use FCF record: fabric name, switch name, mac
2006 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002007 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002008 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002009 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002010 if (lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2011 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002012 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2013 phba->fcf.current_rec.fcf_indx) {
2014 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2015 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2016 /* Stop FCF redisc wait timer */
2017 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2018 phba);
2019 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2020 /* Fast failover, mark completed */
2021 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2022 spin_unlock_irq(&phba->hbalock);
2023 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2024 "2836 New FCF matches in-use "
2025 "FCF (x%x)\n",
2026 phba->fcf.current_rec.fcf_indx);
2027 goto out;
2028 } else
2029 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2030 "2863 New FCF (x%x) matches "
2031 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002032 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002033 new_fcf_record),
2034 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002035 }
James Smartecfd03c2010-02-12 14:41:27 -05002036 /*
2037 * Read next FCF record from HBA searching for the matching
2038 * with in-use record only if not during the fast failover
2039 * period. In case of fast failover period, it shall try to
2040 * determine whether the FCF record just read should be the
2041 * next candidate.
2042 */
2043 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002044 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002045 goto read_next_fcf;
2046 }
James Smart6fb120a2009-05-22 14:52:59 -04002047 }
James Smartecfd03c2010-02-12 14:41:27 -05002048 /*
2049 * Update on failover FCF record only if it's in FCF fast-failover
2050 * period; otherwise, update on current FCF record.
2051 */
James Smartfc2b9892010-02-26 14:15:29 -05002052 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2053 fcf_rec = &phba->fcf.failover_rec;
2054 else
James Smartecfd03c2010-02-12 14:41:27 -05002055 fcf_rec = &phba->fcf.current_rec;
2056
James Smart6fb120a2009-05-22 14:52:59 -04002057 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2058 /*
James Smartecfd03c2010-02-12 14:41:27 -05002059 * If the driver FCF record does not have boot flag
2060 * set and new hba fcf record has boot flag set, use
2061 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002062 */
James Smartecfd03c2010-02-12 14:41:27 -05002063 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2064 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002065 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2066 "2837 Update current FCF record "
2067 "(x%x) with new FCF record (x%x)\n",
2068 fcf_rec->fcf_indx,
2069 bf_get(lpfc_fcf_record_fcf_index,
2070 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002071 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2072 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002073 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002074 goto read_next_fcf;
2075 }
2076 /*
James Smartecfd03c2010-02-12 14:41:27 -05002077 * If the driver FCF record has boot flag set and the
2078 * new hba FCF record does not have boot flag, read
2079 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002080 */
James Smartecfd03c2010-02-12 14:41:27 -05002081 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002082 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002083 goto read_next_fcf;
2084 }
2085 /*
James Smartecfd03c2010-02-12 14:41:27 -05002086 * If the new hba FCF record has lower priority value
2087 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002088 */
James Smartfc2b9892010-02-26 14:15:29 -05002089 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002090 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002091 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2092 "2838 Update current FCF record "
2093 "(x%x) with new FCF record (x%x)\n",
2094 fcf_rec->fcf_indx,
2095 bf_get(lpfc_fcf_record_fcf_index,
2096 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002097 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2098 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002099 /* Reset running random FCF selection count */
2100 phba->fcf.eligible_fcf_cnt = 1;
2101 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2102 /* Update running random FCF selection count */
2103 phba->fcf.eligible_fcf_cnt++;
2104 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2105 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002106 if (select_new_fcf) {
2107 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2108 "2839 Update current FCF record "
2109 "(x%x) with new FCF record (x%x)\n",
2110 fcf_rec->fcf_indx,
2111 bf_get(lpfc_fcf_record_fcf_index,
2112 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002113 /* Choose the new FCF by random selection */
2114 __lpfc_update_fcf_record(phba, fcf_rec,
2115 new_fcf_record,
2116 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002117 }
James Smart6fb120a2009-05-22 14:52:59 -04002118 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002119 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002120 goto read_next_fcf;
2121 }
2122 /*
James Smartecfd03c2010-02-12 14:41:27 -05002123 * This is the first suitable FCF record, choose this record for
2124 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002125 */
James Smartecfd03c2010-02-12 14:41:27 -05002126 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002127 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002128 "2840 Update initial FCF candidate "
2129 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002130 bf_get(lpfc_fcf_record_fcf_index,
2131 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002132 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2133 addr_mode, vlan_id, (boot_flag ?
2134 BOOT_ENABLE : 0));
2135 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002136 /* Setup initial running random FCF selection count */
2137 phba->fcf.eligible_fcf_cnt = 1;
2138 /* Seeding the random number generator for random selection */
2139 seed = (uint32_t)(0xFFFFFFFF & jiffies);
2140 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04002141 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002142 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002143 goto read_next_fcf;
2144
2145read_next_fcf:
2146 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002147 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2148 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2149 /*
2150 * Case of FCF fast failover scan
2151 */
2152
2153 /*
2154 * It has not found any suitable FCF record, cancel
2155 * FCF scan inprogress, and do nothing
2156 */
2157 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002158 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002159 "2782 No suitable FCF found: "
2160 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002161 phba->fcoe_eventtag_at_fcf_scan,
2162 bf_get(lpfc_fcf_record_fcf_index,
2163 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002164 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002165 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2166 phba->hba_flag &= ~FCF_TS_INPROG;
2167 spin_unlock_irq(&phba->hbalock);
2168 /* Unregister in-use FCF and rescan */
2169 lpfc_printf_log(phba, KERN_INFO,
2170 LOG_FIP,
2171 "2864 On devloss tmo "
2172 "unreg in-use FCF and "
2173 "rescan FCF table\n");
2174 lpfc_unregister_fcf_rescan(phba);
2175 return;
2176 }
2177 /*
2178 * Let next new FCF event trigger fast failover
2179 */
2180 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002181 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002182 return;
2183 }
2184 /*
2185 * It has found a suitable FCF record that is not
2186 * the same as in-use FCF record, unregister the
2187 * in-use FCF record, replace the in-use FCF record
2188 * with the new FCF record, mark FCF fast failover
2189 * completed, and then start register the new FCF
2190 * record.
2191 */
2192
James Smart0c9ab6f2010-02-26 14:15:57 -05002193 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002194 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002195
2196 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002197 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002198 "2842 Replace in-use FCF (x%x) "
2199 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002200 phba->fcf.current_rec.fcf_indx,
2201 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002202 memcpy(&phba->fcf.current_rec,
2203 &phba->fcf.failover_rec,
2204 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002205 /*
2206 * Mark the fast FCF failover rediscovery completed
2207 * and the start of the first round of the roundrobin
2208 * FCF failover.
2209 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002210 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002211 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002213 /* Register to the new FCF record */
2214 lpfc_register_fcf(phba);
2215 } else {
2216 /*
2217 * In case of transaction period to fast FCF failover,
2218 * do nothing when search to the end of the FCF table.
2219 */
2220 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2221 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2222 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002223
2224 if (phba->fcf.fcf_flag & FCF_IN_USE) {
2225 /*
2226 * In case the current in-use FCF record no
2227 * longer existed during FCF discovery that
2228 * was not triggered by fast FCF failover
2229 * process, treat it as fast FCF failover.
2230 */
2231 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2232 "2841 In-use FCF record (x%x) "
2233 "not reported, entering fast "
2234 "FCF failover mode scanning.\n",
2235 phba->fcf.current_rec.fcf_indx);
2236 spin_lock_irq(&phba->hbalock);
2237 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2238 spin_unlock_irq(&phba->hbalock);
2239 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2240 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2241 LPFC_FCOE_FCF_GET_FIRST);
2242 return;
2243 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002244 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002245 lpfc_register_fcf(phba);
2246 }
2247 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002248 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002249 return;
2250
2251out:
2252 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2253 lpfc_register_fcf(phba);
2254
2255 return;
2256}
2257
2258/**
James Smarta93ff372010-10-22 11:06:08 -04002259 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002260 * @phba: pointer to lpfc hba data structure.
2261 * @mboxq: pointer to mailbox object.
2262 *
James Smarta93ff372010-10-22 11:06:08 -04002263 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002264 * read FCF record mailbox command from the eligible FCF record bmask for
2265 * performing the failover. If the FCF read back is not valid/available, it
2266 * fails through to retrying FLOGI to the currently registered FCF again.
2267 * Otherwise, if the FCF read back is valid and available, it will set the
2268 * newly read FCF record to the failover FCF record, unregister currently
2269 * registered FCF record, copy the failover FCF record to the current
2270 * FCF record, and then register the current FCF record before proceeding
2271 * to trying FLOGI on the new failover FCF.
2272 */
2273void
2274lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2275{
2276 struct fcf_record *new_fcf_record;
2277 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002278 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002279 uint16_t current_fcf_index;
2280 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002281 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002282
James Smarta93ff372010-10-22 11:06:08 -04002283 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002284 if (phba->link_state < LPFC_LINK_UP) {
2285 spin_lock_irq(&phba->hbalock);
2286 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002287 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002288 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002289 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002290 }
2291
2292 /* Parse the FCF record from the non-embedded mailbox command */
2293 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2294 &next_fcf_index);
2295 if (!new_fcf_record) {
2296 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2297 "2766 Mailbox command READ_FCF_RECORD "
2298 "failed to retrieve a FCF record.\n");
James Smarta93ff372010-10-22 11:06:08 -04002299 goto error_out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002300 }
2301
2302 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002303 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2304 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002305
2306 /* Log the FCF record information if turned on */
2307 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2308 next_fcf_index);
2309
James Smarta93ff372010-10-22 11:06:08 -04002310 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2311 if (!rc) {
2312 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2313 "2848 Remove ineligible FCF (x%x) from "
2314 "from roundrobin bmask\n", fcf_index);
2315 /* Clear roundrobin bmask bit for ineligible FCF */
2316 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2317 /* Perform next round of roundrobin FCF failover */
2318 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2319 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2320 if (rc)
2321 goto out;
2322 goto error_out;
2323 }
2324
2325 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2326 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2327 "2760 Perform FLOGI roundrobin FCF failover: "
2328 "FCF (x%x) back to FCF (x%x)\n",
2329 phba->fcf.current_rec.fcf_indx, fcf_index);
2330 /* Wait 500 ms before retrying FLOGI to current FCF */
2331 msleep(500);
2332 lpfc_initial_flogi(phba->pport);
2333 goto out;
2334 }
2335
James Smart0c9ab6f2010-02-26 14:15:57 -05002336 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002337 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002338 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2339 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002340 spin_lock_irq(&phba->hbalock);
2341 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2342 new_fcf_record, addr_mode, vlan_id,
2343 (boot_flag ? BOOT_ENABLE : 0));
2344 spin_unlock_irq(&phba->hbalock);
2345
2346 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2347
2348 /* Unregister the current in-use FCF record */
2349 lpfc_unregister_fcf(phba);
2350
2351 /* Replace in-use record with the new record */
2352 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2353 sizeof(struct lpfc_fcf_rec));
2354
2355 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002356 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2357 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002358
James Smarta93ff372010-10-22 11:06:08 -04002359error_out:
2360 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002361out:
2362 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002363}
2364
2365/**
2366 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2367 * @phba: pointer to lpfc hba data structure.
2368 * @mboxq: pointer to mailbox object.
2369 *
2370 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002371 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002372 * failover when a new FCF event happened. If the FCF read back is
2373 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002374 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002375 */
2376void
2377lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2378{
2379 struct fcf_record *new_fcf_record;
2380 uint32_t boot_flag, addr_mode;
2381 uint16_t fcf_index, next_fcf_index;
2382 uint16_t vlan_id;
2383 int rc;
2384
2385 /* If link state is not up, no need to proceed */
2386 if (phba->link_state < LPFC_LINK_UP)
2387 goto out;
2388
2389 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002390 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002391 goto out;
2392
2393 /* Parse the FCF record from the non-embedded mailbox command */
2394 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2395 &next_fcf_index);
2396 if (!new_fcf_record) {
2397 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2398 "2767 Mailbox command READ_FCF_RECORD "
2399 "failed to retrieve a FCF record.\n");
2400 goto out;
2401 }
2402
2403 /* Check the connection list for eligibility */
2404 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2405 &addr_mode, &vlan_id);
2406
2407 /* Log the FCF record information if turned on */
2408 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2409 next_fcf_index);
2410
2411 if (!rc)
2412 goto out;
2413
2414 /* Update the eligible FCF record index bmask */
2415 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2416 rc = lpfc_sli4_fcf_rr_index_set(phba, fcf_index);
2417
2418out:
2419 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2420}
2421
2422/**
James Smart1c6834a2009-07-19 10:01:26 -04002423 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2424 * @phba: pointer to lpfc hba data structure.
2425 * @mboxq: pointer to mailbox data structure.
2426 *
2427 * This function handles completion of init vpi mailbox command.
2428 */
James Smart695a8142010-01-26 23:08:03 -05002429void
James Smart1c6834a2009-07-19 10:01:26 -04002430lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2431{
2432 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002433 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002434 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2435
James Smart1c6834a2009-07-19 10:01:26 -04002436 if (mboxq->u.mb.mbxStatus) {
2437 lpfc_printf_vlog(vport, KERN_ERR,
2438 LOG_MBOX,
2439 "2609 Init VPI mailbox failed 0x%x\n",
2440 mboxq->u.mb.mbxStatus);
2441 mempool_free(mboxq, phba->mbox_mem_pool);
2442 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2443 return;
2444 }
James Smart72100cc2010-02-12 14:43:01 -05002445 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002446 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002447 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002448
James Smart695a8142010-01-26 23:08:03 -05002449 /* If this port is physical port or FDISC is done, do reg_vpi */
2450 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2451 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2452 if (!ndlp)
2453 lpfc_printf_vlog(vport, KERN_ERR,
2454 LOG_DISCOVERY,
2455 "2731 Cannot find fabric "
2456 "controller node\n");
2457 else
2458 lpfc_register_new_vport(phba, vport, ndlp);
2459 mempool_free(mboxq, phba->mbox_mem_pool);
2460 return;
2461 }
2462
James Smart1c6834a2009-07-19 10:01:26 -04002463 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2464 lpfc_initial_fdisc(vport);
2465 else {
2466 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002467 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2468 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002469 }
James Smart695a8142010-01-26 23:08:03 -05002470 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002471 return;
2472}
2473
2474/**
James Smartecfd03c2010-02-12 14:41:27 -05002475 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2476 * @vport: pointer to lpfc_vport data structure.
2477 *
2478 * This function issue a init_vpi mailbox command to initialize
2479 * VPI for the vport.
2480 */
2481void
2482lpfc_issue_init_vpi(struct lpfc_vport *vport)
2483{
2484 LPFC_MBOXQ_t *mboxq;
2485 int rc;
2486
2487 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2488 if (!mboxq) {
2489 lpfc_printf_vlog(vport, KERN_ERR,
2490 LOG_MBOX, "2607 Failed to allocate "
2491 "init_vpi mailbox\n");
2492 return;
2493 }
2494 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2495 mboxq->vport = vport;
2496 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2497 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2498 if (rc == MBX_NOT_FINISHED) {
2499 lpfc_printf_vlog(vport, KERN_ERR,
2500 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2501 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2502 }
2503}
2504
2505/**
James Smart6fb120a2009-05-22 14:52:59 -04002506 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2507 * @phba: pointer to lpfc hba data structure.
2508 *
2509 * This function loops through the list of vports on the @phba and issues an
2510 * FDISC if possible.
2511 */
2512void
2513lpfc_start_fdiscs(struct lpfc_hba *phba)
2514{
2515 struct lpfc_vport **vports;
2516 int i;
2517
2518 vports = lpfc_create_vport_work_array(phba);
2519 if (vports != NULL) {
2520 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2521 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2522 continue;
2523 /* There are no vpi for this vport */
2524 if (vports[i]->vpi > phba->max_vpi) {
2525 lpfc_vport_set_state(vports[i],
2526 FC_VPORT_FAILED);
2527 continue;
2528 }
2529 if (phba->fc_topology == TOPOLOGY_LOOP) {
2530 lpfc_vport_set_state(vports[i],
2531 FC_VPORT_LINKDOWN);
2532 continue;
2533 }
James Smart1c6834a2009-07-19 10:01:26 -04002534 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002535 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002536 continue;
2537 }
James Smart6fb120a2009-05-22 14:52:59 -04002538 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2539 lpfc_initial_fdisc(vports[i]);
2540 else {
2541 lpfc_vport_set_state(vports[i],
2542 FC_VPORT_NO_FABRIC_SUPP);
2543 lpfc_printf_vlog(vports[i], KERN_ERR,
2544 LOG_ELS,
2545 "0259 No NPIV "
2546 "Fabric support\n");
2547 }
2548 }
2549 }
2550 lpfc_destroy_vport_work_array(phba, vports);
2551}
2552
2553void
2554lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2555{
2556 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2557 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002558 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002559
2560 if (mboxq->u.mb.mbxStatus) {
2561 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2562 "2018 REG_VFI mbxStatus error x%x "
2563 "HBA state x%x\n",
2564 mboxq->u.mb.mbxStatus, vport->port_state);
2565 if (phba->fc_topology == TOPOLOGY_LOOP) {
2566 /* FLOGI failed, use loop map to make discovery list */
2567 lpfc_disc_list_loopmap(vport);
2568 /* Start discovery */
2569 lpfc_disc_start(vport);
2570 goto fail_free_mem;
2571 }
2572 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2573 goto fail_free_mem;
2574 }
James Smartc8685952009-11-18 15:39:16 -05002575 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002576 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002577 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002578 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002579 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002580 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002581
2582 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
2583 lpfc_start_fdiscs(phba);
2584 lpfc_do_scr_ns_plogi(phba, vport);
2585 }
2586
2587fail_free_mem:
2588 mempool_free(mboxq, phba->mbox_mem_pool);
2589 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2590 kfree(dmabuf);
2591 return;
2592}
2593
2594static void
James Smart2e0fef82007-06-17 19:56:36 -05002595lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002596{
James Smart6fb120a2009-05-22 14:52:59 -04002597 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002598 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002599 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002600
2601
2602 /* Check for error */
2603 if (mb->mbxStatus) {
2604 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002605 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2606 "0319 READ_SPARAM mbxStatus error x%x "
2607 "hba state x%x>\n",
2608 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002609 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002610 goto out;
2611 }
2612
James Smart2e0fef82007-06-17 19:56:36 -05002613 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002614 sizeof (struct serv_parm));
James Smarta12e07b2006-12-02 13:35:30 -05002615 if (phba->cfg_soft_wwnn)
James Smart2e0fef82007-06-17 19:56:36 -05002616 u64_to_wwn(phba->cfg_soft_wwnn,
2617 vport->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -04002618 if (phba->cfg_soft_wwpn)
James Smart2e0fef82007-06-17 19:56:36 -05002619 u64_to_wwn(phba->cfg_soft_wwpn,
2620 vport->fc_sparam.portName.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05002621 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
2622 sizeof(vport->fc_nodename));
2623 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
2624 sizeof(vport->fc_portname));
2625 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2626 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2627 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2628 }
2629
dea31012005-04-17 16:05:31 -05002630 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2631 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002632 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002633 return;
2634
2635out:
2636 pmb->context1 = NULL;
2637 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2638 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002639 lpfc_issue_clear_la(phba, vport);
2640 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002641 return;
2642}
2643
2644static void
James Smart92d7f7b2007-06-17 19:56:38 -05002645lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea31012005-04-17 16:05:31 -05002646{
James Smart92d7f7b2007-06-17 19:56:38 -05002647 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002648 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05002649 int i;
James Smart14691152006-12-02 13:34:28 -05002650 struct lpfc_dmabuf *mp;
2651 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002652 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002653
James Smart92d7f7b2007-06-17 19:56:38 -05002654 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05002655 switch (la->UlnkSpeed) {
James Smart92d7f7b2007-06-17 19:56:38 -05002656 case LA_1GHZ_LINK:
2657 phba->fc_linkspeed = LA_1GHZ_LINK;
2658 break;
2659 case LA_2GHZ_LINK:
2660 phba->fc_linkspeed = LA_2GHZ_LINK;
2661 break;
2662 case LA_4GHZ_LINK:
2663 phba->fc_linkspeed = LA_4GHZ_LINK;
2664 break;
2665 case LA_8GHZ_LINK:
2666 phba->fc_linkspeed = LA_8GHZ_LINK;
2667 break;
James Smartf4b4c682009-05-22 14:53:12 -04002668 case LA_10GHZ_LINK:
2669 phba->fc_linkspeed = LA_10GHZ_LINK;
2670 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002671 default:
2672 phba->fc_linkspeed = LA_UNKNW_LINK;
2673 break;
dea31012005-04-17 16:05:31 -05002674 }
2675
2676 phba->fc_topology = la->topology;
James Smart92d7f7b2007-06-17 19:56:38 -05002677 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002678
2679 if (phba->fc_topology == TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002680 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05002681
James Smart6a9c52c2009-10-02 15:16:51 -04002682 /* if npiv is enabled and this adapter supports npiv log
2683 * a message that npiv is not supported in this topology
2684 */
2685 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04002686 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2687 "1309 Link Up Event npiv not supported in loop "
2688 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05002689 /* Get Loop Map information */
dea31012005-04-17 16:05:31 -05002690 if (la->il)
James Smart2e0fef82007-06-17 19:56:36 -05002691 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002692
James Smart2e0fef82007-06-17 19:56:36 -05002693 vport->fc_myDID = la->granted_AL_PA;
dea31012005-04-17 16:05:31 -05002694 i = la->un.lilpBde64.tus.f.bdeSize;
2695
2696 if (i == 0) {
2697 phba->alpa_map[0] = 0;
2698 } else {
James Smarte8b62012007-08-02 11:10:09 -04002699 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05002700 int numalpa, j, k;
2701 union {
2702 uint8_t pamap[16];
2703 struct {
2704 uint32_t wd1;
2705 uint32_t wd2;
2706 uint32_t wd3;
2707 uint32_t wd4;
2708 } pa;
2709 } un;
2710 numalpa = phba->alpa_map[0];
2711 j = 0;
2712 while (j < numalpa) {
2713 memset(un.pamap, 0, 16);
2714 for (k = 1; j < numalpa; k++) {
2715 un.pamap[k - 1] =
2716 phba->alpa_map[j + 1];
2717 j++;
2718 if (k == 16)
2719 break;
2720 }
2721 /* Link Up Event ALPA map */
2722 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05002723 KERN_WARNING,
2724 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002725 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05002726 "ALPA map Data: x%x "
2727 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002728 un.pa.wd1, un.pa.wd2,
2729 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05002730 }
2731 }
2732 }
2733 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05002734 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04002735 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smart92d7f7b2007-06-17 19:56:38 -05002736 (phba->sli_rev == 3))
2737 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
2738 }
James Smart2e0fef82007-06-17 19:56:36 -05002739 vport->fc_myDID = phba->fc_pref_DID;
2740 vport->fc_flag |= FC_LBIT;
dea31012005-04-17 16:05:31 -05002741 }
James Smart92d7f7b2007-06-17 19:56:38 -05002742 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002743
2744 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05002745 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2746 if (!sparam_mbox)
2747 goto out;
2748
2749 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
2750 if (rc) {
2751 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2752 goto out;
2753 }
2754 sparam_mbox->vport = vport;
2755 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
2756 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
2757 if (rc == MBX_NOT_FINISHED) {
2758 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
2759 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2760 kfree(mp);
2761 mempool_free(sparam_mbox, phba->mbox_mem_pool);
2762 goto out;
dea31012005-04-17 16:05:31 -05002763 }
2764
James Smart6fb120a2009-05-22 14:52:59 -04002765 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
2766 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2767 if (!cfglink_mbox)
2768 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05002769 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05002770 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05002771 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05002772 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04002773 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04002774 if (rc == MBX_NOT_FINISHED) {
2775 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2776 goto out;
2777 }
2778 } else {
James Smart32b97932009-07-19 10:01:21 -04002779 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04002780 /*
2781 * Add the driver's default FCF record at FCF index 0 now. This
2782 * is phase 1 implementation that support FCF index 0 and driver
2783 * defaults.
2784 */
James Smart45ed1192009-10-02 15:17:02 -04002785 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04002786 fcf_record = kzalloc(sizeof(struct fcf_record),
2787 GFP_KERNEL);
2788 if (unlikely(!fcf_record)) {
2789 lpfc_printf_log(phba, KERN_ERR,
2790 LOG_MBOX | LOG_SLI,
2791 "2554 Could not allocate memmory for "
2792 "fcf record\n");
2793 rc = -ENODEV;
2794 goto out;
2795 }
2796
2797 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2798 LPFC_FCOE_FCF_DEF_INDEX);
2799 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2800 if (unlikely(rc)) {
2801 lpfc_printf_log(phba, KERN_ERR,
2802 LOG_MBOX | LOG_SLI,
2803 "2013 Could not manually add FCF "
2804 "record 0, status %d\n", rc);
2805 rc = -ENODEV;
2806 kfree(fcf_record);
2807 goto out;
2808 }
2809 kfree(fcf_record);
2810 }
2811 /*
2812 * The driver is expected to do FIP/FCF. Call the port
2813 * and get the FCF Table.
2814 */
James Smart32b97932009-07-19 10:01:21 -04002815 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002816 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04002817 spin_unlock_irq(&phba->hbalock);
2818 return;
2819 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002820 /* This is the initial FCF discovery scan */
2821 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04002822 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05002823 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2824 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05002825 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2826 LPFC_FCOE_FCF_GET_FIRST);
2827 if (rc) {
2828 spin_lock_irq(&phba->hbalock);
2829 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
2830 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002831 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002832 }
James Smart38b92ef2010-08-04 16:11:39 -04002833 /* Reset FCF roundrobin bmask for new discovery */
2834 memset(phba->fcf.fcf_rr_bmask, 0,
2835 sizeof(*phba->fcf.fcf_rr_bmask));
dea31012005-04-17 16:05:31 -05002836 }
James Smart6fb120a2009-05-22 14:52:59 -04002837
2838 return;
James Smart92d7f7b2007-06-17 19:56:38 -05002839out:
2840 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04002841 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2842 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2843 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05002844 lpfc_issue_clear_la(phba, vport);
2845 return;
dea31012005-04-17 16:05:31 -05002846}
2847
2848static void
James Smart84774a42008-08-24 21:50:06 -04002849lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05002850{
dea31012005-04-17 16:05:31 -05002851 uint32_t control;
2852 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05002853 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002854 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04002855 if (phba->sli_rev <= LPFC_SLI_REV3) {
2856 control = readl(phba->HCregaddr);
2857 control |= HC_LAINT_ENA;
2858 writel(control, phba->HCregaddr);
2859 readl(phba->HCregaddr); /* flush */
2860 }
James Smart2e0fef82007-06-17 19:56:36 -05002861 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002862}
2863
James Smart84774a42008-08-24 21:50:06 -04002864static void
2865lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2866{
2867 lpfc_linkdown(phba);
2868 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04002869 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04002870 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2871}
2872
2873
dea31012005-04-17 16:05:31 -05002874/*
2875 * This routine handles processing a READ_LA mailbox
2876 * command upon completion. It is setup in the LPFC_MBOXQ
2877 * as the completion routine when the command is
2878 * handed off to the SLI layer.
2879 */
2880void
James Smart2e0fef82007-06-17 19:56:36 -05002881lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002882{
James Smart2e0fef82007-06-17 19:56:36 -05002883 struct lpfc_vport *vport = pmb->vport;
2884 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05002885 READ_LA_VAR *la;
James Smart04c68492009-05-22 14:52:52 -04002886 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002887 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2888
James Smart0d2b6b82008-06-14 22:52:47 -04002889 /* Unblock ELS traffic */
2890 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05002891 /* Check for error */
2892 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05002893 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002894 "1307 READ_LA mbox error x%x state x%x\n",
2895 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002896 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05002897 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002898 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2899 }
2900
James Smart04c68492009-05-22 14:52:52 -04002901 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea31012005-04-17 16:05:31 -05002902
2903 memcpy(&phba->alpa_map[0], mp->virt, 128);
2904
James Smart2e0fef82007-06-17 19:56:36 -05002905 spin_lock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002906 if (la->pb)
James Smart2e0fef82007-06-17 19:56:36 -05002907 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002908 else
James Smart2e0fef82007-06-17 19:56:36 -05002909 vport->fc_flag &= ~FC_BYPASSED_MODE;
2910 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05002911
James Smart0c287582009-06-10 17:22:56 -04002912 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05002913 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05002914 phba->fc_stat.LinkMultiEvent++;
James Smart2e0fef82007-06-17 19:56:36 -05002915 if (la->attType == AT_LINK_UP)
dea31012005-04-17 16:05:31 -05002916 if (phba->fc_eventTag != 0)
2917 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002918 }
dea31012005-04-17 16:05:31 -05002919
2920 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05002921 spin_lock_irq(&phba->hbalock);
James Smart84774a42008-08-24 21:50:06 -04002922 if (la->mm)
2923 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2924 else
2925 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05002926 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002927
James Smart4d9ab992009-10-02 15:16:39 -04002928 phba->link_events++;
James Smart84774a42008-08-24 21:50:06 -04002929 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea31012005-04-17 16:05:31 -05002930 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05002931 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05002932 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002933 "1306 Link Up Event in loop back mode "
2934 "x%x received Data: x%x x%x x%x x%x\n",
2935 la->eventTag, phba->fc_eventTag,
2936 la->granted_AL_PA, la->UlnkSpeed,
2937 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04002938 } else {
2939 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002940 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002941 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002942 la->eventTag, phba->fc_eventTag,
2943 la->granted_AL_PA, la->UlnkSpeed,
James Smart84774a42008-08-24 21:50:06 -04002944 phba->alpa_map[0],
2945 la->mm, la->fa,
2946 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04002947 }
James Smart92d7f7b2007-06-17 19:56:38 -05002948 lpfc_mbx_process_link_up(phba, la);
James Smart84774a42008-08-24 21:50:06 -04002949 } else if (la->attType == AT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05002950 phba->fc_stat.LinkDown++;
James Smart3163f722008-02-08 18:50:25 -05002951 if (phba->link_flag & LS_LOOPBACK_MODE) {
2952 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2953 "1308 Link Down Event in loop back mode "
2954 "x%x received "
2955 "Data: x%x x%x x%x\n",
2956 la->eventTag, phba->fc_eventTag,
2957 phba->pport->port_state, vport->fc_flag);
2958 }
2959 else {
2960 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002961 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04002962 "Data: x%x x%x x%x x%x x%x\n",
2963 la->eventTag, phba->fc_eventTag,
2964 phba->pport->port_state, vport->fc_flag,
2965 la->mm, la->fa);
2966 }
2967 lpfc_mbx_issue_link_down(phba);
2968 }
2969 if (la->mm && la->attType == AT_LINK_UP) {
2970 if (phba->link_state != LPFC_LINK_DOWN) {
2971 phba->fc_stat.LinkDown++;
2972 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2973 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05002974 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002975 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05002976 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04002977 lpfc_mbx_issue_link_down(phba);
2978 } else
2979 lpfc_enable_la(phba);
2980
2981 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2982 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2983 "Data: x%x x%x x%x\n",
2984 la->eventTag, phba->fc_eventTag,
2985 phba->pport->port_state, vport->fc_flag);
2986 /*
2987 * The cmnd that triggered this will be waiting for this
2988 * signal.
2989 */
2990 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2991 if (phba->wait_4_mlo_maint_flg) {
2992 phba->wait_4_mlo_maint_flg = 0;
2993 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05002994 }
James Smart84774a42008-08-24 21:50:06 -04002995 }
2996
2997 if (la->fa) {
2998 if (la->mm)
2999 lpfc_issue_clear_la(phba, vport);
3000 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3001 "1311 fa %d\n", la->fa);
dea31012005-04-17 16:05:31 -05003002 }
3003
3004lpfc_mbx_cmpl_read_la_free_mbuf:
3005 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3006 kfree(mp);
3007 mempool_free(pmb, phba->mbox_mem_pool);
3008 return;
3009}
3010
3011/*
3012 * This routine handles processing a REG_LOGIN mailbox
3013 * command upon completion. It is setup in the LPFC_MBOXQ
3014 * as the completion routine when the command is
3015 * handed off to the SLI layer.
3016 */
3017void
James Smart2e0fef82007-06-17 19:56:36 -05003018lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003019{
James Smart2e0fef82007-06-17 19:56:36 -05003020 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003021 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003022 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003023 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003024
dea31012005-04-17 16:05:31 -05003025 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003026 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003027
James Smartffc95492010-06-07 15:23:17 -04003028 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3029 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3030
3031 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3032 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3033 /* We rcvd a rscn after issuing this
3034 * mbox reg login, we may have cycled
3035 * back through the state and be
3036 * back at reg login state so this
3037 * mbox needs to be ignored becase
3038 * there is another reg login in
3039 * proccess.
3040 */
3041 spin_lock_irq(shost->host_lock);
3042 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3043 spin_unlock_irq(shost->host_lock);
3044 if (phba->sli_rev == LPFC_SLI_REV4)
3045 lpfc_sli4_free_rpi(phba,
3046 pmb->u.mb.un.varRegLogin.rpi);
3047
3048 } else
3049 /* Good status, call state machine */
3050 lpfc_disc_state_machine(vport, ndlp, pmb,
3051 NLP_EVT_CMPL_REG_LOGIN);
3052
dea31012005-04-17 16:05:31 -05003053 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3054 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003055 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003056 /* decrement the node reference count held for this callback
3057 * function.
3058 */
James Smart329f9bc2007-04-25 09:53:01 -04003059 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003060
3061 return;
3062}
3063
James Smart92d7f7b2007-06-17 19:56:38 -05003064static void
3065lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3066{
James Smart04c68492009-05-22 14:52:52 -04003067 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003068 struct lpfc_vport *vport = pmb->vport;
3069 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3070
3071 switch (mb->mbxStatus) {
3072 case 0x0011:
3073 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003074 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3075 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3076 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003077 break;
James Smart78730cf2010-04-06 15:06:30 -04003078 /* If VPI is busy, reset the HBA */
3079 case 0x9700:
3080 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3081 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3082 vport->vpi, mb->mbxStatus);
3083 if (!(phba->pport->load_flag & FC_UNLOADING))
3084 lpfc_workq_post_event(phba, NULL, NULL,
3085 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003086 }
James Smart72100cc2010-02-12 14:43:01 -05003087 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003088 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003089 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003090 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003091 vport->unreg_vpi_cmpl = VPORT_OK;
3092 mempool_free(pmb, phba->mbox_mem_pool);
3093 /*
3094 * This shost reference might have been taken at the beginning of
3095 * lpfc_vport_delete()
3096 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003097 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003098 scsi_host_put(shost);
3099}
3100
James Smartd7c255b2008-08-24 21:50:00 -04003101int
James Smart92d7f7b2007-06-17 19:56:38 -05003102lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3103{
3104 struct lpfc_hba *phba = vport->phba;
3105 LPFC_MBOXQ_t *mbox;
3106 int rc;
3107
3108 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3109 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003110 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003111
3112 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3113 mbox->vport = vport;
3114 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003115 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003116 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003117 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3118 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003119 mempool_free(mbox, phba->mbox_mem_pool);
3120 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003121 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003122 }
James Smartd7c255b2008-08-24 21:50:00 -04003123 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003124}
3125
3126static void
3127lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3128{
3129 struct lpfc_vport *vport = pmb->vport;
3130 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003131 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003132
3133 switch (mb->mbxStatus) {
3134 case 0x0011:
3135 case 0x9601:
3136 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003137 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3138 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3139 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003140 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3141 spin_lock_irq(shost->host_lock);
3142 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3143 spin_unlock_irq(shost->host_lock);
3144 vport->fc_myDID = 0;
3145 goto out;
3146 }
James Smart92d7f7b2007-06-17 19:56:38 -05003147
James Smart72100cc2010-02-12 14:43:01 -05003148 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003149 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003150 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003151 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003152 vport->num_disc_nodes = 0;
3153 /* go thru NPR list and issue ELS PLOGIs */
3154 if (vport->fc_npr_cnt)
3155 lpfc_els_disc_plogi(vport);
3156
3157 if (!vport->num_disc_nodes) {
3158 spin_lock_irq(shost->host_lock);
3159 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3160 spin_unlock_irq(shost->host_lock);
3161 lpfc_can_disctmo(vport);
3162 }
3163 vport->port_state = LPFC_VPORT_READY;
3164
3165out:
3166 mempool_free(pmb, phba->mbox_mem_pool);
3167 return;
3168}
3169
James Smart21e9a0a2009-05-22 14:53:21 -04003170/**
3171 * lpfc_create_static_vport - Read HBA config region to create static vports.
3172 * @phba: pointer to lpfc hba data structure.
3173 *
3174 * This routine issue a DUMP mailbox command for config region 22 to get
3175 * the list of static vports to be created. The function create vports
3176 * based on the information returned from the HBA.
3177 **/
3178void
3179lpfc_create_static_vport(struct lpfc_hba *phba)
3180{
3181 LPFC_MBOXQ_t *pmb = NULL;
3182 MAILBOX_t *mb;
3183 struct static_vport_info *vport_info;
James Smart1c6834a2009-07-19 10:01:26 -04003184 int rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003185 struct fc_vport_identifiers vport_id;
3186 struct fc_vport *new_fc_vport;
3187 struct Scsi_Host *shost;
3188 struct lpfc_vport *vport;
3189 uint16_t offset = 0;
3190 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003191 struct lpfc_dmabuf *mp;
3192 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003193
3194 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3195 if (!pmb) {
3196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3197 "0542 lpfc_create_static_vport failed to"
3198 " allocate mailbox memory\n");
3199 return;
3200 }
3201
3202 mb = &pmb->u.mb;
3203
3204 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3205 if (!vport_info) {
3206 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3207 "0543 lpfc_create_static_vport failed to"
3208 " allocate vport_info\n");
3209 mempool_free(pmb, phba->mbox_mem_pool);
3210 return;
3211 }
3212
3213 vport_buff = (uint8_t *) vport_info;
3214 do {
James Smart1c6834a2009-07-19 10:01:26 -04003215 if (lpfc_dump_static_vport(phba, pmb, offset))
3216 goto out;
3217
James Smart21e9a0a2009-05-22 14:53:21 -04003218 pmb->vport = phba->pport;
3219 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
3220
3221 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
3222 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3223 "0544 lpfc_create_static_vport failed to"
3224 " issue dump mailbox command ret 0x%x "
3225 "status 0x%x\n",
3226 rc, mb->mbxStatus);
3227 goto out;
3228 }
3229
James Smart1c6834a2009-07-19 10:01:26 -04003230 if (phba->sli_rev == LPFC_SLI_REV4) {
3231 byte_count = pmb->u.mqe.un.mb_words[5];
3232 mp = (struct lpfc_dmabuf *) pmb->context2;
3233 if (byte_count > sizeof(struct static_vport_info) -
3234 offset)
3235 byte_count = sizeof(struct static_vport_info)
3236 - offset;
3237 memcpy(vport_buff + offset, mp->virt, byte_count);
3238 offset += byte_count;
3239 } else {
3240 if (mb->un.varDmp.word_cnt >
3241 sizeof(struct static_vport_info) - offset)
3242 mb->un.varDmp.word_cnt =
3243 sizeof(struct static_vport_info)
3244 - offset;
3245 byte_count = mb->un.varDmp.word_cnt;
3246 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3247 vport_buff + offset,
3248 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003249
James Smart1c6834a2009-07-19 10:01:26 -04003250 offset += byte_count;
3251 }
James Smart21e9a0a2009-05-22 14:53:21 -04003252
James Smart1c6834a2009-07-19 10:01:26 -04003253 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003254 offset < sizeof(struct static_vport_info));
3255
3256
3257 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3258 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3259 != VPORT_INFO_REV)) {
3260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3261 "0545 lpfc_create_static_vport bad"
3262 " information header 0x%x 0x%x\n",
3263 le32_to_cpu(vport_info->signature),
3264 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3265
3266 goto out;
3267 }
3268
3269 shost = lpfc_shost_from_vport(phba->pport);
3270
3271 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3272 memset(&vport_id, 0, sizeof(vport_id));
3273 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3274 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3275 if (!vport_id.port_name || !vport_id.node_name)
3276 continue;
3277
3278 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3279 vport_id.vport_type = FC_PORTTYPE_NPIV;
3280 vport_id.disable = false;
3281 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3282
3283 if (!new_fc_vport) {
3284 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3285 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003286 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003287 continue;
3288 }
3289
3290 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3291 vport->vport_flag |= STATIC_VPORT;
3292 }
3293
3294out:
James Smart21e9a0a2009-05-22 14:53:21 -04003295 kfree(vport_info);
James Smart1c6834a2009-07-19 10:01:26 -04003296 if (rc != MBX_TIMEOUT) {
3297 if (pmb->context2) {
3298 mp = (struct lpfc_dmabuf *) pmb->context2;
3299 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3300 kfree(mp);
3301 }
James Smart21e9a0a2009-05-22 14:53:21 -04003302 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003303 }
James Smart21e9a0a2009-05-22 14:53:21 -04003304
3305 return;
3306}
3307
dea31012005-04-17 16:05:31 -05003308/*
3309 * This routine handles processing a Fabric REG_LOGIN mailbox
3310 * command upon completion. It is setup in the LPFC_MBOXQ
3311 * as the completion routine when the command is
3312 * handed off to the SLI layer.
3313 */
3314void
James Smart2e0fef82007-06-17 19:56:36 -05003315lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003316{
James Smart92d7f7b2007-06-17 19:56:38 -05003317 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003318 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003319 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003320 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05003321
James Smart549e55c2007-08-02 11:09:51 -04003322 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003323 pmb->context1 = NULL;
3324 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003325
dea31012005-04-17 16:05:31 -05003326 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003327 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3328 "0258 Register Fabric login error: 0x%x\n",
3329 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003330 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3331 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003332 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003333
James Smart92d7f7b2007-06-17 19:56:38 -05003334 if (phba->fc_topology == TOPOLOGY_LOOP) {
3335 /* FLOGI failed, use loop map to make discovery list */
3336 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003337
James Smart92d7f7b2007-06-17 19:56:38 -05003338 /* Start discovery */
3339 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003340 /* Decrement the reference count to ndlp after the
3341 * reference to the ndlp are done.
3342 */
3343 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003344 return;
3345 }
3346
3347 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003348 /* Decrement the reference count to ndlp after the reference
3349 * to the ndlp are done.
3350 */
3351 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003352 return;
3353 }
3354
dea31012005-04-17 16:05:31 -05003355 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003356 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003357 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003358 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003359
James Smart2e0fef82007-06-17 19:56:36 -05003360 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003361 /* when physical port receive logo donot start
3362 * vport discovery */
3363 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3364 lpfc_start_fdiscs(phba);
3365 else
3366 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smart92d7f7b2007-06-17 19:56:38 -05003367 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003368 }
3369
3370 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3371 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003372 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003373
3374 /* Drop the reference count from the mbox at the end after
3375 * all the current reference to the ndlp have been done.
3376 */
3377 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003378 return;
3379}
3380
3381/*
3382 * This routine handles processing a NameServer REG_LOGIN mailbox
3383 * command upon completion. It is setup in the LPFC_MBOXQ
3384 * as the completion routine when the command is
3385 * handed off to the SLI layer.
3386 */
3387void
James Smart2e0fef82007-06-17 19:56:36 -05003388lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003389{
James Smart21e9a0a2009-05-22 14:53:21 -04003390 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003391 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3392 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3393 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003394
James Smartd439d282010-09-29 11:18:45 -04003395 pmb->context1 = NULL;
3396 pmb->context2 = NULL;
3397
dea31012005-04-17 16:05:31 -05003398 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003399out:
James Smart21e9a0a2009-05-22 14:53:21 -04003400 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3401 "0260 Register NameServer error: 0x%x\n",
3402 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003403 /* decrement the node reference count held for this
3404 * callback function.
3405 */
James Smart329f9bc2007-04-25 09:53:01 -04003406 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003407 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3408 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003409 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003410
3411 /* If no other thread is using the ndlp, free it */
3412 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003413
James Smart92d7f7b2007-06-17 19:56:38 -05003414 if (phba->fc_topology == TOPOLOGY_LOOP) {
3415 /*
3416 * RegLogin failed, use loop map to make discovery
3417 * list
3418 */
3419 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003420
James Smart92d7f7b2007-06-17 19:56:38 -05003421 /* Start discovery */
3422 lpfc_disc_start(vport);
3423 return;
3424 }
3425 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003426 return;
3427 }
3428
dea31012005-04-17 16:05:31 -05003429 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04003430 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05003431 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003432 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003433
James Smart2e0fef82007-06-17 19:56:36 -05003434 if (vport->port_state < LPFC_VPORT_READY) {
3435 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003436 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3437 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3438 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3439 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3440 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3441
3442 /* Issue SCR just before NameServer GID_FT Query */
3443 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003444 }
3445
James Smart2e0fef82007-06-17 19:56:36 -05003446 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003447 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003448 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003449 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003450 goto out;
dea31012005-04-17 16:05:31 -05003451 }
3452
James Smartfa4066b2008-01-11 01:53:27 -05003453 /* decrement the node reference count held for this
3454 * callback function.
3455 */
James Smart329f9bc2007-04-25 09:53:01 -04003456 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003457 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3458 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003459 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003460
3461 return;
3462}
3463
3464static void
James Smart2e0fef82007-06-17 19:56:36 -05003465lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003466{
James Smart2e0fef82007-06-17 19:56:36 -05003467 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3468 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003469 struct lpfc_rport_data *rdata;
3470 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003471 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003472
3473 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003474 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3475 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003476 rport_ids.port_id = ndlp->nlp_DID;
3477 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003478
James Smart329f9bc2007-04-25 09:53:01 -04003479 /*
3480 * We leave our node pointer in rport->dd_data when we unregister a
3481 * FCP target port. But fc_remote_port_add zeros the space to which
3482 * rport->dd_data points. So, if we're reusing a previously
3483 * registered port, drop the reference that we took the last time we
3484 * registered the port.
3485 */
3486 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003487 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003488 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003489
3490 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3491 "rport add: did:x%x flg:x%x type x%x",
3492 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3493
James Smart2e0fef82007-06-17 19:56:36 -05003494 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003495 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003496 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3497 "Warning: fc_remote_port_add failed\n");
3498 return;
3499 }
3500
3501 /* initialize static port data */
3502 rport->maxframe_size = ndlp->nlp_maxframe;
3503 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003504 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003505 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003506
3507 if (ndlp->nlp_type & NLP_FCP_TARGET)
3508 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3509 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3510 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3511
3512
3513 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3514 fc_remote_port_rolechg(rport, rport_ids.roles);
3515
James Smart071fbd3d2006-04-15 11:53:20 -04003516 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003517 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003518 ndlp->nlp_sid = rport->scsi_target_id;
3519 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003520 return;
3521}
3522
3523static void
James Smart2e0fef82007-06-17 19:56:36 -05003524lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003525{
3526 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003527
James Smart858c9f62007-06-17 19:56:39 -05003528 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3529 "rport delete: did:x%x flg:x%x type x%x",
3530 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3531
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003532 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003533
3534 return;
3535}
3536
James Smartde0c5b32007-04-25 09:52:27 -04003537static void
James Smart2e0fef82007-06-17 19:56:36 -05003538lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003539{
James Smart2e0fef82007-06-17 19:56:36 -05003540 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3541
3542 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003543 switch (state) {
3544 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003545 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003546 break;
3547 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003548 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003549 break;
3550 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003551 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003552 break;
3553 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003554 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003555 break;
3556 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003557 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003558 break;
3559 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003560 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003561 break;
3562 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003563 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003564 break;
3565 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003566 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003567 break;
3568 }
James Smart2e0fef82007-06-17 19:56:36 -05003569 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003570}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003571
James Smartde0c5b32007-04-25 09:52:27 -04003572static void
James Smart2e0fef82007-06-17 19:56:36 -05003573lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003574 int old_state, int new_state)
3575{
James Smart2e0fef82007-06-17 19:56:36 -05003576 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3577
James Smartde0c5b32007-04-25 09:52:27 -04003578 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003579 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3580 ndlp->nlp_type |= NLP_FC_NODE;
3581 }
3582 if (new_state == NLP_STE_MAPPED_NODE)
3583 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3584 if (new_state == NLP_STE_NPR_NODE)
3585 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003586
James Smartde0c5b32007-04-25 09:52:27 -04003587 /* Transport interface */
3588 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3589 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003590 vport->phba->nport_event_cnt++;
3591 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003592 }
dea31012005-04-17 16:05:31 -05003593
James Smartde0c5b32007-04-25 09:52:27 -04003594 if (new_state == NLP_STE_MAPPED_NODE ||
3595 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003596 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003597 /*
3598 * Tell the fc transport about the port, if we haven't
3599 * already. If we have, and it's a scsi entity, be
3600 * sure to unblock any attached scsi devices
3601 */
3602 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003603 }
James Smartea2151b2008-09-07 11:52:10 -04003604 if ((new_state == NLP_STE_MAPPED_NODE) &&
3605 (vport->stat_data_enabled)) {
3606 /*
3607 * A new target is discovered, if there is no buffer for
3608 * statistical data collection allocate buffer.
3609 */
3610 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3611 sizeof(struct lpfc_scsicmd_bkt),
3612 GFP_KERNEL);
3613
3614 if (!ndlp->lat_data)
3615 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3616 "0286 lpfc_nlp_state_cleanup failed to "
3617 "allocate statistical data buffer DID "
3618 "0x%x\n", ndlp->nlp_DID);
3619 }
James Smart858c9f62007-06-17 19:56:39 -05003620 /*
3621 * if we added to Mapped list, but the remote port
3622 * registration failed or assigned a target id outside
3623 * our presentable range - move the node to the
3624 * Unmapped List
3625 */
James Smartde0c5b32007-04-25 09:52:27 -04003626 if (new_state == NLP_STE_MAPPED_NODE &&
3627 (!ndlp->rport ||
3628 ndlp->rport->scsi_target_id == -1 ||
3629 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003630 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003631 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003632 spin_unlock_irq(shost->host_lock);
3633 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003634 }
James Smartde0c5b32007-04-25 09:52:27 -04003635}
3636
James Smart685f0bf2007-04-25 09:53:08 -04003637static char *
3638lpfc_nlp_state_name(char *buffer, size_t size, int state)
3639{
3640 static char *states[] = {
3641 [NLP_STE_UNUSED_NODE] = "UNUSED",
3642 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3643 [NLP_STE_ADISC_ISSUE] = "ADISC",
3644 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3645 [NLP_STE_PRLI_ISSUE] = "PRLI",
3646 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
3647 [NLP_STE_MAPPED_NODE] = "MAPPED",
3648 [NLP_STE_NPR_NODE] = "NPR",
3649 };
3650
James Smart311464e2007-08-02 11:10:37 -04003651 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04003652 strlcpy(buffer, states[state], size);
3653 else
3654 snprintf(buffer, size, "unknown (%d)", state);
3655 return buffer;
3656}
3657
James Smartde0c5b32007-04-25 09:52:27 -04003658void
James Smart2e0fef82007-06-17 19:56:36 -05003659lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3660 int state)
James Smartde0c5b32007-04-25 09:52:27 -04003661{
James Smart2e0fef82007-06-17 19:56:36 -05003662 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04003663 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04003664 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04003665
James Smarte8b62012007-08-02 11:10:09 -04003666 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3667 "0904 NPort state transition x%06x, %s -> %s\n",
3668 ndlp->nlp_DID,
3669 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
3670 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05003671
3672 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3673 "node statechg did:x%x old:%d ste:%d",
3674 ndlp->nlp_DID, old_state, state);
3675
James Smartde0c5b32007-04-25 09:52:27 -04003676 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04003677 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05003678 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003679 if (old_state == NLP_STE_UNMAPPED_NODE) {
3680 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
3681 ndlp->nlp_type &= ~NLP_FC_NODE;
3682 }
3683
James Smart685f0bf2007-04-25 09:53:08 -04003684 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05003685 spin_lock_irq(shost->host_lock);
3686 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3687 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003688 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05003689 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04003690
3691 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05003692 lpfc_nlp_counters(vport, state, 1);
3693 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04003694}
3695
3696void
James Smarte47c9092008-02-08 18:49:26 -05003697lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3698{
3699 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3700
3701 if (list_empty(&ndlp->nlp_listp)) {
3702 spin_lock_irq(shost->host_lock);
3703 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
3704 spin_unlock_irq(shost->host_lock);
3705 }
3706}
3707
3708void
James Smart2e0fef82007-06-17 19:56:36 -05003709lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003710{
James Smart2e0fef82007-06-17 19:56:36 -05003711 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3712
James Smart0d2b6b82008-06-14 22:52:47 -04003713 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003714 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05003715 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3716 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04003717 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05003718 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05003719 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05003720 NLP_STE_UNUSED_NODE);
3721}
3722
Adrian Bunk4d9db012008-02-14 23:24:02 +02003723static void
James Smarte47c9092008-02-08 18:49:26 -05003724lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
3725{
James Smart0d2b6b82008-06-14 22:52:47 -04003726 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05003727 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
3728 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
3729 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
3730 NLP_STE_UNUSED_NODE);
3731}
James Smart109f6ed2008-12-04 22:39:08 -05003732/**
James Smart3621a712009-04-06 18:47:14 -04003733 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05003734 * @vport: Pointer to Virtual Port object.
3735 * @ndlp: Pointer to FC node object.
3736 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04003737 *
3738 * This function is always called when node object need to be initialized.
3739 * It initializes all the fields of the node object. Although the reference
3740 * to phba from @ndlp can be obtained indirectly through it's reference to
3741 * @vport, a direct reference to phba is taken here by @ndlp. This is due
3742 * to the life-span of the @ndlp might go beyond the existence of @vport as
3743 * the final release of ndlp is determined by its reference count. And, the
3744 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05003745 **/
3746static inline void
3747lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3748 uint32_t did)
3749{
3750 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
3751 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
3752 init_timer(&ndlp->nlp_delayfunc);
3753 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
3754 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
3755 ndlp->nlp_DID = did;
3756 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04003757 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05003758 ndlp->nlp_sid = NLP_NO_SID;
3759 kref_init(&ndlp->kref);
3760 NLP_INT_NODE_ACT(ndlp);
3761 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04003762 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05003763}
James Smarte47c9092008-02-08 18:49:26 -05003764
3765struct lpfc_nodelist *
3766lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3767 int state)
3768{
3769 struct lpfc_hba *phba = vport->phba;
3770 uint32_t did;
3771 unsigned long flags;
3772
3773 if (!ndlp)
3774 return NULL;
3775
3776 spin_lock_irqsave(&phba->ndlp_lock, flags);
3777 /* The ndlp should not be in memory free mode */
3778 if (NLP_CHK_FREE_REQ(ndlp)) {
3779 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3780 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3781 "0277 lpfc_enable_node: ndlp:x%p "
3782 "usgmap:x%x refcnt:%d\n",
3783 (void *)ndlp, ndlp->nlp_usg_map,
3784 atomic_read(&ndlp->kref.refcount));
3785 return NULL;
3786 }
3787 /* The ndlp should not already be in active mode */
3788 if (NLP_CHK_NODE_ACT(ndlp)) {
3789 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3790 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3791 "0278 lpfc_enable_node: ndlp:x%p "
3792 "usgmap:x%x refcnt:%d\n",
3793 (void *)ndlp, ndlp->nlp_usg_map,
3794 atomic_read(&ndlp->kref.refcount));
3795 return NULL;
3796 }
3797
3798 /* Keep the original DID */
3799 did = ndlp->nlp_DID;
3800
3801 /* re-initialize ndlp except of ndlp linked list pointer */
3802 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
3803 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05003804 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05003805
3806 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3807
3808 if (state != NLP_STE_UNUSED_NODE)
3809 lpfc_nlp_set_state(vport, ndlp, state);
3810
3811 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3812 "node enable: did:x%x",
3813 ndlp->nlp_DID, 0, 0);
3814 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04003815}
3816
3817void
James Smart2e0fef82007-06-17 19:56:36 -05003818lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04003819{
James Smart87af33f2007-10-27 13:37:43 -04003820 /*
James Smartfa4066b2008-01-11 01:53:27 -05003821 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04003822 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05003823 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3824 * until ALL other outstanding threads have completed. We check
3825 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04003826 */
James Smartfa4066b2008-01-11 01:53:27 -05003827 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3828 return;
James Smart51ef4c22007-08-02 11:10:31 -04003829 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart87af33f2007-10-27 13:37:43 -04003830 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04003831 return;
dea31012005-04-17 16:05:31 -05003832}
3833
3834/*
3835 * Start / ReStart rescue timer for Discovery / RSCN handling
3836 */
3837void
James Smart2e0fef82007-06-17 19:56:36 -05003838lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003839{
James Smart2e0fef82007-06-17 19:56:36 -05003840 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3841 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003842 uint32_t tmo;
3843
James Smart2e0fef82007-06-17 19:56:36 -05003844 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003845 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003846 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3847 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003848 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003849 * FC spec states we need 3 * ratov for CT requests
3850 */
3851 tmo = ((phba->fc_ratov * 3) + 3);
3852 }
dea31012005-04-17 16:05:31 -05003853
James Smart858c9f62007-06-17 19:56:39 -05003854
3855 if (!timer_pending(&vport->fc_disctmo)) {
3856 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3857 "set disc timer: tmo:x%x state:x%x flg:x%x",
3858 tmo, vport->port_state, vport->fc_flag);
3859 }
3860
James Smart2e0fef82007-06-17 19:56:36 -05003861 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3862 spin_lock_irq(shost->host_lock);
3863 vport->fc_flag |= FC_DISC_TMO;
3864 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003865
3866 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003867 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3868 "0247 Start Discovery Timer state x%x "
3869 "Data: x%x x%lx x%x x%x\n",
3870 vport->port_state, tmo,
3871 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3872 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05003873
3874 return;
3875}
3876
3877/*
3878 * Cancel rescue timer for Discovery / RSCN handling
3879 */
3880int
James Smart2e0fef82007-06-17 19:56:36 -05003881lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05003882{
James Smart2e0fef82007-06-17 19:56:36 -05003883 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05003884 unsigned long iflags;
3885
James Smart858c9f62007-06-17 19:56:39 -05003886 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3887 "can disc timer: state:x%x rtry:x%x flg:x%x",
3888 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3889
dea31012005-04-17 16:05:31 -05003890 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05003891 if (vport->fc_flag & FC_DISC_TMO) {
3892 spin_lock_irqsave(shost->host_lock, iflags);
3893 vport->fc_flag &= ~FC_DISC_TMO;
3894 spin_unlock_irqrestore(shost->host_lock, iflags);
3895 del_timer_sync(&vport->fc_disctmo);
3896 spin_lock_irqsave(&vport->work_port_lock, iflags);
3897 vport->work_port_events &= ~WORKER_DISC_TMO;
3898 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05003899 }
3900
3901 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003902 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3903 "0248 Cancel Discovery Timer state x%x "
3904 "Data: x%x x%x x%x\n",
3905 vport->port_state, vport->fc_flag,
3906 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003907 return 0;
dea31012005-04-17 16:05:31 -05003908}
3909
3910/*
3911 * Check specified ring for outstanding IOCB on the SLI queue
3912 * Return true if iocb matches the specified nport
3913 */
3914int
James Smart2e0fef82007-06-17 19:56:36 -05003915lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3916 struct lpfc_sli_ring *pring,
3917 struct lpfc_iocbq *iocb,
3918 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003919{
James Smart2e0fef82007-06-17 19:56:36 -05003920 struct lpfc_sli *psli = &phba->sli;
3921 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05003922 struct lpfc_vport *vport = ndlp->vport;
3923
3924 if (iocb->vport != vport)
3925 return 0;
3926
dea31012005-04-17 16:05:31 -05003927 if (pring->ringno == LPFC_ELS_RING) {
3928 switch (icmd->ulpCommand) {
3929 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04003930 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003931 return 1;
dea31012005-04-17 16:05:31 -05003932 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04003933 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3934 return 1;
dea31012005-04-17 16:05:31 -05003935 case CMD_XMIT_ELS_RSP64_CX:
3936 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003937 return 1;
dea31012005-04-17 16:05:31 -05003938 }
James Smarta4bc3372006-12-02 13:34:16 -05003939 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05003940
3941 } else if (pring->ringno == psli->fcp_ring) {
3942 /* Skip match check if waiting to relogin to FCP target */
3943 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003944 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003945 return 0;
dea31012005-04-17 16:05:31 -05003946 }
3947 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003948 return 1;
dea31012005-04-17 16:05:31 -05003949 }
3950 } else if (pring->ringno == psli->next_ring) {
3951
3952 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05003953 return 0;
dea31012005-04-17 16:05:31 -05003954}
3955
3956/*
3957 * Free resources / clean up outstanding I/Os
3958 * associated with nlp_rpi in the LPFC_NODELIST entry.
3959 */
3960static int
James Smart2e0fef82007-06-17 19:56:36 -05003961lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003962{
James Smart2534ba72007-04-25 09:52:20 -04003963 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003964 struct lpfc_sli *psli;
3965 struct lpfc_sli_ring *pring;
3966 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04003967 uint32_t i;
dea31012005-04-17 16:05:31 -05003968
James Smart92d7f7b2007-06-17 19:56:38 -05003969 lpfc_fabric_abort_nport(ndlp);
3970
dea31012005-04-17 16:05:31 -05003971 /*
3972 * Everything that matches on txcmplq will be returned
3973 * by firmware with a no rpi error.
3974 */
3975 psli = &phba->sli;
James Smart21e9a0a2009-05-22 14:53:21 -04003976 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea31012005-04-17 16:05:31 -05003977 /* Now process each ring */
3978 for (i = 0; i < psli->num_rings; i++) {
3979 pring = &psli->ring[i];
3980
James Smart2e0fef82007-06-17 19:56:36 -05003981 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003982 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05003983 list) {
dea31012005-04-17 16:05:31 -05003984 /*
3985 * Check to see if iocb matches the nport we are
3986 * looking for
3987 */
James Smart92d7f7b2007-06-17 19:56:38 -05003988 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3989 ndlp))) {
dea31012005-04-17 16:05:31 -05003990 /* It matches, so deque and call compl
3991 with an error */
James Smart2534ba72007-04-25 09:52:20 -04003992 list_move_tail(&iocb->list,
3993 &completions);
dea31012005-04-17 16:05:31 -05003994 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05003995 }
3996 }
James Smart2e0fef82007-06-17 19:56:36 -05003997 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003998 }
3999 }
James Smart2534ba72007-04-25 09:52:20 -04004000
James Smarta257bf92009-04-06 18:48:10 -04004001 /* Cancel all the IOCBs from the completions list */
4002 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4003 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004004
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004005 return 0;
dea31012005-04-17 16:05:31 -05004006}
4007
4008/*
4009 * Free rpi associated with LPFC_NODELIST entry.
4010 * This routine is called from lpfc_freenode(), when we are removing
4011 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4012 * LOGO that completes successfully, and we are waiting to PLOGI back
4013 * to the remote NPort. In addition, it is called after we receive
4014 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4015 * we are waiting to PLOGI back to the remote NPort.
4016 */
4017int
James Smart2e0fef82007-06-17 19:56:36 -05004018lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004019{
James Smart2e0fef82007-06-17 19:56:36 -05004020 struct lpfc_hba *phba = vport->phba;
4021 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004022 int rc;
4023
James Smart21e9a0a2009-05-22 14:53:21 -04004024 if (ndlp->nlp_flag & NLP_RPI_VALID) {
James Smart2e0fef82007-06-17 19:56:36 -05004025 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4026 if (mbox) {
James Smart92d7f7b2007-06-17 19:56:38 -05004027 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004028 mbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004029 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004030 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004031 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004032 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004033 }
dea31012005-04-17 16:05:31 -05004034 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004035
dea31012005-04-17 16:05:31 -05004036 ndlp->nlp_rpi = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04004037 ndlp->nlp_flag &= ~NLP_RPI_VALID;
James Smart0c287582009-06-10 17:22:56 -04004038 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004039 return 1;
4040 }
4041 return 0;
4042}
4043
James Smartecfd03c2010-02-12 14:41:27 -05004044/**
4045 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4046 * @phba: pointer to lpfc hba data structure.
4047 *
4048 * This routine is invoked to unregister all the currently registered RPIs
4049 * to the HBA.
4050 **/
4051void
4052lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4053{
4054 struct lpfc_vport **vports;
4055 struct lpfc_nodelist *ndlp;
4056 struct Scsi_Host *shost;
4057 int i;
4058
4059 vports = lpfc_create_vport_work_array(phba);
4060 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4061 shost = lpfc_shost_from_vport(vports[i]);
4062 spin_lock_irq(shost->host_lock);
4063 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004064 if (ndlp->nlp_flag & NLP_RPI_VALID) {
4065 /* The mempool_alloc might sleep */
4066 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004067 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004068 spin_lock_irq(shost->host_lock);
4069 }
James Smartecfd03c2010-02-12 14:41:27 -05004070 }
4071 spin_unlock_irq(shost->host_lock);
4072 }
4073 lpfc_destroy_vport_work_array(phba, vports);
4074}
4075
James Smart92d7f7b2007-06-17 19:56:38 -05004076void
4077lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4078{
4079 struct lpfc_hba *phba = vport->phba;
4080 LPFC_MBOXQ_t *mbox;
4081 int rc;
4082
4083 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4084 if (mbox) {
4085 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
4086 mbox->vport = vport;
4087 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004088 mbox->context1 = NULL;
4089 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004090 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004091 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004092
4093 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4094 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4095 "1836 Could not issue "
4096 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004097 }
4098}
4099
4100void
4101lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4102{
4103 struct lpfc_hba *phba = vport->phba;
4104 LPFC_MBOXQ_t *mbox;
4105 int rc;
4106
4107 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4108 if (mbox) {
4109 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
4110 mbox->vport = vport;
4111 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004112 mbox->context1 = NULL;
4113 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004114 if (rc != MBX_TIMEOUT)
4115 mempool_free(mbox, phba->mbox_mem_pool);
4116
4117 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004118 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4119 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004120 "unreg_did (default rpis) status %d\n",
4121 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004122 }
4123}
4124
dea31012005-04-17 16:05:31 -05004125/*
4126 * Free resources associated with LPFC_NODELIST entry
4127 * so it can be freed.
4128 */
4129static int
James Smart2e0fef82007-06-17 19:56:36 -05004130lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004131{
James Smart2e0fef82007-06-17 19:56:36 -05004132 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4133 struct lpfc_hba *phba = vport->phba;
4134 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004135 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004136
4137 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004138 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4139 "0900 Cleanup node for NPort x%x "
4140 "Data: x%x x%x x%x\n",
4141 ndlp->nlp_DID, ndlp->nlp_flag,
4142 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004143 if (NLP_CHK_FREE_REQ(ndlp)) {
4144 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4145 "0280 lpfc_cleanup_node: ndlp:x%p "
4146 "usgmap:x%x refcnt:%d\n",
4147 (void *)ndlp, ndlp->nlp_usg_map,
4148 atomic_read(&ndlp->kref.refcount));
4149 lpfc_dequeue_node(vport, ndlp);
4150 } else {
4151 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4152 "0281 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_disable_node(vport, ndlp);
4157 }
dea31012005-04-17 16:05:31 -05004158
dea31012005-04-17 16:05:31 -05004159 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4160 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004161 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea31012005-04-17 16:05:31 -05004162 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4163 mb->context2 = NULL;
4164 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4165 }
4166 }
James Smart33ccf8d2006-08-17 11:57:58 -04004167
James Smart2e0fef82007-06-17 19:56:36 -05004168 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004169 /* Cleanup REG_LOGIN completions which are not yet processed */
4170 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4171 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
4172 (ndlp != (struct lpfc_nodelist *) mb->context2))
4173 continue;
4174
4175 mb->context2 = NULL;
4176 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4177 }
4178
dea31012005-04-17 16:05:31 -05004179 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004180 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004181 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004182 mp = (struct lpfc_dmabuf *) (mb->context1);
4183 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004184 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004185 kfree(mp);
4186 }
4187 list_del(&mb->list);
James Smartffc95492010-06-07 15:23:17 -04004188 if (phba->sli_rev == LPFC_SLI_REV4)
4189 lpfc_sli4_free_rpi(phba,
4190 mb->u.mb.un.varRegLogin.rpi);
dea31012005-04-17 16:05:31 -05004191 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004192 /* We shall not invoke the lpfc_nlp_put to decrement
4193 * the ndlp reference count as we are in the process
4194 * of lpfc_nlp_release.
4195 */
dea31012005-04-17 16:05:31 -05004196 }
4197 }
James Smart2e0fef82007-06-17 19:56:36 -05004198 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004199
James Smarte47c9092008-02-08 18:49:26 -05004200 lpfc_els_abort(phba, ndlp);
4201
James Smart2e0fef82007-06-17 19:56:36 -05004202 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004203 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004204 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004205
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004206 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004207 del_timer_sync(&ndlp->nlp_delayfunc);
4208
James Smart0d2b6b82008-06-14 22:52:47 -04004209 list_del_init(&ndlp->els_retry_evt.evt_listp);
4210 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea31012005-04-17 16:05:31 -05004211
James Smart2e0fef82007-06-17 19:56:36 -05004212 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004213
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004214 return 0;
dea31012005-04-17 16:05:31 -05004215}
4216
4217/*
4218 * Check to see if we can free the nlp back to the freelist.
4219 * If we are in the middle of using the nlp in the discovery state
4220 * machine, defer the free till we reach the end of the state machine.
4221 */
James Smart329f9bc2007-04-25 09:53:01 -04004222static void
James Smart2e0fef82007-06-17 19:56:36 -05004223lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004224{
James Smarta8adb832007-10-27 13:37:53 -04004225 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004226 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004227 LPFC_MBOXQ_t *mbox;
4228 int rc;
dea31012005-04-17 16:05:31 -05004229
James Smart0d2b6b82008-06-14 22:52:47 -04004230 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004231 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smartffc95492010-06-07 15:23:17 -04004232 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
James Smart21e9a0a2009-05-22 14:53:21 -04004233 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
James Smarta8adb832007-10-27 13:37:53 -04004234 /* For this case we need to cleanup the default rpi
4235 * allocated by the firmware.
4236 */
4237 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4238 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004239 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smarta8adb832007-10-27 13:37:53 -04004240 (uint8_t *) &vport->fc_sparam, mbox, 0);
4241 if (rc) {
4242 mempool_free(mbox, phba->mbox_mem_pool);
4243 }
4244 else {
4245 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4246 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4247 mbox->vport = vport;
James Smart09372822008-01-11 01:52:54 -05004248 mbox->context2 = NULL;
James Smarta8adb832007-10-27 13:37:53 -04004249 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4250 if (rc == MBX_NOT_FINISHED) {
4251 mempool_free(mbox, phba->mbox_mem_pool);
4252 }
4253 }
4254 }
4255 }
James Smart2e0fef82007-06-17 19:56:36 -05004256 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004257
James Smart2e0fef82007-06-17 19:56:36 -05004258 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004259 * We can get here with a non-NULL ndlp->rport because when we
4260 * unregister a rport we don't break the rport/node linkage. So if we
4261 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004262 */
James Smart92d7f7b2007-06-17 19:56:38 -05004263 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004264 rdata = ndlp->rport->dd_data;
4265 rdata->pnode = NULL;
4266 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004267 }
dea31012005-04-17 16:05:31 -05004268}
4269
4270static int
James Smart2e0fef82007-06-17 19:56:36 -05004271lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4272 uint32_t did)
dea31012005-04-17 16:05:31 -05004273{
James Smart2e0fef82007-06-17 19:56:36 -05004274 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004275
4276 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004277 return 0;
dea31012005-04-17 16:05:31 -05004278
dea31012005-04-17 16:05:31 -05004279 /* First check for Direct match */
4280 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004281 return 1;
dea31012005-04-17 16:05:31 -05004282
4283 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004284 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004285 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004286 return 0;
dea31012005-04-17 16:05:31 -05004287 }
4288
4289 matchdid.un.word = did;
4290 ndlpdid.un.word = ndlp->nlp_DID;
4291 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4292 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4293 (mydid.un.b.area == matchdid.un.b.area)) {
4294 if ((ndlpdid.un.b.domain == 0) &&
4295 (ndlpdid.un.b.area == 0)) {
4296 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004297 return 1;
dea31012005-04-17 16:05:31 -05004298 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004299 return 0;
dea31012005-04-17 16:05:31 -05004300 }
4301
4302 matchdid.un.word = ndlp->nlp_DID;
4303 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4304 (mydid.un.b.area == ndlpdid.un.b.area)) {
4305 if ((matchdid.un.b.domain == 0) &&
4306 (matchdid.un.b.area == 0)) {
4307 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004308 return 1;
dea31012005-04-17 16:05:31 -05004309 }
4310 }
4311 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004312 return 0;
dea31012005-04-17 16:05:31 -05004313}
4314
James Smart685f0bf2007-04-25 09:53:08 -04004315/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004316static struct lpfc_nodelist *
4317__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004318{
James Smart2fb9bd82006-12-02 13:33:57 -05004319 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004320 uint32_t data1;
4321
James Smart2e0fef82007-06-17 19:56:36 -05004322 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4323 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004324 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4325 ((uint32_t) ndlp->nlp_xri << 16) |
4326 ((uint32_t) ndlp->nlp_type << 8) |
4327 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004328 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4329 "0929 FIND node DID "
4330 "Data: x%p x%x x%x x%x\n",
4331 ndlp, ndlp->nlp_DID,
4332 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004333 return ndlp;
dea31012005-04-17 16:05:31 -05004334 }
4335 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004336
dea31012005-04-17 16:05:31 -05004337 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004338 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4339 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004340 return NULL;
4341}
4342
4343struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004344lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004345{
James Smart2e0fef82007-06-17 19:56:36 -05004346 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004347 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004348
James Smart2e0fef82007-06-17 19:56:36 -05004349 spin_lock_irq(shost->host_lock);
4350 ndlp = __lpfc_findnode_did(vport, did);
4351 spin_unlock_irq(shost->host_lock);
4352 return ndlp;
4353}
4354
4355struct lpfc_nodelist *
4356lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4357{
4358 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4359 struct lpfc_nodelist *ndlp;
4360
4361 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004362 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004363 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4364 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004365 return NULL;
4366 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004367 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004368 if (!ndlp)
4369 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004370 lpfc_nlp_init(vport, ndlp, did);
4371 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4372 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004373 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004374 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004375 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004376 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4377 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4378 if (!ndlp)
4379 return NULL;
4380 spin_lock_irq(shost->host_lock);
4381 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4382 spin_unlock_irq(shost->host_lock);
4383 return ndlp;
dea31012005-04-17 16:05:31 -05004384 }
James Smarte47c9092008-02-08 18:49:26 -05004385
James Smart58da1ff2008-04-07 10:15:56 -04004386 if ((vport->fc_flag & FC_RSCN_MODE) &&
4387 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004388 if (lpfc_rscn_payload_check(vport, did)) {
James Smart87af33f2007-10-27 13:37:43 -04004389 /* If we've already recieved a PLOGI from this NPort
4390 * we don't need to try to discover it again.
4391 */
4392 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4393 return NULL;
4394
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004395 /* Since this node is marked for discovery,
4396 * delay timeout is not needed.
4397 */
James Smart0d2b6b82008-06-14 22:52:47 -04004398 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004399 spin_lock_irq(shost->host_lock);
4400 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4401 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004402 } else
dea31012005-04-17 16:05:31 -05004403 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004404 } else {
James Smart87af33f2007-10-27 13:37:43 -04004405 /* If we've already recieved a PLOGI from this NPort,
4406 * or we are already in the process of discovery on it,
4407 * we don't need to try to discover it again.
4408 */
James Smart685f0bf2007-04-25 09:53:08 -04004409 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004410 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4411 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004412 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004413 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4414 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004415 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004416 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004417 }
4418 return ndlp;
4419}
4420
4421/* Build a list of nodes to discover based on the loopmap */
4422void
James Smart2e0fef82007-06-17 19:56:36 -05004423lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004424{
James Smart2e0fef82007-06-17 19:56:36 -05004425 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004426 int j;
4427 uint32_t alpa, index;
4428
James Smart2e0fef82007-06-17 19:56:36 -05004429 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004430 return;
James Smart2e0fef82007-06-17 19:56:36 -05004431
4432 if (phba->fc_topology != TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004433 return;
dea31012005-04-17 16:05:31 -05004434
4435 /* Check for loop map present or not */
4436 if (phba->alpa_map[0]) {
4437 for (j = 1; j <= phba->alpa_map[0]; j++) {
4438 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004439 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004440 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004441 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004442 }
4443 } else {
4444 /* No alpamap, so try all alpa's */
4445 for (j = 0; j < FC_MAXLOOP; j++) {
4446 /* If cfg_scan_down is set, start from highest
4447 * ALPA (0xef) to lowest (0x1).
4448 */
James Smart3de2a652007-08-02 11:09:59 -04004449 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004450 index = j;
4451 else
4452 index = FC_MAXLOOP - j - 1;
4453 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004454 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004455 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004456 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004457 }
4458 }
4459 return;
4460}
4461
dea31012005-04-17 16:05:31 -05004462void
James Smart2e0fef82007-06-17 19:56:36 -05004463lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004464{
dea31012005-04-17 16:05:31 -05004465 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004466 struct lpfc_sli *psli = &phba->sli;
4467 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4468 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4469 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4470 int rc;
4471
James Smart92d7f7b2007-06-17 19:56:38 -05004472 /*
4473 * if it's not a physical port or if we already send
4474 * clear_la then don't send it.
4475 */
4476 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004477 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4478 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004479 return;
4480
James Smart2e0fef82007-06-17 19:56:36 -05004481 /* Link up discovery */
4482 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4483 phba->link_state = LPFC_CLEAR_LA;
4484 lpfc_clear_la(phba, mbox);
4485 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4486 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004487 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004488 if (rc == MBX_NOT_FINISHED) {
4489 mempool_free(mbox, phba->mbox_mem_pool);
4490 lpfc_disc_flush_list(vport);
4491 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4492 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4493 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004494 phba->link_state = LPFC_HBA_ERROR;
4495 }
4496 }
4497}
4498
4499/* Reg_vpi to tell firmware to resume normal operations */
4500void
4501lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4502{
4503 LPFC_MBOXQ_t *regvpimbox;
4504
4505 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4506 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004507 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004508 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4509 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004510 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004511 == MBX_NOT_FINISHED) {
4512 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004513 }
4514 }
4515}
4516
4517/* Start Link up / RSCN discovery on NPR nodes */
4518void
4519lpfc_disc_start(struct lpfc_vport *vport)
4520{
4521 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4522 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004523 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004524 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004525 int did_changed;
dea31012005-04-17 16:05:31 -05004526
James Smart2e0fef82007-06-17 19:56:36 -05004527 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004528 return;
James Smart2e0fef82007-06-17 19:56:36 -05004529
4530 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004531 clear_la_pending = 1;
4532 else
4533 clear_la_pending = 0;
4534
James Smart2e0fef82007-06-17 19:56:36 -05004535 if (vport->port_state < LPFC_VPORT_READY)
4536 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004537
James Smart2e0fef82007-06-17 19:56:36 -05004538 lpfc_set_disctmo(vport);
4539
4540 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004541 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004542 else
dea31012005-04-17 16:05:31 -05004543 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004544
4545 vport->fc_prevDID = vport->fc_myDID;
4546 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004547
4548 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004549 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4550 "0202 Start Discovery hba state x%x "
4551 "Data: x%x x%x x%x\n",
4552 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4553 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004554
4555 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004556 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004557
4558 if (num_sent)
4559 return;
4560
James Smart92d7f7b2007-06-17 19:56:38 -05004561 /*
4562 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
4563 * continue discovery.
4564 */
4565 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004566 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004567 !(vport->fc_flag & FC_RSCN_MODE) &&
4568 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004569 lpfc_issue_reg_vpi(phba, vport);
4570 return;
4571 }
James Smart2e0fef82007-06-17 19:56:36 -05004572
James Smart92d7f7b2007-06-17 19:56:38 -05004573 /*
4574 * For SLI2, we need to set port_state to READY and continue
4575 * discovery.
4576 */
4577 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4578 /* If we get here, there is nothing to ADISC */
4579 if (vport->port_type == LPFC_PHYSICAL_PORT)
4580 lpfc_issue_clear_la(phba, vport);
4581
4582 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004583 vport->num_disc_nodes = 0;
4584 /* go thru NPR nodes and issue ELS PLOGIs */
4585 if (vport->fc_npr_cnt)
4586 lpfc_els_disc_plogi(vport);
4587
4588 if (!vport->num_disc_nodes) {
4589 spin_lock_irq(shost->host_lock);
4590 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4591 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004592 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004593 }
4594 }
James Smart92d7f7b2007-06-17 19:56:38 -05004595 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004596 } else {
4597 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004598 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05004599
4600 if (num_sent)
4601 return;
4602
James Smart2e0fef82007-06-17 19:56:36 -05004603 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05004604 /* Check to see if more RSCNs came in while we
4605 * were processing this one.
4606 */
James Smart2e0fef82007-06-17 19:56:36 -05004607 if ((vport->fc_rscn_id_cnt == 0) &&
4608 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
4609 spin_lock_irq(shost->host_lock);
4610 vport->fc_flag &= ~FC_RSCN_MODE;
4611 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004612 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004613 } else
James Smart2e0fef82007-06-17 19:56:36 -05004614 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05004615 }
4616 }
4617 return;
4618}
4619
4620/*
4621 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
4622 * ring the match the sppecified nodelist.
4623 */
4624static void
James Smart2e0fef82007-06-17 19:56:36 -05004625lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004626{
James Smart2534ba72007-04-25 09:52:20 -04004627 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004628 struct lpfc_sli *psli;
4629 IOCB_t *icmd;
4630 struct lpfc_iocbq *iocb, *next_iocb;
4631 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004632
4633 psli = &phba->sli;
4634 pring = &psli->ring[LPFC_ELS_RING];
4635
4636 /* Error matching iocb on txq or txcmplq
4637 * First check the txq.
4638 */
James Smart2e0fef82007-06-17 19:56:36 -05004639 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004640 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4641 if (iocb->context1 != ndlp) {
4642 continue;
4643 }
4644 icmd = &iocb->iocb;
4645 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
4646 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
4647
James Smart2534ba72007-04-25 09:52:20 -04004648 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05004649 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004650 }
4651 }
4652
4653 /* Next check the txcmplq */
4654 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
4655 if (iocb->context1 != ndlp) {
4656 continue;
4657 }
4658 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05004659 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
4660 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04004661 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05004662 }
4663 }
James Smart2e0fef82007-06-17 19:56:36 -05004664 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04004665
James Smarta257bf92009-04-06 18:48:10 -04004666 /* Cancel all the IOCBs from the completions list */
4667 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4668 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004669}
4670
Adrian Bunka6ababd2007-11-05 18:07:33 +01004671static void
James Smart2e0fef82007-06-17 19:56:36 -05004672lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004673{
4674 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004675 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004676
James Smart2e0fef82007-06-17 19:56:36 -05004677 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
4678 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004679 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004680 if (!NLP_CHK_NODE_ACT(ndlp))
4681 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004682 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4683 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
4684 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04004685 }
dea31012005-04-17 16:05:31 -05004686 }
4687 }
dea31012005-04-17 16:05:31 -05004688}
4689
James Smart92d7f7b2007-06-17 19:56:38 -05004690void
4691lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
4692{
4693 lpfc_els_flush_rscn(vport);
4694 lpfc_els_flush_cmd(vport);
4695 lpfc_disc_flush_list(vport);
4696}
4697
dea31012005-04-17 16:05:31 -05004698/*****************************************************************************/
4699/*
4700 * NAME: lpfc_disc_timeout
4701 *
4702 * FUNCTION: Fibre Channel driver discovery timeout routine.
4703 *
4704 * EXECUTION ENVIRONMENT: interrupt only
4705 *
4706 * CALLED FROM:
4707 * Timer function
4708 *
4709 * RETURNS:
4710 * none
4711 */
4712/*****************************************************************************/
4713void
4714lpfc_disc_timeout(unsigned long ptr)
4715{
James Smart2e0fef82007-06-17 19:56:36 -05004716 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4717 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04004718 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004719 unsigned long flags = 0;
4720
4721 if (unlikely(!phba))
4722 return;
4723
James Smart5e9d9b82008-06-14 22:52:53 -04004724 spin_lock_irqsave(&vport->work_port_lock, flags);
4725 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
4726 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05004727 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04004728 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05004729
James Smart5e9d9b82008-06-14 22:52:53 -04004730 if (!tmo_posted)
4731 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004732 return;
4733}
4734
4735static void
James Smart2e0fef82007-06-17 19:56:36 -05004736lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004737{
James Smart2e0fef82007-06-17 19:56:36 -05004738 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4739 struct lpfc_hba *phba = vport->phba;
4740 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004741 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05004742 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05004743 int rc, clrlaerr = 0;
4744
James Smart2e0fef82007-06-17 19:56:36 -05004745 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05004746 return;
4747
James Smart2e0fef82007-06-17 19:56:36 -05004748 spin_lock_irq(shost->host_lock);
4749 vport->fc_flag &= ~FC_DISC_TMO;
4750 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004751
James Smart858c9f62007-06-17 19:56:39 -05004752 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4753 "disc timeout: state:x%x rtry:x%x flg:x%x",
4754 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4755
James Smart2e0fef82007-06-17 19:56:36 -05004756 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05004757
4758 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05004759 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
4760 * FAN
4761 */
4762 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04004763 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
4764 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004765 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05004766 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04004767 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05004768 if (!NLP_CHK_NODE_ACT(ndlp))
4769 continue;
James Smart685f0bf2007-04-25 09:53:08 -04004770 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
4771 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004772 if (ndlp->nlp_type & NLP_FABRIC) {
4773 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05004774 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004775
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004776 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004777 /* Fail outstanding IO now since device
4778 * is marked for PLOGI.
4779 */
James Smart2e0fef82007-06-17 19:56:36 -05004780 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004781 }
4782 }
James Smart92d7f7b2007-06-17 19:56:38 -05004783 if (vport->port_state != LPFC_FLOGI) {
James Smart92d7f7b2007-06-17 19:56:38 -05004784 lpfc_initial_flogi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05004785 return;
James Smart92d7f7b2007-06-17 19:56:38 -05004786 }
dea31012005-04-17 16:05:31 -05004787 break;
4788
James Smart92d7f7b2007-06-17 19:56:38 -05004789 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05004790 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05004791 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05004792 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04004793 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4794 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04004795 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05004796
4797 /* Assume no Fabric and go on with discovery.
4798 * Check for outstanding ELS FLOGI to abort.
4799 */
4800
4801 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05004802 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004803
4804 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05004805 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05004806 break;
4807
4808 case LPFC_FABRIC_CFG_LINK:
4809 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
4810 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04004811 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4812 "0223 Timeout while waiting for "
4813 "NameServer login\n");
dea31012005-04-17 16:05:31 -05004814 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05004815 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05004816 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04004817 lpfc_els_abort(phba, ndlp);
4818
4819 /* ReStart discovery */
4820 goto restart_disc;
dea31012005-04-17 16:05:31 -05004821
4822 case LPFC_NS_QRY:
4823 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04004824 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4825 "0224 NameServer Query timeout "
4826 "Data: x%x x%x\n",
4827 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004828
James Smart92d7f7b2007-06-17 19:56:38 -05004829 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
4830 /* Try it one more time */
4831 vport->fc_ns_retry++;
4832 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
4833 vport->fc_ns_retry, 0);
4834 if (rc == 0)
4835 break;
dea31012005-04-17 16:05:31 -05004836 }
James Smart92d7f7b2007-06-17 19:56:38 -05004837 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05004838
James Smart87af33f2007-10-27 13:37:43 -04004839restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05004840 /*
4841 * Discovery is over.
4842 * set port_state to PORT_READY if SLI2.
4843 * cmpl_reg_vpi will set port_state to READY for SLI3.
4844 */
James Smart3772a992009-05-22 14:50:54 -04004845 if (phba->sli_rev < LPFC_SLI_REV4) {
4846 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4847 lpfc_issue_reg_vpi(phba, vport);
4848 else { /* NPIV Not enabled */
4849 lpfc_issue_clear_la(phba, vport);
4850 vport->port_state = LPFC_VPORT_READY;
4851 }
dea31012005-04-17 16:05:31 -05004852 }
4853
4854 /* Setup and issue mailbox INITIALIZE LINK command */
4855 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4856 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04004857 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4858 "0206 Device Discovery "
4859 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05004860 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004861 break;
4862 }
4863
4864 lpfc_linkdown(phba);
4865 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
4866 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04004867 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05004868 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05004869 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04004870 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04004871 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05004872 if (rc == MBX_NOT_FINISHED)
4873 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4874
4875 break;
4876
4877 case LPFC_DISC_AUTH:
4878 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04004879 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4880 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004881 lpfc_disc_flush_list(vport);
4882
James Smart92d7f7b2007-06-17 19:56:38 -05004883 /*
4884 * set port_state to PORT_READY if SLI2.
4885 * cmpl_reg_vpi will set port_state to READY for SLI3.
4886 */
James Smart3772a992009-05-22 14:50:54 -04004887 if (phba->sli_rev < LPFC_SLI_REV4) {
4888 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4889 lpfc_issue_reg_vpi(phba, vport);
4890 else { /* NPIV Not enabled */
4891 lpfc_issue_clear_la(phba, vport);
4892 vport->port_state = LPFC_VPORT_READY;
4893 }
dea31012005-04-17 16:05:31 -05004894 }
4895 break;
4896
James Smart2e0fef82007-06-17 19:56:36 -05004897 case LPFC_VPORT_READY:
4898 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04004899 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4900 "0231 RSCN timeout Data: x%x "
4901 "x%x\n",
4902 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05004903
4904 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05004905 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05004906
James Smart2e0fef82007-06-17 19:56:36 -05004907 lpfc_els_flush_rscn(vport);
4908 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05004909 }
4910 break;
James Smart2e0fef82007-06-17 19:56:36 -05004911
James Smart92d7f7b2007-06-17 19:56:38 -05004912 default:
James Smarte8b62012007-08-02 11:10:09 -04004913 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04004914 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04004915 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05004916 break;
4917 }
4918
4919 switch (phba->link_state) {
4920 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05004921 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04004922 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4923 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05004924 clrlaerr = 1;
4925 break;
4926
James Smart09372822008-01-11 01:52:54 -05004927 case LPFC_LINK_UP:
4928 lpfc_issue_clear_la(phba, vport);
4929 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05004930 case LPFC_LINK_UNKNOWN:
4931 case LPFC_WARM_START:
4932 case LPFC_INIT_START:
4933 case LPFC_INIT_MBX_CMDS:
4934 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05004935 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04004936 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4937 "0230 Unexpected timeout, hba link "
4938 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05004939 clrlaerr = 1;
4940 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004941
4942 case LPFC_HBA_READY:
4943 break;
dea31012005-04-17 16:05:31 -05004944 }
4945
4946 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05004947 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05004948 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05004949 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4950 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05004951 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05004952 }
4953
4954 return;
4955}
4956
dea31012005-04-17 16:05:31 -05004957/*
4958 * This routine handles processing a NameServer REG_LOGIN mailbox
4959 * command upon completion. It is setup in the LPFC_MBOXQ
4960 * as the completion routine when the command is
4961 * handed off to the SLI layer.
4962 */
4963void
James Smart2e0fef82007-06-17 19:56:36 -05004964lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004965{
James Smart04c68492009-05-22 14:52:52 -04004966 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05004967 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4968 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4969 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004970
4971 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04004972 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05004973
dea31012005-04-17 16:05:31 -05004974 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart21e9a0a2009-05-22 14:53:21 -04004975 ndlp->nlp_flag |= NLP_RPI_VALID;
dea31012005-04-17 16:05:31 -05004976 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004977 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004978
James Smart2e0fef82007-06-17 19:56:36 -05004979 /*
4980 * Start issuing Fabric-Device Management Interface (FDMI) command to
4981 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4982 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05004983 */
James Smart2e0fef82007-06-17 19:56:36 -05004984
James Smart3de2a652007-08-02 11:09:59 -04004985 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05004986 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4987 else
4988 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05004989
James Smartfa4066b2008-01-11 01:53:27 -05004990 /* decrement the node reference count held for this callback
4991 * function.
4992 */
James Smart329f9bc2007-04-25 09:53:01 -04004993 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004994 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4995 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04004996 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004997
4998 return;
4999}
5000
James Smart685f0bf2007-04-25 09:53:08 -04005001static int
5002lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5003{
5004 uint16_t *rpi = param;
5005
5006 return ndlp->nlp_rpi == *rpi;
5007}
5008
5009static int
5010lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5011{
5012 return memcmp(&ndlp->nlp_portname, param,
5013 sizeof(ndlp->nlp_portname)) == 0;
5014}
5015
Adrian Bunka6ababd2007-11-05 18:07:33 +01005016static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005017__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005018{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005019 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005020
James Smart2e0fef82007-06-17 19:56:36 -05005021 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart87af33f2007-10-27 13:37:43 -04005022 if (filter(ndlp, param))
James Smart685f0bf2007-04-25 09:53:08 -04005023 return ndlp;
5024 }
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005025 return NULL;
dea31012005-04-17 16:05:31 -05005026}
5027
James Smart685f0bf2007-04-25 09:53:08 -04005028/*
5029 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005030 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005031 */
5032struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005033__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005034{
James Smart2e0fef82007-06-17 19:56:36 -05005035 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005036}
5037
James Smart488d1462006-03-07 15:02:37 -05005038/*
James Smart685f0bf2007-04-25 09:53:08 -04005039 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005040 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005041 */
5042struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005043lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005044{
James Smart2e0fef82007-06-17 19:56:36 -05005045 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005046 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005047
James Smart2e0fef82007-06-17 19:56:36 -05005048 spin_lock_irq(shost->host_lock);
5049 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5050 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005051 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005052}
5053
dea31012005-04-17 16:05:31 -05005054void
James Smart2e0fef82007-06-17 19:56:36 -05005055lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5056 uint32_t did)
dea31012005-04-17 16:05:31 -05005057{
5058 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005059
5060 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005061 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05005062
5063 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5064 "node init: did:x%x",
5065 ndlp->nlp_DID, 0, 0);
5066
dea31012005-04-17 16:05:31 -05005067 return;
5068}
James Smart329f9bc2007-04-25 09:53:01 -04005069
James Smart98c9ea52007-10-27 13:37:33 -04005070/* This routine releases all resources associated with a specifc NPort's ndlp
5071 * and mempool_free's the nodelist.
5072 */
James Smart311464e2007-08-02 11:10:37 -04005073static void
James Smart329f9bc2007-04-25 09:53:01 -04005074lpfc_nlp_release(struct kref *kref)
5075{
James Smarte47c9092008-02-08 18:49:26 -05005076 struct lpfc_hba *phba;
5077 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005078 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5079 kref);
James Smart858c9f62007-06-17 19:56:39 -05005080
5081 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5082 "node release: did:x%x flg:x%x type:x%x",
5083 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5084
James Smarte47c9092008-02-08 18:49:26 -05005085 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
5086 "0279 lpfc_nlp_release: ndlp:x%p "
5087 "usgmap:x%x refcnt:%d\n",
5088 (void *)ndlp, ndlp->nlp_usg_map,
5089 atomic_read(&ndlp->kref.refcount));
5090
5091 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005092 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005093
5094 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005095 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005096 spin_lock_irqsave(&phba->ndlp_lock, flags);
5097 NLP_CLR_NODE_ACT(ndlp);
5098 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5099
5100 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005101 if (NLP_CHK_FREE_REQ(ndlp)) {
5102 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04005103 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005104 }
James Smart329f9bc2007-04-25 09:53:01 -04005105}
5106
James Smart98c9ea52007-10-27 13:37:33 -04005107/* This routine bumps the reference count for a ndlp structure to ensure
5108 * that one discovery thread won't free a ndlp while another discovery thread
5109 * is using it.
5110 */
James Smart329f9bc2007-04-25 09:53:01 -04005111struct lpfc_nodelist *
5112lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5113{
James Smarte47c9092008-02-08 18:49:26 -05005114 struct lpfc_hba *phba;
5115 unsigned long flags;
5116
James Smart98c9ea52007-10-27 13:37:33 -04005117 if (ndlp) {
5118 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5119 "node get: did:x%x flg:x%x refcnt:x%x",
5120 ndlp->nlp_DID, ndlp->nlp_flag,
5121 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005122 /* The check of ndlp usage to prevent incrementing the
5123 * ndlp reference count that is in the process of being
5124 * released.
5125 */
James Smarta257bf92009-04-06 18:48:10 -04005126 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005127 spin_lock_irqsave(&phba->ndlp_lock, flags);
5128 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5129 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5130 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5131 "0276 lpfc_nlp_get: ndlp:x%p "
5132 "usgmap:x%x refcnt:%d\n",
5133 (void *)ndlp, ndlp->nlp_usg_map,
5134 atomic_read(&ndlp->kref.refcount));
5135 return NULL;
5136 } else
5137 kref_get(&ndlp->kref);
5138 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005139 }
James Smart329f9bc2007-04-25 09:53:01 -04005140 return ndlp;
5141}
5142
James Smart98c9ea52007-10-27 13:37:33 -04005143/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005144 * count goes to 0, this indicates the the associated nodelist should be
5145 * freed. Returning 1 indicates the ndlp resource has been released; on the
5146 * other hand, returning 0 indicates the ndlp resource has not been released
5147 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005148 */
James Smart329f9bc2007-04-25 09:53:01 -04005149int
5150lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5151{
James Smarte47c9092008-02-08 18:49:26 -05005152 struct lpfc_hba *phba;
5153 unsigned long flags;
5154
5155 if (!ndlp)
5156 return 1;
5157
5158 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5159 "node put: did:x%x flg:x%x refcnt:x%x",
5160 ndlp->nlp_DID, ndlp->nlp_flag,
5161 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005162 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005163 spin_lock_irqsave(&phba->ndlp_lock, flags);
5164 /* Check the ndlp memory free acknowledge flag to avoid the
5165 * possible race condition that kref_put got invoked again
5166 * after previous one has done ndlp memory free.
5167 */
5168 if (NLP_CHK_FREE_ACK(ndlp)) {
5169 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5170 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5171 "0274 lpfc_nlp_put: ndlp:x%p "
5172 "usgmap:x%x refcnt:%d\n",
5173 (void *)ndlp, ndlp->nlp_usg_map,
5174 atomic_read(&ndlp->kref.refcount));
5175 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005176 }
James Smarte47c9092008-02-08 18:49:26 -05005177 /* Check the ndlp inactivate log flag to avoid the possible
5178 * race condition that kref_put got invoked again after ndlp
5179 * is already in inactivating state.
5180 */
5181 if (NLP_CHK_IACT_REQ(ndlp)) {
5182 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5183 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5184 "0275 lpfc_nlp_put: ndlp:x%p "
5185 "usgmap:x%x refcnt:%d\n",
5186 (void *)ndlp, ndlp->nlp_usg_map,
5187 atomic_read(&ndlp->kref.refcount));
5188 return 1;
5189 }
5190 /* For last put, mark the ndlp usage flags to make sure no
5191 * other kref_get and kref_put on the same ndlp shall get
5192 * in between the process when the final kref_put has been
5193 * invoked on this ndlp.
5194 */
5195 if (atomic_read(&ndlp->kref.refcount) == 1) {
5196 /* Indicate ndlp is put to inactive state. */
5197 NLP_SET_IACT_REQ(ndlp);
5198 /* Acknowledge ndlp memory free has been seen. */
5199 if (NLP_CHK_FREE_REQ(ndlp))
5200 NLP_SET_FREE_ACK(ndlp);
5201 }
5202 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5203 /* Note, the kref_put returns 1 when decrementing a reference
5204 * count that was 1, it invokes the release callback function,
5205 * but it still left the reference count as 1 (not actually
5206 * performs the last decrementation). Otherwise, it actually
5207 * decrements the reference count and returns 0.
5208 */
5209 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005210}
James Smart98c9ea52007-10-27 13:37:33 -04005211
5212/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005213 * by any other discovery thread. This routine returns 1 if the
5214 * ndlp has been freed. A return value of 0 indicates the ndlp is
5215 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005216 */
5217int
5218lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5219{
5220 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5221 "node not used: did:x%x flg:x%x refcnt:x%x",
5222 ndlp->nlp_DID, ndlp->nlp_flag,
5223 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005224 if (atomic_read(&ndlp->kref.refcount) == 1)
5225 if (lpfc_nlp_put(ndlp))
5226 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005227 return 0;
5228}
James Smart6fb120a2009-05-22 14:52:59 -04005229
5230/**
5231 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5232 * @phba: Pointer to hba context object.
5233 *
5234 * This function iterate through all FC nodes associated
5235 * will all vports to check if there is any node with
5236 * fc_rports associated with it. If there is an fc_rport
5237 * associated with the node, then the node is either in
5238 * discovered state or its devloss_timer is pending.
5239 */
5240static int
5241lpfc_fcf_inuse(struct lpfc_hba *phba)
5242{
5243 struct lpfc_vport **vports;
5244 int i, ret = 0;
5245 struct lpfc_nodelist *ndlp;
5246 struct Scsi_Host *shost;
5247
5248 vports = lpfc_create_vport_work_array(phba);
5249
5250 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5251 shost = lpfc_shost_from_vport(vports[i]);
5252 spin_lock_irq(shost->host_lock);
5253 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5254 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5255 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5256 ret = 1;
5257 spin_unlock_irq(shost->host_lock);
5258 goto out;
James Smart891478a2009-11-18 15:40:23 -05005259 } else {
5260 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
5261 "2624 RPI %x DID %x flg %x still "
5262 "logged in\n",
5263 ndlp->nlp_rpi, ndlp->nlp_DID,
5264 ndlp->nlp_flag);
5265 if (ndlp->nlp_flag & NLP_RPI_VALID)
5266 ret = 1;
James Smart6fb120a2009-05-22 14:52:59 -04005267 }
5268 }
5269 spin_unlock_irq(shost->host_lock);
5270 }
5271out:
5272 lpfc_destroy_vport_work_array(phba, vports);
5273 return ret;
5274}
5275
5276/**
5277 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5278 * @phba: Pointer to hba context object.
5279 * @mboxq: Pointer to mailbox object.
5280 *
5281 * This function frees memory associated with the mailbox command.
5282 */
5283static void
5284lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5285{
5286 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005287 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005288
5289 if (mboxq->u.mb.mbxStatus) {
5290 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5291 "2555 UNREG_VFI mbxStatus error x%x "
5292 "HBA state x%x\n",
5293 mboxq->u.mb.mbxStatus, vport->port_state);
5294 }
James Smart38b92ef2010-08-04 16:11:39 -04005295 spin_lock_irq(shost->host_lock);
5296 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5297 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005298 mempool_free(mboxq, phba->mbox_mem_pool);
5299 return;
5300}
5301
5302/**
5303 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5304 * @phba: Pointer to hba context object.
5305 * @mboxq: Pointer to mailbox object.
5306 *
5307 * This function frees memory associated with the mailbox command.
5308 */
5309static void
5310lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5311{
5312 struct lpfc_vport *vport = mboxq->vport;
5313
5314 if (mboxq->u.mb.mbxStatus) {
5315 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5316 "2550 UNREG_FCFI mbxStatus error x%x "
5317 "HBA state x%x\n",
5318 mboxq->u.mb.mbxStatus, vport->port_state);
5319 }
5320 mempool_free(mboxq, phba->mbox_mem_pool);
5321 return;
5322}
5323
5324/**
James Smartecfd03c2010-02-12 14:41:27 -05005325 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005326 * @phba: Pointer to hba context object.
5327 *
James Smartecfd03c2010-02-12 14:41:27 -05005328 * This function prepare the HBA for unregistering the currently registered
5329 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5330 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005331 */
James Smartecfd03c2010-02-12 14:41:27 -05005332int
5333lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005334{
5335 LPFC_MBOXQ_t *mbox;
James Smart6fb120a2009-05-22 14:52:59 -04005336 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005337 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005338 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005339 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005340
James Smartecfd03c2010-02-12 14:41:27 -05005341 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005342 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005343 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005344
James Smart4d9ab992009-10-02 15:16:39 -04005345 /* At this point, all discovery is aborted */
5346 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005347
5348 /* Unregister VPIs */
5349 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005350 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005351 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005352 /* Stop FLOGI/FDISC retries */
5353 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5354 if (ndlp)
5355 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005356 lpfc_cleanup_pending_mbox(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005357 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005358 shost = lpfc_shost_from_vport(vports[i]);
5359 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005360 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005361 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005362 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005363 }
5364 lpfc_destroy_vport_work_array(phba, vports);
5365
James Smart695a8142010-01-26 23:08:03 -05005366 /* Cleanup any outstanding ELS commands */
5367 lpfc_els_flush_all_cmd(phba);
5368
James Smart6fb120a2009-05-22 14:52:59 -04005369 /* Unregister VFI */
5370 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5371 if (!mbox) {
5372 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005373 "2556 UNREG_VFI mbox allocation failed"
5374 "HBA state x%x\n", phba->pport->port_state);
5375 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005376 }
5377
James Smart6669f9b2009-10-02 15:16:45 -04005378 lpfc_unreg_vfi(mbox, phba->pport);
James Smart6fb120a2009-05-22 14:52:59 -04005379 mbox->vport = phba->pport;
5380 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
5381
5382 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5383 if (rc == MBX_NOT_FINISHED) {
5384 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005385 "2557 UNREG_VFI issue mbox failed rc x%x "
5386 "HBA state x%x\n",
5387 rc, phba->pport->port_state);
James Smart6fb120a2009-05-22 14:52:59 -04005388 mempool_free(mbox, phba->mbox_mem_pool);
James Smartecfd03c2010-02-12 14:41:27 -05005389 return -EIO;
James Smart6fb120a2009-05-22 14:52:59 -04005390 }
5391
James Smart72100cc2010-02-12 14:43:01 -05005392 shost = lpfc_shost_from_vport(phba->pport);
5393 spin_lock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005394 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005395 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05005396
James Smartecfd03c2010-02-12 14:41:27 -05005397 return 0;
5398}
5399
5400/**
5401 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5402 * @phba: Pointer to hba context object.
5403 *
5404 * This function issues synchronous unregister FCF mailbox command to HBA to
5405 * unregister the currently registered FCF record. The driver does not reset
5406 * the driver FCF usage state flags.
5407 *
5408 * Return 0 if successfully issued, none-zero otherwise.
5409 */
5410int
5411lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5412{
5413 LPFC_MBOXQ_t *mbox;
5414 int rc;
5415
James Smart6fb120a2009-05-22 14:52:59 -04005416 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5417 if (!mbox) {
5418 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005419 "2551 UNREG_FCFI mbox allocation failed"
5420 "HBA state x%x\n", phba->pport->port_state);
5421 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005422 }
James Smart6fb120a2009-05-22 14:52:59 -04005423 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5424 mbox->vport = phba->pport;
5425 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5426 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5427
5428 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5430 "2552 Unregister FCFI command failed rc x%x "
5431 "HBA state x%x\n",
5432 rc, phba->pport->port_state);
5433 return -EINVAL;
5434 }
5435 return 0;
5436}
5437
5438/**
5439 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5440 * @phba: Pointer to hba context object.
5441 *
5442 * This function unregisters the currently reigstered FCF. This function
5443 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5444 */
5445void
5446lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5447{
5448 int rc;
5449
5450 /* Preparation for unregistering fcf */
5451 rc = lpfc_unregister_fcf_prep(phba);
5452 if (rc) {
5453 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5454 "2748 Failed to prepare for unregistering "
5455 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005456 return;
5457 }
5458
James Smartecfd03c2010-02-12 14:41:27 -05005459 /* Now, unregister FCF record and reset HBA FCF state */
5460 rc = lpfc_sli4_unregister_fcf(phba);
5461 if (rc)
5462 return;
5463 /* Reset HBA FCF states after successful unregister FCF */
5464 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005465 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005466
5467 /*
5468 * If driver is not unloading, check if there is any other
5469 * FCF record that can be used for discovery.
5470 */
5471 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005472 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005473 return;
5474
James Smart0c9ab6f2010-02-26 14:15:57 -05005475 /* This is considered as the initial FCF discovery scan */
5476 spin_lock_irq(&phba->hbalock);
5477 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5478 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005479
5480 /* Reset FCF roundrobin bmask for new discovery */
5481 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
5482
James Smart0c9ab6f2010-02-26 14:15:57 -05005483 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005484
James Smart0c9ab6f2010-02-26 14:15:57 -05005485 if (rc) {
5486 spin_lock_irq(&phba->hbalock);
5487 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5488 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005489 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005490 "2553 lpfc_unregister_unused_fcf failed "
5491 "to read FCF record HBA state x%x\n",
5492 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005493 }
James Smartecfd03c2010-02-12 14:41:27 -05005494}
5495
5496/**
5497 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5498 * @phba: Pointer to hba context object.
5499 *
5500 * This function just unregisters the currently reigstered FCF. It does not
5501 * try to find another FCF for discovery.
5502 */
5503void
5504lpfc_unregister_fcf(struct lpfc_hba *phba)
5505{
5506 int rc;
5507
5508 /* Preparation for unregistering fcf */
5509 rc = lpfc_unregister_fcf_prep(phba);
5510 if (rc) {
5511 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5512 "2749 Failed to prepare for unregistering "
5513 "HBA's FCF record: rc=%d\n", rc);
5514 return;
5515 }
5516
5517 /* Now, unregister FCF record and reset HBA FCF state */
5518 rc = lpfc_sli4_unregister_fcf(phba);
5519 if (rc)
5520 return;
5521 /* Set proper HBA FCF states after successful unregister FCF */
5522 spin_lock_irq(&phba->hbalock);
5523 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
5524 spin_unlock_irq(&phba->hbalock);
5525}
5526
5527/**
5528 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
5529 * @phba: Pointer to hba context object.
5530 *
5531 * This function check if there are any connected remote port for the FCF and
5532 * if all the devices are disconnected, this function unregister FCFI.
5533 * This function also tries to use another FCF for discovery.
5534 */
5535void
5536lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
5537{
5538 /*
James Smart3804dc82010-07-14 15:31:37 -04005539 * If HBA is not running in FIP mode, if HBA does not support
5540 * FCoE, if FCF discovery is ongoing, or if FCF has not been
5541 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05005542 */
5543 spin_lock_irq(&phba->hbalock);
5544 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
5545 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04005546 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04005547 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04005548 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05005549 spin_unlock_irq(&phba->hbalock);
5550 return;
5551 }
5552 spin_unlock_irq(&phba->hbalock);
5553
5554 if (lpfc_fcf_inuse(phba))
5555 return;
5556
5557 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005558}
5559
5560/**
5561 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
5562 * @phba: Pointer to hba context object.
5563 * @buff: Buffer containing the FCF connection table as in the config
5564 * region.
5565 * This function create driver data structure for the FCF connection
5566 * record table read from config region 23.
5567 */
5568static void
5569lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
5570 uint8_t *buff)
5571{
5572 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
5573 struct lpfc_fcf_conn_hdr *conn_hdr;
5574 struct lpfc_fcf_conn_rec *conn_rec;
5575 uint32_t record_count;
5576 int i;
5577
5578 /* Free the current connect table */
5579 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04005580 &phba->fcf_conn_rec_list, list) {
5581 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04005582 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04005583 }
James Smart6fb120a2009-05-22 14:52:59 -04005584
5585 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
5586 record_count = conn_hdr->length * sizeof(uint32_t)/
5587 sizeof(struct lpfc_fcf_conn_rec);
5588
5589 conn_rec = (struct lpfc_fcf_conn_rec *)
5590 (buff + sizeof(struct lpfc_fcf_conn_hdr));
5591
5592 for (i = 0; i < record_count; i++) {
5593 if (!(conn_rec[i].flags & FCFCNCT_VALID))
5594 continue;
5595 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
5596 GFP_KERNEL);
5597 if (!conn_entry) {
5598 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5599 "2566 Failed to allocate connection"
5600 " table entry\n");
5601 return;
5602 }
5603
5604 memcpy(&conn_entry->conn_rec, &conn_rec[i],
5605 sizeof(struct lpfc_fcf_conn_rec));
5606 conn_entry->conn_rec.vlan_tag =
5607 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
5608 conn_entry->conn_rec.flags =
5609 le16_to_cpu(conn_entry->conn_rec.flags);
5610 list_add_tail(&conn_entry->list,
5611 &phba->fcf_conn_rec_list);
5612 }
5613}
5614
5615/**
5616 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
5617 * @phba: Pointer to hba context object.
5618 * @buff: Buffer containing the FCoE parameter data structure.
5619 *
5620 * This function update driver data structure with config
5621 * parameters read from config region 23.
5622 */
5623static void
5624lpfc_read_fcoe_param(struct lpfc_hba *phba,
5625 uint8_t *buff)
5626{
5627 struct lpfc_fip_param_hdr *fcoe_param_hdr;
5628 struct lpfc_fcoe_params *fcoe_param;
5629
5630 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
5631 buff;
5632 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04005633 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04005634
5635 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
5636 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
5637 return;
5638
James Smart6fb120a2009-05-22 14:52:59 -04005639 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
5640 phba->valid_vlan = 1;
5641 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
5642 0xFFF;
5643 }
5644
5645 phba->fc_map[0] = fcoe_param->fc_map[0];
5646 phba->fc_map[1] = fcoe_param->fc_map[1];
5647 phba->fc_map[2] = fcoe_param->fc_map[2];
5648 return;
5649}
5650
5651/**
5652 * lpfc_get_rec_conf23 - Get a record type in config region data.
5653 * @buff: Buffer containing config region 23 data.
5654 * @size: Size of the data buffer.
5655 * @rec_type: Record type to be searched.
5656 *
5657 * This function searches config region data to find the begining
5658 * of the record specified by record_type. If record found, this
5659 * function return pointer to the record else return NULL.
5660 */
5661static uint8_t *
5662lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
5663{
5664 uint32_t offset = 0, rec_length;
5665
5666 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
5667 (size < sizeof(uint32_t)))
5668 return NULL;
5669
5670 rec_length = buff[offset + 1];
5671
5672 /*
5673 * One TLV record has one word header and number of data words
5674 * specified in the rec_length field of the record header.
5675 */
5676 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
5677 <= size) {
5678 if (buff[offset] == rec_type)
5679 return &buff[offset];
5680
5681 if (buff[offset] == LPFC_REGION23_LAST_REC)
5682 return NULL;
5683
5684 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
5685 rec_length = buff[offset + 1];
5686 }
5687 return NULL;
5688}
5689
5690/**
5691 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
5692 * @phba: Pointer to lpfc_hba data structure.
5693 * @buff: Buffer containing config region 23 data.
5694 * @size: Size of the data buffer.
5695 *
Stefan Weileef35c22010-08-06 21:11:15 +02005696 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04005697 * populate driver data structure with the parameters.
5698 */
5699void
5700lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
5701 uint8_t *buff,
5702 uint32_t size)
5703{
5704 uint32_t offset = 0, rec_length;
5705 uint8_t *rec_ptr;
5706
5707 /*
5708 * If data size is less than 2 words signature and version cannot be
5709 * verified.
5710 */
5711 if (size < 2*sizeof(uint32_t))
5712 return;
5713
5714 /* Check the region signature first */
5715 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
5716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5717 "2567 Config region 23 has bad signature\n");
5718 return;
5719 }
5720
5721 offset += 4;
5722
5723 /* Check the data structure version */
5724 if (buff[offset] != LPFC_REGION23_VERSION) {
5725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5726 "2568 Config region 23 has bad version\n");
5727 return;
5728 }
5729 offset += 4;
5730
5731 rec_length = buff[offset + 1];
5732
5733 /* Read FCoE param record */
5734 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5735 size - offset, FCOE_PARAM_TYPE);
5736 if (rec_ptr)
5737 lpfc_read_fcoe_param(phba, rec_ptr);
5738
5739 /* Read FCF connection table */
5740 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
5741 size - offset, FCOE_CONN_TBL_TYPE);
5742 if (rec_ptr)
5743 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
5744
5745}