blob: e9845d2ecf10db6f6fd9eca8681aba1febee305a [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd4379ac2012-03-01 22:37:07 -05004 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
James Smarta93ff372010-10-22 11:06:08 -040023#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050025#include <linux/pci.h>
26#include <linux/kthread.h>
27#include <linux/interrupt.h>
28
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport_fc.h>
33
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040036#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050037#include "lpfc_disc.h"
38#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050044#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050045#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050046
47/* AlpaArray for assignment of scsid for scan-down and bind_method */
48static uint8_t lpfcAlpaArray[] = {
49 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
50 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
51 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
52 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
53 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
54 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
55 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
56 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
57 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
58 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
59 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
60 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
61 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
62};
63
James Smart2e0fef82007-06-17 19:56:36 -050064static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010065static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040066static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
James Smarta93ff372010-10-22 11:06:08 -040067static int lpfc_fcf_inuse(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050068
James Smartc01f3202006-08-18 17:47:08 -040069void
70lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050071{
James Smartc01f3202006-08-18 17:47:08 -040072 struct lpfc_rport_data *rdata;
73 struct lpfc_nodelist * ndlp;
74 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050075
James Smartc01f3202006-08-18 17:47:08 -040076 rdata = rport->dd_data;
77 ndlp = rdata->pnode;
78
James Smart58da1ff2008-04-07 10:15:56 -040079 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040080 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
81 printk(KERN_ERR "Cannot find remote node"
82 " to terminate I/O Data x%x\n",
83 rport->port_id);
dea31012005-04-17 16:05:31 -050084 return;
85 }
86
James Smarta257bf92009-04-06 18:48:10 -040087 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050088
James Smart858c9f62007-06-17 19:56:39 -050089 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
90 "rport terminate: sid:x%x did:x%x flg:x%x",
91 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
92
James Smartc01f3202006-08-18 17:47:08 -040093 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040094 lpfc_sli_abort_iocb(ndlp->vport,
95 &phba->sli.ring[phba->sli.fcp_ring],
96 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -040097 }
James Smartc01f3202006-08-18 17:47:08 -040098}
99
100/*
101 * This function will be called when dev_loss_tmo fire.
102 */
103void
104lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
105{
106 struct lpfc_rport_data *rdata;
107 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500108 struct lpfc_vport *vport;
James Smart858c9f62007-06-17 19:56:39 -0500109 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500110 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400111 int put_node;
112 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400113
114 rdata = rport->dd_data;
115 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400116 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400117 return;
James Smartc01f3202006-08-18 17:47:08 -0400118
James Smart858c9f62007-06-17 19:56:39 -0500119 vport = ndlp->vport;
120 phba = vport->phba;
121
122 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
123 "rport devlosscb: sid:x%x did:x%x flg:x%x",
124 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
125
James Smart34f5ad82012-08-03 12:35:03 -0400126 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
127 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
128 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
129
James Smarta8adb832007-10-27 13:37:53 -0400130 /* Don't defer this if we are in the process of deleting the vport
131 * or unloading the driver. The unload will cleanup the node
132 * appropriately we just need to cleanup the ndlp rport info here.
133 */
134 if (vport->load_flag & FC_UNLOADING) {
135 put_node = rdata->pnode != NULL;
136 put_rport = ndlp->rport != NULL;
137 rdata->pnode = NULL;
138 ndlp->rport = NULL;
139 if (put_node)
140 lpfc_nlp_put(ndlp);
141 if (put_rport)
142 put_device(&rport->dev);
143 return;
144 }
145
146 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
147 return;
148
James Smart34f5ad82012-08-03 12:35:03 -0400149 if (ndlp->nlp_type & NLP_FABRIC) {
150
151 /* If the WWPN of the rport and ndlp don't match, ignore it */
152 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn)) {
153 put_device(&rport->dev);
154 return;
155 }
156 }
157
James Smart858c9f62007-06-17 19:56:39 -0500158 evtp = &ndlp->dev_loss_evt;
159
160 if (!list_empty(&evtp->evt_listp))
161 return;
162
163 spin_lock_irq(&phba->hbalock);
James Smartfa4066b2008-01-11 01:53:27 -0500164 /* We need to hold the node by incrementing the reference
165 * count until this queued work is done
166 */
167 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
James Smart5e9d9b82008-06-14 22:52:53 -0400168 if (evtp->evt_arg1) {
169 evtp->evt = LPFC_EVT_DEV_LOSS;
170 list_add_tail(&evtp->evt_listp, &phba->work_list);
171 lpfc_worker_wake_up(phba);
172 }
James Smart858c9f62007-06-17 19:56:39 -0500173 spin_unlock_irq(&phba->hbalock);
174
James Smart858c9f62007-06-17 19:56:39 -0500175 return;
176}
177
James Smarta93ff372010-10-22 11:06:08 -0400178/**
179 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
180 * @ndlp: Pointer to remote node object.
181 *
182 * This function is called from the worker thread when devloss timeout timer
183 * expires. For SLI4 host, this routine shall return 1 when at lease one
184 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
185 * routine shall return 0 when there is no remote node is still in use of FCF
186 * when devloss timeout happened to this @ndlp.
187 **/
188static int
James Smart858c9f62007-06-17 19:56:39 -0500189lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
190{
191 struct lpfc_rport_data *rdata;
192 struct fc_rport *rport;
193 struct lpfc_vport *vport;
194 struct lpfc_hba *phba;
195 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400196 int put_node;
197 int put_rport;
James Smart858c9f62007-06-17 19:56:39 -0500198 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400199 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500200
201 rport = ndlp->rport;
202
203 if (!rport)
James Smarta93ff372010-10-22 11:06:08 -0400204 return fcf_inuse;
James Smart858c9f62007-06-17 19:56:39 -0500205
206 rdata = rport->dd_data;
207 name = (uint8_t *) &ndlp->nlp_portname;
208 vport = ndlp->vport;
209 phba = vport->phba;
210
James Smarta93ff372010-10-22 11:06:08 -0400211 if (phba->sli_rev == LPFC_SLI_REV4)
212 fcf_inuse = lpfc_fcf_inuse(phba);
213
James Smart858c9f62007-06-17 19:56:39 -0500214 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
215 "rport devlosstmo:did:x%x type:x%x id:x%x",
216 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
217
James Smart34f5ad82012-08-03 12:35:03 -0400218 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
219 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
220 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
221
James Smarta8adb832007-10-27 13:37:53 -0400222 /* Don't defer this if we are in the process of deleting the vport
223 * or unloading the driver. The unload will cleanup the node
224 * appropriately we just need to cleanup the ndlp rport info here.
225 */
226 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500227 if (ndlp->nlp_sid != NLP_NO_SID) {
228 /* flush the target */
229 lpfc_sli_abort_iocb(vport,
230 &phba->sli.ring[phba->sli.fcp_ring],
231 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
232 }
James Smarta8adb832007-10-27 13:37:53 -0400233 put_node = rdata->pnode != NULL;
234 put_rport = ndlp->rport != NULL;
235 rdata->pnode = NULL;
236 ndlp->rport = NULL;
237 if (put_node)
238 lpfc_nlp_put(ndlp);
239 if (put_rport)
240 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400241 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400242 }
243
James Smartd7c255b2008-08-24 21:50:00 -0400244 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
245 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
246 "0284 Devloss timeout Ignored on "
247 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
248 "NPort x%x\n",
249 *name, *(name+1), *(name+2), *(name+3),
250 *(name+4), *(name+5), *(name+6), *(name+7),
251 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400252 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400253 }
James Smart858c9f62007-06-17 19:56:39 -0500254
James Smart92d7f7b2007-06-17 19:56:38 -0500255 if (ndlp->nlp_type & NLP_FABRIC) {
256 /* We will clean up these Nodes in linkup */
257 put_node = rdata->pnode != NULL;
258 put_rport = ndlp->rport != NULL;
259 rdata->pnode = NULL;
260 ndlp->rport = NULL;
261 if (put_node)
262 lpfc_nlp_put(ndlp);
263 if (put_rport)
264 put_device(&rport->dev);
James Smarta93ff372010-10-22 11:06:08 -0400265 return fcf_inuse;
James Smart92d7f7b2007-06-17 19:56:38 -0500266 }
James Smart82085712007-04-25 09:52:41 -0400267
dea31012005-04-17 16:05:31 -0500268 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400269 warn_on = 1;
dea31012005-04-17 16:05:31 -0500270 /* flush the target */
James Smart51ef4c22007-08-02 11:10:31 -0400271 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
272 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500273 }
James Smartc01f3202006-08-18 17:47:08 -0400274
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400275 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400276 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
277 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400278 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
279 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400280 *name, *(name+1), *(name+2), *(name+3),
281 *(name+4), *(name+5), *(name+6), *(name+7),
282 ndlp->nlp_DID, ndlp->nlp_flag,
283 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400284 } else {
James Smarte8b62012007-08-02 11:10:09 -0400285 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
286 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400287 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
288 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400289 *name, *(name+1), *(name+2), *(name+3),
290 *(name+4), *(name+5), *(name+6), *(name+7),
291 ndlp->nlp_DID, ndlp->nlp_flag,
292 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400293 }
294
James Smart87af33f2007-10-27 13:37:43 -0400295 put_node = rdata->pnode != NULL;
296 put_rport = ndlp->rport != NULL;
297 rdata->pnode = NULL;
298 ndlp->rport = NULL;
299 if (put_node)
300 lpfc_nlp_put(ndlp);
301 if (put_rport)
302 put_device(&rport->dev);
303
James Smart2e0fef82007-06-17 19:56:36 -0500304 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400305 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400306 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400307 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400308 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
309 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500310 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400311
James Smarta93ff372010-10-22 11:06:08 -0400312 return fcf_inuse;
313}
314
315/**
316 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
317 * @phba: Pointer to hba context object.
318 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
319 * @nlp_did: remote node identifer with devloss timeout.
320 *
321 * This function is called from the worker thread after invoking devloss
322 * timeout handler and releasing the reference count for the ndlp with
323 * which the devloss timeout was handled for SLI4 host. For the devloss
324 * timeout of the last remote node which had been in use of FCF, when this
325 * routine is invoked, it shall be guaranteed that none of the remote are
326 * in-use of FCF. When devloss timeout to the last remote using the FCF,
327 * if the FIP engine is neither in FCF table scan process nor roundrobin
328 * failover process, the in-use FCF shall be unregistered. If the FIP
329 * engine is in FCF discovery process, the devloss timeout state shall
330 * be set for either the FCF table scan process or roundrobin failover
331 * process to unregister the in-use FCF.
332 **/
333static void
334lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
335 uint32_t nlp_did)
336{
337 /* If devloss timeout happened to a remote node when FCF had no
338 * longer been in-use, do nothing.
339 */
340 if (!fcf_inuse)
341 return;
342
343 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
344 spin_lock_irq(&phba->hbalock);
345 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
346 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
347 spin_unlock_irq(&phba->hbalock);
348 return;
349 }
350 phba->hba_flag |= HBA_DEVLOSS_TMO;
351 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
352 "2847 Last remote node (x%x) using "
353 "FCF devloss tmo\n", nlp_did);
354 }
355 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
356 spin_unlock_irq(&phba->hbalock);
357 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
358 "2868 Devloss tmo to FCF rediscovery "
359 "in progress\n");
360 return;
361 }
362 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
363 spin_unlock_irq(&phba->hbalock);
364 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
365 "2869 Devloss tmo to idle FIP engine, "
366 "unreg in-use FCF and rescan.\n");
367 /* Unregister in-use FCF and rescan */
368 lpfc_unregister_fcf_rescan(phba);
369 return;
370 }
371 spin_unlock_irq(&phba->hbalock);
372 if (phba->hba_flag & FCF_TS_INPROG)
373 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
374 "2870 FCF table scan in progress\n");
375 if (phba->hba_flag & FCF_RR_INPROG)
376 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
377 "2871 FLOGI roundrobin FCF failover "
378 "in progress\n");
379 }
James Smart6fb120a2009-05-22 14:52:59 -0400380 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500381}
James Smartc01f3202006-08-18 17:47:08 -0400382
James Smartea2151b2008-09-07 11:52:10 -0400383/**
James Smart3621a712009-04-06 18:47:14 -0400384 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400385 * @phba: Pointer to hba context object.
386 *
387 * This function is called from the functions which need to post
388 * events from interrupt context. This function allocates data
389 * structure required for posting event. It also keeps track of
390 * number of events pending and prevent event storm when there are
391 * too many events.
392 **/
393struct lpfc_fast_path_event *
394lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
395 struct lpfc_fast_path_event *ret;
396
397 /* If there are lot of fast event do not exhaust memory due to this */
398 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
399 return NULL;
400
401 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
402 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400403 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400404 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400405 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
406 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
407 }
James Smartea2151b2008-09-07 11:52:10 -0400408 return ret;
409}
410
411/**
James Smart3621a712009-04-06 18:47:14 -0400412 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400413 * @phba: Pointer to hba context object.
414 * @evt: Event object which need to be freed.
415 *
416 * This function frees the data structure required for posting
417 * events.
418 **/
419void
420lpfc_free_fast_evt(struct lpfc_hba *phba,
421 struct lpfc_fast_path_event *evt) {
422
423 atomic_dec(&phba->fast_event_count);
424 kfree(evt);
425}
426
427/**
James Smart3621a712009-04-06 18:47:14 -0400428 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400429 * @phba: Pointer to hba context object.
430 * @evtp: Event data structure.
431 *
432 * This function is called from worker thread, when the interrupt
433 * context need to post an event. This function posts the event
434 * to fc transport netlink interface.
435 **/
436static void
437lpfc_send_fastpath_evt(struct lpfc_hba *phba,
438 struct lpfc_work_evt *evtp)
439{
440 unsigned long evt_category, evt_sub_category;
441 struct lpfc_fast_path_event *fast_evt_data;
442 char *evt_data;
443 uint32_t evt_data_size;
444 struct Scsi_Host *shost;
445
446 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
447 work_evt);
448
449 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
450 evt_sub_category = (unsigned long) fast_evt_data->un.
451 fabric_evt.subcategory;
452 shost = lpfc_shost_from_vport(fast_evt_data->vport);
453 if (evt_category == FC_REG_FABRIC_EVENT) {
454 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
455 evt_data = (char *) &fast_evt_data->un.read_check_error;
456 evt_data_size = sizeof(fast_evt_data->un.
457 read_check_error);
458 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500459 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400460 evt_data = (char *) &fast_evt_data->un.fabric_evt;
461 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
462 } else {
463 lpfc_free_fast_evt(phba, fast_evt_data);
464 return;
465 }
466 } else if (evt_category == FC_REG_SCSI_EVENT) {
467 switch (evt_sub_category) {
468 case LPFC_EVENT_QFULL:
469 case LPFC_EVENT_DEVBSY:
470 evt_data = (char *) &fast_evt_data->un.scsi_evt;
471 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
472 break;
473 case LPFC_EVENT_CHECK_COND:
474 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
475 evt_data_size = sizeof(fast_evt_data->un.
476 check_cond_evt);
477 break;
478 case LPFC_EVENT_VARQUEDEPTH:
479 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
480 evt_data_size = sizeof(fast_evt_data->un.
481 queue_depth_evt);
482 break;
483 default:
484 lpfc_free_fast_evt(phba, fast_evt_data);
485 return;
486 }
487 } else {
488 lpfc_free_fast_evt(phba, fast_evt_data);
489 return;
490 }
491
492 fc_host_post_vendor_event(shost,
493 fc_get_event_number(),
494 evt_data_size,
495 evt_data,
James Smartddcc50f2008-12-04 22:38:46 -0500496 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400497
498 lpfc_free_fast_evt(phba, fast_evt_data);
499 return;
500}
501
dea31012005-04-17 16:05:31 -0500502static void
James Smart2e0fef82007-06-17 19:56:36 -0500503lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500504{
505 struct lpfc_work_evt *evtp = NULL;
506 struct lpfc_nodelist *ndlp;
507 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400508 int fcf_inuse;
509 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500510
James Smart2e0fef82007-06-17 19:56:36 -0500511 spin_lock_irq(&phba->hbalock);
512 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500513 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
514 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500515 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500516 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500517 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500518 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500519 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500520 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500521 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500522 /* decrement the node reference count held
523 * for this queued work
524 */
525 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500526 break;
James Smart858c9f62007-06-17 19:56:39 -0500527 case LPFC_EVT_DEV_LOSS:
528 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400529 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500530 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500531 /* decrement the node reference count held for
532 * this queued work
533 */
James Smarta93ff372010-10-22 11:06:08 -0400534 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500535 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400536 if (phba->sli_rev == LPFC_SLI_REV4)
537 lpfc_sli4_post_dev_loss_tmo_handler(phba,
538 fcf_inuse,
539 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500540 break;
dea31012005-04-17 16:05:31 -0500541 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500542 if (phba->link_state < LPFC_LINK_DOWN)
543 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500544 else
James Smart2e0fef82007-06-17 19:56:36 -0500545 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500546 complete((struct completion *)(evtp->evt_arg2));
547 break;
James Smart46fa3112007-04-25 09:51:45 -0400548 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500549 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400550 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400551 *(int *)(evtp->evt_arg1) = 0;
552 complete((struct completion *)(evtp->evt_arg2));
553 break;
554 case LPFC_EVT_OFFLINE:
555 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500556 lpfc_sli_brdrestart(phba);
557 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400558 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
559 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500560 complete((struct completion *)(evtp->evt_arg2));
561 break;
562 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400563 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500564 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500565 lpfc_sli_brdreset(phba);
566 lpfc_hba_down_post(phba);
567 *(int *)(evtp->evt_arg1) =
568 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400569 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500570 complete((struct completion *)(evtp->evt_arg2));
571 break;
572 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400573 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500574 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500575 = (phba->pport->stopped)
576 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400577 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500578 complete((struct completion *)(evtp->evt_arg2));
579 break;
James Smartea2151b2008-09-07 11:52:10 -0400580 case LPFC_EVT_FASTPATH_MGMT_EVT:
581 lpfc_send_fastpath_evt(phba, evtp);
582 free_evt = 0;
583 break;
James Smart78730cf2010-04-06 15:06:30 -0400584 case LPFC_EVT_RESET_HBA:
585 if (!(phba->pport->load_flag & FC_UNLOADING))
586 lpfc_reset_hba(phba);
587 break;
dea31012005-04-17 16:05:31 -0500588 }
589 if (free_evt)
590 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500591 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500592 }
James Smart2e0fef82007-06-17 19:56:36 -0500593 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500594
595}
596
James Smart311464e2007-08-02 11:10:37 -0400597static void
James Smart2e0fef82007-06-17 19:56:36 -0500598lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500599{
600 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500601 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400602 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400603 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400604 int i;
dea31012005-04-17 16:05:31 -0500605
James Smart2e0fef82007-06-17 19:56:36 -0500606 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500607 ha_copy = phba->work_ha;
608 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500609 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500610
James Smartda0436e2009-05-22 14:51:39 -0400611 /* First, try to post the next mailbox command to SLI4 device */
612 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
613 lpfc_sli4_post_async_mbox(phba);
614
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500615 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400616 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500617 lpfc_handle_eratt(phba);
618
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500619 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500620 lpfc_sli_handle_mb_event(phba);
621
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500622 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500623 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400624
James Smartda0436e2009-05-22 14:51:39 -0400625 /* Process SLI4 events */
626 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500627 if (phba->hba_flag & HBA_RRQ_ACTIVE)
628 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400629 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
630 lpfc_sli4_fcp_xri_abort_event_proc(phba);
631 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
632 lpfc_sli4_els_xri_abort_event_proc(phba);
633 if (phba->hba_flag & ASYNC_EVENT)
634 lpfc_sli4_async_event_proc(phba);
635 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
636 spin_lock_irq(&phba->hbalock);
637 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
638 spin_unlock_irq(&phba->hbalock);
639 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
640 }
James Smartecfd03c2010-02-12 14:41:27 -0500641 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
642 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400643 }
644
James Smart549e55c2007-08-02 11:09:51 -0400645 vports = lpfc_create_vport_work_array(phba);
646 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400647 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400648 /*
649 * We could have no vports in array if unloading, so if
650 * this happens then just use the pport
651 */
652 if (vports[i] == NULL && i == 0)
653 vport = phba->pport;
654 else
655 vport = vports[i];
656 if (vport == NULL)
657 break;
James Smart58da1ff2008-04-07 10:15:56 -0400658 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400659 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400660 vport->work_port_events &= ~work_port_events;
661 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400662 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400663 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400664 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400665 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400666 if (work_port_events & WORKER_HB_TMO)
667 lpfc_hb_timeout_handler(phba);
668 if (work_port_events & WORKER_MBOX_TMO)
669 lpfc_mbox_timeout_handler(phba);
670 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
671 lpfc_unblock_fabric_iocbs(phba);
672 if (work_port_events & WORKER_FDMI_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400673 lpfc_fdmi_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400674 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
675 lpfc_ramp_down_queue_handler(phba);
676 if (work_port_events & WORKER_RAMP_UP_QUEUE)
677 lpfc_ramp_up_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500678 if (work_port_events & WORKER_DELAYED_DISC_TMO)
679 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500680 }
James Smart09372822008-01-11 01:52:54 -0500681 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500682
James Smart858c9f62007-06-17 19:56:39 -0500683 pring = &phba->sli.ring[LPFC_ELS_RING];
684 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
685 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400686 if ((status & HA_RXMASK) ||
687 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400688 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400689 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500690 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400691 /* Set the lpfc data pending flag */
692 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500693 } else {
James Smart58da1ff2008-04-07 10:15:56 -0400694 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
James Smart858c9f62007-06-17 19:56:39 -0500695 lpfc_sli_handle_slow_ring_event(phba, pring,
696 (status &
697 HA_RXMASK));
James Smart858c9f62007-06-17 19:56:39 -0500698 }
James Smartbc739052010-08-04 16:11:18 -0400699 if ((phba->sli_rev == LPFC_SLI_REV4) && pring->txq_cnt)
James Smart2a9bf3d2010-06-07 15:24:45 -0400700 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500701 /*
702 * Turn on Ring interrupts
703 */
James Smart3772a992009-05-22 14:50:54 -0400704 if (phba->sli_rev <= LPFC_SLI_REV3) {
705 spin_lock_irq(&phba->hbalock);
706 control = readl(phba->HCregaddr);
707 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
708 lpfc_debugfs_slow_ring_trc(phba,
709 "WRK Enable ring: cntl:x%x hacopy:x%x",
710 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400711
James Smart3772a992009-05-22 14:50:54 -0400712 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
713 writel(control, phba->HCregaddr);
714 readl(phba->HCregaddr); /* flush */
715 } else {
716 lpfc_debugfs_slow_ring_trc(phba,
717 "WRK Ring ok: cntl:x%x hacopy:x%x",
718 control, ha_copy, 0);
719 }
720 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500721 }
722 }
James Smart2e0fef82007-06-17 19:56:36 -0500723 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500724}
725
dea31012005-04-17 16:05:31 -0500726int
727lpfc_do_work(void *p)
728{
729 struct lpfc_hba *phba = p;
730 int rc;
dea31012005-04-17 16:05:31 -0500731
732 set_user_nice(current, -20);
James Smart043c9562012-05-09 21:17:43 -0400733 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400734 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500735
James Smart3a55b532008-12-04 22:38:54 -0500736 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400737 /* wait and check worker queue activities */
738 rc = wait_event_interruptible(phba->work_waitq,
739 (test_and_clear_bit(LPFC_DATA_READY,
740 &phba->data_flags)
741 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500742 /* Signal wakeup shall terminate the worker thread */
743 if (rc) {
744 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
745 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500746 break;
James Smart3a55b532008-12-04 22:38:54 -0500747 }
dea31012005-04-17 16:05:31 -0500748
James Smart5e9d9b82008-06-14 22:52:53 -0400749 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500750 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500751 }
James Smart3a55b532008-12-04 22:38:54 -0500752 phba->worker_thread = NULL;
753 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
754 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500755 return 0;
756}
757
758/*
759 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300760 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500761 * embedding it in the IOCB.
762 */
763int
James Smart2e0fef82007-06-17 19:56:36 -0500764lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500765 uint32_t evt)
766{
767 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500768 unsigned long flags;
dea31012005-04-17 16:05:31 -0500769
770 /*
771 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
772 * be queued to worker thread for processing
773 */
James Smart92d7f7b2007-06-17 19:56:38 -0500774 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500775 if (!evtp)
776 return 0;
777
778 evtp->evt_arg1 = arg1;
779 evtp->evt_arg2 = arg2;
780 evtp->evt = evt;
781
James Smarted957682007-06-17 19:56:37 -0500782 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400783 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500784 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500785
James Smart5e9d9b82008-06-14 22:52:53 -0400786 lpfc_worker_wake_up(phba);
787
dea31012005-04-17 16:05:31 -0500788 return 1;
789}
790
James Smart92d7f7b2007-06-17 19:56:38 -0500791void
792lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
793{
James Smart09372822008-01-11 01:52:54 -0500794 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500795 struct lpfc_hba *phba = vport->phba;
796 struct lpfc_nodelist *ndlp, *next_ndlp;
797 int rc;
798
799 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500800 if (!NLP_CHK_NODE_ACT(ndlp))
801 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500802 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
803 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400804 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
805 ((vport->port_type == LPFC_NPIV_PORT) &&
806 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500807 lpfc_unreg_rpi(vport, ndlp);
808
809 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400810 if ((phba->sli_rev < LPFC_SLI_REV4) &&
811 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500812 continue;
813 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
814 remove
815 ? NLP_EVT_DEVICE_RM
816 : NLP_EVT_DEVICE_RECOVERY);
817 }
818 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400819 if (phba->sli_rev == LPFC_SLI_REV4)
820 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500821 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500822 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500823 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500824 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500825 }
826}
827
James Smart87af33f2007-10-27 13:37:43 -0400828void
James Smart98c9ea52007-10-27 13:37:33 -0400829lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500830{
James Smart695a8142010-01-26 23:08:03 -0500831 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
832
James Smart45ed1192009-10-02 15:17:02 -0400833 /* Cleanup any outstanding received buffers */
834 lpfc_cleanup_rcv_buffers(vport);
835
James Smart92d7f7b2007-06-17 19:56:38 -0500836 /* Cleanup any outstanding RSCN activity */
837 lpfc_els_flush_rscn(vport);
838
839 /* Cleanup any outstanding ELS commands */
840 lpfc_els_flush_cmd(vport);
841
842 lpfc_cleanup_rpis(vport, 0);
843
James Smart92d7f7b2007-06-17 19:56:38 -0500844 /* Turn off discovery timer if its running */
845 lpfc_can_disctmo(vport);
846}
847
James Smart3772a992009-05-22 14:50:54 -0400848void
James Smart98c9ea52007-10-27 13:37:33 -0400849lpfc_linkdown_port(struct lpfc_vport *vport)
850{
851 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
852
853 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
854
855 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
856 "Link Down: state:x%x rtry:x%x flg:x%x",
857 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
858
859 lpfc_port_link_failure(vport);
860
James Smart92494142011-02-16 12:39:44 -0500861 /* Stop delayed Nport discovery */
862 spin_lock_irq(shost->host_lock);
863 vport->fc_flag &= ~FC_DISC_DELAYED;
864 spin_unlock_irq(shost->host_lock);
865 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400866}
867
dea31012005-04-17 16:05:31 -0500868int
James Smart685f0bf2007-04-25 09:53:08 -0400869lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500870{
James Smart2e0fef82007-06-17 19:56:36 -0500871 struct lpfc_vport *vport = phba->pport;
872 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400873 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400874 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400875 int i;
dea31012005-04-17 16:05:31 -0500876
James Smart3163f722008-02-08 18:50:25 -0500877 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500878 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500879
880 /* Block all SCSI stack I/Os */
881 lpfc_scsi_dev_block(phba);
882
James Smart2e0fef82007-06-17 19:56:36 -0500883 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500884 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500885 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500886 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500887 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500888 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500889 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500890 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500891 }
James Smart549e55c2007-08-02 11:09:51 -0400892 vports = lpfc_create_vport_work_array(phba);
893 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -0400894 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400895 /* Issue a LINK DOWN event to all nodes */
896 lpfc_linkdown_port(vports[i]);
897 }
James Smart09372822008-01-11 01:52:54 -0500898 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500899 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500900 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
901 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400902 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500903 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500904 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400905 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500906 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500907 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500908 }
909 }
910
dea31012005-04-17 16:05:31 -0500911 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500912 if (phba->pport->fc_flag & FC_PT2PT) {
913 phba->pport->fc_myDID = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500914 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
915 if (mb) {
dea31012005-04-17 16:05:31 -0500916 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500917 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500918 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400919 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500920 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500921 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500922 }
923 }
James Smart2e0fef82007-06-17 19:56:36 -0500924 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500925 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500926 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500927 }
James Smart2e0fef82007-06-17 19:56:36 -0500928
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500929 return 0;
dea31012005-04-17 16:05:31 -0500930}
931
James Smart92d7f7b2007-06-17 19:56:38 -0500932static void
933lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500934{
James Smart92d7f7b2007-06-17 19:56:38 -0500935 struct lpfc_nodelist *ndlp;
936
937 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500938 if (!NLP_CHK_NODE_ACT(ndlp))
939 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500940 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
941 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500942 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500943 /* On Linkup its safe to clean up the ndlp
944 * from Fabric connections.
945 */
James Smart92d7f7b2007-06-17 19:56:38 -0500946 if (ndlp->nlp_DID != Fabric_DID)
947 lpfc_unreg_rpi(vport, ndlp);
948 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
949 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500950 /* Fail outstanding IO now since device is
951 * marked for PLOGI.
952 */
James Smart92d7f7b2007-06-17 19:56:38 -0500953 lpfc_unreg_rpi(vport, ndlp);
954 }
955 }
956}
957
958static void
959lpfc_linkup_port(struct lpfc_vport *vport)
960{
961 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500962 struct lpfc_hba *phba = vport->phba;
963
964 if ((vport->load_flag & FC_UNLOADING) != 0)
965 return;
966
James Smart858c9f62007-06-17 19:56:39 -0500967 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
968 "Link Up: top:x%x speed:x%x flg:x%x",
969 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
970
James Smart92d7f7b2007-06-17 19:56:38 -0500971 /* If NPIV is not enabled, only bring the physical port up */
972 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
973 (vport != phba->pport))
974 return;
dea31012005-04-17 16:05:31 -0500975
James Smart2e0fef82007-06-17 19:56:36 -0500976 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400977
James Smart2e0fef82007-06-17 19:56:36 -0500978 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500979 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
980 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
981 vport->fc_flag |= FC_NDISC_ACTIVE;
982 vport->fc_ns_retry = 0;
983 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500984
James Smart92d7f7b2007-06-17 19:56:38 -0500985 if (vport->fc_flag & FC_LBIT)
986 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -0500987
James Smart92d7f7b2007-06-17 19:56:38 -0500988}
989
990static int
991lpfc_linkup(struct lpfc_hba *phba)
992{
James Smart549e55c2007-08-02 11:09:51 -0400993 struct lpfc_vport **vports;
994 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500995
James Smart19ca7602010-11-20 23:11:55 -0500996 lpfc_cleanup_wt_rrqs(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500997 phba->link_state = LPFC_LINK_UP;
998
999 /* Unblock fabric iocbs if they are blocked */
1000 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1001 del_timer_sync(&phba->fabric_block_timer);
1002
James Smart549e55c2007-08-02 11:09:51 -04001003 vports = lpfc_create_vport_work_array(phba);
1004 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001005 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001006 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001007 lpfc_destroy_vport_work_array(phba, vports);
James Smart6fb120a2009-05-22 14:52:59 -04001008 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1009 (phba->sli_rev < LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05001010 lpfc_issue_clear_la(phba, phba->pport);
dea31012005-04-17 16:05:31 -05001011
1012 return 0;
1013}
1014
1015/*
1016 * This routine handles processing a CLEAR_LA mailbox
1017 * command upon completion. It is setup in the LPFC_MBOXQ
1018 * as the completion routine when the command is
1019 * handed off to the SLI layer.
1020 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001021static void
James Smart2e0fef82007-06-17 19:56:36 -05001022lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001023{
James Smart2e0fef82007-06-17 19:56:36 -05001024 struct lpfc_vport *vport = pmb->vport;
1025 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1026 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001027 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001028 uint32_t control;
1029
dea31012005-04-17 16:05:31 -05001030 /* Since we don't do discovery right now, turn these off here */
James Smarta4bc3372006-12-02 13:34:16 -05001031 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001032 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1033 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
1034
1035 /* Check for error */
1036 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001037 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001038 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1039 "0320 CLEAR_LA mbxStatus error x%x hba "
1040 "state x%x\n",
1041 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001042 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001043 goto out;
1044 }
1045
James Smart92d7f7b2007-06-17 19:56:38 -05001046 if (vport->port_type == LPFC_PHYSICAL_PORT)
1047 phba->link_state = LPFC_HBA_READY;
1048
1049 spin_lock_irq(&phba->hbalock);
1050 psli->sli_flag |= LPFC_PROCESS_LA;
1051 control = readl(phba->HCregaddr);
1052 control |= HC_LAINT_ENA;
1053 writel(control, phba->HCregaddr);
1054 readl(phba->HCregaddr); /* flush */
1055 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001056 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001057 return;
dea31012005-04-17 16:05:31 -05001058
dea31012005-04-17 16:05:31 -05001059out:
1060 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001061 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1062 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001063 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001064
James Smart2e0fef82007-06-17 19:56:36 -05001065 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001066 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001067 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001068
James Smart2e0fef82007-06-17 19:56:36 -05001069 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001070
1071 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001072
1073 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001074 psli->sli_flag |= LPFC_PROCESS_LA;
1075 control = readl(phba->HCregaddr);
1076 control |= HC_LAINT_ENA;
1077 writel(control, phba->HCregaddr);
1078 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001079 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001080
1081 return;
1082}
1083
James Smart2e0fef82007-06-17 19:56:36 -05001084
dea31012005-04-17 16:05:31 -05001085static void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001086lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001087{
James Smart2e0fef82007-06-17 19:56:36 -05001088 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001089
James Smart04c68492009-05-22 14:52:52 -04001090 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001091 goto out;
dea31012005-04-17 16:05:31 -05001092
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001093 mempool_free(pmb, phba->mbox_mem_pool);
1094
James Smart1b511972011-12-13 13:23:09 -05001095 /* don't perform discovery for SLI4 loopback diagnostic test */
1096 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1097 !(phba->hba_flag & HBA_FCOE_MODE) &&
1098 (phba->link_flag & LS_LOOPBACK_MODE))
1099 return;
1100
James Smart76a95d72010-11-20 23:11:48 -05001101 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001102 vport->fc_flag & FC_PUBLIC_LOOP &&
1103 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001104 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001105 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001106 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1107 */
James Smart2e0fef82007-06-17 19:56:36 -05001108 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001109 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001110 }
dea31012005-04-17 16:05:31 -05001111
James Smart2e0fef82007-06-17 19:56:36 -05001112 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001113 * LPFC_FLOGI while waiting for FLOGI cmpl
1114 */
James Smarte6446432012-05-09 21:16:42 -04001115 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001116 lpfc_initial_flogi(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001117 return;
dea31012005-04-17 16:05:31 -05001118
1119out:
James Smarte8b62012007-08-02 11:10:09 -04001120 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1121 "0306 CONFIG_LINK mbxStatus error x%x "
1122 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001123 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001124 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001125
1126 lpfc_linkdown(phba);
1127
James Smarte8b62012007-08-02 11:10:09 -04001128 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1129 "0200 CONFIG_LINK bad hba state x%x\n",
1130 vport->port_state);
dea31012005-04-17 16:05:31 -05001131
James Smart92d7f7b2007-06-17 19:56:38 -05001132 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001133 return;
1134}
1135
James Smart7d791df2011-07-22 18:37:52 -04001136/**
1137 * lpfc_sli4_clear_fcf_rr_bmask
1138 * @phba pointer to the struct lpfc_hba for this port.
1139 * This fucnction resets the round robin bit mask and clears the
1140 * fcf priority list. The list deletions are done while holding the
1141 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1142 * from the lpfc_fcf_pri record.
1143 **/
1144void
1145lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1146{
1147 struct lpfc_fcf_pri *fcf_pri;
1148 struct lpfc_fcf_pri *next_fcf_pri;
1149 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1150 spin_lock_irq(&phba->hbalock);
1151 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1152 &phba->fcf.fcf_pri_list, list) {
1153 list_del_init(&fcf_pri->list);
1154 fcf_pri->fcf_rec.flag = 0;
1155 }
1156 spin_unlock_irq(&phba->hbalock);
1157}
dea31012005-04-17 16:05:31 -05001158static void
James Smart6fb120a2009-05-22 14:52:59 -04001159lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1160{
1161 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001162
1163 if (mboxq->u.mb.mbxStatus) {
1164 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1165 "2017 REG_FCFI mbxStatus error x%x "
1166 "HBA state x%x\n",
1167 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001168 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001169 }
1170
1171 /* Start FCoE discovery by sending a FLOGI. */
1172 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1173 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001174 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001175 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001176 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001177
James Smart32b97932009-07-19 10:01:21 -04001178 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001179 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1180 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001181 goto fail_out;
1182
1183 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001184 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001185 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001186 phba->hba_flag &= ~FCF_TS_INPROG;
1187 if (vport->port_state != LPFC_FLOGI) {
1188 phba->hba_flag |= FCF_RR_INPROG;
1189 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001190 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001191 goto out;
1192 }
1193 spin_unlock_irq(&phba->hbalock);
1194 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001195
James Smarta93ff372010-10-22 11:06:08 -04001196fail_out:
1197 spin_lock_irq(&phba->hbalock);
1198 phba->hba_flag &= ~FCF_RR_INPROG;
1199 spin_unlock_irq(&phba->hbalock);
1200out:
James Smart6fb120a2009-05-22 14:52:59 -04001201 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001202}
1203
1204/**
1205 * lpfc_fab_name_match - Check if the fcf fabric name match.
1206 * @fab_name: pointer to fabric name.
1207 * @new_fcf_record: pointer to fcf record.
1208 *
1209 * This routine compare the fcf record's fabric name with provided
1210 * fabric name. If the fabric name are identical this function
1211 * returns 1 else return 0.
1212 **/
1213static uint32_t
1214lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1215{
James Smartecfd03c2010-02-12 14:41:27 -05001216 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001217 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001218 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1219 return 0;
1220 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1221 return 0;
1222 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1223 return 0;
1224 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1225 return 0;
1226 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1227 return 0;
1228 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1229 return 0;
1230 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1231 return 0;
1232 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001233}
1234
1235/**
James Smart8fa38512009-07-19 10:01:03 -04001236 * lpfc_sw_name_match - Check if the fcf switch name match.
1237 * @fab_name: pointer to fabric name.
1238 * @new_fcf_record: pointer to fcf record.
1239 *
1240 * This routine compare the fcf record's switch name with provided
1241 * switch name. If the switch name are identical this function
1242 * returns 1 else return 0.
1243 **/
1244static uint32_t
1245lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1246{
James Smartecfd03c2010-02-12 14:41:27 -05001247 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001248 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001249 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1250 return 0;
1251 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1252 return 0;
1253 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1254 return 0;
1255 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1256 return 0;
1257 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1258 return 0;
1259 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1260 return 0;
1261 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1262 return 0;
1263 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001264}
1265
1266/**
James Smart6fb120a2009-05-22 14:52:59 -04001267 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001268 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001269 * @new_fcf_record: pointer to fcf record.
1270 *
1271 * This routine compare the fcf record's mac address with HBA's
1272 * FCF mac address. If the mac addresses are identical this function
1273 * returns 1 else return 0.
1274 **/
1275static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001276lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001277{
James Smartecfd03c2010-02-12 14:41:27 -05001278 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001279 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001280 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1281 return 0;
1282 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1283 return 0;
1284 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1285 return 0;
1286 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1287 return 0;
1288 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1289 return 0;
1290 return 1;
1291}
1292
1293static bool
1294lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1295{
1296 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001297}
1298
1299/**
James Smart7d791df2011-07-22 18:37:52 -04001300 * lpfc_update_fcf_record - Update driver fcf record
1301 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1302 * @phba: pointer to lpfc hba data structure.
1303 * @fcf_index: Index for the lpfc_fcf_record.
1304 * @new_fcf_record: pointer to hba fcf record.
1305 *
1306 * This routine updates the driver FCF priority record from the new HBA FCF
1307 * record. This routine is called with the host lock held.
1308 **/
1309static void
1310__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1311 struct fcf_record *new_fcf_record
1312 )
1313{
1314 struct lpfc_fcf_pri *fcf_pri;
1315
1316 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1317 fcf_pri->fcf_rec.fcf_index = fcf_index;
1318 /* FCF record priority */
1319 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1320
1321}
1322
1323/**
James Smart6fb120a2009-05-22 14:52:59 -04001324 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001325 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001326 * @new_fcf_record: pointer to fcf record.
1327 *
1328 * This routine copies the FCF information from the FCF
1329 * record to lpfc_hba data structure.
1330 **/
1331static void
James Smartecfd03c2010-02-12 14:41:27 -05001332lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1333 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001334{
James Smartecfd03c2010-02-12 14:41:27 -05001335 /* Fabric name */
1336 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001337 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001338 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001339 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001340 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001341 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001342 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001343 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001344 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001345 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001346 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001347 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001348 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001349 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001350 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001351 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001352 /* Mac address */
1353 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1354 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1355 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1356 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1357 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1358 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1359 /* FCF record index */
1360 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1361 /* FCF record priority */
1362 fcf_rec->priority = new_fcf_record->fip_priority;
1363 /* Switch name */
1364 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001365 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001366 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001367 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001368 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001369 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001370 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001371 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001372 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001373 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001374 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001375 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001376 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001377 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001378 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001379 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001380}
1381
1382/**
James Smartecfd03c2010-02-12 14:41:27 -05001383 * lpfc_update_fcf_record - Update driver fcf record
1384 * @phba: pointer to lpfc hba data structure.
1385 * @fcf_rec: pointer to driver fcf record.
1386 * @new_fcf_record: pointer to hba fcf record.
1387 * @addr_mode: address mode to be set to the driver fcf record.
1388 * @vlan_id: vlan tag to be set to the driver fcf record.
1389 * @flag: flag bits to be set to the driver fcf record.
1390 *
1391 * This routine updates the driver FCF record from the new HBA FCF record
1392 * together with the address mode, vlan_id, and other informations. This
1393 * routine is called with the host lock held.
1394 **/
1395static void
1396__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1397 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1398 uint16_t vlan_id, uint32_t flag)
1399{
1400 /* Copy the fields from the HBA's FCF record */
1401 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1402 /* Update other fields of driver FCF record */
1403 fcf_rec->addr_mode = addr_mode;
1404 fcf_rec->vlan_id = vlan_id;
1405 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001406 __lpfc_update_fcf_record_pri(phba,
1407 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1408 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001409}
1410
1411/**
James Smart6fb120a2009-05-22 14:52:59 -04001412 * lpfc_register_fcf - Register the FCF with hba.
1413 * @phba: pointer to lpfc hba data structure.
1414 *
1415 * This routine issues a register fcfi mailbox command to register
1416 * the fcf with HBA.
1417 **/
1418static void
1419lpfc_register_fcf(struct lpfc_hba *phba)
1420{
1421 LPFC_MBOXQ_t *fcf_mbxq;
1422 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001423
James Smartd439d282010-09-29 11:18:45 -04001424 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001425 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001426 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001427 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001428 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001429 return;
1430 }
1431
1432 /* The FCF is already registered, start discovery */
1433 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001434 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001435 phba->hba_flag &= ~FCF_TS_INPROG;
1436 if (phba->pport->port_state != LPFC_FLOGI) {
1437 phba->hba_flag |= FCF_RR_INPROG;
1438 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001439 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001440 return;
1441 }
1442 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001443 return;
1444 }
James Smartd439d282010-09-29 11:18:45 -04001445 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001446
James Smarta93ff372010-10-22 11:06:08 -04001447 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001448 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001449 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001450 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001451 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001452 return;
James Smart4d9ab992009-10-02 15:16:39 -04001453 }
James Smart6fb120a2009-05-22 14:52:59 -04001454
1455 lpfc_reg_fcfi(phba, fcf_mbxq);
1456 fcf_mbxq->vport = phba->pport;
1457 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1458 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001459 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001460 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001461 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001462 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001463 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001464 }
James Smart6fb120a2009-05-22 14:52:59 -04001465
1466 return;
1467}
1468
1469/**
1470 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1471 * @phba: pointer to lpfc hba data structure.
1472 * @new_fcf_record: pointer to fcf record.
1473 * @boot_flag: Indicates if this record used by boot bios.
1474 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001475 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001476 *
1477 * This routine compare the fcf record with connect list obtained from the
1478 * config region to decide if this FCF can be used for SAN discovery. It returns
1479 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1480 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1481 * is used by boot bios and addr_mode will indicate the addressing mode to be
1482 * used for this FCF when the function returns.
1483 * If the FCF record need to be used with a particular vlan id, the vlan is
1484 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001485 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001486 **/
1487static int
1488lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1489 struct fcf_record *new_fcf_record,
1490 uint32_t *boot_flag, uint32_t *addr_mode,
1491 uint16_t *vlan_id)
1492{
1493 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001494 int i, j, fcf_vlan_id = 0;
1495
1496 /* Find the lowest VLAN id in the FCF record */
1497 for (i = 0; i < 512; i++) {
1498 if (new_fcf_record->vlan_bitmap[i]) {
1499 fcf_vlan_id = i * 8;
1500 j = 0;
1501 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1502 j++;
1503 fcf_vlan_id++;
1504 }
1505 break;
1506 }
1507 }
James Smart6fb120a2009-05-22 14:52:59 -04001508
James Smart0c287582009-06-10 17:22:56 -04001509 /* If FCF not available return 0 */
1510 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1511 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1512 return 0;
1513
James Smart45ed1192009-10-02 15:17:02 -04001514 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001515 *boot_flag = 0;
1516 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1517 new_fcf_record);
1518 if (phba->valid_vlan)
1519 *vlan_id = phba->vlan_id;
1520 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001521 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001522 return 1;
1523 }
1524
1525 /*
1526 * If there are no FCF connection table entry, driver connect to all
1527 * FCFs.
1528 */
1529 if (list_empty(&phba->fcf_conn_rec_list)) {
1530 *boot_flag = 0;
1531 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1532 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001533
1534 /*
1535 * When there are no FCF connect entries, use driver's default
1536 * addressing mode - FPMA.
1537 */
1538 if (*addr_mode & LPFC_FCF_FPMA)
1539 *addr_mode = LPFC_FCF_FPMA;
1540
James Smart4d9ab992009-10-02 15:16:39 -04001541 /* If FCF record report a vlan id use that vlan id */
1542 if (fcf_vlan_id)
1543 *vlan_id = fcf_vlan_id;
1544 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001545 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001546 return 1;
1547 }
1548
James Smartecfd03c2010-02-12 14:41:27 -05001549 list_for_each_entry(conn_entry,
1550 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001551 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1552 continue;
1553
1554 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1555 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001556 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001557 continue;
James Smart8fa38512009-07-19 10:01:03 -04001558 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1559 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1560 new_fcf_record))
1561 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001562 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1563 /*
1564 * If the vlan bit map does not have the bit set for the
1565 * vlan id to be used, then it is not a match.
1566 */
1567 if (!(new_fcf_record->vlan_bitmap
1568 [conn_entry->conn_rec.vlan_tag / 8] &
1569 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1570 continue;
1571 }
1572
1573 /*
James Smart0c287582009-06-10 17:22:56 -04001574 * If connection record does not support any addressing mode,
1575 * skip the FCF record.
1576 */
1577 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1578 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1579 continue;
1580
1581 /*
James Smart6fb120a2009-05-22 14:52:59 -04001582 * Check if the connection record specifies a required
1583 * addressing mode.
1584 */
1585 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1586 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1587
1588 /*
1589 * If SPMA required but FCF not support this continue.
1590 */
1591 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1592 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1593 new_fcf_record) & LPFC_FCF_SPMA))
1594 continue;
1595
1596 /*
1597 * If FPMA required but FCF not support this continue.
1598 */
1599 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1600 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1601 new_fcf_record) & LPFC_FCF_FPMA))
1602 continue;
1603 }
1604
1605 /*
1606 * This fcf record matches filtering criteria.
1607 */
1608 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1609 *boot_flag = 1;
1610 else
1611 *boot_flag = 0;
1612
James Smart0c287582009-06-10 17:22:56 -04001613 /*
1614 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001615 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001616 * by FCF, allow fabric to pick the addressing mode.
1617 */
James Smart6fb120a2009-05-22 14:52:59 -04001618 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1619 new_fcf_record);
1620 /*
1621 * If the user specified a required address mode, assign that
1622 * address mode
1623 */
1624 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1625 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1626 *addr_mode = (conn_entry->conn_rec.flags &
1627 FCFCNCT_AM_SPMA) ?
1628 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1629 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001630 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001631 * addr mode only if FCF support the addr_mode.
1632 */
1633 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1634 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1635 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1636 (*addr_mode & LPFC_FCF_SPMA))
1637 *addr_mode = LPFC_FCF_SPMA;
1638 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1639 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1640 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1641 (*addr_mode & LPFC_FCF_FPMA))
1642 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001643
James Smart4d9ab992009-10-02 15:16:39 -04001644 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001645 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1646 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001647 /*
1648 * If no vlan id is specified in connect list, use the vlan id
1649 * in the FCF record
1650 */
1651 else if (fcf_vlan_id)
1652 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001653 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001654 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001655
1656 return 1;
1657 }
1658
1659 return 0;
1660}
1661
1662/**
James Smart32b97932009-07-19 10:01:21 -04001663 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1664 * @phba: pointer to lpfc hba data structure.
1665 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1666 *
1667 * This function check if there is any fcoe event pending while driver
1668 * scan FCF entries. If there is any pending event, it will restart the
1669 * FCF saning and return 1 else return 0.
1670 */
1671int
1672lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1673{
James Smart32b97932009-07-19 10:01:21 -04001674 /*
1675 * If the Link is up and no FCoE events while in the
1676 * FCF discovery, no need to restart FCF discovery.
1677 */
1678 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001679 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001680 return 0;
1681
James Smart0c9ab6f2010-02-26 14:15:57 -05001682 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1683 "2768 Pending link or FCF event during current "
1684 "handling of the previous event: link_state:x%x, "
1685 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1686 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1687 phba->fcoe_eventtag);
1688
James Smart32b97932009-07-19 10:01:21 -04001689 spin_lock_irq(&phba->hbalock);
1690 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1691 spin_unlock_irq(&phba->hbalock);
1692
James Smart0c9ab6f2010-02-26 14:15:57 -05001693 if (phba->link_state >= LPFC_LINK_UP) {
1694 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1695 "2780 Restart FCF table scan due to "
1696 "pending FCF event:evt_tag_at_scan:x%x, "
1697 "evt_tag_current:x%x\n",
1698 phba->fcoe_eventtag_at_fcf_scan,
1699 phba->fcoe_eventtag);
1700 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1701 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001702 /*
James Smarta93ff372010-10-22 11:06:08 -04001703 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001704 * flag
1705 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001706 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1707 "2833 Stop FCF discovery process due to link "
1708 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001709 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001710 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001711 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001712 spin_unlock_irq(&phba->hbalock);
1713 }
James Smart32b97932009-07-19 10:01:21 -04001714
James Smart0c9ab6f2010-02-26 14:15:57 -05001715 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001716 if (unreg_fcf) {
1717 spin_lock_irq(&phba->hbalock);
1718 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1719 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001720 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001721 }
James Smart32b97932009-07-19 10:01:21 -04001722 return 1;
1723}
1724
1725/**
James Smart999d8132010-03-15 11:24:56 -04001726 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1727 * @phba: pointer to lpfc hba data structure.
1728 * @fcf_cnt: number of eligible fcf record seen so far.
1729 *
1730 * This function makes an running random selection decision on FCF record to
1731 * use through a sequence of @fcf_cnt eligible FCF records with equal
1732 * probability. To perform integer manunipulation of random numbers with
1733 * size unit32_t, the lower 16 bits of the 32-bit random number returned
1734 * from random32() are taken as the random random number generated.
1735 *
1736 * Returns true when outcome is for the newly read FCF record should be
1737 * chosen; otherwise, return false when outcome is for keeping the previously
1738 * chosen FCF record.
1739 **/
1740static bool
1741lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1742{
1743 uint32_t rand_num;
1744
1745 /* Get 16-bit uniform random number */
1746 rand_num = (0xFFFF & random32());
1747
1748 /* Decision with probability 1/fcf_cnt */
1749 if ((fcf_cnt * rand_num) < 0xFFFF)
1750 return true;
1751 else
1752 return false;
1753}
1754
1755/**
James Smart3804dc82010-07-14 15:31:37 -04001756 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001757 * @phba: pointer to lpfc hba data structure.
1758 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001759 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001760 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001761 * This routine parses the non-embedded fcf mailbox command by performing the
1762 * necessarily error checking, non-embedded read FCF record mailbox command
1763 * SGE parsing, and endianness swapping.
1764 *
1765 * Returns the pointer to the new FCF record in the non-embedded mailbox
1766 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001767 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001768static struct fcf_record *
1769lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1770 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001771{
1772 void *virt_addr;
1773 dma_addr_t phys_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001774 struct lpfc_mbx_sge sge;
1775 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1776 uint32_t shdr_status, shdr_add_status;
1777 union lpfc_sli4_cfg_shdr *shdr;
1778 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001779
James Smart6fb120a2009-05-22 14:52:59 -04001780 /* Get the first SGE entry from the non-embedded DMA memory. This
1781 * routine only uses a single SGE.
1782 */
1783 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1784 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1785 if (unlikely(!mboxq->sge_array)) {
1786 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1787 "2524 Failed to get the non-embedded SGE "
1788 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001789 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001790 }
1791 virt_addr = mboxq->sge_array->addr[0];
1792
1793 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1794 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smart0c9ab6f2010-02-26 14:15:57 -05001795 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001796 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -05001797 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1798 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001799 "2726 READ_FCF_RECORD Indicates empty "
1800 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001801 else
1802 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001803 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001804 "with status x%x add_status x%x, "
1805 "mbx\n", shdr_status, shdr_add_status);
1806 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001807 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001808
1809 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001810 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1811 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1812 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001813 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001814 new_fcf_record = (struct fcf_record *)(virt_addr +
1815 sizeof(struct lpfc_mbx_read_fcf_tbl));
1816 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001817 offsetof(struct fcf_record, vlan_bitmap));
1818 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1819 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001820
James Smart0c9ab6f2010-02-26 14:15:57 -05001821 return new_fcf_record;
1822}
1823
1824/**
1825 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1826 * @phba: pointer to lpfc hba data structure.
1827 * @fcf_record: pointer to the fcf record.
1828 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1829 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1830 *
1831 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1832 * enabled.
1833 **/
1834static void
1835lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1836 struct fcf_record *fcf_record,
1837 uint16_t vlan_id,
1838 uint16_t next_fcf_index)
1839{
1840 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1841 "2764 READ_FCF_RECORD:\n"
1842 "\tFCF_Index : x%x\n"
1843 "\tFCF_Avail : x%x\n"
1844 "\tFCF_Valid : x%x\n"
1845 "\tFIP_Priority : x%x\n"
1846 "\tMAC_Provider : x%x\n"
1847 "\tLowest VLANID : x%x\n"
1848 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1849 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1850 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1851 "\tNext_FCF_Index: x%x\n",
1852 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1853 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1854 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
1855 fcf_record->fip_priority,
1856 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1857 vlan_id,
1858 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1859 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1860 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1861 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1862 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1863 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1864 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1865 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1866 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1867 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1868 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1869 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1870 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1871 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1872 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1873 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1874 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1875 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1876 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1877 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1878 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1879 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1880 next_fcf_index);
1881}
1882
1883/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001884 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1885 * @phba: pointer to lpfc hba data structure.
1886 * @fcf_rec: pointer to an existing FCF record.
1887 * @new_fcf_record: pointer to a new FCF record.
1888 * @new_vlan_id: vlan id from the new FCF record.
1889 *
1890 * This function performs matching test of a new FCF record against an existing
1891 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1892 * will not be used as part of the FCF record matching criteria.
1893 *
1894 * Returns true if all the fields matching, otherwise returns false.
1895 */
1896static bool
1897lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1898 struct lpfc_fcf_rec *fcf_rec,
1899 struct fcf_record *new_fcf_record,
1900 uint16_t new_vlan_id)
1901{
1902 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1903 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1904 return false;
1905 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1906 return false;
1907 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1908 return false;
1909 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1910 return false;
James Smart7d791df2011-07-22 18:37:52 -04001911 if (fcf_rec->priority != new_fcf_record->fip_priority)
1912 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001913 return true;
1914}
1915
1916/**
James Smarta93ff372010-10-22 11:06:08 -04001917 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1918 * @vport: Pointer to vport object.
1919 * @fcf_index: index to next fcf.
1920 *
1921 * This function processing the roundrobin fcf failover to next fcf index.
1922 * When this function is invoked, there will be a current fcf registered
1923 * for flogi.
1924 * Return: 0 for continue retrying flogi on currently registered fcf;
1925 * 1 for stop flogi on currently registered fcf;
1926 */
1927int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1928{
1929 struct lpfc_hba *phba = vport->phba;
1930 int rc;
1931
1932 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1933 spin_lock_irq(&phba->hbalock);
1934 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1935 spin_unlock_irq(&phba->hbalock);
1936 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1937 "2872 Devloss tmo with no eligible "
1938 "FCF, unregister in-use FCF (x%x) "
1939 "and rescan FCF table\n",
1940 phba->fcf.current_rec.fcf_indx);
1941 lpfc_unregister_fcf_rescan(phba);
1942 goto stop_flogi_current_fcf;
1943 }
1944 /* Mark the end to FLOGI roundrobin failover */
1945 phba->hba_flag &= ~FCF_RR_INPROG;
1946 /* Allow action to new fcf asynchronous event */
1947 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1948 spin_unlock_irq(&phba->hbalock);
1949 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1950 "2865 No FCF available, stop roundrobin FCF "
1951 "failover and change port state:x%x/x%x\n",
1952 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1953 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1954 goto stop_flogi_current_fcf;
1955 } else {
1956 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1957 "2794 Try FLOGI roundrobin FCF failover to "
1958 "(x%x)\n", fcf_index);
1959 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1960 if (rc)
1961 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1962 "2761 FLOGI roundrobin FCF failover "
1963 "failed (rc:x%x) to read FCF (x%x)\n",
1964 rc, phba->fcf.current_rec.fcf_indx);
1965 else
1966 goto stop_flogi_current_fcf;
1967 }
1968 return 0;
1969
1970stop_flogi_current_fcf:
1971 lpfc_can_disctmo(vport);
1972 return 1;
1973}
1974
1975/**
James Smart7d791df2011-07-22 18:37:52 -04001976 * lpfc_sli4_fcf_pri_list_del
1977 * @phba: pointer to lpfc hba data structure.
1978 * @fcf_index the index of the fcf record to delete
1979 * This routine checks the on list flag of the fcf_index to be deleted.
1980 * If it is one the list then it is removed from the list, and the flag
1981 * is cleared. This routine grab the hbalock before removing the fcf
1982 * record from the list.
1983 **/
1984static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1985 uint16_t fcf_index)
1986{
1987 struct lpfc_fcf_pri *new_fcf_pri;
1988
1989 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1990 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1991 "3058 deleting idx x%x pri x%x flg x%x\n",
1992 fcf_index, new_fcf_pri->fcf_rec.priority,
1993 new_fcf_pri->fcf_rec.flag);
1994 spin_lock_irq(&phba->hbalock);
1995 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
1996 if (phba->fcf.current_rec.priority ==
1997 new_fcf_pri->fcf_rec.priority)
1998 phba->fcf.eligible_fcf_cnt--;
1999 list_del_init(&new_fcf_pri->list);
2000 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2001 }
2002 spin_unlock_irq(&phba->hbalock);
2003}
2004
2005/**
2006 * lpfc_sli4_set_fcf_flogi_fail
2007 * @phba: pointer to lpfc hba data structure.
2008 * @fcf_index the index of the fcf record to update
2009 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2010 * flag so the the round robin slection for the particular priority level
2011 * will try a different fcf record that does not have this bit set.
2012 * If the fcf record is re-read for any reason this flag is cleared brfore
2013 * adding it to the priority list.
2014 **/
2015void
2016lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2017{
2018 struct lpfc_fcf_pri *new_fcf_pri;
2019 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2020 spin_lock_irq(&phba->hbalock);
2021 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2022 spin_unlock_irq(&phba->hbalock);
2023}
2024
2025/**
2026 * lpfc_sli4_fcf_pri_list_add
2027 * @phba: pointer to lpfc hba data structure.
2028 * @fcf_index the index of the fcf record to add
2029 * This routine checks the priority of the fcf_index to be added.
2030 * If it is a lower priority than the current head of the fcf_pri list
2031 * then it is added to the list in the right order.
2032 * If it is the same priority as the current head of the list then it
2033 * is added to the head of the list and its bit in the rr_bmask is set.
2034 * If the fcf_index to be added is of a higher priority than the current
2035 * head of the list then the rr_bmask is cleared, its bit is set in the
2036 * rr_bmask and it is added to the head of the list.
2037 * returns:
2038 * 0=success 1=failure
2039 **/
2040int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba, uint16_t fcf_index,
2041 struct fcf_record *new_fcf_record)
2042{
2043 uint16_t current_fcf_pri;
2044 uint16_t last_index;
2045 struct lpfc_fcf_pri *fcf_pri;
2046 struct lpfc_fcf_pri *next_fcf_pri;
2047 struct lpfc_fcf_pri *new_fcf_pri;
2048 int ret;
2049
2050 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2051 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2052 "3059 adding idx x%x pri x%x flg x%x\n",
2053 fcf_index, new_fcf_record->fip_priority,
2054 new_fcf_pri->fcf_rec.flag);
2055 spin_lock_irq(&phba->hbalock);
2056 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2057 list_del_init(&new_fcf_pri->list);
2058 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2059 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2060 if (list_empty(&phba->fcf.fcf_pri_list)) {
2061 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2062 ret = lpfc_sli4_fcf_rr_index_set(phba,
2063 new_fcf_pri->fcf_rec.fcf_index);
2064 goto out;
2065 }
2066
2067 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2068 LPFC_SLI4_FCF_TBL_INDX_MAX);
2069 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2070 ret = 0; /* Empty rr list */
2071 goto out;
2072 }
2073 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2074 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2075 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2076 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2077 memset(phba->fcf.fcf_rr_bmask, 0,
2078 sizeof(*phba->fcf.fcf_rr_bmask));
2079 /* fcfs_at_this_priority_level = 1; */
2080 phba->fcf.eligible_fcf_cnt = 1;
2081 } else
2082 /* fcfs_at_this_priority_level++; */
2083 phba->fcf.eligible_fcf_cnt++;
2084 ret = lpfc_sli4_fcf_rr_index_set(phba,
2085 new_fcf_pri->fcf_rec.fcf_index);
2086 goto out;
2087 }
2088
2089 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2090 &phba->fcf.fcf_pri_list, list) {
2091 if (new_fcf_pri->fcf_rec.priority <=
2092 fcf_pri->fcf_rec.priority) {
2093 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2094 list_add(&new_fcf_pri->list,
2095 &phba->fcf.fcf_pri_list);
2096 else
2097 list_add(&new_fcf_pri->list,
2098 &((struct lpfc_fcf_pri *)
2099 fcf_pri->list.prev)->list);
2100 ret = 0;
2101 goto out;
2102 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2103 || new_fcf_pri->fcf_rec.priority <
2104 next_fcf_pri->fcf_rec.priority) {
2105 list_add(&new_fcf_pri->list, &fcf_pri->list);
2106 ret = 0;
2107 goto out;
2108 }
2109 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2110 continue;
2111
2112 }
2113 ret = 1;
2114out:
2115 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2116 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2117 spin_unlock_irq(&phba->hbalock);
2118 return ret;
2119}
2120
2121/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002122 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2123 * @phba: pointer to lpfc hba data structure.
2124 * @mboxq: pointer to mailbox object.
2125 *
2126 * This function iterates through all the fcf records available in
2127 * HBA and chooses the optimal FCF record for discovery. After finding
2128 * the FCF for discovery it registers the FCF record and kicks start
2129 * discovery.
2130 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2131 * use an FCF record which matches fabric name and mac address of the
2132 * currently used FCF record.
2133 * If the driver supports only one FCF, it will try to use the FCF record
2134 * used by BOOT_BIOS.
2135 */
2136void
2137lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2138{
2139 struct fcf_record *new_fcf_record;
2140 uint32_t boot_flag, addr_mode;
2141 uint16_t fcf_index, next_fcf_index;
2142 struct lpfc_fcf_rec *fcf_rec = NULL;
2143 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002144 uint32_t seed;
2145 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002146 int rc;
2147
2148 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002149 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002150 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2151 return;
2152 }
2153
2154 /* Parse the FCF record from the non-embedded mailbox command */
2155 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2156 &next_fcf_index);
2157 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002158 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002159 "2765 Mailbox command READ_FCF_RECORD "
2160 "failed to retrieve a FCF record.\n");
2161 /* Let next new FCF event trigger fast failover */
2162 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002163 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002164 spin_unlock_irq(&phba->hbalock);
2165 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2166 return;
2167 }
2168
2169 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002170 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2171 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002172
2173 /* Log the FCF record information if turned on */
2174 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2175 next_fcf_index);
2176
James Smart6fb120a2009-05-22 14:52:59 -04002177 /*
2178 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002179 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002180 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002181 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002182 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002183 lpfc_sli4_fcf_pri_list_del(phba,
2184 bf_get(lpfc_fcf_record_fcf_index,
2185 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002186 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002187 "2781 FCF (x%x) failed connection "
2188 "list check: (x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002189 bf_get(lpfc_fcf_record_fcf_index,
2190 new_fcf_record),
2191 bf_get(lpfc_fcf_record_fcf_avail,
2192 new_fcf_record),
2193 bf_get(lpfc_fcf_record_fcf_valid,
2194 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002195 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2196 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2197 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002198 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2199 phba->fcf.current_rec.fcf_indx) {
2200 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2201 "2862 FCF (x%x) matches property "
2202 "of in-use FCF (x%x)\n",
2203 bf_get(lpfc_fcf_record_fcf_index,
2204 new_fcf_record),
2205 phba->fcf.current_rec.fcf_indx);
2206 goto read_next_fcf;
2207 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002208 /*
2209 * In case the current in-use FCF record becomes
2210 * invalid/unavailable during FCF discovery that
2211 * was not triggered by fast FCF failover process,
2212 * treat it as fast FCF failover.
2213 */
2214 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2215 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2216 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2217 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002218 "(x%x), enter FCF failover "
2219 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002220 phba->fcf.current_rec.fcf_indx);
2221 spin_lock_irq(&phba->hbalock);
2222 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2223 spin_unlock_irq(&phba->hbalock);
2224 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2225 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2226 LPFC_FCOE_FCF_GET_FIRST);
2227 return;
2228 }
2229 }
James Smart6fb120a2009-05-22 14:52:59 -04002230 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002231 } else {
2232 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002233 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2234 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002235 if (rc)
2236 goto read_next_fcf;
2237 }
2238
James Smart6fb120a2009-05-22 14:52:59 -04002239 /*
2240 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002241 * FCF record for the discovery. The condition that a rescan
2242 * matches the in-use FCF record: fabric name, switch name, mac
2243 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002244 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002245 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002246 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002247 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2248 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002249 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002250 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2251 phba->fcf.current_rec.fcf_indx) {
2252 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2253 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2254 /* Stop FCF redisc wait timer */
2255 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2256 phba);
2257 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2258 /* Fast failover, mark completed */
2259 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2260 spin_unlock_irq(&phba->hbalock);
2261 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2262 "2836 New FCF matches in-use "
2263 "FCF (x%x)\n",
2264 phba->fcf.current_rec.fcf_indx);
2265 goto out;
2266 } else
2267 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2268 "2863 New FCF (x%x) matches "
2269 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002270 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002271 new_fcf_record),
2272 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002273 }
James Smartecfd03c2010-02-12 14:41:27 -05002274 /*
2275 * Read next FCF record from HBA searching for the matching
2276 * with in-use record only if not during the fast failover
2277 * period. In case of fast failover period, it shall try to
2278 * determine whether the FCF record just read should be the
2279 * next candidate.
2280 */
2281 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002282 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002283 goto read_next_fcf;
2284 }
James Smart6fb120a2009-05-22 14:52:59 -04002285 }
James Smartecfd03c2010-02-12 14:41:27 -05002286 /*
2287 * Update on failover FCF record only if it's in FCF fast-failover
2288 * period; otherwise, update on current FCF record.
2289 */
James Smartfc2b9892010-02-26 14:15:29 -05002290 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2291 fcf_rec = &phba->fcf.failover_rec;
2292 else
James Smartecfd03c2010-02-12 14:41:27 -05002293 fcf_rec = &phba->fcf.current_rec;
2294
James Smart6fb120a2009-05-22 14:52:59 -04002295 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2296 /*
James Smartecfd03c2010-02-12 14:41:27 -05002297 * If the driver FCF record does not have boot flag
2298 * set and new hba fcf record has boot flag set, use
2299 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002300 */
James Smartecfd03c2010-02-12 14:41:27 -05002301 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2302 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002303 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2304 "2837 Update current FCF record "
2305 "(x%x) with new FCF record (x%x)\n",
2306 fcf_rec->fcf_indx,
2307 bf_get(lpfc_fcf_record_fcf_index,
2308 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002309 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2310 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002311 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002312 goto read_next_fcf;
2313 }
2314 /*
James Smartecfd03c2010-02-12 14:41:27 -05002315 * If the driver FCF record has boot flag set and the
2316 * new hba FCF record does not have boot flag, read
2317 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002318 */
James Smartecfd03c2010-02-12 14:41:27 -05002319 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002320 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002321 goto read_next_fcf;
2322 }
2323 /*
James Smartecfd03c2010-02-12 14:41:27 -05002324 * If the new hba FCF record has lower priority value
2325 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002326 */
James Smartfc2b9892010-02-26 14:15:29 -05002327 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002328 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002329 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2330 "2838 Update current FCF record "
2331 "(x%x) with new FCF record (x%x)\n",
2332 fcf_rec->fcf_indx,
2333 bf_get(lpfc_fcf_record_fcf_index,
2334 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002335 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2336 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002337 /* Reset running random FCF selection count */
2338 phba->fcf.eligible_fcf_cnt = 1;
2339 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2340 /* Update running random FCF selection count */
2341 phba->fcf.eligible_fcf_cnt++;
2342 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2343 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002344 if (select_new_fcf) {
2345 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2346 "2839 Update current FCF record "
2347 "(x%x) with new FCF record (x%x)\n",
2348 fcf_rec->fcf_indx,
2349 bf_get(lpfc_fcf_record_fcf_index,
2350 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002351 /* Choose the new FCF by random selection */
2352 __lpfc_update_fcf_record(phba, fcf_rec,
2353 new_fcf_record,
2354 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002355 }
James Smart6fb120a2009-05-22 14:52:59 -04002356 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002357 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002358 goto read_next_fcf;
2359 }
2360 /*
James Smartecfd03c2010-02-12 14:41:27 -05002361 * This is the first suitable FCF record, choose this record for
2362 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002363 */
James Smartecfd03c2010-02-12 14:41:27 -05002364 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002365 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002366 "2840 Update initial FCF candidate "
2367 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002368 bf_get(lpfc_fcf_record_fcf_index,
2369 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002370 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2371 addr_mode, vlan_id, (boot_flag ?
2372 BOOT_ENABLE : 0));
2373 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002374 /* Setup initial running random FCF selection count */
2375 phba->fcf.eligible_fcf_cnt = 1;
2376 /* Seeding the random number generator for random selection */
2377 seed = (uint32_t)(0xFFFFFFFF & jiffies);
2378 srandom32(seed);
James Smart6fb120a2009-05-22 14:52:59 -04002379 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002380 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002381 goto read_next_fcf;
2382
2383read_next_fcf:
2384 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002385 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2386 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2387 /*
2388 * Case of FCF fast failover scan
2389 */
2390
2391 /*
2392 * It has not found any suitable FCF record, cancel
2393 * FCF scan inprogress, and do nothing
2394 */
2395 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002396 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002397 "2782 No suitable FCF found: "
2398 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002399 phba->fcoe_eventtag_at_fcf_scan,
2400 bf_get(lpfc_fcf_record_fcf_index,
2401 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002402 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002403 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2404 phba->hba_flag &= ~FCF_TS_INPROG;
2405 spin_unlock_irq(&phba->hbalock);
2406 /* Unregister in-use FCF and rescan */
2407 lpfc_printf_log(phba, KERN_INFO,
2408 LOG_FIP,
2409 "2864 On devloss tmo "
2410 "unreg in-use FCF and "
2411 "rescan FCF table\n");
2412 lpfc_unregister_fcf_rescan(phba);
2413 return;
2414 }
2415 /*
2416 * Let next new FCF event trigger fast failover
2417 */
2418 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002419 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002420 return;
2421 }
2422 /*
2423 * It has found a suitable FCF record that is not
2424 * the same as in-use FCF record, unregister the
2425 * in-use FCF record, replace the in-use FCF record
2426 * with the new FCF record, mark FCF fast failover
2427 * completed, and then start register the new FCF
2428 * record.
2429 */
2430
James Smart0c9ab6f2010-02-26 14:15:57 -05002431 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002432 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002433
2434 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002435 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002436 "2842 Replace in-use FCF (x%x) "
2437 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002438 phba->fcf.current_rec.fcf_indx,
2439 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002440 memcpy(&phba->fcf.current_rec,
2441 &phba->fcf.failover_rec,
2442 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002443 /*
2444 * Mark the fast FCF failover rediscovery completed
2445 * and the start of the first round of the roundrobin
2446 * FCF failover.
2447 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002448 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002449 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002450 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002451 /* Register to the new FCF record */
2452 lpfc_register_fcf(phba);
2453 } else {
2454 /*
2455 * In case of transaction period to fast FCF failover,
2456 * do nothing when search to the end of the FCF table.
2457 */
2458 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2459 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2460 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002461
James Smart7d791df2011-07-22 18:37:52 -04002462 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2463 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002464 /*
2465 * In case the current in-use FCF record no
2466 * longer existed during FCF discovery that
2467 * was not triggered by fast FCF failover
2468 * process, treat it as fast FCF failover.
2469 */
2470 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2471 "2841 In-use FCF record (x%x) "
2472 "not reported, entering fast "
2473 "FCF failover mode scanning.\n",
2474 phba->fcf.current_rec.fcf_indx);
2475 spin_lock_irq(&phba->hbalock);
2476 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2477 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002478 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2479 LPFC_FCOE_FCF_GET_FIRST);
2480 return;
2481 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002482 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002483 lpfc_register_fcf(phba);
2484 }
2485 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002486 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002487 return;
2488
2489out:
2490 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2491 lpfc_register_fcf(phba);
2492
2493 return;
2494}
2495
2496/**
James Smarta93ff372010-10-22 11:06:08 -04002497 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002498 * @phba: pointer to lpfc hba data structure.
2499 * @mboxq: pointer to mailbox object.
2500 *
James Smarta93ff372010-10-22 11:06:08 -04002501 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002502 * read FCF record mailbox command from the eligible FCF record bmask for
2503 * performing the failover. If the FCF read back is not valid/available, it
2504 * fails through to retrying FLOGI to the currently registered FCF again.
2505 * Otherwise, if the FCF read back is valid and available, it will set the
2506 * newly read FCF record to the failover FCF record, unregister currently
2507 * registered FCF record, copy the failover FCF record to the current
2508 * FCF record, and then register the current FCF record before proceeding
2509 * to trying FLOGI on the new failover FCF.
2510 */
2511void
2512lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2513{
2514 struct fcf_record *new_fcf_record;
2515 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002516 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002517 uint16_t current_fcf_index;
2518 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002519 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002520
James Smarta93ff372010-10-22 11:06:08 -04002521 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002522 if (phba->link_state < LPFC_LINK_UP) {
2523 spin_lock_irq(&phba->hbalock);
2524 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002525 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002526 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002527 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002528 }
2529
2530 /* Parse the FCF record from the non-embedded mailbox command */
2531 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2532 &next_fcf_index);
2533 if (!new_fcf_record) {
2534 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2535 "2766 Mailbox command READ_FCF_RECORD "
2536 "failed to retrieve a FCF record.\n");
James Smarta93ff372010-10-22 11:06:08 -04002537 goto error_out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002538 }
2539
2540 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002541 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2542 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002543
2544 /* Log the FCF record information if turned on */
2545 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2546 next_fcf_index);
2547
James Smarta93ff372010-10-22 11:06:08 -04002548 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2549 if (!rc) {
2550 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2551 "2848 Remove ineligible FCF (x%x) from "
2552 "from roundrobin bmask\n", fcf_index);
2553 /* Clear roundrobin bmask bit for ineligible FCF */
2554 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2555 /* Perform next round of roundrobin FCF failover */
2556 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2557 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2558 if (rc)
2559 goto out;
2560 goto error_out;
2561 }
2562
2563 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2564 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2565 "2760 Perform FLOGI roundrobin FCF failover: "
2566 "FCF (x%x) back to FCF (x%x)\n",
2567 phba->fcf.current_rec.fcf_indx, fcf_index);
2568 /* Wait 500 ms before retrying FLOGI to current FCF */
2569 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002570 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002571 goto out;
2572 }
2573
James Smart0c9ab6f2010-02-26 14:15:57 -05002574 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002575 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002576 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2577 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002578 spin_lock_irq(&phba->hbalock);
2579 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2580 new_fcf_record, addr_mode, vlan_id,
2581 (boot_flag ? BOOT_ENABLE : 0));
2582 spin_unlock_irq(&phba->hbalock);
2583
2584 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2585
2586 /* Unregister the current in-use FCF record */
2587 lpfc_unregister_fcf(phba);
2588
2589 /* Replace in-use record with the new record */
2590 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2591 sizeof(struct lpfc_fcf_rec));
2592
2593 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002594 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2595 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002596
James Smarta93ff372010-10-22 11:06:08 -04002597error_out:
2598 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002599out:
2600 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002601}
2602
2603/**
2604 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2605 * @phba: pointer to lpfc hba data structure.
2606 * @mboxq: pointer to mailbox object.
2607 *
2608 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002609 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002610 * failover when a new FCF event happened. If the FCF read back is
2611 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002612 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002613 */
2614void
2615lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2616{
2617 struct fcf_record *new_fcf_record;
2618 uint32_t boot_flag, addr_mode;
2619 uint16_t fcf_index, next_fcf_index;
2620 uint16_t vlan_id;
2621 int rc;
2622
2623 /* If link state is not up, no need to proceed */
2624 if (phba->link_state < LPFC_LINK_UP)
2625 goto out;
2626
2627 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002628 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002629 goto out;
2630
2631 /* Parse the FCF record from the non-embedded mailbox command */
2632 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2633 &next_fcf_index);
2634 if (!new_fcf_record) {
2635 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2636 "2767 Mailbox command READ_FCF_RECORD "
2637 "failed to retrieve a FCF record.\n");
2638 goto out;
2639 }
2640
2641 /* Check the connection list for eligibility */
2642 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2643 &addr_mode, &vlan_id);
2644
2645 /* Log the FCF record information if turned on */
2646 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2647 next_fcf_index);
2648
2649 if (!rc)
2650 goto out;
2651
2652 /* Update the eligible FCF record index bmask */
2653 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002654
2655 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002656
2657out:
2658 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2659}
2660
2661/**
James Smart76a95d72010-11-20 23:11:48 -05002662 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2663 * @phba: pointer to lpfc hba data structure.
2664 * @mboxq: pointer to mailbox data structure.
2665 *
2666 * This function handles completion of init vfi mailbox command.
2667 */
2668void
2669lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2670{
2671 struct lpfc_vport *vport = mboxq->vport;
2672
James Smartdf9e1b52011-12-13 13:22:17 -05002673 /*
2674 * VFI not supported on interface type 0, just do the flogi
2675 * Also continue if the VFI is in use - just use the same one.
2676 */
2677 if (mboxq->u.mb.mbxStatus &&
2678 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2679 LPFC_SLI_INTF_IF_TYPE_0) &&
2680 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002681 lpfc_printf_vlog(vport, KERN_ERR,
2682 LOG_MBOX,
2683 "2891 Init VFI mailbox failed 0x%x\n",
2684 mboxq->u.mb.mbxStatus);
2685 mempool_free(mboxq, phba->mbox_mem_pool);
2686 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2687 return;
2688 }
James Smart73d91e52011-10-10 21:32:10 -04002689
James Smart76a95d72010-11-20 23:11:48 -05002690 lpfc_initial_flogi(vport);
2691 mempool_free(mboxq, phba->mbox_mem_pool);
2692 return;
2693}
2694
2695/**
2696 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2697 * @vport: pointer to lpfc_vport data structure.
2698 *
2699 * This function issue a init_vfi mailbox command to initialize the VFI and
2700 * VPI for the physical port.
2701 */
2702void
2703lpfc_issue_init_vfi(struct lpfc_vport *vport)
2704{
2705 LPFC_MBOXQ_t *mboxq;
2706 int rc;
2707 struct lpfc_hba *phba = vport->phba;
2708
2709 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2710 if (!mboxq) {
2711 lpfc_printf_vlog(vport, KERN_ERR,
2712 LOG_MBOX, "2892 Failed to allocate "
2713 "init_vfi mailbox\n");
2714 return;
2715 }
2716 lpfc_init_vfi(mboxq, vport);
2717 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2718 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2719 if (rc == MBX_NOT_FINISHED) {
2720 lpfc_printf_vlog(vport, KERN_ERR,
2721 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2722 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2723 }
2724}
2725
2726/**
James Smart1c6834a2009-07-19 10:01:26 -04002727 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2728 * @phba: pointer to lpfc hba data structure.
2729 * @mboxq: pointer to mailbox data structure.
2730 *
2731 * This function handles completion of init vpi mailbox command.
2732 */
James Smart695a8142010-01-26 23:08:03 -05002733void
James Smart1c6834a2009-07-19 10:01:26 -04002734lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2735{
2736 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002737 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002738 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2739
James Smart1c6834a2009-07-19 10:01:26 -04002740 if (mboxq->u.mb.mbxStatus) {
2741 lpfc_printf_vlog(vport, KERN_ERR,
2742 LOG_MBOX,
2743 "2609 Init VPI mailbox failed 0x%x\n",
2744 mboxq->u.mb.mbxStatus);
2745 mempool_free(mboxq, phba->mbox_mem_pool);
2746 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2747 return;
2748 }
James Smart72100cc2010-02-12 14:43:01 -05002749 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002750 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002751 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002752
James Smart695a8142010-01-26 23:08:03 -05002753 /* If this port is physical port or FDISC is done, do reg_vpi */
2754 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2755 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2756 if (!ndlp)
2757 lpfc_printf_vlog(vport, KERN_ERR,
2758 LOG_DISCOVERY,
2759 "2731 Cannot find fabric "
2760 "controller node\n");
2761 else
2762 lpfc_register_new_vport(phba, vport, ndlp);
2763 mempool_free(mboxq, phba->mbox_mem_pool);
2764 return;
2765 }
2766
James Smart1c6834a2009-07-19 10:01:26 -04002767 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2768 lpfc_initial_fdisc(vport);
2769 else {
2770 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002771 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2772 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002773 }
James Smart695a8142010-01-26 23:08:03 -05002774 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002775 return;
2776}
2777
2778/**
James Smartecfd03c2010-02-12 14:41:27 -05002779 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2780 * @vport: pointer to lpfc_vport data structure.
2781 *
2782 * This function issue a init_vpi mailbox command to initialize
2783 * VPI for the vport.
2784 */
2785void
2786lpfc_issue_init_vpi(struct lpfc_vport *vport)
2787{
2788 LPFC_MBOXQ_t *mboxq;
2789 int rc;
2790
2791 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2792 if (!mboxq) {
2793 lpfc_printf_vlog(vport, KERN_ERR,
2794 LOG_MBOX, "2607 Failed to allocate "
2795 "init_vpi mailbox\n");
2796 return;
2797 }
2798 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2799 mboxq->vport = vport;
2800 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2801 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2802 if (rc == MBX_NOT_FINISHED) {
2803 lpfc_printf_vlog(vport, KERN_ERR,
2804 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2805 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2806 }
2807}
2808
2809/**
James Smart6fb120a2009-05-22 14:52:59 -04002810 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2811 * @phba: pointer to lpfc hba data structure.
2812 *
2813 * This function loops through the list of vports on the @phba and issues an
2814 * FDISC if possible.
2815 */
2816void
2817lpfc_start_fdiscs(struct lpfc_hba *phba)
2818{
2819 struct lpfc_vport **vports;
2820 int i;
2821
2822 vports = lpfc_create_vport_work_array(phba);
2823 if (vports != NULL) {
2824 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2825 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2826 continue;
2827 /* There are no vpi for this vport */
2828 if (vports[i]->vpi > phba->max_vpi) {
2829 lpfc_vport_set_state(vports[i],
2830 FC_VPORT_FAILED);
2831 continue;
2832 }
James Smart76a95d72010-11-20 23:11:48 -05002833 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002834 lpfc_vport_set_state(vports[i],
2835 FC_VPORT_LINKDOWN);
2836 continue;
2837 }
James Smart1c6834a2009-07-19 10:01:26 -04002838 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002839 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002840 continue;
2841 }
James Smart6fb120a2009-05-22 14:52:59 -04002842 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2843 lpfc_initial_fdisc(vports[i]);
2844 else {
2845 lpfc_vport_set_state(vports[i],
2846 FC_VPORT_NO_FABRIC_SUPP);
2847 lpfc_printf_vlog(vports[i], KERN_ERR,
2848 LOG_ELS,
2849 "0259 No NPIV "
2850 "Fabric support\n");
2851 }
2852 }
2853 }
2854 lpfc_destroy_vport_work_array(phba, vports);
2855}
2856
2857void
2858lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2859{
2860 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2861 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002862 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002863
James Smartf5eca9b2012-03-01 22:37:54 -05002864 /*
2865 * VFI not supported for interface type 0, so ignore any mailbox
2866 * error (except VFI in use) and continue with the discovery.
2867 */
2868 if (mboxq->u.mb.mbxStatus &&
2869 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2870 LPFC_SLI_INTF_IF_TYPE_0) &&
2871 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002872 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2873 "2018 REG_VFI mbxStatus error x%x "
2874 "HBA state x%x\n",
2875 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002876 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002877 /* FLOGI failed, use loop map to make discovery list */
2878 lpfc_disc_list_loopmap(vport);
2879 /* Start discovery */
2880 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002881 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002882 }
2883 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002884 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002885 }
James Smartc8685952009-11-18 15:39:16 -05002886 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002887 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002888 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002889 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002890 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002891 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002892 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002893
James Smart1b511972011-12-13 13:23:09 -05002894 /* In case SLI4 FC loopback test, we are ready */
2895 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2896 (phba->link_flag & LS_LOOPBACK_MODE)) {
2897 phba->link_state = LPFC_HBA_READY;
2898 goto out_free_mem;
2899 }
2900
James Smart6fb120a2009-05-22 14:52:59 -04002901 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002902 /*
2903 * For private loop or for NPort pt2pt,
2904 * just start discovery and we are done.
2905 */
2906 if ((vport->fc_flag & FC_PT2PT) ||
2907 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2908 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2909
James Smart76a95d72010-11-20 23:11:48 -05002910 /* Use loop map to make discovery list */
2911 lpfc_disc_list_loopmap(vport);
2912 /* Start discovery */
2913 lpfc_disc_start(vport);
2914 } else {
2915 lpfc_start_fdiscs(phba);
2916 lpfc_do_scr_ns_plogi(phba, vport);
2917 }
James Smart6fb120a2009-05-22 14:52:59 -04002918 }
2919
James Smart1b511972011-12-13 13:23:09 -05002920out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002921 mempool_free(mboxq, phba->mbox_mem_pool);
2922 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2923 kfree(dmabuf);
2924 return;
2925}
2926
2927static void
James Smart2e0fef82007-06-17 19:56:36 -05002928lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002929{
James Smart6fb120a2009-05-22 14:52:59 -04002930 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002931 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002932 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002933
2934
2935 /* Check for error */
2936 if (mb->mbxStatus) {
2937 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04002938 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2939 "0319 READ_SPARAM mbxStatus error x%x "
2940 "hba state x%x>\n",
2941 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05002942 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05002943 goto out;
2944 }
2945
James Smart2e0fef82007-06-17 19:56:36 -05002946 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05002947 sizeof (struct serv_parm));
James Smart05580562011-05-24 11:40:48 -04002948 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05002949 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2950 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2951 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2952 }
2953
dea31012005-04-17 16:05:31 -05002954 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2955 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05002956 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002957 return;
2958
2959out:
2960 pmb->context1 = NULL;
2961 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2962 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05002963 lpfc_issue_clear_la(phba, vport);
2964 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002965 return;
2966}
2967
2968static void
James Smart76a95d72010-11-20 23:11:48 -05002969lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05002970{
James Smart92d7f7b2007-06-17 19:56:38 -05002971 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04002972 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05002973 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05002974 int i;
James Smart14691152006-12-02 13:34:28 -05002975 struct lpfc_dmabuf *mp;
2976 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04002977 struct fcf_record *fcf_record;
James Smart14691152006-12-02 13:34:28 -05002978
James Smart92d7f7b2007-06-17 19:56:38 -05002979 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05002980 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
2981 case LPFC_LINK_SPEED_1GHZ:
2982 case LPFC_LINK_SPEED_2GHZ:
2983 case LPFC_LINK_SPEED_4GHZ:
2984 case LPFC_LINK_SPEED_8GHZ:
2985 case LPFC_LINK_SPEED_10GHZ:
2986 case LPFC_LINK_SPEED_16GHZ:
2987 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
James Smartf4b4c682009-05-22 14:53:12 -04002988 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002989 default:
James Smart76a95d72010-11-20 23:11:48 -05002990 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
James Smart92d7f7b2007-06-17 19:56:38 -05002991 break;
dea31012005-04-17 16:05:31 -05002992 }
2993
James Smart76a95d72010-11-20 23:11:48 -05002994 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05002995 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05002996
James Smartdf9e1b52011-12-13 13:22:17 -05002997 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05002998 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05002999 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003000
James Smart6a9c52c2009-10-02 15:16:51 -04003001 /* if npiv is enabled and this adapter supports npiv log
3002 * a message that npiv is not supported in this topology
3003 */
3004 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003005 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3006 "1309 Link Up Event npiv not supported in loop "
3007 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003008 /* Get Loop Map information */
James Smartdf9e1b52011-12-13 13:22:17 -05003009 if (bf_get(lpfc_mbx_read_top_il, la)) {
James Smart72859902012-01-18 16:25:38 -05003010 spin_lock(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003011 vport->fc_flag |= FC_LBIT;
James Smart72859902012-01-18 16:25:38 -05003012 spin_unlock(shost->host_lock);
James Smartdf9e1b52011-12-13 13:22:17 -05003013 }
dea31012005-04-17 16:05:31 -05003014
James Smart76a95d72010-11-20 23:11:48 -05003015 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3016 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003017
3018 if (i == 0) {
3019 phba->alpa_map[0] = 0;
3020 } else {
James Smarte8b62012007-08-02 11:10:09 -04003021 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003022 int numalpa, j, k;
3023 union {
3024 uint8_t pamap[16];
3025 struct {
3026 uint32_t wd1;
3027 uint32_t wd2;
3028 uint32_t wd3;
3029 uint32_t wd4;
3030 } pa;
3031 } un;
3032 numalpa = phba->alpa_map[0];
3033 j = 0;
3034 while (j < numalpa) {
3035 memset(un.pamap, 0, 16);
3036 for (k = 1; j < numalpa; k++) {
3037 un.pamap[k - 1] =
3038 phba->alpa_map[j + 1];
3039 j++;
3040 if (k == 16)
3041 break;
3042 }
3043 /* Link Up Event ALPA map */
3044 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003045 KERN_WARNING,
3046 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003047 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003048 "ALPA map Data: x%x "
3049 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003050 un.pa.wd1, un.pa.wd2,
3051 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003052 }
3053 }
3054 }
3055 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003056 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003057 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003058 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003059 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3060 }
James Smart2e0fef82007-06-17 19:56:36 -05003061 vport->fc_myDID = phba->fc_pref_DID;
James Smart72859902012-01-18 16:25:38 -05003062 spin_lock(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05003063 vport->fc_flag |= FC_LBIT;
James Smart72859902012-01-18 16:25:38 -05003064 spin_unlock(shost->host_lock);
dea31012005-04-17 16:05:31 -05003065 }
James Smart92d7f7b2007-06-17 19:56:38 -05003066 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003067
3068 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003069 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3070 if (!sparam_mbox)
3071 goto out;
3072
3073 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3074 if (rc) {
3075 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3076 goto out;
3077 }
3078 sparam_mbox->vport = vport;
3079 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3080 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3081 if (rc == MBX_NOT_FINISHED) {
3082 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3083 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3084 kfree(mp);
3085 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3086 goto out;
dea31012005-04-17 16:05:31 -05003087 }
3088
James Smart76a95d72010-11-20 23:11:48 -05003089 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003090 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3091 if (!cfglink_mbox)
3092 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003093 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003094 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003095 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003096 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003097 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003098 if (rc == MBX_NOT_FINISHED) {
3099 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3100 goto out;
3101 }
3102 } else {
James Smart32b97932009-07-19 10:01:21 -04003103 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003104 /*
3105 * Add the driver's default FCF record at FCF index 0 now. This
3106 * is phase 1 implementation that support FCF index 0 and driver
3107 * defaults.
3108 */
James Smart45ed1192009-10-02 15:17:02 -04003109 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003110 fcf_record = kzalloc(sizeof(struct fcf_record),
3111 GFP_KERNEL);
3112 if (unlikely(!fcf_record)) {
3113 lpfc_printf_log(phba, KERN_ERR,
3114 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003115 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003116 "fcf record\n");
3117 rc = -ENODEV;
3118 goto out;
3119 }
3120
3121 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3122 LPFC_FCOE_FCF_DEF_INDEX);
3123 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3124 if (unlikely(rc)) {
3125 lpfc_printf_log(phba, KERN_ERR,
3126 LOG_MBOX | LOG_SLI,
3127 "2013 Could not manually add FCF "
3128 "record 0, status %d\n", rc);
3129 rc = -ENODEV;
3130 kfree(fcf_record);
3131 goto out;
3132 }
3133 kfree(fcf_record);
3134 }
3135 /*
3136 * The driver is expected to do FIP/FCF. Call the port
3137 * and get the FCF Table.
3138 */
James Smart32b97932009-07-19 10:01:21 -04003139 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003140 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003141 spin_unlock_irq(&phba->hbalock);
3142 return;
3143 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003144 /* This is the initial FCF discovery scan */
3145 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003146 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003147 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3148 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003149 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3150 LPFC_FCOE_FCF_GET_FIRST);
3151 if (rc) {
3152 spin_lock_irq(&phba->hbalock);
3153 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3154 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003155 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003156 }
James Smart38b92ef2010-08-04 16:11:39 -04003157 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003158 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003159 }
James Smart6fb120a2009-05-22 14:52:59 -04003160
3161 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003162out:
3163 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003164 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3165 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3166 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003167 lpfc_issue_clear_la(phba, vport);
3168 return;
dea31012005-04-17 16:05:31 -05003169}
3170
3171static void
James Smart84774a42008-08-24 21:50:06 -04003172lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003173{
dea31012005-04-17 16:05:31 -05003174 uint32_t control;
3175 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003176 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003177 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003178 if (phba->sli_rev <= LPFC_SLI_REV3) {
3179 control = readl(phba->HCregaddr);
3180 control |= HC_LAINT_ENA;
3181 writel(control, phba->HCregaddr);
3182 readl(phba->HCregaddr); /* flush */
3183 }
James Smart2e0fef82007-06-17 19:56:36 -05003184 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003185}
3186
James Smart84774a42008-08-24 21:50:06 -04003187static void
3188lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3189{
3190 lpfc_linkdown(phba);
3191 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003192 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003193 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3194}
3195
3196
dea31012005-04-17 16:05:31 -05003197/*
James Smart76a95d72010-11-20 23:11:48 -05003198 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003199 * command upon completion. It is setup in the LPFC_MBOXQ
3200 * as the completion routine when the command is
3201 * handed off to the SLI layer.
3202 */
3203void
James Smart76a95d72010-11-20 23:11:48 -05003204lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003205{
James Smart2e0fef82007-06-17 19:56:36 -05003206 struct lpfc_vport *vport = pmb->vport;
3207 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003208 struct lpfc_mbx_read_top *la;
James Smart04c68492009-05-22 14:52:52 -04003209 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003210 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3211
James Smart0d2b6b82008-06-14 22:52:47 -04003212 /* Unblock ELS traffic */
3213 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05003214 /* Check for error */
3215 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003216 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003217 "1307 READ_LA mbox error x%x state x%x\n",
3218 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003219 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003220 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003221 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003222 }
3223
James Smart76a95d72010-11-20 23:11:48 -05003224 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003225
3226 memcpy(&phba->alpa_map[0], mp->virt, 128);
3227
James Smart2e0fef82007-06-17 19:56:36 -05003228 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003229 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003230 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003231 else
James Smart2e0fef82007-06-17 19:56:36 -05003232 vport->fc_flag &= ~FC_BYPASSED_MODE;
3233 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003234
James Smart0c287582009-06-10 17:22:56 -04003235 if ((phba->fc_eventTag < la->eventTag) ||
James Smart92d7f7b2007-06-17 19:56:38 -05003236 (phba->fc_eventTag == la->eventTag)) {
dea31012005-04-17 16:05:31 -05003237 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003238 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003239 if (phba->fc_eventTag != 0)
3240 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003241 }
dea31012005-04-17 16:05:31 -05003242
3243 phba->fc_eventTag = la->eventTag;
James Smart72100cc2010-02-12 14:43:01 -05003244 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05003245 if (bf_get(lpfc_mbx_read_top_mm, la))
James Smart84774a42008-08-24 21:50:06 -04003246 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3247 else
3248 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart72100cc2010-02-12 14:43:01 -05003249 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003250
James Smart4d9ab992009-10-02 15:16:39 -04003251 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003252 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
3253 (!bf_get(lpfc_mbx_read_top_mm, la))) {
dea31012005-04-17 16:05:31 -05003254 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003255 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003256 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003257 "1306 Link Up Event in loop back mode "
3258 "x%x received Data: x%x x%x x%x x%x\n",
3259 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003260 bf_get(lpfc_mbx_read_top_alpa_granted,
3261 la),
3262 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003263 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003264 } else {
3265 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003266 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003267 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003268 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003269 bf_get(lpfc_mbx_read_top_alpa_granted,
3270 la),
3271 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003272 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003273 bf_get(lpfc_mbx_read_top_mm, la),
3274 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003275 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003276 }
James Smart92d7f7b2007-06-17 19:56:38 -05003277 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003278 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3279 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003280 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003281 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003282 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3283 "1308 Link Down Event in loop back mode "
3284 "x%x received "
3285 "Data: x%x x%x x%x\n",
3286 la->eventTag, phba->fc_eventTag,
3287 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003288 else
James Smart3163f722008-02-08 18:50:25 -05003289 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003290 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003291 "Data: x%x x%x x%x x%x x%x\n",
3292 la->eventTag, phba->fc_eventTag,
3293 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003294 bf_get(lpfc_mbx_read_top_mm, la),
3295 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003296 lpfc_mbx_issue_link_down(phba);
3297 }
James Smart76a95d72010-11-20 23:11:48 -05003298 if ((bf_get(lpfc_mbx_read_top_mm, la)) &&
3299 (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)) {
James Smart84774a42008-08-24 21:50:06 -04003300 if (phba->link_state != LPFC_LINK_DOWN) {
3301 phba->fc_stat.LinkDown++;
3302 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3303 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003304 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003305 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003306 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003307 lpfc_mbx_issue_link_down(phba);
3308 } else
3309 lpfc_enable_la(phba);
3310
3311 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3312 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3313 "Data: x%x x%x x%x\n",
3314 la->eventTag, phba->fc_eventTag,
3315 phba->pport->port_state, vport->fc_flag);
3316 /*
3317 * The cmnd that triggered this will be waiting for this
3318 * signal.
3319 */
3320 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3321 if (phba->wait_4_mlo_maint_flg) {
3322 phba->wait_4_mlo_maint_flg = 0;
3323 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003324 }
James Smart84774a42008-08-24 21:50:06 -04003325 }
3326
James Smart76a95d72010-11-20 23:11:48 -05003327 if (bf_get(lpfc_mbx_read_top_fa, la)) {
3328 if (bf_get(lpfc_mbx_read_top_mm, la))
James Smart84774a42008-08-24 21:50:06 -04003329 lpfc_issue_clear_la(phba, vport);
3330 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003331 "1311 fa %d\n",
3332 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003333 }
3334
James Smart76a95d72010-11-20 23:11:48 -05003335lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003336 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3337 kfree(mp);
3338 mempool_free(pmb, phba->mbox_mem_pool);
3339 return;
3340}
3341
3342/*
3343 * This routine handles processing a REG_LOGIN mailbox
3344 * command upon completion. It is setup in the LPFC_MBOXQ
3345 * as the completion routine when the command is
3346 * handed off to the SLI layer.
3347 */
3348void
James Smart2e0fef82007-06-17 19:56:36 -05003349lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003350{
James Smart2e0fef82007-06-17 19:56:36 -05003351 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003352 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003353 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003354 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003355
dea31012005-04-17 16:05:31 -05003356 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003357 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003358
James Smartffc95492010-06-07 15:23:17 -04003359 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3360 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3361
James Smart40426292010-12-15 17:58:10 -05003362 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3363 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003364 /* We rcvd a rscn after issuing this
3365 * mbox reg login, we may have cycled
3366 * back through the state and be
3367 * back at reg login state so this
3368 * mbox needs to be ignored becase
3369 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003370 * process.
James Smartffc95492010-06-07 15:23:17 -04003371 */
3372 spin_lock_irq(shost->host_lock);
3373 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3374 spin_unlock_irq(shost->host_lock);
James Smartffc95492010-06-07 15:23:17 -04003375 } else
3376 /* Good status, call state machine */
3377 lpfc_disc_state_machine(vport, ndlp, pmb,
3378 NLP_EVT_CMPL_REG_LOGIN);
3379
dea31012005-04-17 16:05:31 -05003380 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3381 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003382 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003383 /* decrement the node reference count held for this callback
3384 * function.
3385 */
James Smart329f9bc2007-04-25 09:53:01 -04003386 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003387
3388 return;
3389}
3390
James Smart92d7f7b2007-06-17 19:56:38 -05003391static void
3392lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3393{
James Smart04c68492009-05-22 14:52:52 -04003394 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003395 struct lpfc_vport *vport = pmb->vport;
3396 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3397
3398 switch (mb->mbxStatus) {
3399 case 0x0011:
3400 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003401 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3402 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3403 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003404 break;
James Smart78730cf2010-04-06 15:06:30 -04003405 /* If VPI is busy, reset the HBA */
3406 case 0x9700:
3407 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3408 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3409 vport->vpi, mb->mbxStatus);
3410 if (!(phba->pport->load_flag & FC_UNLOADING))
3411 lpfc_workq_post_event(phba, NULL, NULL,
3412 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003413 }
James Smart72100cc2010-02-12 14:43:01 -05003414 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003415 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003416 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003417 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003418 vport->unreg_vpi_cmpl = VPORT_OK;
3419 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003420 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003421 /*
3422 * This shost reference might have been taken at the beginning of
3423 * lpfc_vport_delete()
3424 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003425 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003426 scsi_host_put(shost);
3427}
3428
James Smartd7c255b2008-08-24 21:50:00 -04003429int
James Smart92d7f7b2007-06-17 19:56:38 -05003430lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3431{
3432 struct lpfc_hba *phba = vport->phba;
3433 LPFC_MBOXQ_t *mbox;
3434 int rc;
3435
3436 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3437 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003438 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003439
3440 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3441 mbox->vport = vport;
3442 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003443 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003444 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003445 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3446 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003447 mempool_free(mbox, phba->mbox_mem_pool);
3448 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003449 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003450 }
James Smartd7c255b2008-08-24 21:50:00 -04003451 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003452}
3453
3454static void
3455lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3456{
3457 struct lpfc_vport *vport = pmb->vport;
3458 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003459 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003460
3461 switch (mb->mbxStatus) {
3462 case 0x0011:
3463 case 0x9601:
3464 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003465 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3466 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3467 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003468 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3469 spin_lock_irq(shost->host_lock);
3470 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3471 spin_unlock_irq(shost->host_lock);
3472 vport->fc_myDID = 0;
3473 goto out;
3474 }
James Smart92d7f7b2007-06-17 19:56:38 -05003475
James Smart72100cc2010-02-12 14:43:01 -05003476 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003477 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003478 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003479 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003480 vport->num_disc_nodes = 0;
3481 /* go thru NPR list and issue ELS PLOGIs */
3482 if (vport->fc_npr_cnt)
3483 lpfc_els_disc_plogi(vport);
3484
3485 if (!vport->num_disc_nodes) {
3486 spin_lock_irq(shost->host_lock);
3487 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3488 spin_unlock_irq(shost->host_lock);
3489 lpfc_can_disctmo(vport);
3490 }
3491 vport->port_state = LPFC_VPORT_READY;
3492
3493out:
3494 mempool_free(pmb, phba->mbox_mem_pool);
3495 return;
3496}
3497
James Smart21e9a0a2009-05-22 14:53:21 -04003498/**
3499 * lpfc_create_static_vport - Read HBA config region to create static vports.
3500 * @phba: pointer to lpfc hba data structure.
3501 *
3502 * This routine issue a DUMP mailbox command for config region 22 to get
3503 * the list of static vports to be created. The function create vports
3504 * based on the information returned from the HBA.
3505 **/
3506void
3507lpfc_create_static_vport(struct lpfc_hba *phba)
3508{
3509 LPFC_MBOXQ_t *pmb = NULL;
3510 MAILBOX_t *mb;
3511 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003512 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003513 struct fc_vport_identifiers vport_id;
3514 struct fc_vport *new_fc_vport;
3515 struct Scsi_Host *shost;
3516 struct lpfc_vport *vport;
3517 uint16_t offset = 0;
3518 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003519 struct lpfc_dmabuf *mp;
3520 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003521
3522 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3523 if (!pmb) {
3524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3525 "0542 lpfc_create_static_vport failed to"
3526 " allocate mailbox memory\n");
3527 return;
3528 }
James Smartcdcc2342012-08-14 14:26:35 -04003529 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003530 mb = &pmb->u.mb;
3531
3532 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3533 if (!vport_info) {
3534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3535 "0543 lpfc_create_static_vport failed to"
3536 " allocate vport_info\n");
3537 mempool_free(pmb, phba->mbox_mem_pool);
3538 return;
3539 }
3540
3541 vport_buff = (uint8_t *) vport_info;
3542 do {
James Smartcdcc2342012-08-14 14:26:35 -04003543 /* free dma buffer from previous round */
3544 if (pmb->context1) {
3545 mp = (struct lpfc_dmabuf *)pmb->context1;
3546 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3547 kfree(mp);
3548 }
James Smart1c6834a2009-07-19 10:01:26 -04003549 if (lpfc_dump_static_vport(phba, pmb, offset))
3550 goto out;
3551
James Smart21e9a0a2009-05-22 14:53:21 -04003552 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003553 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3554 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003555
James Smartcdcc2342012-08-14 14:26:35 -04003556 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003557 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3558 "0544 lpfc_create_static_vport failed to"
3559 " issue dump mailbox command ret 0x%x "
3560 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003561 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003562 goto out;
3563 }
3564
James Smart1c6834a2009-07-19 10:01:26 -04003565 if (phba->sli_rev == LPFC_SLI_REV4) {
3566 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003567 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003568 if (byte_count > sizeof(struct static_vport_info) -
3569 offset)
3570 byte_count = sizeof(struct static_vport_info)
3571 - offset;
3572 memcpy(vport_buff + offset, mp->virt, byte_count);
3573 offset += byte_count;
3574 } else {
3575 if (mb->un.varDmp.word_cnt >
3576 sizeof(struct static_vport_info) - offset)
3577 mb->un.varDmp.word_cnt =
3578 sizeof(struct static_vport_info)
3579 - offset;
3580 byte_count = mb->un.varDmp.word_cnt;
3581 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3582 vport_buff + offset,
3583 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003584
James Smart1c6834a2009-07-19 10:01:26 -04003585 offset += byte_count;
3586 }
James Smart21e9a0a2009-05-22 14:53:21 -04003587
James Smart1c6834a2009-07-19 10:01:26 -04003588 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003589 offset < sizeof(struct static_vport_info));
3590
3591
3592 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3593 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3594 != VPORT_INFO_REV)) {
3595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3596 "0545 lpfc_create_static_vport bad"
3597 " information header 0x%x 0x%x\n",
3598 le32_to_cpu(vport_info->signature),
3599 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3600
3601 goto out;
3602 }
3603
3604 shost = lpfc_shost_from_vport(phba->pport);
3605
3606 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3607 memset(&vport_id, 0, sizeof(vport_id));
3608 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3609 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3610 if (!vport_id.port_name || !vport_id.node_name)
3611 continue;
3612
3613 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3614 vport_id.vport_type = FC_PORTTYPE_NPIV;
3615 vport_id.disable = false;
3616 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3617
3618 if (!new_fc_vport) {
3619 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3620 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003621 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003622 continue;
3623 }
3624
3625 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3626 vport->vport_flag |= STATIC_VPORT;
3627 }
3628
3629out:
James Smart21e9a0a2009-05-22 14:53:21 -04003630 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003631 if (mbx_wait_rc != MBX_TIMEOUT) {
3632 if (pmb->context1) {
3633 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003634 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3635 kfree(mp);
3636 }
James Smart21e9a0a2009-05-22 14:53:21 -04003637 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003638 }
James Smart21e9a0a2009-05-22 14:53:21 -04003639
3640 return;
3641}
3642
dea31012005-04-17 16:05:31 -05003643/*
3644 * This routine handles processing a Fabric REG_LOGIN mailbox
3645 * command upon completion. It is setup in the LPFC_MBOXQ
3646 * as the completion routine when the command is
3647 * handed off to the SLI layer.
3648 */
3649void
James Smart2e0fef82007-06-17 19:56:36 -05003650lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003651{
James Smart92d7f7b2007-06-17 19:56:38 -05003652 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003653 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003654 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003655 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003656 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003657
James Smart549e55c2007-08-02 11:09:51 -04003658 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003659 pmb->context1 = NULL;
3660 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003661
dea31012005-04-17 16:05:31 -05003662 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003663 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3664 "0258 Register Fabric login error: 0x%x\n",
3665 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003666 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3667 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003668 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003669
James Smart76a95d72010-11-20 23:11:48 -05003670 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003671 /* FLOGI failed, use loop map to make discovery list */
3672 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003673
James Smart92d7f7b2007-06-17 19:56:38 -05003674 /* Start discovery */
3675 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003676 /* Decrement the reference count to ndlp after the
3677 * reference to the ndlp are done.
3678 */
3679 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003680 return;
3681 }
3682
3683 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003684 /* Decrement the reference count to ndlp after the reference
3685 * to the ndlp are done.
3686 */
3687 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003688 return;
3689 }
3690
James Smart6d368e52011-05-24 11:44:12 -04003691 if (phba->sli_rev < LPFC_SLI_REV4)
3692 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003693 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003694 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003695 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003696
James Smart2e0fef82007-06-17 19:56:36 -05003697 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003698 /* when physical port receive logo donot start
3699 * vport discovery */
3700 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3701 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003702 else {
3703 shost = lpfc_shost_from_vport(vport);
3704 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003705 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003706 spin_unlock_irq(shost->host_lock);
3707 }
James Smart92d7f7b2007-06-17 19:56:38 -05003708 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003709 }
3710
3711 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3712 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003713 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003714
3715 /* Drop the reference count from the mbox at the end after
3716 * all the current reference to the ndlp have been done.
3717 */
3718 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003719 return;
3720}
3721
3722/*
3723 * This routine handles processing a NameServer REG_LOGIN mailbox
3724 * command upon completion. It is setup in the LPFC_MBOXQ
3725 * as the completion routine when the command is
3726 * handed off to the SLI layer.
3727 */
3728void
James Smart2e0fef82007-06-17 19:56:36 -05003729lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003730{
James Smart21e9a0a2009-05-22 14:53:21 -04003731 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003732 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3733 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3734 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003735
James Smartd439d282010-09-29 11:18:45 -04003736 pmb->context1 = NULL;
3737 pmb->context2 = NULL;
3738
dea31012005-04-17 16:05:31 -05003739 if (mb->mbxStatus) {
James Smart92d7f7b2007-06-17 19:56:38 -05003740out:
James Smart21e9a0a2009-05-22 14:53:21 -04003741 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3742 "0260 Register NameServer error: 0x%x\n",
3743 mb->mbxStatus);
James Smartfa4066b2008-01-11 01:53:27 -05003744 /* decrement the node reference count held for this
3745 * callback function.
3746 */
James Smart329f9bc2007-04-25 09:53:01 -04003747 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003748 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3749 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003750 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003751
3752 /* If no other thread is using the ndlp, free it */
3753 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003754
James Smart76a95d72010-11-20 23:11:48 -05003755 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003756 /*
3757 * RegLogin failed, use loop map to make discovery
3758 * list
3759 */
3760 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003761
James Smart92d7f7b2007-06-17 19:56:38 -05003762 /* Start discovery */
3763 lpfc_disc_start(vport);
3764 return;
3765 }
3766 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003767 return;
3768 }
3769
James Smart6d368e52011-05-24 11:44:12 -04003770 if (phba->sli_rev < LPFC_SLI_REV4)
3771 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003772 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003773 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003774 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003775
James Smart2e0fef82007-06-17 19:56:36 -05003776 if (vport->port_state < LPFC_VPORT_READY) {
3777 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003778 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3779 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3780 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3781 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3782 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3783
3784 /* Issue SCR just before NameServer GID_FT Query */
3785 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003786 }
3787
James Smart2e0fef82007-06-17 19:56:36 -05003788 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05003789 /* Good status, issue CT Request to NameServer */
James Smart92d7f7b2007-06-17 19:56:38 -05003790 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea31012005-04-17 16:05:31 -05003791 /* Cannot issue NameServer Query, so finish up discovery */
James Smart92d7f7b2007-06-17 19:56:38 -05003792 goto out;
dea31012005-04-17 16:05:31 -05003793 }
3794
James Smartfa4066b2008-01-11 01:53:27 -05003795 /* decrement the node reference count held for this
3796 * callback function.
3797 */
James Smart329f9bc2007-04-25 09:53:01 -04003798 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003799 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3800 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003801 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003802
3803 return;
3804}
3805
3806static void
James Smart2e0fef82007-06-17 19:56:36 -05003807lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003808{
James Smart2e0fef82007-06-17 19:56:36 -05003809 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3810 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003811 struct lpfc_rport_data *rdata;
3812 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003813 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003814
3815 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003816 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3817 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003818 rport_ids.port_id = ndlp->nlp_DID;
3819 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003820
James Smart329f9bc2007-04-25 09:53:01 -04003821 /*
3822 * We leave our node pointer in rport->dd_data when we unregister a
3823 * FCP target port. But fc_remote_port_add zeros the space to which
3824 * rport->dd_data points. So, if we're reusing a previously
3825 * registered port, drop the reference that we took the last time we
3826 * registered the port.
3827 */
3828 if (ndlp->rport && ndlp->rport->dd_data &&
James Smarte47c9092008-02-08 18:49:26 -05003829 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
James Smart329f9bc2007-04-25 09:53:01 -04003830 lpfc_nlp_put(ndlp);
James Smart858c9f62007-06-17 19:56:39 -05003831
3832 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3833 "rport add: did:x%x flg:x%x type x%x",
3834 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3835
James Smart9589b0622011-04-16 11:03:17 -04003836 /* Don't add the remote port if unloading. */
3837 if (vport->load_flag & FC_UNLOADING)
3838 return;
3839
James Smart2e0fef82007-06-17 19:56:36 -05003840 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04003841 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05003842 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3843 "Warning: fc_remote_port_add failed\n");
3844 return;
3845 }
3846
3847 /* initialize static port data */
3848 rport->maxframe_size = ndlp->nlp_maxframe;
3849 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05003850 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04003851 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003852
3853 if (ndlp->nlp_type & NLP_FCP_TARGET)
3854 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3855 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3856 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3857
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05003858 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3859 fc_remote_port_rolechg(rport, rport_ids.roles);
3860
James Smart34f5ad82012-08-03 12:35:03 -04003861 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3862 "3183 rport register x%06x, rport %p role x%x\n",
3863 ndlp->nlp_DID, rport, rport_ids.roles);
3864
James Smart071fbd3d2006-04-15 11:53:20 -04003865 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05003866 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04003867 ndlp->nlp_sid = rport->scsi_target_id;
3868 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003869 return;
3870}
3871
3872static void
James Smart2e0fef82007-06-17 19:56:36 -05003873lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003874{
3875 struct fc_rport *rport = ndlp->rport;
James Smartc01f3202006-08-18 17:47:08 -04003876
James Smart858c9f62007-06-17 19:56:39 -05003877 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3878 "rport delete: did:x%x flg:x%x type x%x",
3879 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3880
James Smart34f5ad82012-08-03 12:35:03 -04003881 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3882 "3184 rport unregister x%06x, rport %p\n",
3883 ndlp->nlp_DID, rport);
3884
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003885 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05003886
3887 return;
3888}
3889
James Smartde0c5b32007-04-25 09:52:27 -04003890static void
James Smart2e0fef82007-06-17 19:56:36 -05003891lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04003892{
James Smart2e0fef82007-06-17 19:56:36 -05003893 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3894
3895 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003896 switch (state) {
3897 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003898 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003899 break;
3900 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003901 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003902 break;
3903 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003904 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003905 break;
3906 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003907 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003908 break;
3909 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05003910 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003911 break;
3912 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003913 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003914 break;
3915 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003916 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003917 break;
3918 case NLP_STE_NPR_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05003919 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04003920 break;
3921 }
James Smart2e0fef82007-06-17 19:56:36 -05003922 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003923}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05003924
James Smartde0c5b32007-04-25 09:52:27 -04003925static void
James Smart2e0fef82007-06-17 19:56:36 -05003926lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04003927 int old_state, int new_state)
3928{
James Smart2e0fef82007-06-17 19:56:36 -05003929 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3930
James Smartde0c5b32007-04-25 09:52:27 -04003931 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04003932 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3933 ndlp->nlp_type |= NLP_FC_NODE;
3934 }
3935 if (new_state == NLP_STE_MAPPED_NODE)
3936 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3937 if (new_state == NLP_STE_NPR_NODE)
3938 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05003939
James Smartde0c5b32007-04-25 09:52:27 -04003940 /* Transport interface */
3941 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3942 old_state == NLP_STE_UNMAPPED_NODE)) {
James Smart2e0fef82007-06-17 19:56:36 -05003943 vport->phba->nport_event_cnt++;
3944 lpfc_unregister_remote_port(ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003945 }
dea31012005-04-17 16:05:31 -05003946
James Smartde0c5b32007-04-25 09:52:27 -04003947 if (new_state == NLP_STE_MAPPED_NODE ||
3948 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart2e0fef82007-06-17 19:56:36 -05003949 vport->phba->nport_event_cnt++;
James Smart858c9f62007-06-17 19:56:39 -05003950 /*
3951 * Tell the fc transport about the port, if we haven't
3952 * already. If we have, and it's a scsi entity, be
3953 * sure to unblock any attached scsi devices
3954 */
3955 lpfc_register_remote_port(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04003956 }
James Smartea2151b2008-09-07 11:52:10 -04003957 if ((new_state == NLP_STE_MAPPED_NODE) &&
3958 (vport->stat_data_enabled)) {
3959 /*
3960 * A new target is discovered, if there is no buffer for
3961 * statistical data collection allocate buffer.
3962 */
3963 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3964 sizeof(struct lpfc_scsicmd_bkt),
3965 GFP_KERNEL);
3966
3967 if (!ndlp->lat_data)
3968 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3969 "0286 lpfc_nlp_state_cleanup failed to "
3970 "allocate statistical data buffer DID "
3971 "0x%x\n", ndlp->nlp_DID);
3972 }
James Smart858c9f62007-06-17 19:56:39 -05003973 /*
3974 * if we added to Mapped list, but the remote port
3975 * registration failed or assigned a target id outside
3976 * our presentable range - move the node to the
3977 * Unmapped List
3978 */
James Smartde0c5b32007-04-25 09:52:27 -04003979 if (new_state == NLP_STE_MAPPED_NODE &&
3980 (!ndlp->rport ||
3981 ndlp->rport->scsi_target_id == -1 ||
3982 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05003983 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04003984 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05003985 spin_unlock_irq(shost->host_lock);
3986 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003987 }
James Smartde0c5b32007-04-25 09:52:27 -04003988}
3989
James Smart685f0bf2007-04-25 09:53:08 -04003990static char *
3991lpfc_nlp_state_name(char *buffer, size_t size, int state)
3992{
3993 static char *states[] = {
3994 [NLP_STE_UNUSED_NODE] = "UNUSED",
3995 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
3996 [NLP_STE_ADISC_ISSUE] = "ADISC",
3997 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
3998 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04003999 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004000 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4001 [NLP_STE_MAPPED_NODE] = "MAPPED",
4002 [NLP_STE_NPR_NODE] = "NPR",
4003 };
4004
James Smart311464e2007-08-02 11:10:37 -04004005 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004006 strlcpy(buffer, states[state], size);
4007 else
4008 snprintf(buffer, size, "unknown (%d)", state);
4009 return buffer;
4010}
4011
James Smartde0c5b32007-04-25 09:52:27 -04004012void
James Smart2e0fef82007-06-17 19:56:36 -05004013lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4014 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004015{
James Smart2e0fef82007-06-17 19:56:36 -05004016 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004017 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004018 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004019
James Smarte8b62012007-08-02 11:10:09 -04004020 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4021 "0904 NPort state transition x%06x, %s -> %s\n",
4022 ndlp->nlp_DID,
4023 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4024 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004025
4026 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4027 "node statechg did:x%x old:%d ste:%d",
4028 ndlp->nlp_DID, old_state, state);
4029
James Smartde0c5b32007-04-25 09:52:27 -04004030 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004031 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004032 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004033 if (old_state == NLP_STE_UNMAPPED_NODE) {
4034 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4035 ndlp->nlp_type &= ~NLP_FC_NODE;
4036 }
4037
James Smart685f0bf2007-04-25 09:53:08 -04004038 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004039 spin_lock_irq(shost->host_lock);
4040 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4041 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004042 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004043 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004044
4045 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004046 lpfc_nlp_counters(vport, state, 1);
4047 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004048}
4049
4050void
James Smarte47c9092008-02-08 18:49:26 -05004051lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4052{
4053 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4054
4055 if (list_empty(&ndlp->nlp_listp)) {
4056 spin_lock_irq(shost->host_lock);
4057 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4058 spin_unlock_irq(shost->host_lock);
4059 }
4060}
4061
4062void
James Smart2e0fef82007-06-17 19:56:36 -05004063lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004064{
James Smart2e0fef82007-06-17 19:56:36 -05004065 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4066
James Smart0d2b6b82008-06-14 22:52:47 -04004067 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004068 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004069 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4070 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004071 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004072 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004073 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004074 NLP_STE_UNUSED_NODE);
4075}
4076
Adrian Bunk4d9db012008-02-14 23:24:02 +02004077static void
James Smarte47c9092008-02-08 18:49:26 -05004078lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4079{
James Smart0d2b6b82008-06-14 22:52:47 -04004080 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004081 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4082 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4083 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4084 NLP_STE_UNUSED_NODE);
4085}
James Smart109f6ed2008-12-04 22:39:08 -05004086/**
James Smart3621a712009-04-06 18:47:14 -04004087 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004088 * @vport: Pointer to Virtual Port object.
4089 * @ndlp: Pointer to FC node object.
4090 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004091 *
4092 * This function is always called when node object need to be initialized.
4093 * It initializes all the fields of the node object. Although the reference
4094 * to phba from @ndlp can be obtained indirectly through it's reference to
4095 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4096 * to the life-span of the @ndlp might go beyond the existence of @vport as
4097 * the final release of ndlp is determined by its reference count. And, the
4098 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004099 **/
4100static inline void
4101lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4102 uint32_t did)
4103{
4104 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4105 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4106 init_timer(&ndlp->nlp_delayfunc);
4107 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4108 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4109 ndlp->nlp_DID = did;
4110 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004111 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004112 ndlp->nlp_sid = NLP_NO_SID;
4113 kref_init(&ndlp->kref);
4114 NLP_INT_NODE_ACT(ndlp);
4115 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004116 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart40426292010-12-15 17:58:10 -05004117 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4118 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smart109f6ed2008-12-04 22:39:08 -05004119}
James Smarte47c9092008-02-08 18:49:26 -05004120
4121struct lpfc_nodelist *
4122lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4123 int state)
4124{
4125 struct lpfc_hba *phba = vport->phba;
4126 uint32_t did;
4127 unsigned long flags;
4128
4129 if (!ndlp)
4130 return NULL;
4131
4132 spin_lock_irqsave(&phba->ndlp_lock, flags);
4133 /* The ndlp should not be in memory free mode */
4134 if (NLP_CHK_FREE_REQ(ndlp)) {
4135 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4136 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4137 "0277 lpfc_enable_node: ndlp:x%p "
4138 "usgmap:x%x refcnt:%d\n",
4139 (void *)ndlp, ndlp->nlp_usg_map,
4140 atomic_read(&ndlp->kref.refcount));
4141 return NULL;
4142 }
4143 /* The ndlp should not already be in active mode */
4144 if (NLP_CHK_NODE_ACT(ndlp)) {
4145 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4146 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4147 "0278 lpfc_enable_node: ndlp:x%p "
4148 "usgmap:x%x refcnt:%d\n",
4149 (void *)ndlp, ndlp->nlp_usg_map,
4150 atomic_read(&ndlp->kref.refcount));
4151 return NULL;
4152 }
4153
4154 /* Keep the original DID */
4155 did = ndlp->nlp_DID;
4156
4157 /* re-initialize ndlp except of ndlp linked list pointer */
4158 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4159 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004160 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004161
4162 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4163
4164 if (state != NLP_STE_UNUSED_NODE)
4165 lpfc_nlp_set_state(vport, ndlp, state);
4166
4167 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4168 "node enable: did:x%x",
4169 ndlp->nlp_DID, 0, 0);
4170 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004171}
4172
4173void
James Smart2e0fef82007-06-17 19:56:36 -05004174lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004175{
James Smart87af33f2007-10-27 13:37:43 -04004176 /*
James Smartfa4066b2008-01-11 01:53:27 -05004177 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004178 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004179 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4180 * until ALL other outstanding threads have completed. We check
4181 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004182 */
James Smartfa4066b2008-01-11 01:53:27 -05004183 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4184 return;
James Smart51ef4c22007-08-02 11:10:31 -04004185 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smart1151e3e2011-02-16 12:39:35 -05004186 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4187 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04004188 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004189 return;
dea31012005-04-17 16:05:31 -05004190}
4191
4192/*
4193 * Start / ReStart rescue timer for Discovery / RSCN handling
4194 */
4195void
James Smart2e0fef82007-06-17 19:56:36 -05004196lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004197{
James Smart2e0fef82007-06-17 19:56:36 -05004198 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4199 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004200 uint32_t tmo;
4201
James Smart2e0fef82007-06-17 19:56:36 -05004202 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004203 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004204 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4205 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004206 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004207 * FC spec states we need 3 * ratov for CT requests
4208 */
4209 tmo = ((phba->fc_ratov * 3) + 3);
4210 }
dea31012005-04-17 16:05:31 -05004211
James Smart858c9f62007-06-17 19:56:39 -05004212
4213 if (!timer_pending(&vport->fc_disctmo)) {
4214 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4215 "set disc timer: tmo:x%x state:x%x flg:x%x",
4216 tmo, vport->port_state, vport->fc_flag);
4217 }
4218
James Smart2e0fef82007-06-17 19:56:36 -05004219 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
4220 spin_lock_irq(shost->host_lock);
4221 vport->fc_flag |= FC_DISC_TMO;
4222 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004223
4224 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004225 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4226 "0247 Start Discovery Timer state x%x "
4227 "Data: x%x x%lx x%x x%x\n",
4228 vport->port_state, tmo,
4229 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4230 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004231
4232 return;
4233}
4234
4235/*
4236 * Cancel rescue timer for Discovery / RSCN handling
4237 */
4238int
James Smart2e0fef82007-06-17 19:56:36 -05004239lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004240{
James Smart2e0fef82007-06-17 19:56:36 -05004241 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004242 unsigned long iflags;
4243
James Smart858c9f62007-06-17 19:56:39 -05004244 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4245 "can disc timer: state:x%x rtry:x%x flg:x%x",
4246 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4247
dea31012005-04-17 16:05:31 -05004248 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004249 if (vport->fc_flag & FC_DISC_TMO) {
4250 spin_lock_irqsave(shost->host_lock, iflags);
4251 vport->fc_flag &= ~FC_DISC_TMO;
4252 spin_unlock_irqrestore(shost->host_lock, iflags);
4253 del_timer_sync(&vport->fc_disctmo);
4254 spin_lock_irqsave(&vport->work_port_lock, iflags);
4255 vport->work_port_events &= ~WORKER_DISC_TMO;
4256 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004257 }
4258
4259 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004260 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4261 "0248 Cancel Discovery Timer state x%x "
4262 "Data: x%x x%x x%x\n",
4263 vport->port_state, vport->fc_flag,
4264 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004265 return 0;
dea31012005-04-17 16:05:31 -05004266}
4267
4268/*
4269 * Check specified ring for outstanding IOCB on the SLI queue
4270 * Return true if iocb matches the specified nport
4271 */
4272int
James Smart2e0fef82007-06-17 19:56:36 -05004273lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4274 struct lpfc_sli_ring *pring,
4275 struct lpfc_iocbq *iocb,
4276 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004277{
James Smart2e0fef82007-06-17 19:56:36 -05004278 struct lpfc_sli *psli = &phba->sli;
4279 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004280 struct lpfc_vport *vport = ndlp->vport;
4281
4282 if (iocb->vport != vport)
4283 return 0;
4284
dea31012005-04-17 16:05:31 -05004285 if (pring->ringno == LPFC_ELS_RING) {
4286 switch (icmd->ulpCommand) {
4287 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004288 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004289 return 1;
dea31012005-04-17 16:05:31 -05004290 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004291 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4292 return 1;
dea31012005-04-17 16:05:31 -05004293 case CMD_XMIT_ELS_RSP64_CX:
4294 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004295 return 1;
dea31012005-04-17 16:05:31 -05004296 }
James Smarta4bc3372006-12-02 13:34:16 -05004297 } else if (pring->ringno == psli->extra_ring) {
dea31012005-04-17 16:05:31 -05004298
4299 } else if (pring->ringno == psli->fcp_ring) {
4300 /* Skip match check if waiting to relogin to FCP target */
4301 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004302 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004303 return 0;
dea31012005-04-17 16:05:31 -05004304 }
4305 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004306 return 1;
dea31012005-04-17 16:05:31 -05004307 }
4308 } else if (pring->ringno == psli->next_ring) {
4309
4310 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004311 return 0;
dea31012005-04-17 16:05:31 -05004312}
4313
4314/*
4315 * Free resources / clean up outstanding I/Os
4316 * associated with nlp_rpi in the LPFC_NODELIST entry.
4317 */
4318static int
James Smart2e0fef82007-06-17 19:56:36 -05004319lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004320{
James Smart2534ba72007-04-25 09:52:20 -04004321 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004322 struct lpfc_sli *psli;
4323 struct lpfc_sli_ring *pring;
4324 struct lpfc_iocbq *iocb, *next_iocb;
James Smart6a9c52c2009-10-02 15:16:51 -04004325 uint32_t i;
dea31012005-04-17 16:05:31 -05004326
James Smart92d7f7b2007-06-17 19:56:38 -05004327 lpfc_fabric_abort_nport(ndlp);
4328
dea31012005-04-17 16:05:31 -05004329 /*
4330 * Everything that matches on txcmplq will be returned
4331 * by firmware with a no rpi error.
4332 */
4333 psli = &phba->sli;
James Smart40426292010-12-15 17:58:10 -05004334 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea31012005-04-17 16:05:31 -05004335 /* Now process each ring */
4336 for (i = 0; i < psli->num_rings; i++) {
4337 pring = &psli->ring[i];
4338
James Smart2e0fef82007-06-17 19:56:36 -05004339 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004340 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
James Smart2e0fef82007-06-17 19:56:36 -05004341 list) {
dea31012005-04-17 16:05:31 -05004342 /*
4343 * Check to see if iocb matches the nport we are
4344 * looking for
4345 */
James Smart92d7f7b2007-06-17 19:56:38 -05004346 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4347 ndlp))) {
dea31012005-04-17 16:05:31 -05004348 /* It matches, so deque and call compl
4349 with an error */
James Smart2534ba72007-04-25 09:52:20 -04004350 list_move_tail(&iocb->list,
4351 &completions);
dea31012005-04-17 16:05:31 -05004352 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05004353 }
4354 }
James Smart2e0fef82007-06-17 19:56:36 -05004355 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004356 }
4357 }
James Smart2534ba72007-04-25 09:52:20 -04004358
James Smarta257bf92009-04-06 18:48:10 -04004359 /* Cancel all the IOCBs from the completions list */
4360 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4361 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004362
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004363 return 0;
dea31012005-04-17 16:05:31 -05004364}
4365
James Smart086a3452012-08-14 14:25:21 -04004366/**
4367 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4368 * @phba: Pointer to HBA context object.
4369 * @pmb: Pointer to mailbox object.
4370 *
4371 * This function will issue an ELS LOGO command after completing
4372 * the UNREG_RPI.
4373 **/
4374void
4375lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4376{
4377 struct lpfc_vport *vport = pmb->vport;
4378 struct lpfc_nodelist *ndlp;
4379
4380 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4381 if (!ndlp)
4382 return;
4383 lpfc_issue_els_logo(vport, ndlp, 0);
4384}
4385
dea31012005-04-17 16:05:31 -05004386/*
4387 * Free rpi associated with LPFC_NODELIST entry.
4388 * This routine is called from lpfc_freenode(), when we are removing
4389 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4390 * LOGO that completes successfully, and we are waiting to PLOGI back
4391 * to the remote NPort. In addition, it is called after we receive
4392 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4393 * we are waiting to PLOGI back to the remote NPort.
4394 */
4395int
James Smart2e0fef82007-06-17 19:56:36 -05004396lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004397{
James Smart2e0fef82007-06-17 19:56:36 -05004398 struct lpfc_hba *phba = vport->phba;
4399 LPFC_MBOXQ_t *mbox;
dea31012005-04-17 16:05:31 -05004400 int rc;
James Smart6d368e52011-05-24 11:44:12 -04004401 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004402
James Smart40426292010-12-15 17:58:10 -05004403 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart2e0fef82007-06-17 19:56:36 -05004404 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4405 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004406 /* SLI4 ports require the physical rpi value. */
4407 rpi = ndlp->nlp_rpi;
4408 if (phba->sli_rev == LPFC_SLI_REV4)
4409 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004410
James Smart6d368e52011-05-24 11:44:12 -04004411 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004412 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004413 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4414 mbox->context1 = ndlp;
4415 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4416 } else {
4417 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4418 }
4419
James Smart0b727fe2007-10-27 13:37:25 -04004420 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -05004421 if (rc == MBX_NOT_FINISHED)
James Smart2e0fef82007-06-17 19:56:36 -05004422 mempool_free(mbox, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004423 }
dea31012005-04-17 16:05:31 -05004424 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004425
James Smart40426292010-12-15 17:58:10 -05004426 if (phba->sli_rev != LPFC_SLI_REV4)
4427 ndlp->nlp_rpi = 0;
4428 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004429 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea31012005-04-17 16:05:31 -05004430 return 1;
4431 }
4432 return 0;
4433}
4434
James Smartecfd03c2010-02-12 14:41:27 -05004435/**
4436 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4437 * @phba: pointer to lpfc hba data structure.
4438 *
4439 * This routine is invoked to unregister all the currently registered RPIs
4440 * to the HBA.
4441 **/
4442void
4443lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4444{
4445 struct lpfc_vport **vports;
4446 struct lpfc_nodelist *ndlp;
4447 struct Scsi_Host *shost;
4448 int i;
4449
4450 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004451 if (!vports) {
4452 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4453 "2884 Vport array allocation failed \n");
4454 return;
4455 }
James Smartecfd03c2010-02-12 14:41:27 -05004456 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4457 shost = lpfc_shost_from_vport(vports[i]);
4458 spin_lock_irq(shost->host_lock);
4459 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004460 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004461 /* The mempool_alloc might sleep */
4462 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004463 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004464 spin_lock_irq(shost->host_lock);
4465 }
James Smartecfd03c2010-02-12 14:41:27 -05004466 }
4467 spin_unlock_irq(shost->host_lock);
4468 }
4469 lpfc_destroy_vport_work_array(phba, vports);
4470}
4471
James Smart92d7f7b2007-06-17 19:56:38 -05004472void
4473lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4474{
4475 struct lpfc_hba *phba = vport->phba;
4476 LPFC_MBOXQ_t *mbox;
4477 int rc;
4478
James Smart5af5eee2010-10-22 11:06:38 -04004479 if (phba->sli_rev == LPFC_SLI_REV4) {
4480 lpfc_sli4_unreg_all_rpis(vport);
4481 return;
4482 }
4483
James Smart92d7f7b2007-06-17 19:56:38 -05004484 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4485 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004486 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4487 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004488 mbox->vport = vport;
4489 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004490 mbox->context1 = NULL;
4491 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004492 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004493 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004494
4495 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4496 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4497 "1836 Could not issue "
4498 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004499 }
4500}
4501
4502void
4503lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4504{
4505 struct lpfc_hba *phba = vport->phba;
4506 LPFC_MBOXQ_t *mbox;
4507 int rc;
4508
4509 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4510 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004511 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4512 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004513 mbox->vport = vport;
4514 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004515 mbox->context1 = NULL;
4516 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004517 if (rc != MBX_TIMEOUT)
4518 mempool_free(mbox, phba->mbox_mem_pool);
4519
4520 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004521 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4522 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004523 "unreg_did (default rpis) status %d\n",
4524 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004525 }
4526}
4527
dea31012005-04-17 16:05:31 -05004528/*
4529 * Free resources associated with LPFC_NODELIST entry
4530 * so it can be freed.
4531 */
4532static int
James Smart2e0fef82007-06-17 19:56:36 -05004533lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004534{
James Smart2e0fef82007-06-17 19:56:36 -05004535 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4536 struct lpfc_hba *phba = vport->phba;
4537 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004538 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004539
4540 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004541 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4542 "0900 Cleanup node for NPort x%x "
4543 "Data: x%x x%x x%x\n",
4544 ndlp->nlp_DID, ndlp->nlp_flag,
4545 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004546 if (NLP_CHK_FREE_REQ(ndlp)) {
4547 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4548 "0280 lpfc_cleanup_node: ndlp:x%p "
4549 "usgmap:x%x refcnt:%d\n",
4550 (void *)ndlp, ndlp->nlp_usg_map,
4551 atomic_read(&ndlp->kref.refcount));
4552 lpfc_dequeue_node(vport, ndlp);
4553 } else {
4554 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4555 "0281 lpfc_cleanup_node: ndlp:x%p "
4556 "usgmap:x%x refcnt:%d\n",
4557 (void *)ndlp, ndlp->nlp_usg_map,
4558 atomic_read(&ndlp->kref.refcount));
4559 lpfc_disable_node(vport, ndlp);
4560 }
dea31012005-04-17 16:05:31 -05004561
James Smart086a3452012-08-14 14:25:21 -04004562
4563 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4564
dea31012005-04-17 16:05:31 -05004565 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4566 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004567 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004568 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004569 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4570 mb->context2 = NULL;
4571 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4572 }
4573 }
James Smart33ccf8d2006-08-17 11:57:58 -04004574
James Smart2e0fef82007-06-17 19:56:36 -05004575 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004576 /* Cleanup REG_LOGIN completions which are not yet processed */
4577 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4578 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004579 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004580 (ndlp != (struct lpfc_nodelist *) mb->context2))
4581 continue;
4582
4583 mb->context2 = NULL;
4584 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4585 }
4586
dea31012005-04-17 16:05:31 -05004587 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004588 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004589 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004590 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004591 mp = (struct lpfc_dmabuf *) (mb->context1);
4592 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004593 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004594 kfree(mp);
4595 }
4596 list_del(&mb->list);
4597 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004598 /* We shall not invoke the lpfc_nlp_put to decrement
4599 * the ndlp reference count as we are in the process
4600 * of lpfc_nlp_release.
4601 */
dea31012005-04-17 16:05:31 -05004602 }
4603 }
James Smart2e0fef82007-06-17 19:56:36 -05004604 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004605
James Smarte47c9092008-02-08 18:49:26 -05004606 lpfc_els_abort(phba, ndlp);
4607
James Smart2e0fef82007-06-17 19:56:36 -05004608 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004609 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004610 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004611
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004612 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004613 del_timer_sync(&ndlp->nlp_delayfunc);
4614
James Smart0d2b6b82008-06-14 22:52:47 -04004615 list_del_init(&ndlp->els_retry_evt.evt_listp);
4616 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004617 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004618 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004619
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004620 return 0;
dea31012005-04-17 16:05:31 -05004621}
4622
4623/*
4624 * Check to see if we can free the nlp back to the freelist.
4625 * If we are in the middle of using the nlp in the discovery state
4626 * machine, defer the free till we reach the end of the state machine.
4627 */
James Smart329f9bc2007-04-25 09:53:01 -04004628static void
James Smart2e0fef82007-06-17 19:56:36 -05004629lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004630{
James Smarta8adb832007-10-27 13:37:53 -04004631 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004632 struct lpfc_rport_data *rdata;
James Smarta8adb832007-10-27 13:37:53 -04004633 LPFC_MBOXQ_t *mbox;
4634 int rc;
dea31012005-04-17 16:05:31 -05004635
James Smart0d2b6b82008-06-14 22:52:47 -04004636 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004637 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004638 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4639 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004640 /* For this case we need to cleanup the default rpi
4641 * allocated by the firmware.
4642 */
4643 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4644 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004645 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004646 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004647 if (rc) {
4648 mempool_free(mbox, phba->mbox_mem_pool);
4649 }
4650 else {
4651 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4652 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4653 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004654 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004655 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4656 if (rc == MBX_NOT_FINISHED) {
4657 mempool_free(mbox, phba->mbox_mem_pool);
4658 }
4659 }
4660 }
4661 }
James Smart2e0fef82007-06-17 19:56:36 -05004662 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004663
James Smart2e0fef82007-06-17 19:56:36 -05004664 /*
James Smart92d7f7b2007-06-17 19:56:38 -05004665 * We can get here with a non-NULL ndlp->rport because when we
4666 * unregister a rport we don't break the rport/node linkage. So if we
4667 * do, make sure we don't leaving any dangling pointers behind.
James Smart2e0fef82007-06-17 19:56:36 -05004668 */
James Smart92d7f7b2007-06-17 19:56:38 -05004669 if (ndlp->rport) {
James Smart329f9bc2007-04-25 09:53:01 -04004670 rdata = ndlp->rport->dd_data;
4671 rdata->pnode = NULL;
4672 ndlp->rport = NULL;
dea31012005-04-17 16:05:31 -05004673 }
dea31012005-04-17 16:05:31 -05004674}
4675
4676static int
James Smart2e0fef82007-06-17 19:56:36 -05004677lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4678 uint32_t did)
dea31012005-04-17 16:05:31 -05004679{
James Smart2e0fef82007-06-17 19:56:36 -05004680 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004681
4682 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004683 return 0;
dea31012005-04-17 16:05:31 -05004684
dea31012005-04-17 16:05:31 -05004685 /* First check for Direct match */
4686 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004687 return 1;
dea31012005-04-17 16:05:31 -05004688
4689 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004690 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004691 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004692 return 0;
dea31012005-04-17 16:05:31 -05004693 }
4694
4695 matchdid.un.word = did;
4696 ndlpdid.un.word = ndlp->nlp_DID;
4697 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4698 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4699 (mydid.un.b.area == matchdid.un.b.area)) {
4700 if ((ndlpdid.un.b.domain == 0) &&
4701 (ndlpdid.un.b.area == 0)) {
4702 if (ndlpdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004703 return 1;
dea31012005-04-17 16:05:31 -05004704 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004705 return 0;
dea31012005-04-17 16:05:31 -05004706 }
4707
4708 matchdid.un.word = ndlp->nlp_DID;
4709 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4710 (mydid.un.b.area == ndlpdid.un.b.area)) {
4711 if ((matchdid.un.b.domain == 0) &&
4712 (matchdid.un.b.area == 0)) {
4713 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004714 return 1;
dea31012005-04-17 16:05:31 -05004715 }
4716 }
4717 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004718 return 0;
dea31012005-04-17 16:05:31 -05004719}
4720
James Smart685f0bf2007-04-25 09:53:08 -04004721/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05004722static struct lpfc_nodelist *
4723__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004724{
James Smart2fb9bd82006-12-02 13:33:57 -05004725 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05004726 uint32_t data1;
4727
James Smart2e0fef82007-06-17 19:56:36 -05004728 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4729 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04004730 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4731 ((uint32_t) ndlp->nlp_xri << 16) |
4732 ((uint32_t) ndlp->nlp_type << 8) |
4733 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04004734 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4735 "0929 FIND node DID "
4736 "Data: x%p x%x x%x x%x\n",
4737 ndlp, ndlp->nlp_DID,
4738 ndlp->nlp_flag, data1);
James Smart685f0bf2007-04-25 09:53:08 -04004739 return ndlp;
dea31012005-04-17 16:05:31 -05004740 }
4741 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004742
dea31012005-04-17 16:05:31 -05004743 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04004744 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4745 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05004746 return NULL;
4747}
4748
4749struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05004750lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05004751{
James Smart2e0fef82007-06-17 19:56:36 -05004752 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05004753 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05004754 unsigned long iflags;
dea31012005-04-17 16:05:31 -05004755
James Smartfedd3b72011-02-16 12:39:24 -05004756 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004757 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05004758 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05004759 return ndlp;
4760}
4761
4762struct lpfc_nodelist *
4763lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
4764{
4765 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4766 struct lpfc_nodelist *ndlp;
4767
4768 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004769 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05004770 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4771 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05004772 return NULL;
4773 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05004774 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05004775 if (!ndlp)
4776 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004777 lpfc_nlp_init(vport, ndlp, did);
4778 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4779 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004780 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004781 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004782 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05004783 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4784 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4785 if (!ndlp)
4786 return NULL;
4787 spin_lock_irq(shost->host_lock);
4788 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4789 spin_unlock_irq(shost->host_lock);
4790 return ndlp;
dea31012005-04-17 16:05:31 -05004791 }
James Smarte47c9092008-02-08 18:49:26 -05004792
James Smart58da1ff2008-04-07 10:15:56 -04004793 if ((vport->fc_flag & FC_RSCN_MODE) &&
4794 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004795 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004796 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04004797 * we don't need to try to discover it again.
4798 */
4799 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4800 return NULL;
4801
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004802 /* Since this node is marked for discovery,
4803 * delay timeout is not needed.
4804 */
James Smart0d2b6b82008-06-14 22:52:47 -04004805 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04004806 spin_lock_irq(shost->host_lock);
4807 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4808 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04004809 } else
dea31012005-04-17 16:05:31 -05004810 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004811 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004812 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04004813 * or we are already in the process of discovery on it,
4814 * we don't need to try to discover it again.
4815 */
James Smart685f0bf2007-04-25 09:53:08 -04004816 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04004817 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4818 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05004819 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004820 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4821 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004822 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05004823 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004824 }
4825 return ndlp;
4826}
4827
4828/* Build a list of nodes to discover based on the loopmap */
4829void
James Smart2e0fef82007-06-17 19:56:36 -05004830lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004831{
James Smart2e0fef82007-06-17 19:56:36 -05004832 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004833 int j;
4834 uint32_t alpa, index;
4835
James Smart2e0fef82007-06-17 19:56:36 -05004836 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004837 return;
James Smart2e0fef82007-06-17 19:56:36 -05004838
James Smart76a95d72010-11-20 23:11:48 -05004839 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05004840 return;
dea31012005-04-17 16:05:31 -05004841
4842 /* Check for loop map present or not */
4843 if (phba->alpa_map[0]) {
4844 for (j = 1; j <= phba->alpa_map[0]; j++) {
4845 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05004846 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05004847 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004848 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004849 }
4850 } else {
4851 /* No alpamap, so try all alpa's */
4852 for (j = 0; j < FC_MAXLOOP; j++) {
4853 /* If cfg_scan_down is set, start from highest
4854 * ALPA (0xef) to lowest (0x1).
4855 */
James Smart3de2a652007-08-02 11:09:59 -04004856 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05004857 index = j;
4858 else
4859 index = FC_MAXLOOP - j - 1;
4860 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05004861 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05004862 continue;
James Smart2e0fef82007-06-17 19:56:36 -05004863 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05004864 }
4865 }
4866 return;
4867}
4868
dea31012005-04-17 16:05:31 -05004869void
James Smart2e0fef82007-06-17 19:56:36 -05004870lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004871{
dea31012005-04-17 16:05:31 -05004872 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05004873 struct lpfc_sli *psli = &phba->sli;
4874 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4875 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4876 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4877 int rc;
4878
James Smart92d7f7b2007-06-17 19:56:38 -05004879 /*
4880 * if it's not a physical port or if we already send
4881 * clear_la then don't send it.
4882 */
4883 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04004884 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4885 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05004886 return;
4887
James Smart2e0fef82007-06-17 19:56:36 -05004888 /* Link up discovery */
4889 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4890 phba->link_state = LPFC_CLEAR_LA;
4891 lpfc_clear_la(phba, mbox);
4892 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4893 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004894 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05004895 if (rc == MBX_NOT_FINISHED) {
4896 mempool_free(mbox, phba->mbox_mem_pool);
4897 lpfc_disc_flush_list(vport);
4898 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4899 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4900 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05004901 phba->link_state = LPFC_HBA_ERROR;
4902 }
4903 }
4904}
4905
4906/* Reg_vpi to tell firmware to resume normal operations */
4907void
4908lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4909{
4910 LPFC_MBOXQ_t *regvpimbox;
4911
4912 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4913 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04004914 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004915 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4916 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04004917 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05004918 == MBX_NOT_FINISHED) {
4919 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05004920 }
4921 }
4922}
4923
4924/* Start Link up / RSCN discovery on NPR nodes */
4925void
4926lpfc_disc_start(struct lpfc_vport *vport)
4927{
4928 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4929 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04004930 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05004931 uint32_t clear_la_pending;
James Smart685f0bf2007-04-25 09:53:08 -04004932 int did_changed;
dea31012005-04-17 16:05:31 -05004933
James Smart2e0fef82007-06-17 19:56:36 -05004934 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05004935 return;
James Smart2e0fef82007-06-17 19:56:36 -05004936
4937 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05004938 clear_la_pending = 1;
4939 else
4940 clear_la_pending = 0;
4941
James Smart2e0fef82007-06-17 19:56:36 -05004942 if (vport->port_state < LPFC_VPORT_READY)
4943 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05004944
James Smart2e0fef82007-06-17 19:56:36 -05004945 lpfc_set_disctmo(vport);
4946
4947 if (vport->fc_prevDID == vport->fc_myDID)
dea31012005-04-17 16:05:31 -05004948 did_changed = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004949 else
dea31012005-04-17 16:05:31 -05004950 did_changed = 1;
James Smart2e0fef82007-06-17 19:56:36 -05004951
4952 vport->fc_prevDID = vport->fc_myDID;
4953 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05004954
4955 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004956 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4957 "0202 Start Discovery hba state x%x "
4958 "Data: x%x x%x x%x\n",
4959 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4960 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004961
4962 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05004963 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05004964
4965 if (num_sent)
4966 return;
4967
James Smart6d368e52011-05-24 11:44:12 -04004968 /* Register the VPI for SLI3, NON-NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05004969 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05004970 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04004971 !(vport->fc_flag & FC_RSCN_MODE) &&
4972 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart92d7f7b2007-06-17 19:56:38 -05004973 lpfc_issue_reg_vpi(phba, vport);
4974 return;
4975 }
James Smart2e0fef82007-06-17 19:56:36 -05004976
James Smart92d7f7b2007-06-17 19:56:38 -05004977 /*
4978 * For SLI2, we need to set port_state to READY and continue
4979 * discovery.
4980 */
4981 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
4982 /* If we get here, there is nothing to ADISC */
4983 if (vport->port_type == LPFC_PHYSICAL_PORT)
4984 lpfc_issue_clear_la(phba, vport);
4985
4986 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05004987 vport->num_disc_nodes = 0;
4988 /* go thru NPR nodes and issue ELS PLOGIs */
4989 if (vport->fc_npr_cnt)
4990 lpfc_els_disc_plogi(vport);
4991
4992 if (!vport->num_disc_nodes) {
4993 spin_lock_irq(shost->host_lock);
4994 vport->fc_flag &= ~FC_NDISC_ACTIVE;
4995 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05004996 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05004997 }
4998 }
James Smart92d7f7b2007-06-17 19:56:38 -05004999 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005000 } else {
5001 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005002 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005003
5004 if (num_sent)
5005 return;
5006
James Smart2e0fef82007-06-17 19:56:36 -05005007 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005008 /* Check to see if more RSCNs came in while we
5009 * were processing this one.
5010 */
James Smart2e0fef82007-06-17 19:56:36 -05005011 if ((vport->fc_rscn_id_cnt == 0) &&
5012 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5013 spin_lock_irq(shost->host_lock);
5014 vport->fc_flag &= ~FC_RSCN_MODE;
5015 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005016 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005017 } else
James Smart2e0fef82007-06-17 19:56:36 -05005018 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005019 }
5020 }
5021 return;
5022}
5023
5024/*
5025 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5026 * ring the match the sppecified nodelist.
5027 */
5028static void
James Smart2e0fef82007-06-17 19:56:36 -05005029lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005030{
James Smart2534ba72007-04-25 09:52:20 -04005031 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005032 struct lpfc_sli *psli;
5033 IOCB_t *icmd;
5034 struct lpfc_iocbq *iocb, *next_iocb;
5035 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005036
5037 psli = &phba->sli;
5038 pring = &psli->ring[LPFC_ELS_RING];
5039
5040 /* Error matching iocb on txq or txcmplq
5041 * First check the txq.
5042 */
James Smart2e0fef82007-06-17 19:56:36 -05005043 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005044 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5045 if (iocb->context1 != ndlp) {
5046 continue;
5047 }
5048 icmd = &iocb->iocb;
5049 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5050 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5051
James Smart2534ba72007-04-25 09:52:20 -04005052 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005053 pring->txq_cnt--;
dea31012005-04-17 16:05:31 -05005054 }
5055 }
5056
5057 /* Next check the txcmplq */
5058 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5059 if (iocb->context1 != ndlp) {
5060 continue;
5061 }
5062 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005063 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5064 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005065 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005066 }
5067 }
James Smart2e0fef82007-06-17 19:56:36 -05005068 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005069
James Smarta257bf92009-04-06 18:48:10 -04005070 /* Cancel all the IOCBs from the completions list */
5071 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5072 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005073}
5074
Adrian Bunka6ababd2007-11-05 18:07:33 +01005075static void
James Smart2e0fef82007-06-17 19:56:36 -05005076lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005077{
5078 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005079 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005080
James Smart2e0fef82007-06-17 19:56:36 -05005081 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5082 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005083 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005084 if (!NLP_CHK_NODE_ACT(ndlp))
5085 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005086 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5087 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5088 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005089 }
dea31012005-04-17 16:05:31 -05005090 }
5091 }
dea31012005-04-17 16:05:31 -05005092}
5093
James Smart92d7f7b2007-06-17 19:56:38 -05005094void
5095lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5096{
5097 lpfc_els_flush_rscn(vport);
5098 lpfc_els_flush_cmd(vport);
5099 lpfc_disc_flush_list(vport);
5100}
5101
dea31012005-04-17 16:05:31 -05005102/*****************************************************************************/
5103/*
5104 * NAME: lpfc_disc_timeout
5105 *
5106 * FUNCTION: Fibre Channel driver discovery timeout routine.
5107 *
5108 * EXECUTION ENVIRONMENT: interrupt only
5109 *
5110 * CALLED FROM:
5111 * Timer function
5112 *
5113 * RETURNS:
5114 * none
5115 */
5116/*****************************************************************************/
5117void
5118lpfc_disc_timeout(unsigned long ptr)
5119{
James Smart2e0fef82007-06-17 19:56:36 -05005120 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5121 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005122 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005123 unsigned long flags = 0;
5124
5125 if (unlikely(!phba))
5126 return;
5127
James Smart5e9d9b82008-06-14 22:52:53 -04005128 spin_lock_irqsave(&vport->work_port_lock, flags);
5129 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5130 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005131 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005132 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005133
James Smart5e9d9b82008-06-14 22:52:53 -04005134 if (!tmo_posted)
5135 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005136 return;
5137}
5138
5139static void
James Smart2e0fef82007-06-17 19:56:36 -05005140lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005141{
James Smart2e0fef82007-06-17 19:56:36 -05005142 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5143 struct lpfc_hba *phba = vport->phba;
5144 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005145 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005146 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005147 int rc, clrlaerr = 0;
5148
James Smart2e0fef82007-06-17 19:56:36 -05005149 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005150 return;
5151
James Smart2e0fef82007-06-17 19:56:36 -05005152 spin_lock_irq(shost->host_lock);
5153 vport->fc_flag &= ~FC_DISC_TMO;
5154 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005155
James Smart858c9f62007-06-17 19:56:39 -05005156 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5157 "disc timeout: state:x%x rtry:x%x flg:x%x",
5158 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5159
James Smart2e0fef82007-06-17 19:56:36 -05005160 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005161
5162 case LPFC_LOCAL_CFG_LINK:
James Smart2e0fef82007-06-17 19:56:36 -05005163 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5164 * FAN
5165 */
5166 /* FAN timeout */
James Smarte8b62012007-08-02 11:10:09 -04005167 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5168 "0221 FAN timeout\n");
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005169 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005170 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005171 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005172 if (!NLP_CHK_NODE_ACT(ndlp))
5173 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005174 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5175 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005176 if (ndlp->nlp_type & NLP_FABRIC) {
5177 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005178 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005179
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005180 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005181 /* Fail outstanding IO now since device
5182 * is marked for PLOGI.
5183 */
James Smart2e0fef82007-06-17 19:56:36 -05005184 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005185 }
5186 }
James Smart92d7f7b2007-06-17 19:56:38 -05005187 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005188 if (phba->sli_rev <= LPFC_SLI_REV3)
5189 lpfc_initial_flogi(vport);
5190 else
5191 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005192 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005193 }
dea31012005-04-17 16:05:31 -05005194 break;
5195
James Smart92d7f7b2007-06-17 19:56:38 -05005196 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005197 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005198 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005199 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005200 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5201 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005202 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005203
5204 /* Assume no Fabric and go on with discovery.
5205 * Check for outstanding ELS FLOGI to abort.
5206 */
5207
5208 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005209 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005210
5211 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005212 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005213 break;
5214
5215 case LPFC_FABRIC_CFG_LINK:
5216 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5217 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005218 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5219 "0223 Timeout while waiting for "
5220 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005221 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005222 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005223 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005224 lpfc_els_abort(phba, ndlp);
5225
5226 /* ReStart discovery */
5227 goto restart_disc;
dea31012005-04-17 16:05:31 -05005228
5229 case LPFC_NS_QRY:
5230 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005231 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5232 "0224 NameServer Query timeout "
5233 "Data: x%x x%x\n",
5234 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005235
James Smart92d7f7b2007-06-17 19:56:38 -05005236 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5237 /* Try it one more time */
5238 vport->fc_ns_retry++;
5239 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5240 vport->fc_ns_retry, 0);
5241 if (rc == 0)
5242 break;
dea31012005-04-17 16:05:31 -05005243 }
James Smart92d7f7b2007-06-17 19:56:38 -05005244 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005245
James Smart87af33f2007-10-27 13:37:43 -04005246restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005247 /*
5248 * Discovery is over.
5249 * set port_state to PORT_READY if SLI2.
5250 * cmpl_reg_vpi will set port_state to READY for SLI3.
5251 */
James Smart3772a992009-05-22 14:50:54 -04005252 if (phba->sli_rev < LPFC_SLI_REV4) {
5253 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5254 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005255 else {
James Smart3772a992009-05-22 14:50:54 -04005256 lpfc_issue_clear_la(phba, vport);
5257 vport->port_state = LPFC_VPORT_READY;
5258 }
dea31012005-04-17 16:05:31 -05005259 }
5260
5261 /* Setup and issue mailbox INITIALIZE LINK command */
5262 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5263 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005264 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5265 "0206 Device Discovery "
5266 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005267 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005268 break;
5269 }
5270
5271 lpfc_linkdown(phba);
5272 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5273 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005274 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005275 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005276 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005277 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005278 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005279 if (rc == MBX_NOT_FINISHED)
5280 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5281
5282 break;
5283
5284 case LPFC_DISC_AUTH:
5285 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005286 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5287 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005288 lpfc_disc_flush_list(vport);
5289
James Smart92d7f7b2007-06-17 19:56:38 -05005290 /*
5291 * set port_state to PORT_READY if SLI2.
5292 * cmpl_reg_vpi will set port_state to READY for SLI3.
5293 */
James Smart3772a992009-05-22 14:50:54 -04005294 if (phba->sli_rev < LPFC_SLI_REV4) {
5295 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5296 lpfc_issue_reg_vpi(phba, vport);
5297 else { /* NPIV Not enabled */
5298 lpfc_issue_clear_la(phba, vport);
5299 vport->port_state = LPFC_VPORT_READY;
5300 }
dea31012005-04-17 16:05:31 -05005301 }
5302 break;
5303
James Smart2e0fef82007-06-17 19:56:36 -05005304 case LPFC_VPORT_READY:
5305 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005306 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5307 "0231 RSCN timeout Data: x%x "
5308 "x%x\n",
5309 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005310
5311 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005312 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005313
James Smart2e0fef82007-06-17 19:56:36 -05005314 lpfc_els_flush_rscn(vport);
5315 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005316 }
5317 break;
James Smart2e0fef82007-06-17 19:56:36 -05005318
James Smart92d7f7b2007-06-17 19:56:38 -05005319 default:
James Smarte8b62012007-08-02 11:10:09 -04005320 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005321 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005322 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005323 break;
5324 }
5325
5326 switch (phba->link_state) {
5327 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005328 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005329 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5330 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005331 clrlaerr = 1;
5332 break;
5333
James Smart09372822008-01-11 01:52:54 -05005334 case LPFC_LINK_UP:
5335 lpfc_issue_clear_la(phba, vport);
5336 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005337 case LPFC_LINK_UNKNOWN:
5338 case LPFC_WARM_START:
5339 case LPFC_INIT_START:
5340 case LPFC_INIT_MBX_CMDS:
5341 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005342 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005343 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5344 "0230 Unexpected timeout, hba link "
5345 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005346 clrlaerr = 1;
5347 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005348
5349 case LPFC_HBA_READY:
5350 break;
dea31012005-04-17 16:05:31 -05005351 }
5352
5353 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005354 lpfc_disc_flush_list(vport);
James Smarta4bc3372006-12-02 13:34:16 -05005355 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05005356 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5357 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart2e0fef82007-06-17 19:56:36 -05005358 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005359 }
5360
5361 return;
5362}
5363
dea31012005-04-17 16:05:31 -05005364/*
5365 * This routine handles processing a NameServer REG_LOGIN mailbox
5366 * command upon completion. It is setup in the LPFC_MBOXQ
5367 * as the completion routine when the command is
5368 * handed off to the SLI layer.
5369 */
5370void
James Smart2e0fef82007-06-17 19:56:36 -05005371lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005372{
James Smart04c68492009-05-22 14:52:52 -04005373 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005374 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5375 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5376 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005377
5378 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005379 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005380
James Smart6d368e52011-05-24 11:44:12 -04005381 if (phba->sli_rev < LPFC_SLI_REV4)
5382 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005383 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005384 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005385 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05005386
James Smart2e0fef82007-06-17 19:56:36 -05005387 /*
5388 * Start issuing Fabric-Device Management Interface (FDMI) command to
5389 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5390 * fdmi-on=2 (supporting RPA/hostnmae)
dea31012005-04-17 16:05:31 -05005391 */
James Smart2e0fef82007-06-17 19:56:36 -05005392
James Smart3de2a652007-08-02 11:09:59 -04005393 if (vport->cfg_fdmi_on == 1)
James Smart2e0fef82007-06-17 19:56:36 -05005394 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
5395 else
5396 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea31012005-04-17 16:05:31 -05005397
James Smartfa4066b2008-01-11 01:53:27 -05005398 /* decrement the node reference count held for this callback
5399 * function.
5400 */
James Smart329f9bc2007-04-25 09:53:01 -04005401 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005402 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5403 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005404 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005405
5406 return;
5407}
5408
James Smart685f0bf2007-04-25 09:53:08 -04005409static int
5410lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5411{
5412 uint16_t *rpi = param;
5413
James Smarteff4a012012-01-18 16:25:25 -05005414 /* check for active node */
5415 if (!NLP_CHK_NODE_ACT(ndlp))
5416 return 0;
5417
James Smart685f0bf2007-04-25 09:53:08 -04005418 return ndlp->nlp_rpi == *rpi;
5419}
5420
5421static int
5422lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5423{
5424 return memcmp(&ndlp->nlp_portname, param,
5425 sizeof(ndlp->nlp_portname)) == 0;
5426}
5427
Adrian Bunka6ababd2007-11-05 18:07:33 +01005428static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005429__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005430{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005431 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005432
James Smart2e0fef82007-06-17 19:56:36 -05005433 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005434 if (filter(ndlp, param)) {
5435 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5436 "3185 FIND node filter %p DID "
5437 "Data: x%p x%x x%x\n",
5438 filter, ndlp, ndlp->nlp_DID,
5439 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005440 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005441 }
James Smart685f0bf2007-04-25 09:53:08 -04005442 }
James Smart34f5ad82012-08-03 12:35:03 -04005443 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5444 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005445 return NULL;
dea31012005-04-17 16:05:31 -05005446}
5447
James Smart685f0bf2007-04-25 09:53:08 -04005448/*
5449 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005450 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005451 */
5452struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005453__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005454{
James Smart2e0fef82007-06-17 19:56:36 -05005455 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005456}
5457
James Smart488d1462006-03-07 15:02:37 -05005458/*
James Smart685f0bf2007-04-25 09:53:08 -04005459 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005460 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005461 */
5462struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005463lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005464{
James Smart2e0fef82007-06-17 19:56:36 -05005465 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005466 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005467
James Smart2e0fef82007-06-17 19:56:36 -05005468 spin_lock_irq(shost->host_lock);
5469 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5470 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005471 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005472}
5473
James Smartcb69f7d2011-12-13 13:21:57 -05005474/*
5475 * This routine looks up the ndlp lists for the given RPI. If the rpi
5476 * is found, the routine returns the node element list pointer else
5477 * return NULL.
5478 */
5479struct lpfc_nodelist *
5480lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5481{
5482 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5483 struct lpfc_nodelist *ndlp;
5484
5485 spin_lock_irq(shost->host_lock);
5486 ndlp = __lpfc_findnode_rpi(vport, rpi);
5487 spin_unlock_irq(shost->host_lock);
5488 return ndlp;
5489}
5490
5491/**
5492 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5493 * @phba: pointer to lpfc hba data structure.
5494 * @vpi: the physical host virtual N_Port identifier.
5495 *
5496 * This routine finds a vport on a HBA (referred by @phba) through a
5497 * @vpi. The function walks the HBA's vport list and returns the address
5498 * of the vport with the matching @vpi.
5499 *
5500 * Return code
5501 * NULL - No vport with the matching @vpi found
5502 * Otherwise - Address to the vport with the matching @vpi.
5503 **/
5504struct lpfc_vport *
5505lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5506{
5507 struct lpfc_vport *vport;
5508 unsigned long flags;
5509 int i = 0;
5510
5511 /* The physical ports are always vpi 0 - translate is unnecessary. */
5512 if (vpi > 0) {
5513 /*
5514 * Translate the physical vpi to the logical vpi. The
5515 * vport stores the logical vpi.
5516 */
5517 for (i = 0; i < phba->max_vpi; i++) {
5518 if (vpi == phba->vpi_ids[i])
5519 break;
5520 }
5521
5522 if (i >= phba->max_vpi) {
5523 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5524 "2936 Could not find Vport mapped "
5525 "to vpi %d\n", vpi);
5526 return NULL;
5527 }
5528 }
5529
5530 spin_lock_irqsave(&phba->hbalock, flags);
5531 list_for_each_entry(vport, &phba->port_list, listentry) {
5532 if (vport->vpi == i) {
5533 spin_unlock_irqrestore(&phba->hbalock, flags);
5534 return vport;
5535 }
5536 }
5537 spin_unlock_irqrestore(&phba->hbalock, flags);
5538 return NULL;
5539}
5540
dea31012005-04-17 16:05:31 -05005541void
James Smart2e0fef82007-06-17 19:56:36 -05005542lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5543 uint32_t did)
dea31012005-04-17 16:05:31 -05005544{
5545 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005546
5547 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005548 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smart858c9f62007-06-17 19:56:39 -05005549
5550 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5551 "node init: did:x%x",
5552 ndlp->nlp_DID, 0, 0);
5553
dea31012005-04-17 16:05:31 -05005554 return;
5555}
James Smart329f9bc2007-04-25 09:53:01 -04005556
James Smart98c9ea52007-10-27 13:37:33 -04005557/* This routine releases all resources associated with a specifc NPort's ndlp
5558 * and mempool_free's the nodelist.
5559 */
James Smart311464e2007-08-02 11:10:37 -04005560static void
James Smart329f9bc2007-04-25 09:53:01 -04005561lpfc_nlp_release(struct kref *kref)
5562{
James Smarte47c9092008-02-08 18:49:26 -05005563 struct lpfc_hba *phba;
5564 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005565 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5566 kref);
James Smart858c9f62007-06-17 19:56:39 -05005567
5568 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5569 "node release: did:x%x flg:x%x type:x%x",
5570 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5571
James Smarte47c9092008-02-08 18:49:26 -05005572 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005573 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smarte47c9092008-02-08 18:49:26 -05005574 "usgmap:x%x refcnt:%d\n",
James Smart939723a2012-05-09 21:19:03 -04005575 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
James Smarte47c9092008-02-08 18:49:26 -05005576 atomic_read(&ndlp->kref.refcount));
5577
5578 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005579 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005580
5581 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005582 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005583 spin_lock_irqsave(&phba->ndlp_lock, flags);
5584 NLP_CLR_NODE_ACT(ndlp);
5585 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005586 if (phba->sli_rev == LPFC_SLI_REV4)
5587 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005588
5589 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005590 if (NLP_CHK_FREE_REQ(ndlp)) {
5591 kfree(ndlp->lat_data);
James Smarta257bf92009-04-06 18:48:10 -04005592 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005593 }
James Smart329f9bc2007-04-25 09:53:01 -04005594}
5595
James Smart98c9ea52007-10-27 13:37:33 -04005596/* This routine bumps the reference count for a ndlp structure to ensure
5597 * that one discovery thread won't free a ndlp while another discovery thread
5598 * is using it.
5599 */
James Smart329f9bc2007-04-25 09:53:01 -04005600struct lpfc_nodelist *
5601lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5602{
James Smarte47c9092008-02-08 18:49:26 -05005603 struct lpfc_hba *phba;
5604 unsigned long flags;
5605
James Smart98c9ea52007-10-27 13:37:33 -04005606 if (ndlp) {
5607 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5608 "node get: did:x%x flg:x%x refcnt:x%x",
5609 ndlp->nlp_DID, ndlp->nlp_flag,
5610 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005611 /* The check of ndlp usage to prevent incrementing the
5612 * ndlp reference count that is in the process of being
5613 * released.
5614 */
James Smarta257bf92009-04-06 18:48:10 -04005615 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005616 spin_lock_irqsave(&phba->ndlp_lock, flags);
5617 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5618 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5619 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5620 "0276 lpfc_nlp_get: ndlp:x%p "
5621 "usgmap:x%x refcnt:%d\n",
5622 (void *)ndlp, ndlp->nlp_usg_map,
5623 atomic_read(&ndlp->kref.refcount));
5624 return NULL;
5625 } else
5626 kref_get(&ndlp->kref);
5627 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005628 }
James Smart329f9bc2007-04-25 09:53:01 -04005629 return ndlp;
5630}
5631
James Smart98c9ea52007-10-27 13:37:33 -04005632/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005633 * count goes to 0, this indicates the the associated nodelist should be
5634 * freed. Returning 1 indicates the ndlp resource has been released; on the
5635 * other hand, returning 0 indicates the ndlp resource has not been released
5636 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005637 */
James Smart329f9bc2007-04-25 09:53:01 -04005638int
5639lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5640{
James Smarte47c9092008-02-08 18:49:26 -05005641 struct lpfc_hba *phba;
5642 unsigned long flags;
5643
5644 if (!ndlp)
5645 return 1;
5646
5647 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5648 "node put: did:x%x flg:x%x refcnt:x%x",
5649 ndlp->nlp_DID, ndlp->nlp_flag,
5650 atomic_read(&ndlp->kref.refcount));
James Smarta257bf92009-04-06 18:48:10 -04005651 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005652 spin_lock_irqsave(&phba->ndlp_lock, flags);
5653 /* Check the ndlp memory free acknowledge flag to avoid the
5654 * possible race condition that kref_put got invoked again
5655 * after previous one has done ndlp memory free.
5656 */
5657 if (NLP_CHK_FREE_ACK(ndlp)) {
5658 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5659 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5660 "0274 lpfc_nlp_put: ndlp:x%p "
5661 "usgmap:x%x refcnt:%d\n",
5662 (void *)ndlp, ndlp->nlp_usg_map,
5663 atomic_read(&ndlp->kref.refcount));
5664 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005665 }
James Smarte47c9092008-02-08 18:49:26 -05005666 /* Check the ndlp inactivate log flag to avoid the possible
5667 * race condition that kref_put got invoked again after ndlp
5668 * is already in inactivating state.
5669 */
5670 if (NLP_CHK_IACT_REQ(ndlp)) {
5671 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5672 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5673 "0275 lpfc_nlp_put: ndlp:x%p "
5674 "usgmap:x%x refcnt:%d\n",
5675 (void *)ndlp, ndlp->nlp_usg_map,
5676 atomic_read(&ndlp->kref.refcount));
5677 return 1;
5678 }
5679 /* For last put, mark the ndlp usage flags to make sure no
5680 * other kref_get and kref_put on the same ndlp shall get
5681 * in between the process when the final kref_put has been
5682 * invoked on this ndlp.
5683 */
5684 if (atomic_read(&ndlp->kref.refcount) == 1) {
5685 /* Indicate ndlp is put to inactive state. */
5686 NLP_SET_IACT_REQ(ndlp);
5687 /* Acknowledge ndlp memory free has been seen. */
5688 if (NLP_CHK_FREE_REQ(ndlp))
5689 NLP_SET_FREE_ACK(ndlp);
5690 }
5691 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5692 /* Note, the kref_put returns 1 when decrementing a reference
5693 * count that was 1, it invokes the release callback function,
5694 * but it still left the reference count as 1 (not actually
5695 * performs the last decrementation). Otherwise, it actually
5696 * decrements the reference count and returns 0.
5697 */
5698 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04005699}
James Smart98c9ea52007-10-27 13:37:33 -04005700
5701/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05005702 * by any other discovery thread. This routine returns 1 if the
5703 * ndlp has been freed. A return value of 0 indicates the ndlp is
5704 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04005705 */
5706int
5707lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5708{
5709 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5710 "node not used: did:x%x flg:x%x refcnt:x%x",
5711 ndlp->nlp_DID, ndlp->nlp_flag,
5712 atomic_read(&ndlp->kref.refcount));
James Smarte47c9092008-02-08 18:49:26 -05005713 if (atomic_read(&ndlp->kref.refcount) == 1)
5714 if (lpfc_nlp_put(ndlp))
5715 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04005716 return 0;
5717}
James Smart6fb120a2009-05-22 14:52:59 -04005718
5719/**
5720 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5721 * @phba: Pointer to hba context object.
5722 *
5723 * This function iterate through all FC nodes associated
5724 * will all vports to check if there is any node with
5725 * fc_rports associated with it. If there is an fc_rport
5726 * associated with the node, then the node is either in
5727 * discovered state or its devloss_timer is pending.
5728 */
5729static int
5730lpfc_fcf_inuse(struct lpfc_hba *phba)
5731{
5732 struct lpfc_vport **vports;
5733 int i, ret = 0;
5734 struct lpfc_nodelist *ndlp;
5735 struct Scsi_Host *shost;
5736
5737 vports = lpfc_create_vport_work_array(phba);
5738
James Smart63e801c2010-11-20 23:14:19 -05005739 /* If driver cannot allocate memory, indicate fcf is in use */
5740 if (!vports)
5741 return 1;
5742
James Smart6fb120a2009-05-22 14:52:59 -04005743 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5744 shost = lpfc_shost_from_vport(vports[i]);
5745 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04005746 /*
5747 * IF the CVL_RCVD bit is not set then we have sent the
5748 * flogi.
5749 * If dev_loss fires while we are waiting we do not want to
5750 * unreg the fcf.
5751 */
5752 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5753 spin_unlock_irq(shost->host_lock);
5754 ret = 1;
5755 goto out;
5756 }
James Smart6fb120a2009-05-22 14:52:59 -04005757 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5758 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5759 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5760 ret = 1;
5761 spin_unlock_irq(shost->host_lock);
5762 goto out;
James Smart80c17842012-03-01 22:35:45 -05005763 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5764 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05005765 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05005766 "2624 RPI %x DID %x flag %x "
5767 "still logged in\n",
5768 ndlp->nlp_rpi, ndlp->nlp_DID,
5769 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04005770 }
5771 }
5772 spin_unlock_irq(shost->host_lock);
5773 }
5774out:
5775 lpfc_destroy_vport_work_array(phba, vports);
5776 return ret;
5777}
5778
5779/**
5780 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5781 * @phba: Pointer to hba context object.
5782 * @mboxq: Pointer to mailbox object.
5783 *
5784 * This function frees memory associated with the mailbox command.
5785 */
James Smart1b511972011-12-13 13:23:09 -05005786void
James Smart6fb120a2009-05-22 14:52:59 -04005787lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5788{
5789 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04005790 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04005791
5792 if (mboxq->u.mb.mbxStatus) {
5793 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5794 "2555 UNREG_VFI mbxStatus error x%x "
5795 "HBA state x%x\n",
5796 mboxq->u.mb.mbxStatus, vport->port_state);
5797 }
James Smart38b92ef2010-08-04 16:11:39 -04005798 spin_lock_irq(shost->host_lock);
5799 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5800 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005801 mempool_free(mboxq, phba->mbox_mem_pool);
5802 return;
5803}
5804
5805/**
5806 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5807 * @phba: Pointer to hba context object.
5808 * @mboxq: Pointer to mailbox object.
5809 *
5810 * This function frees memory associated with the mailbox command.
5811 */
5812static void
5813lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5814{
5815 struct lpfc_vport *vport = mboxq->vport;
5816
5817 if (mboxq->u.mb.mbxStatus) {
5818 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5819 "2550 UNREG_FCFI mbxStatus error x%x "
5820 "HBA state x%x\n",
5821 mboxq->u.mb.mbxStatus, vport->port_state);
5822 }
5823 mempool_free(mboxq, phba->mbox_mem_pool);
5824 return;
5825}
5826
5827/**
James Smartecfd03c2010-02-12 14:41:27 -05005828 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04005829 * @phba: Pointer to hba context object.
5830 *
James Smartecfd03c2010-02-12 14:41:27 -05005831 * This function prepare the HBA for unregistering the currently registered
5832 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5833 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04005834 */
James Smartecfd03c2010-02-12 14:41:27 -05005835int
5836lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04005837{
James Smart6fb120a2009-05-22 14:52:59 -04005838 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05005839 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05005840 struct Scsi_Host *shost;
James Smartecfd03c2010-02-12 14:41:27 -05005841 int i, rc;
James Smart6fb120a2009-05-22 14:52:59 -04005842
James Smartecfd03c2010-02-12 14:41:27 -05005843 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04005844 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05005845 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04005846
James Smart4d9ab992009-10-02 15:16:39 -04005847 /* At this point, all discovery is aborted */
5848 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04005849
5850 /* Unregister VPIs */
5851 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05005852 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04005853 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05005854 /* Stop FLOGI/FDISC retries */
5855 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5856 if (ndlp)
5857 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04005858 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04005859 if (phba->sli_rev == LPFC_SLI_REV4)
5860 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04005861 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05005862 shost = lpfc_shost_from_vport(vports[i]);
5863 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05005864 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05005865 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05005866 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04005867 }
5868 lpfc_destroy_vport_work_array(phba, vports);
5869
James Smart695a8142010-01-26 23:08:03 -05005870 /* Cleanup any outstanding ELS commands */
5871 lpfc_els_flush_all_cmd(phba);
5872
James Smart1b511972011-12-13 13:23:09 -05005873 /* Unregister the physical port VFI */
5874 rc = lpfc_issue_unreg_vfi(phba->pport);
5875 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05005876}
5877
5878/**
5879 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5880 * @phba: Pointer to hba context object.
5881 *
5882 * This function issues synchronous unregister FCF mailbox command to HBA to
5883 * unregister the currently registered FCF record. The driver does not reset
5884 * the driver FCF usage state flags.
5885 *
5886 * Return 0 if successfully issued, none-zero otherwise.
5887 */
5888int
5889lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5890{
5891 LPFC_MBOXQ_t *mbox;
5892 int rc;
5893
James Smart6fb120a2009-05-22 14:52:59 -04005894 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5895 if (!mbox) {
5896 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005897 "2551 UNREG_FCFI mbox allocation failed"
5898 "HBA state x%x\n", phba->pport->port_state);
5899 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04005900 }
James Smart6fb120a2009-05-22 14:52:59 -04005901 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5902 mbox->vport = phba->pport;
5903 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5904 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5905
5906 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05005907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5908 "2552 Unregister FCFI command failed rc x%x "
5909 "HBA state x%x\n",
5910 rc, phba->pport->port_state);
5911 return -EINVAL;
5912 }
5913 return 0;
5914}
5915
5916/**
5917 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5918 * @phba: Pointer to hba context object.
5919 *
5920 * This function unregisters the currently reigstered FCF. This function
5921 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5922 */
5923void
5924lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5925{
5926 int rc;
5927
5928 /* Preparation for unregistering fcf */
5929 rc = lpfc_unregister_fcf_prep(phba);
5930 if (rc) {
5931 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5932 "2748 Failed to prepare for unregistering "
5933 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04005934 return;
5935 }
5936
James Smartecfd03c2010-02-12 14:41:27 -05005937 /* Now, unregister FCF record and reset HBA FCF state */
5938 rc = lpfc_sli4_unregister_fcf(phba);
5939 if (rc)
5940 return;
5941 /* Reset HBA FCF states after successful unregister FCF */
5942 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05005943 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04005944
5945 /*
5946 * If driver is not unloading, check if there is any other
5947 * FCF record that can be used for discovery.
5948 */
5949 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05005950 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04005951 return;
5952
James Smart0c9ab6f2010-02-26 14:15:57 -05005953 /* This is considered as the initial FCF discovery scan */
5954 spin_lock_irq(&phba->hbalock);
5955 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5956 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04005957
5958 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04005959 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04005960
James Smart0c9ab6f2010-02-26 14:15:57 -05005961 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04005962
James Smart0c9ab6f2010-02-26 14:15:57 -05005963 if (rc) {
5964 spin_lock_irq(&phba->hbalock);
5965 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5966 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04005967 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05005968 "2553 lpfc_unregister_unused_fcf failed "
5969 "to read FCF record HBA state x%x\n",
5970 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05005971 }
James Smartecfd03c2010-02-12 14:41:27 -05005972}
5973
5974/**
5975 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5976 * @phba: Pointer to hba context object.
5977 *
5978 * This function just unregisters the currently reigstered FCF. It does not
5979 * try to find another FCF for discovery.
5980 */
5981void
5982lpfc_unregister_fcf(struct lpfc_hba *phba)
5983{
5984 int rc;
5985
5986 /* Preparation for unregistering fcf */
5987 rc = lpfc_unregister_fcf_prep(phba);
5988 if (rc) {
5989 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5990 "2749 Failed to prepare for unregistering "
5991 "HBA's FCF record: rc=%d\n", rc);
5992 return;
5993 }
5994
5995 /* Now, unregister FCF record and reset HBA FCF state */
5996 rc = lpfc_sli4_unregister_fcf(phba);
5997 if (rc)
5998 return;
5999 /* Set proper HBA FCF states after successful unregister FCF */
6000 spin_lock_irq(&phba->hbalock);
6001 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6002 spin_unlock_irq(&phba->hbalock);
6003}
6004
6005/**
6006 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6007 * @phba: Pointer to hba context object.
6008 *
6009 * This function check if there are any connected remote port for the FCF and
6010 * if all the devices are disconnected, this function unregister FCFI.
6011 * This function also tries to use another FCF for discovery.
6012 */
6013void
6014lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6015{
6016 /*
James Smart3804dc82010-07-14 15:31:37 -04006017 * If HBA is not running in FIP mode, if HBA does not support
6018 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6019 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006020 */
6021 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006022 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006023 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006024 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006025 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006026 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006027 spin_unlock_irq(&phba->hbalock);
6028 return;
6029 }
6030 spin_unlock_irq(&phba->hbalock);
6031
6032 if (lpfc_fcf_inuse(phba))
6033 return;
6034
6035 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006036}
6037
6038/**
6039 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6040 * @phba: Pointer to hba context object.
6041 * @buff: Buffer containing the FCF connection table as in the config
6042 * region.
6043 * This function create driver data structure for the FCF connection
6044 * record table read from config region 23.
6045 */
6046static void
6047lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6048 uint8_t *buff)
6049{
6050 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6051 struct lpfc_fcf_conn_hdr *conn_hdr;
6052 struct lpfc_fcf_conn_rec *conn_rec;
6053 uint32_t record_count;
6054 int i;
6055
6056 /* Free the current connect table */
6057 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006058 &phba->fcf_conn_rec_list, list) {
6059 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006060 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006061 }
James Smart6fb120a2009-05-22 14:52:59 -04006062
6063 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6064 record_count = conn_hdr->length * sizeof(uint32_t)/
6065 sizeof(struct lpfc_fcf_conn_rec);
6066
6067 conn_rec = (struct lpfc_fcf_conn_rec *)
6068 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6069
6070 for (i = 0; i < record_count; i++) {
6071 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6072 continue;
6073 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6074 GFP_KERNEL);
6075 if (!conn_entry) {
6076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6077 "2566 Failed to allocate connection"
6078 " table entry\n");
6079 return;
6080 }
6081
6082 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6083 sizeof(struct lpfc_fcf_conn_rec));
6084 conn_entry->conn_rec.vlan_tag =
6085 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
6086 conn_entry->conn_rec.flags =
6087 le16_to_cpu(conn_entry->conn_rec.flags);
6088 list_add_tail(&conn_entry->list,
6089 &phba->fcf_conn_rec_list);
6090 }
6091}
6092
6093/**
6094 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6095 * @phba: Pointer to hba context object.
6096 * @buff: Buffer containing the FCoE parameter data structure.
6097 *
6098 * This function update driver data structure with config
6099 * parameters read from config region 23.
6100 */
6101static void
6102lpfc_read_fcoe_param(struct lpfc_hba *phba,
6103 uint8_t *buff)
6104{
6105 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6106 struct lpfc_fcoe_params *fcoe_param;
6107
6108 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6109 buff;
6110 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006111 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006112
6113 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6114 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6115 return;
6116
James Smart6fb120a2009-05-22 14:52:59 -04006117 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6118 phba->valid_vlan = 1;
6119 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6120 0xFFF;
6121 }
6122
6123 phba->fc_map[0] = fcoe_param->fc_map[0];
6124 phba->fc_map[1] = fcoe_param->fc_map[1];
6125 phba->fc_map[2] = fcoe_param->fc_map[2];
6126 return;
6127}
6128
6129/**
6130 * lpfc_get_rec_conf23 - Get a record type in config region data.
6131 * @buff: Buffer containing config region 23 data.
6132 * @size: Size of the data buffer.
6133 * @rec_type: Record type to be searched.
6134 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006135 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006136 * of the record specified by record_type. If record found, this
6137 * function return pointer to the record else return NULL.
6138 */
6139static uint8_t *
6140lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6141{
6142 uint32_t offset = 0, rec_length;
6143
6144 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6145 (size < sizeof(uint32_t)))
6146 return NULL;
6147
6148 rec_length = buff[offset + 1];
6149
6150 /*
6151 * One TLV record has one word header and number of data words
6152 * specified in the rec_length field of the record header.
6153 */
6154 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6155 <= size) {
6156 if (buff[offset] == rec_type)
6157 return &buff[offset];
6158
6159 if (buff[offset] == LPFC_REGION23_LAST_REC)
6160 return NULL;
6161
6162 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6163 rec_length = buff[offset + 1];
6164 }
6165 return NULL;
6166}
6167
6168/**
6169 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6170 * @phba: Pointer to lpfc_hba data structure.
6171 * @buff: Buffer containing config region 23 data.
6172 * @size: Size of the data buffer.
6173 *
Stefan Weileef35c22010-08-06 21:11:15 +02006174 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006175 * populate driver data structure with the parameters.
6176 */
6177void
6178lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6179 uint8_t *buff,
6180 uint32_t size)
6181{
6182 uint32_t offset = 0, rec_length;
6183 uint8_t *rec_ptr;
6184
6185 /*
6186 * If data size is less than 2 words signature and version cannot be
6187 * verified.
6188 */
6189 if (size < 2*sizeof(uint32_t))
6190 return;
6191
6192 /* Check the region signature first */
6193 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6194 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6195 "2567 Config region 23 has bad signature\n");
6196 return;
6197 }
6198
6199 offset += 4;
6200
6201 /* Check the data structure version */
6202 if (buff[offset] != LPFC_REGION23_VERSION) {
6203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6204 "2568 Config region 23 has bad version\n");
6205 return;
6206 }
6207 offset += 4;
6208
6209 rec_length = buff[offset + 1];
6210
6211 /* Read FCoE param record */
6212 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6213 size - offset, FCOE_PARAM_TYPE);
6214 if (rec_ptr)
6215 lpfc_read_fcoe_param(phba, rec_ptr);
6216
6217 /* Read FCF connection table */
6218 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6219 size - offset, FCOE_CONN_TBL_TYPE);
6220 if (rec_ptr)
6221 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6222
6223}