scsi: rename random32() to prandom_u32()
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
d4379acd 4 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
a93ff37a 23#include <linux/delay.h>
5a0e3ad6 24#include <linux/slab.h>
dea3101e 25#include <linux/pci.h>
26#include <linux/kthread.h>
27#include <linux/interrupt.h>
28
91886523 29#include <scsi/scsi.h>
dea3101e 30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_transport_fc.h>
33
da0436e9 34#include "lpfc_hw4.h"
dea3101e 35#include "lpfc_hw.h"
ea2151b4 36#include "lpfc_nl.h"
dea3101e 37#include "lpfc_disc.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
dea3101e 40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
92d7f7b0 44#include "lpfc_vport.h"
858c9f6c 45#include "lpfc_debugfs.h"
dea3101e 46
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
2e0fef85 64static void lpfc_disc_timeout_handler(struct lpfc_vport *);
a6ababd2 65static void lpfc_disc_flush_list(struct lpfc_vport *vport);
32b9793f 66static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
a93ff37a 67static int lpfc_fcf_inuse(struct lpfc_hba *);
dea3101e 68
c01f3208
JS
69void
70lpfc_terminate_rport_io(struct fc_rport *rport)
dea3101e 71{
c01f3208
JS
72 struct lpfc_rport_data *rdata;
73 struct lpfc_nodelist * ndlp;
74 struct lpfc_hba *phba;
dea3101e 75
c01f3208
JS
76 rdata = rport->dd_data;
77 ndlp = rdata->pnode;
78
58da1ffb 79 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
c01f3208
JS
80 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);
dea3101e 84 return;
85 }
86
a257bf90 87 phba = ndlp->phba;
c01f3208 88
858c9f6c
JS
89 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
c01f3208 93 if (ndlp->nlp_sid != NLP_NO_SID) {
51ef4c26
JS
94 lpfc_sli_abort_iocb(ndlp->vport,
95 &phba->sli.ring[phba->sli.fcp_ring],
96 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
c01f3208 97 }
c01f3208
JS
98}
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;
2e0fef85 108 struct lpfc_vport *vport;
858c9f6c 109 struct lpfc_hba *phba;
858c9f6c 110 struct lpfc_work_evt *evtp;
a8adb832
JS
111 int put_node;
112 int put_rport;
c01f3208
JS
113
114 rdata = rport->dd_data;
115 ndlp = rdata->pnode;
58da1ffb 116 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
c01f3208 117 return;
c01f3208 118
858c9f6c
JS
119 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
34f5ad8b
JS
126 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
a8adb832
JS
130 /* 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
34f5ad8b
JS
149 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
858c9f6c
JS
158 evtp = &ndlp->dev_loss_evt;
159
160 if (!list_empty(&evtp->evt_listp))
161 return;
162
163 spin_lock_irq(&phba->hbalock);
fa4066b6
JS
164 /* 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);
5e9d9b82
JS
168 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 }
858c9f6c
JS
173 spin_unlock_irq(&phba->hbalock);
174
858c9f6c
JS
175 return;
176}
177
a93ff37a
JS
178/**
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
858c9f6c
JS
189lpfc_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;
87af33fe
JS
196 int put_node;
197 int put_rport;
858c9f6c 198 int warn_on = 0;
a93ff37a 199 int fcf_inuse = 0;
858c9f6c
JS
200
201 rport = ndlp->rport;
202
203 if (!rport)
a93ff37a 204 return fcf_inuse;
858c9f6c
JS
205
206 rdata = rport->dd_data;
207 name = (uint8_t *) &ndlp->nlp_portname;
208 vport = ndlp->vport;
209 phba = vport->phba;
210
a93ff37a
JS
211 if (phba->sli_rev == LPFC_SLI_REV4)
212 fcf_inuse = lpfc_fcf_inuse(phba);
213
858c9f6c
JS
214 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
34f5ad8b
JS
218 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
a8adb832
JS
222 /* 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) {
09372820
JS
227 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 }
a8adb832
JS
233 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);
a93ff37a 241 return fcf_inuse;
a8adb832
JS
242 }
243
d7c255b2
JS
244 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);
a93ff37a 252 return fcf_inuse;
d7c255b2 253 }
858c9f6c 254
92d7f7b0
JS
255 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);
a93ff37a 265 return fcf_inuse;
92d7f7b0 266 }
82085718 267
dea3101e 268 if (ndlp->nlp_sid != NLP_NO_SID) {
6e8215e4 269 warn_on = 1;
dea3101e 270 /* flush the target */
51ef4c26
JS
271 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
272 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea3101e 273 }
c01f3208 274
6e8215e4 275 if (warn_on) {
e8b62011
JS
276 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
277 "0203 Devloss timeout on "
58da1ffb
JS
278 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
279 "NPort x%06x Data: x%x x%x x%x\n",
e8b62011
JS
280 *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);
6e8215e4 284 } else {
e8b62011
JS
285 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
286 "0204 Devloss timeout on "
58da1ffb
JS
287 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
288 "NPort x%06x Data: x%x x%x x%x\n",
e8b62011
JS
289 *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);
6e8215e4
JSEC
293 }
294
87af33fe
JS
295 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
2e0fef85 304 if (!(vport->load_flag & FC_UNLOADING) &&
1dcb58e5 305 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
82085718 306 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
ffc95493 307 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
589a52d6
JS
308 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
309 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
2e0fef85 310 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
6fb120a7 311
a93ff37a
JS
312 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 }
6fb120a7 380 lpfc_unregister_unused_fcf(phba);
92d7f7b0 381}
c01f3208 382
ea2151b4 383/**
3621a710 384 * lpfc_alloc_fast_evt - Allocates data structure for posting event
ea2151b4
JS
385 * @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);
6fb120a7 403 if (ret) {
ea2151b4 404 atomic_inc(&phba->fast_event_count);
6fb120a7
JS
405 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
406 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
407 }
ea2151b4
JS
408 return ret;
409}
410
411/**
3621a710 412 * lpfc_free_fast_evt - Frees event data structure
ea2151b4
JS
413 * @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/**
3621a710 428 * lpfc_send_fastpath_evt - Posts events generated from fast path
ea2151b4
JS
429 * @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) ||
eaf15d5b 459 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
ea2151b4
JS
460 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,
ddcc50f0 496 LPFC_NL_VENDOR_ID);
ea2151b4
JS
497
498 lpfc_free_fast_evt(phba, fast_evt_data);
499 return;
500}
501
dea3101e 502static void
2e0fef85 503lpfc_work_list_done(struct lpfc_hba *phba)
dea3101e 504{
505 struct lpfc_work_evt *evtp = NULL;
506 struct lpfc_nodelist *ndlp;
507 int free_evt;
a93ff37a
JS
508 int fcf_inuse;
509 uint32_t nlp_did;
dea3101e 510
2e0fef85
JS
511 spin_lock_irq(&phba->hbalock);
512 while (!list_empty(&phba->work_list)) {
dea3101e 513 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
514 evt_listp);
2e0fef85 515 spin_unlock_irq(&phba->hbalock);
dea3101e 516 free_evt = 1;
2fe165b6 517 switch (evtp->evt) {
dea3101e 518 case LPFC_EVT_ELS_RETRY:
2e0fef85 519 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea3101e 520 lpfc_els_retry_delay_handler(ndlp);
92d7f7b0 521 free_evt = 0; /* evt is part of ndlp */
fa4066b6
JS
522 /* decrement the node reference count held
523 * for this queued work
524 */
525 lpfc_nlp_put(ndlp);
dea3101e 526 break;
858c9f6c
JS
527 case LPFC_EVT_DEV_LOSS:
528 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
a93ff37a 529 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
858c9f6c 530 free_evt = 0;
fa4066b6
JS
531 /* decrement the node reference count held for
532 * this queued work
533 */
a93ff37a 534 nlp_did = ndlp->nlp_DID;
858c9f6c 535 lpfc_nlp_put(ndlp);
a93ff37a
JS
536 if (phba->sli_rev == LPFC_SLI_REV4)
537 lpfc_sli4_post_dev_loss_tmo_handler(phba,
538 fcf_inuse,
539 nlp_did);
858c9f6c 540 break;
dea3101e 541 case LPFC_EVT_ONLINE:
2e0fef85
JS
542 if (phba->link_state < LPFC_LINK_DOWN)
543 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
41415862 544 else
2e0fef85 545 *(int *) (evtp->evt_arg1) = 0;
dea3101e 546 complete((struct completion *)(evtp->evt_arg2));
547 break;
46fa311e 548 case LPFC_EVT_OFFLINE_PREP:
2e0fef85 549 if (phba->link_state >= LPFC_LINK_DOWN)
618a5230 550 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
46fa311e
JS
551 *(int *)(evtp->evt_arg1) = 0;
552 complete((struct completion *)(evtp->evt_arg2));
553 break;
554 case LPFC_EVT_OFFLINE:
555 lpfc_offline(phba);
41415862
JW
556 lpfc_sli_brdrestart(phba);
557 *(int *)(evtp->evt_arg1) =
46fa311e
JS
558 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
559 lpfc_unblock_mgmt_io(phba);
41415862
JW
560 complete((struct completion *)(evtp->evt_arg2));
561 break;
562 case LPFC_EVT_WARM_START:
46fa311e 563 lpfc_offline(phba);
9290831f 564 lpfc_reset_barrier(phba);
41415862
JW
565 lpfc_sli_brdreset(phba);
566 lpfc_hba_down_post(phba);
567 *(int *)(evtp->evt_arg1) =
568 lpfc_sli_brdready(phba, HS_MBRDY);
46fa311e 569 lpfc_unblock_mgmt_io(phba);
41415862
JW
570 complete((struct completion *)(evtp->evt_arg2));
571 break;
572 case LPFC_EVT_KILL:
46fa311e 573 lpfc_offline(phba);
9290831f 574 *(int *)(evtp->evt_arg1)
2e0fef85
JS
575 = (phba->pport->stopped)
576 ? 0 : lpfc_sli_brdkill(phba);
46fa311e 577 lpfc_unblock_mgmt_io(phba);
dea3101e 578 complete((struct completion *)(evtp->evt_arg2));
579 break;
ea2151b4
JS
580 case LPFC_EVT_FASTPATH_MGMT_EVT:
581 lpfc_send_fastpath_evt(phba, evtp);
582 free_evt = 0;
583 break;
78730cfe
JS
584 case LPFC_EVT_RESET_HBA:
585 if (!(phba->pport->load_flag & FC_UNLOADING))
586 lpfc_reset_hba(phba);
587 break;
dea3101e 588 }
589 if (free_evt)
590 kfree(evtp);
2e0fef85 591 spin_lock_irq(&phba->hbalock);
dea3101e 592 }
2e0fef85 593 spin_unlock_irq(&phba->hbalock);
dea3101e 594
595}
596
311464ec 597static void
2e0fef85 598lpfc_work_done(struct lpfc_hba *phba)
dea3101e 599{
600 struct lpfc_sli_ring *pring;
858c9f6c 601 uint32_t ha_copy, status, control, work_port_events;
549e55cd 602 struct lpfc_vport **vports;
51ef4c26 603 struct lpfc_vport *vport;
549e55cd 604 int i;
dea3101e 605
2e0fef85 606 spin_lock_irq(&phba->hbalock);
dea3101e 607 ha_copy = phba->work_ha;
608 phba->work_ha = 0;
2e0fef85 609 spin_unlock_irq(&phba->hbalock);
dea3101e 610
da0436e9
JS
611 /* 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
2fe165b6 615 if (ha_copy & HA_ERATT)
9399627f 616 /* Handle the error attention event */
dea3101e 617 lpfc_handle_eratt(phba);
618
2fe165b6 619 if (ha_copy & HA_MBATT)
dea3101e 620 lpfc_sli_handle_mb_event(phba);
621
2fe165b6 622 if (ha_copy & HA_LATT)
dea3101e 623 lpfc_handle_latt(phba);
9399627f 624
da0436e9
JS
625 /* Process SLI4 events */
626 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
19ca7609
JS
627 if (phba->hba_flag & HBA_RRQ_ACTIVE)
628 lpfc_handle_rrq_active(phba);
da0436e9
JS
629 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 }
ecfd03c6
JS
641 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
642 lpfc_sli4_fcf_redisc_event_proc(phba);
da0436e9
JS
643 }
644
549e55cd
JS
645 vports = lpfc_create_vport_work_array(phba);
646 if (vports != NULL)
da0436e9 647 for (i = 0; i <= phba->max_vports; i++) {
51ef4c26
JS
648 /*
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;
58da1ffb 658 spin_lock_irq(&vport->work_port_lock);
51ef4c26 659 work_port_events = vport->work_port_events;
58da1ffb
JS
660 vport->work_port_events &= ~work_port_events;
661 spin_unlock_irq(&vport->work_port_lock);
549e55cd 662 if (work_port_events & WORKER_DISC_TMO)
51ef4c26 663 lpfc_disc_timeout_handler(vport);
549e55cd 664 if (work_port_events & WORKER_ELS_TMO)
51ef4c26 665 lpfc_els_timeout_handler(vport);
549e55cd
JS
666 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)
51ef4c26 673 lpfc_fdmi_timeout_handler(vport);
549e55cd
JS
674 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);
92494144
JS
678 if (work_port_events & WORKER_DELAYED_DISC_TMO)
679 lpfc_delayed_disc_timeout_handler(vport);
92d7f7b0 680 }
09372820 681 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 682
858c9f6c
JS
683 pring = &phba->sli.ring[LPFC_ELS_RING];
684 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
685 status >>= (4*LPFC_ELS_RING);
4d9ab994
JS
686 if ((status & HA_RXMASK) ||
687 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
45ed1190 688 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
0b727fea 689 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
858c9f6c 690 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
691 /* Set the lpfc data pending flag */
692 set_bit(LPFC_DATA_READY, &phba->data_flags);
858c9f6c 693 } else {
58da1ffb 694 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
858c9f6c
JS
695 lpfc_sli_handle_slow_ring_event(phba, pring,
696 (status &
697 HA_RXMASK));
858c9f6c 698 }
bc73905a 699 if ((phba->sli_rev == LPFC_SLI_REV4) && pring->txq_cnt)
2a9bf3d0 700 lpfc_drain_txq(phba);
858c9f6c
JS
701 /*
702 * Turn on Ring interrupts
703 */
3772a991
JS
704 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);
711
712 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);
a58cbd52 721 }
dea3101e 722 }
2e0fef85 723 lpfc_work_list_done(phba);
dea3101e 724}
725
dea3101e 726int
727lpfc_do_work(void *p)
728{
729 struct lpfc_hba *phba = p;
730 int rc;
dea3101e 731
732 set_user_nice(current, -20);
043c956f 733 current->flags |= PF_NOFREEZE;
5e9d9b82 734 phba->data_flags = 0;
dea3101e 735
3a55b532 736 while (!kthread_should_stop()) {
5e9d9b82
JS
737 /* 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()));
3a55b532
JS
742 /* 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);
dea3101e 746 break;
3a55b532 747 }
dea3101e 748
5e9d9b82 749 /* Attend pending lpfc data processing */
dea3101e 750 lpfc_work_done(phba);
dea3101e 751 }
3a55b532
JS
752 phba->worker_thread = NULL;
753 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
754 "0432 Worker thread stopped.\n");
dea3101e 755 return 0;
756}
757
758/*
759 * This is only called to handle FC worker events. Since this a rare
25985edc 760 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea3101e 761 * embedding it in the IOCB.
762 */
763int
2e0fef85 764lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea3101e 765 uint32_t evt)
766{
767 struct lpfc_work_evt *evtp;
ed957684 768 unsigned long flags;
dea3101e 769
770 /*
771 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
772 * be queued to worker thread for processing
773 */
92d7f7b0 774 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea3101e 775 if (!evtp)
776 return 0;
777
778 evtp->evt_arg1 = arg1;
779 evtp->evt_arg2 = arg2;
780 evtp->evt = evt;
781
ed957684 782 spin_lock_irqsave(&phba->hbalock, flags);
071fbd3d 783 list_add_tail(&evtp->evt_listp, &phba->work_list);
ed957684 784 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 785
5e9d9b82
JS
786 lpfc_worker_wake_up(phba);
787
dea3101e 788 return 1;
789}
790
92d7f7b0
JS
791void
792lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
793{
09372820 794 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
795 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) {
e47c9093
JS
800 if (!NLP_CHK_NODE_ACT(ndlp))
801 continue;
92d7f7b0
JS
802 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
803 continue;
98c9ea5c
JS
804 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
805 ((vport->port_type == LPFC_NPIV_PORT) &&
806 (ndlp->nlp_DID == NameServer_DID)))
92d7f7b0
JS
807 lpfc_unreg_rpi(vport, ndlp);
808
809 /* Leave Fabric nodes alone on link down */
4d9ab994
JS
810 if ((phba->sli_rev < LPFC_SLI_REV4) &&
811 (!remove && ndlp->nlp_type & NLP_FABRIC))
92d7f7b0
JS
812 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) {
5af5eee7
JS
819 if (phba->sli_rev == LPFC_SLI_REV4)
820 lpfc_sli4_unreg_all_rpis(vport);
92d7f7b0 821 lpfc_mbx_unreg_vpi(vport);
09372820 822 spin_lock_irq(shost->host_lock);
92d7f7b0 823 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
09372820 824 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
825 }
826}
827
87af33fe 828void
98c9ea5c 829lpfc_port_link_failure(struct lpfc_vport *vport)
92d7f7b0 830{
695a814e
JS
831 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
832
45ed1190
JS
833 /* Cleanup any outstanding received buffers */
834 lpfc_cleanup_rcv_buffers(vport);
835
92d7f7b0
JS
836 /* 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
92d7f7b0
JS
844 /* Turn off discovery timer if its running */
845 lpfc_can_disctmo(vport);
846}
847
3772a991 848void
98c9ea5c
JS
849lpfc_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
92494144
JS
861 /* 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);
98c9ea5c
JS
866}
867
dea3101e 868int
685f0bf7 869lpfc_linkdown(struct lpfc_hba *phba)
dea3101e 870{
2e0fef85
JS
871 struct lpfc_vport *vport = phba->pport;
872 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
549e55cd 873 struct lpfc_vport **vports;
685f0bf7 874 LPFC_MBOXQ_t *mb;
549e55cd 875 int i;
dea3101e 876
3163f725 877 if (phba->link_state == LPFC_LINK_DOWN)
2e0fef85 878 return 0;
aacc20e3
JS
879
880 /* Block all SCSI stack I/Os */
881 lpfc_scsi_dev_block(phba);
882
2e0fef85 883 spin_lock_irq(&phba->hbalock);
ecfd03c6 884 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
72100cc4 885 spin_unlock_irq(&phba->hbalock);
92d7f7b0 886 if (phba->link_state > LPFC_LINK_DOWN) {
2e0fef85 887 phba->link_state = LPFC_LINK_DOWN;
72100cc4 888 spin_lock_irq(shost->host_lock);
92d7f7b0 889 phba->pport->fc_flag &= ~FC_LBIT;
72100cc4 890 spin_unlock_irq(shost->host_lock);
92d7f7b0 891 }
549e55cd
JS
892 vports = lpfc_create_vport_work_array(phba);
893 if (vports != NULL)
6fb120a7 894 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
895 /* Issue a LINK DOWN event to all nodes */
896 lpfc_linkdown_port(vports[i]);
897 }
09372820 898 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 899 /* Clean up any firmware default rpi's */
2e0fef85
JS
900 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
901 if (mb) {
6d368e53 902 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
ed957684 903 mb->vport = vport;
2e0fef85 904 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0b727fea 905 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea3101e 906 == MBX_NOT_FINISHED) {
2e0fef85 907 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 908 }
909 }
910
dea3101e 911 /* Setup myDID for link up if we are in pt2pt mode */
92d7f7b0
JS
912 if (phba->pport->fc_flag & FC_PT2PT) {
913 phba->pport->fc_myDID = 0;
2e0fef85
JS
914 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
915 if (mb) {
dea3101e 916 lpfc_config_link(phba, mb);
92d7f7b0 917 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
ed957684 918 mb->vport = vport;
0b727fea 919 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea3101e 920 == MBX_NOT_FINISHED) {
2e0fef85 921 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 922 }
923 }
2e0fef85 924 spin_lock_irq(shost->host_lock);
92d7f7b0 925 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
2e0fef85 926 spin_unlock_irq(shost->host_lock);
dea3101e 927 }
2e0fef85 928
92d7f7b0
JS
929 return 0;
930}
dea3101e 931
92d7f7b0
JS
932static void
933lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
934{
935 struct lpfc_nodelist *ndlp;
dea3101e 936
92d7f7b0 937 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
938 if (!NLP_CHK_NODE_ACT(ndlp))
939 continue;
92d7f7b0
JS
940 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
941 continue;
92d7f7b0 942 if (ndlp->nlp_type & NLP_FABRIC) {
e47c9093
JS
943 /* On Linkup its safe to clean up the ndlp
944 * from Fabric connections.
945 */
92d7f7b0
JS
946 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)) {
e47c9093
JS
950 /* Fail outstanding IO now since device is
951 * marked for PLOGI.
952 */
92d7f7b0
JS
953 lpfc_unreg_rpi(vport, ndlp);
954 }
955 }
dea3101e 956}
957
92d7f7b0
JS
958static void
959lpfc_linkup_port(struct lpfc_vport *vport)
dea3101e 960{
92d7f7b0 961 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
962 struct lpfc_hba *phba = vport->phba;
963
964 if ((vport->load_flag & FC_UNLOADING) != 0)
965 return;
966
858c9f6c
JS
967 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
92d7f7b0
JS
971 /* 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;
dea3101e 975
2e0fef85 976 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
d2873e4c 977
2e0fef85 978 spin_lock_irq(shost->host_lock);
2e0fef85
JS
979 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);
dea3101e 984
92d7f7b0
JS
985 if (vport->fc_flag & FC_LBIT)
986 lpfc_linkup_cleanup_nodes(vport);
dea3101e 987
92d7f7b0
JS
988}
989
990static int
991lpfc_linkup(struct lpfc_hba *phba)
992{
549e55cd
JS
993 struct lpfc_vport **vports;
994 int i;
92d7f7b0 995
19ca7609 996 lpfc_cleanup_wt_rrqs(phba);
92d7f7b0
JS
997 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
549e55cd
JS
1003 vports = lpfc_create_vport_work_array(phba);
1004 if (vports != NULL)
6fb120a7 1005 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
549e55cd 1006 lpfc_linkup_port(vports[i]);
09372820 1007 lpfc_destroy_vport_work_array(phba, vports);
6fb120a7
JS
1008 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1009 (phba->sli_rev < LPFC_SLI_REV4))
92d7f7b0 1010 lpfc_issue_clear_la(phba, phba->pport);
dea3101e 1011
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 */
a6ababd2 1021static void
2e0fef85 1022lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1023{
2e0fef85
JS
1024 struct lpfc_vport *vport = pmb->vport;
1025 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1026 struct lpfc_sli *psli = &phba->sli;
04c68496 1027 MAILBOX_t *mb = &pmb->u.mb;
dea3101e 1028 uint32_t control;
1029
dea3101e 1030 /* Since we don't do discovery right now, turn these off here */
a4bc3379 1031 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 1032 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)) {
92d7f7b0 1037 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
e8b62011
JS
1038 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);
2e0fef85 1042 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1043 goto out;
1044 }
1045
92d7f7b0
JS
1046 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);
1b32f6aa 1056 mempool_free(pmb, phba->mbox_mem_pool);
92d7f7b0 1057 return;
dea3101e 1058
dea3101e 1059out:
1060 /* Device Discovery completes */
e8b62011
JS
1061 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1062 "0225 Device Discovery completes\n");
2e0fef85 1063 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1064
2e0fef85 1065 spin_lock_irq(shost->host_lock);
58da1ffb 1066 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
2e0fef85 1067 spin_unlock_irq(shost->host_lock);
dea3101e 1068
2e0fef85 1069 lpfc_can_disctmo(vport);
dea3101e 1070
1071 /* turn on Link Attention interrupts */
2e0fef85
JS
1072
1073 spin_lock_irq(&phba->hbalock);
dea3101e 1074 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 */
2e0fef85 1079 spin_unlock_irq(&phba->hbalock);
dea3101e 1080
1081 return;
1082}
1083
2e0fef85 1084
dea3101e 1085static void
25594c6b 1086lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1087{
2e0fef85 1088 struct lpfc_vport *vport = pmb->vport;
dea3101e 1089
04c68496 1090 if (pmb->u.mb.mbxStatus)
dea3101e 1091 goto out;
dea3101e 1092
25594c6b
JW
1093 mempool_free(pmb, phba->mbox_mem_pool);
1094
1b51197d
JS
1095 /* 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
76a95d75 1101 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
2e0fef85
JS
1102 vport->fc_flag & FC_PUBLIC_LOOP &&
1103 !(vport->fc_flag & FC_LBIT)) {
25594c6b 1104 /* Need to wait for FAN - use discovery timer
2e0fef85 1105 * for timeout. port_state is identically
25594c6b
JW
1106 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1107 */
2e0fef85 1108 lpfc_set_disctmo(vport);
25594c6b 1109 return;
92d7f7b0 1110 }
dea3101e 1111
2e0fef85 1112 /* Start discovery by sending a FLOGI. port_state is identically
25594c6b
JW
1113 * LPFC_FLOGI while waiting for FLOGI cmpl
1114 */
e6446439 1115 if (vport->port_state != LPFC_FLOGI || vport->fc_flag & FC_PT2PT_PLOGI)
92d7f7b0 1116 lpfc_initial_flogi(vport);
25594c6b 1117 return;
dea3101e 1118
1119out:
e8b62011
JS
1120 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1121 "0306 CONFIG_LINK mbxStatus error x%x "
1122 "HBA state x%x\n",
04c68496 1123 pmb->u.mb.mbxStatus, vport->port_state);
92d7f7b0 1124 mempool_free(pmb, phba->mbox_mem_pool);
25594c6b 1125
92d7f7b0 1126 lpfc_linkdown(phba);
25594c6b 1127
e8b62011
JS
1128 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1129 "0200 CONFIG_LINK bad hba state x%x\n",
1130 vport->port_state);
dea3101e 1131
92d7f7b0 1132 lpfc_issue_clear_la(phba, vport);
dea3101e 1133 return;
1134}
1135
7d791df7
JS
1136/**
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}
6fb120a7
JS
1158static void
1159lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1160{
1161 struct lpfc_vport *vport = mboxq->vport;
6fb120a7
JS
1162
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);
a93ff37a 1168 goto fail_out;
6fb120a7
JS
1169 }
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 */
d439d286 1174 spin_lock_irq(&phba->hbalock);
6fb120a7 1175 phba->fcf.fcf_flag |= FCF_REGISTERED;
d439d286 1176 spin_unlock_irq(&phba->hbalock);
a93ff37a 1177
32b9793f 1178 /* If there is a pending FCoE event, restart FCF table scan. */
7d791df7
JS
1179 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1180 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
a93ff37a
JS
1181 goto fail_out;
1182
1183 /* Mark successful completion of FCF table scan */
d439d286 1184 spin_lock_irq(&phba->hbalock);
ecfd03c6 1185 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
a93ff37a
JS
1186 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);
76a95d75 1190 lpfc_issue_init_vfi(vport);
a93ff37a
JS
1191 goto out;
1192 }
1193 spin_unlock_irq(&phba->hbalock);
1194 goto out;
6fb120a7 1195
a93ff37a
JS
1196fail_out:
1197 spin_lock_irq(&phba->hbalock);
1198 phba->hba_flag &= ~FCF_RR_INPROG;
1199 spin_unlock_irq(&phba->hbalock);
1200out:
6fb120a7 1201 mempool_free(mboxq, phba->mbox_mem_pool);
6fb120a7
JS
1202}
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{
ecfd03c6
JS
1216 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1217 return 0;
1218 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))
6fb120a7 1227 return 0;
ecfd03c6
JS
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;
6fb120a7
JS
1233}
1234
8fa38513
JS
1235/**
1236 * 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{
ecfd03c6
JS
1247 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1248 return 0;
1249 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))
8fa38513 1252 return 0;
ecfd03c6
JS
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;
8fa38513
JS
1264}
1265
6fb120a7
JS
1266/**
1267 * lpfc_mac_addr_match - Check if the fcf mac address match.
ecfd03c6 1268 * @mac_addr: pointer to mac address.
6fb120a7
JS
1269 * @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
ecfd03c6 1276lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
6fb120a7 1277{
ecfd03c6
JS
1278 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1279 return 0;
1280 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))
6fb120a7 1289 return 0;
ecfd03c6
JS
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);
6fb120a7
JS
1297}
1298
7d791df7
JS
1299/**
1300 * 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
6fb120a7
JS
1323/**
1324 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
ecfd03c6 1325 * @fcf: pointer to driver fcf record.
6fb120a7
JS
1326 * @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
ecfd03c6
JS
1332lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1333 struct fcf_record *new_fcf_record)
6fb120a7 1334{
ecfd03c6
JS
1335 /* Fabric name */
1336 fcf_rec->fabric_name[0] =
6fb120a7 1337 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
ecfd03c6 1338 fcf_rec->fabric_name[1] =
6fb120a7 1339 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
ecfd03c6 1340 fcf_rec->fabric_name[2] =
6fb120a7 1341 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
ecfd03c6 1342 fcf_rec->fabric_name[3] =
6fb120a7 1343 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
ecfd03c6 1344 fcf_rec->fabric_name[4] =
6fb120a7 1345 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
ecfd03c6 1346 fcf_rec->fabric_name[5] =
6fb120a7 1347 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
ecfd03c6 1348 fcf_rec->fabric_name[6] =
6fb120a7 1349 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
ecfd03c6 1350 fcf_rec->fabric_name[7] =
6fb120a7 1351 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
ecfd03c6
JS
1352 /* 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] =
8fa38513 1365 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
ecfd03c6 1366 fcf_rec->switch_name[1] =
8fa38513 1367 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
ecfd03c6 1368 fcf_rec->switch_name[2] =
8fa38513 1369 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
ecfd03c6 1370 fcf_rec->switch_name[3] =
8fa38513 1371 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
ecfd03c6 1372 fcf_rec->switch_name[4] =
8fa38513 1373 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
ecfd03c6 1374 fcf_rec->switch_name[5] =
8fa38513 1375 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
ecfd03c6 1376 fcf_rec->switch_name[6] =
8fa38513 1377 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
ecfd03c6 1378 fcf_rec->switch_name[7] =
8fa38513 1379 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
6fb120a7
JS
1380}
1381
ecfd03c6
JS
1382/**
1383 * 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);
7d791df7
JS
1406 __lpfc_update_fcf_record_pri(phba,
1407 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1408 new_fcf_record);
ecfd03c6
JS
1409}
1410
6fb120a7
JS
1411/**
1412 * 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;
6fb120a7 1423
d439d286 1424 spin_lock_irq(&phba->hbalock);
25985edc 1425 /* If the FCF is not available do nothing. */
6fb120a7 1426 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
a93ff37a 1427 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
d439d286 1428 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
1429 return;
1430 }
1431
1432 /* The FCF is already registered, start discovery */
1433 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
ecfd03c6 1434 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
a93ff37a
JS
1435 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);
bf08611b 1439 lpfc_initial_flogi(phba->pport);
a93ff37a
JS
1440 return;
1441 }
1442 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
1443 return;
1444 }
d439d286 1445 spin_unlock_irq(&phba->hbalock);
6fb120a7 1446
a93ff37a 1447 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4d9ab994 1448 if (!fcf_mbxq) {
d439d286 1449 spin_lock_irq(&phba->hbalock);
a93ff37a 1450 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
d439d286 1451 spin_unlock_irq(&phba->hbalock);
6fb120a7 1452 return;
4d9ab994 1453 }
6fb120a7
JS
1454
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);
4d9ab994 1459 if (rc == MBX_NOT_FINISHED) {
d439d286 1460 spin_lock_irq(&phba->hbalock);
a93ff37a 1461 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
d439d286 1462 spin_unlock_irq(&phba->hbalock);
6fb120a7 1463 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
4d9ab994 1464 }
6fb120a7
JS
1465
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
ecfd03c6 1475 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
6fb120a7
JS
1476 *
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
dbb6b3ab 1485 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
6fb120a7
JS
1486 **/
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;
4d9ab994
JS
1494 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 }
6fb120a7 1508
26979ced 1509 /* FCF not valid/available or solicitation in progress */
0c287589 1510 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
26979ced
JS
1511 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1512 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
0c287589
JS
1513 return 0;
1514
45ed1190 1515 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
6fb120a7
JS
1516 *boot_flag = 0;
1517 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1518 new_fcf_record);
1519 if (phba->valid_vlan)
1520 *vlan_id = phba->vlan_id;
1521 else
dbb6b3ab 1522 *vlan_id = LPFC_FCOE_NULL_VID;
6fb120a7
JS
1523 return 1;
1524 }
1525
1526 /*
1527 * If there are no FCF connection table entry, driver connect to all
1528 * FCFs.
1529 */
1530 if (list_empty(&phba->fcf_conn_rec_list)) {
1531 *boot_flag = 0;
1532 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1533 new_fcf_record);
0c287589
JS
1534
1535 /*
1536 * When there are no FCF connect entries, use driver's default
1537 * addressing mode - FPMA.
1538 */
1539 if (*addr_mode & LPFC_FCF_FPMA)
1540 *addr_mode = LPFC_FCF_FPMA;
1541
4d9ab994
JS
1542 /* If FCF record report a vlan id use that vlan id */
1543 if (fcf_vlan_id)
1544 *vlan_id = fcf_vlan_id;
1545 else
dbb6b3ab 1546 *vlan_id = LPFC_FCOE_NULL_VID;
6fb120a7
JS
1547 return 1;
1548 }
1549
ecfd03c6
JS
1550 list_for_each_entry(conn_entry,
1551 &phba->fcf_conn_rec_list, list) {
6fb120a7
JS
1552 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1553 continue;
1554
1555 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1556 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
8fa38513
JS
1557 new_fcf_record))
1558 continue;
1559 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1560 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1561 new_fcf_record))
6fb120a7 1562 continue;
6fb120a7
JS
1563 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1564 /*
1565 * If the vlan bit map does not have the bit set for the
1566 * vlan id to be used, then it is not a match.
1567 */
1568 if (!(new_fcf_record->vlan_bitmap
1569 [conn_entry->conn_rec.vlan_tag / 8] &
1570 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1571 continue;
1572 }
1573
0c287589
JS
1574 /*
1575 * If connection record does not support any addressing mode,
1576 * skip the FCF record.
1577 */
1578 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1579 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1580 continue;
1581
6fb120a7
JS
1582 /*
1583 * Check if the connection record specifies a required
1584 * addressing mode.
1585 */
1586 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1587 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1588
1589 /*
1590 * If SPMA required but FCF not support this continue.
1591 */
1592 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1593 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1594 new_fcf_record) & LPFC_FCF_SPMA))
1595 continue;
1596
1597 /*
1598 * If FPMA required but FCF not support this continue.
1599 */
1600 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1601 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1602 new_fcf_record) & LPFC_FCF_FPMA))
1603 continue;
1604 }
1605
1606 /*
1607 * This fcf record matches filtering criteria.
1608 */
1609 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1610 *boot_flag = 1;
1611 else
1612 *boot_flag = 0;
1613
0c287589
JS
1614 /*
1615 * If user did not specify any addressing mode, or if the
25985edc 1616 * preferred addressing mode specified by user is not supported
0c287589
JS
1617 * by FCF, allow fabric to pick the addressing mode.
1618 */
6fb120a7
JS
1619 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1620 new_fcf_record);
1621 /*
1622 * If the user specified a required address mode, assign that
1623 * address mode
1624 */
1625 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1626 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1627 *addr_mode = (conn_entry->conn_rec.flags &
1628 FCFCNCT_AM_SPMA) ?
1629 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1630 /*
25985edc 1631 * If the user specified a preferred address mode, use the
6fb120a7
JS
1632 * addr mode only if FCF support the addr_mode.
1633 */
1634 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1635 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1636 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1637 (*addr_mode & LPFC_FCF_SPMA))
1638 *addr_mode = LPFC_FCF_SPMA;
1639 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1640 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1641 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1642 (*addr_mode & LPFC_FCF_FPMA))
1643 *addr_mode = LPFC_FCF_FPMA;
6fb120a7 1644
4d9ab994 1645 /* If matching connect list has a vlan id, use it */
6fb120a7
JS
1646 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1647 *vlan_id = conn_entry->conn_rec.vlan_tag;
4d9ab994
JS
1648 /*
1649 * If no vlan id is specified in connect list, use the vlan id
1650 * in the FCF record
1651 */
1652 else if (fcf_vlan_id)
1653 *vlan_id = fcf_vlan_id;
6fb120a7 1654 else
dbb6b3ab 1655 *vlan_id = LPFC_FCOE_NULL_VID;
6fb120a7
JS
1656
1657 return 1;
1658 }
1659
1660 return 0;
1661}
1662
32b9793f
JS
1663/**
1664 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1665 * @phba: pointer to lpfc hba data structure.
1666 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1667 *
1668 * This function check if there is any fcoe event pending while driver
1669 * scan FCF entries. If there is any pending event, it will restart the
1670 * FCF saning and return 1 else return 0.
1671 */
1672int
1673lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1674{
32b9793f
JS
1675 /*
1676 * If the Link is up and no FCoE events while in the
1677 * FCF discovery, no need to restart FCF discovery.
1678 */
1679 if ((phba->link_state >= LPFC_LINK_UP) &&
a93ff37a 1680 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
32b9793f
JS
1681 return 0;
1682
0c9ab6f5
JS
1683 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1684 "2768 Pending link or FCF event during current "
1685 "handling of the previous event: link_state:x%x, "
1686 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1687 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1688 phba->fcoe_eventtag);
1689
32b9793f
JS
1690 spin_lock_irq(&phba->hbalock);
1691 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1692 spin_unlock_irq(&phba->hbalock);
1693
0c9ab6f5
JS
1694 if (phba->link_state >= LPFC_LINK_UP) {
1695 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1696 "2780 Restart FCF table scan due to "
1697 "pending FCF event:evt_tag_at_scan:x%x, "
1698 "evt_tag_current:x%x\n",
1699 phba->fcoe_eventtag_at_fcf_scan,
1700 phba->fcoe_eventtag);
1701 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1702 } else {
4d9ab994 1703 /*
a93ff37a 1704 * Do not continue FCF discovery and clear FCF_TS_INPROG
4d9ab994
JS
1705 * flag
1706 */
dbb6b3ab
JS
1707 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1708 "2833 Stop FCF discovery process due to link "
1709 "state change (x%x)\n", phba->link_state);
1c6f4ef5 1710 spin_lock_irq(&phba->hbalock);
a93ff37a 1711 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
0c9ab6f5 1712 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
1c6f4ef5
JS
1713 spin_unlock_irq(&phba->hbalock);
1714 }
32b9793f 1715
0c9ab6f5 1716 /* Unregister the currently registered FCF if required */
32b9793f
JS
1717 if (unreg_fcf) {
1718 spin_lock_irq(&phba->hbalock);
1719 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1720 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 1721 lpfc_sli4_unregister_fcf(phba);
32b9793f 1722 }
32b9793f
JS
1723 return 1;
1724}
1725
6fb120a7 1726/**
999d813f
JS
1727 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1728 * @phba: pointer to lpfc hba data structure.
1729 * @fcf_cnt: number of eligible fcf record seen so far.
1730 *
1731 * This function makes an running random selection decision on FCF record to
1732 * use through a sequence of @fcf_cnt eligible FCF records with equal
1733 * probability. To perform integer manunipulation of random numbers with
1734 * size unit32_t, the lower 16 bits of the 32-bit random number returned
3b60a64f 1735 * from prandom_u32() are taken as the random random number generated.
999d813f
JS
1736 *
1737 * Returns true when outcome is for the newly read FCF record should be
1738 * chosen; otherwise, return false when outcome is for keeping the previously
1739 * chosen FCF record.
1740 **/
1741static bool
1742lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1743{
1744 uint32_t rand_num;
1745
1746 /* Get 16-bit uniform random number */
3b60a64f 1747 rand_num = 0xFFFF & prandom_u32();
999d813f
JS
1748
1749 /* Decision with probability 1/fcf_cnt */
1750 if ((fcf_cnt * rand_num) < 0xFFFF)
1751 return true;
1752 else
1753 return false;
1754}
1755
1756/**
3804dc84 1757 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
6fb120a7
JS
1758 * @phba: pointer to lpfc hba data structure.
1759 * @mboxq: pointer to mailbox object.
0c9ab6f5 1760 * @next_fcf_index: pointer to holder of next fcf index.
6fb120a7 1761 *
0c9ab6f5
JS
1762 * This routine parses the non-embedded fcf mailbox command by performing the
1763 * necessarily error checking, non-embedded read FCF record mailbox command
1764 * SGE parsing, and endianness swapping.
1765 *
1766 * Returns the pointer to the new FCF record in the non-embedded mailbox
1767 * command DMA memory if successfully, other NULL.
6fb120a7 1768 */
0c9ab6f5
JS
1769static struct fcf_record *
1770lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1771 uint16_t *next_fcf_index)
6fb120a7
JS
1772{
1773 void *virt_addr;
1774 dma_addr_t phys_addr;
6fb120a7
JS
1775 struct lpfc_mbx_sge sge;
1776 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1777 uint32_t shdr_status, shdr_add_status;
1778 union lpfc_sli4_cfg_shdr *shdr;
1779 struct fcf_record *new_fcf_record;
32b9793f 1780
6fb120a7
JS
1781 /* Get the first SGE entry from the non-embedded DMA memory. This
1782 * routine only uses a single SGE.
1783 */
1784 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1785 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1786 if (unlikely(!mboxq->sge_array)) {
1787 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1788 "2524 Failed to get the non-embedded SGE "
1789 "virtual address\n");
0c9ab6f5 1790 return NULL;
6fb120a7
JS
1791 }
1792 virt_addr = mboxq->sge_array->addr[0];
1793
1794 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1795 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
0c9ab6f5 1796 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6fb120a7 1797 if (shdr_status || shdr_add_status) {
0c9ab6f5
JS
1798 if (shdr_status == STATUS_FCF_TABLE_EMPTY)
1799 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
def9c7a9
JS
1800 "2726 READ_FCF_RECORD Indicates empty "
1801 "FCF table.\n");
0c9ab6f5
JS
1802 else
1803 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
def9c7a9 1804 "2521 READ_FCF_RECORD mailbox failed "
0c9ab6f5
JS
1805 "with status x%x add_status x%x, "
1806 "mbx\n", shdr_status, shdr_add_status);
1807 return NULL;
6fb120a7 1808 }
0c9ab6f5
JS
1809
1810 /* Interpreting the returned information of the FCF record */
6fb120a7
JS
1811 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1812 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1813 sizeof(struct lpfc_mbx_read_fcf_tbl));
0c9ab6f5 1814 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
6fb120a7
JS
1815 new_fcf_record = (struct fcf_record *)(virt_addr +
1816 sizeof(struct lpfc_mbx_read_fcf_tbl));
1817 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
cb5172ea
JS
1818 offsetof(struct fcf_record, vlan_bitmap));
1819 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1820 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
6fb120a7 1821
0c9ab6f5
JS
1822 return new_fcf_record;
1823}
1824
1825/**
1826 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1827 * @phba: pointer to lpfc hba data structure.
1828 * @fcf_record: pointer to the fcf record.
1829 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1830 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1831 *
1832 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1833 * enabled.
1834 **/
1835static void
1836lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1837 struct fcf_record *fcf_record,
1838 uint16_t vlan_id,
1839 uint16_t next_fcf_index)
1840{
1841 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1842 "2764 READ_FCF_RECORD:\n"
1843 "\tFCF_Index : x%x\n"
1844 "\tFCF_Avail : x%x\n"
1845 "\tFCF_Valid : x%x\n"
26979ced 1846 "\tFCF_SOL : x%x\n"
0c9ab6f5
JS
1847 "\tFIP_Priority : x%x\n"
1848 "\tMAC_Provider : x%x\n"
1849 "\tLowest VLANID : x%x\n"
1850 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1851 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1852 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1853 "\tNext_FCF_Index: x%x\n",
1854 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1855 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1856 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
26979ced 1857 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
0c9ab6f5
JS
1858 fcf_record->fip_priority,
1859 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1860 vlan_id,
1861 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1862 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1863 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1864 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1865 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1866 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1867 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1868 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1869 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1870 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1871 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1872 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1873 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1874 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1875 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1876 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1877 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1878 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1879 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1880 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1881 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1882 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1883 next_fcf_index);
1884}
1885
dbb6b3ab
JS
1886/**
1887 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1888 * @phba: pointer to lpfc hba data structure.
1889 * @fcf_rec: pointer to an existing FCF record.
1890 * @new_fcf_record: pointer to a new FCF record.
1891 * @new_vlan_id: vlan id from the new FCF record.
1892 *
1893 * This function performs matching test of a new FCF record against an existing
1894 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1895 * will not be used as part of the FCF record matching criteria.
1896 *
1897 * Returns true if all the fields matching, otherwise returns false.
1898 */
1899static bool
1900lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1901 struct lpfc_fcf_rec *fcf_rec,
1902 struct fcf_record *new_fcf_record,
1903 uint16_t new_vlan_id)
1904{
1905 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1906 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1907 return false;
1908 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1909 return false;
1910 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1911 return false;
1912 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1913 return false;
7d791df7
JS
1914 if (fcf_rec->priority != new_fcf_record->fip_priority)
1915 return false;
dbb6b3ab
JS
1916 return true;
1917}
1918
a93ff37a
JS
1919/**
1920 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1921 * @vport: Pointer to vport object.
1922 * @fcf_index: index to next fcf.
1923 *
1924 * This function processing the roundrobin fcf failover to next fcf index.
1925 * When this function is invoked, there will be a current fcf registered
1926 * for flogi.
1927 * Return: 0 for continue retrying flogi on currently registered fcf;
1928 * 1 for stop flogi on currently registered fcf;
1929 */
1930int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1931{
1932 struct lpfc_hba *phba = vport->phba;
1933 int rc;
1934
1935 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1936 spin_lock_irq(&phba->hbalock);
1937 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1938 spin_unlock_irq(&phba->hbalock);
1939 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1940 "2872 Devloss tmo with no eligible "
1941 "FCF, unregister in-use FCF (x%x) "
1942 "and rescan FCF table\n",
1943 phba->fcf.current_rec.fcf_indx);
1944 lpfc_unregister_fcf_rescan(phba);
1945 goto stop_flogi_current_fcf;
1946 }
1947 /* Mark the end to FLOGI roundrobin failover */
1948 phba->hba_flag &= ~FCF_RR_INPROG;
1949 /* Allow action to new fcf asynchronous event */
1950 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1951 spin_unlock_irq(&phba->hbalock);
1952 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1953 "2865 No FCF available, stop roundrobin FCF "
1954 "failover and change port state:x%x/x%x\n",
1955 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1956 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1957 goto stop_flogi_current_fcf;
1958 } else {
1959 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1960 "2794 Try FLOGI roundrobin FCF failover to "
1961 "(x%x)\n", fcf_index);
1962 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1963 if (rc)
1964 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1965 "2761 FLOGI roundrobin FCF failover "
1966 "failed (rc:x%x) to read FCF (x%x)\n",
1967 rc, phba->fcf.current_rec.fcf_indx);
1968 else
1969 goto stop_flogi_current_fcf;
1970 }
1971 return 0;
1972
1973stop_flogi_current_fcf:
1974 lpfc_can_disctmo(vport);
1975 return 1;
1976}
1977
7d791df7
JS
1978/**
1979 * lpfc_sli4_fcf_pri_list_del
1980 * @phba: pointer to lpfc hba data structure.
1981 * @fcf_index the index of the fcf record to delete
1982 * This routine checks the on list flag of the fcf_index to be deleted.
1983 * If it is one the list then it is removed from the list, and the flag
1984 * is cleared. This routine grab the hbalock before removing the fcf
1985 * record from the list.
1986 **/
1987static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
1988 uint16_t fcf_index)
1989{
1990 struct lpfc_fcf_pri *new_fcf_pri;
1991
1992 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1993 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1994 "3058 deleting idx x%x pri x%x flg x%x\n",
1995 fcf_index, new_fcf_pri->fcf_rec.priority,
1996 new_fcf_pri->fcf_rec.flag);
1997 spin_lock_irq(&phba->hbalock);
1998 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
1999 if (phba->fcf.current_rec.priority ==
2000 new_fcf_pri->fcf_rec.priority)
2001 phba->fcf.eligible_fcf_cnt--;
2002 list_del_init(&new_fcf_pri->list);
2003 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2004 }
2005 spin_unlock_irq(&phba->hbalock);
2006}
2007
2008/**
2009 * lpfc_sli4_set_fcf_flogi_fail
2010 * @phba: pointer to lpfc hba data structure.
2011 * @fcf_index the index of the fcf record to update
2012 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2013 * flag so the the round robin slection for the particular priority level
2014 * will try a different fcf record that does not have this bit set.
2015 * If the fcf record is re-read for any reason this flag is cleared brfore
2016 * adding it to the priority list.
2017 **/
2018void
2019lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2020{
2021 struct lpfc_fcf_pri *new_fcf_pri;
2022 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2023 spin_lock_irq(&phba->hbalock);
2024 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2025 spin_unlock_irq(&phba->hbalock);
2026}
2027
2028/**
2029 * lpfc_sli4_fcf_pri_list_add
2030 * @phba: pointer to lpfc hba data structure.
2031 * @fcf_index the index of the fcf record to add
2032 * This routine checks the priority of the fcf_index to be added.
2033 * If it is a lower priority than the current head of the fcf_pri list
2034 * then it is added to the list in the right order.
2035 * If it is the same priority as the current head of the list then it
2036 * is added to the head of the list and its bit in the rr_bmask is set.
2037 * If the fcf_index to be added is of a higher priority than the current
2038 * head of the list then the rr_bmask is cleared, its bit is set in the
2039 * rr_bmask and it is added to the head of the list.
2040 * returns:
2041 * 0=success 1=failure
2042 **/
2043int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba, uint16_t fcf_index,
2044 struct fcf_record *new_fcf_record)
2045{
2046 uint16_t current_fcf_pri;
2047 uint16_t last_index;
2048 struct lpfc_fcf_pri *fcf_pri;
2049 struct lpfc_fcf_pri *next_fcf_pri;
2050 struct lpfc_fcf_pri *new_fcf_pri;
2051 int ret;
2052
2053 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2054 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2055 "3059 adding idx x%x pri x%x flg x%x\n",
2056 fcf_index, new_fcf_record->fip_priority,
2057 new_fcf_pri->fcf_rec.flag);
2058 spin_lock_irq(&phba->hbalock);
2059 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2060 list_del_init(&new_fcf_pri->list);
2061 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2062 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2063 if (list_empty(&phba->fcf.fcf_pri_list)) {
2064 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2065 ret = lpfc_sli4_fcf_rr_index_set(phba,
2066 new_fcf_pri->fcf_rec.fcf_index);
2067 goto out;
2068 }
2069
2070 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2071 LPFC_SLI4_FCF_TBL_INDX_MAX);
2072 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2073 ret = 0; /* Empty rr list */
2074 goto out;
2075 }
2076 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2077 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2078 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2079 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2080 memset(phba->fcf.fcf_rr_bmask, 0,
2081 sizeof(*phba->fcf.fcf_rr_bmask));
2082 /* fcfs_at_this_priority_level = 1; */
2083 phba->fcf.eligible_fcf_cnt = 1;
2084 } else
2085 /* fcfs_at_this_priority_level++; */
2086 phba->fcf.eligible_fcf_cnt++;
2087 ret = lpfc_sli4_fcf_rr_index_set(phba,
2088 new_fcf_pri->fcf_rec.fcf_index);
2089 goto out;
2090 }
2091
2092 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2093 &phba->fcf.fcf_pri_list, list) {
2094 if (new_fcf_pri->fcf_rec.priority <=
2095 fcf_pri->fcf_rec.priority) {
2096 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2097 list_add(&new_fcf_pri->list,
2098 &phba->fcf.fcf_pri_list);
2099 else
2100 list_add(&new_fcf_pri->list,
2101 &((struct lpfc_fcf_pri *)
2102 fcf_pri->list.prev)->list);
2103 ret = 0;
2104 goto out;
2105 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2106 || new_fcf_pri->fcf_rec.priority <
2107 next_fcf_pri->fcf_rec.priority) {
2108 list_add(&new_fcf_pri->list, &fcf_pri->list);
2109 ret = 0;
2110 goto out;
2111 }
2112 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2113 continue;
2114
2115 }
2116 ret = 1;
2117out:
2118 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2119 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2120 spin_unlock_irq(&phba->hbalock);
2121 return ret;
2122}
2123
0c9ab6f5
JS
2124/**
2125 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2126 * @phba: pointer to lpfc hba data structure.
2127 * @mboxq: pointer to mailbox object.
2128 *
2129 * This function iterates through all the fcf records available in
2130 * HBA and chooses the optimal FCF record for discovery. After finding
2131 * the FCF for discovery it registers the FCF record and kicks start
2132 * discovery.
2133 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2134 * use an FCF record which matches fabric name and mac address of the
2135 * currently used FCF record.
2136 * If the driver supports only one FCF, it will try to use the FCF record
2137 * used by BOOT_BIOS.
2138 */
2139void
2140lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2141{
2142 struct fcf_record *new_fcf_record;
2143 uint32_t boot_flag, addr_mode;
2144 uint16_t fcf_index, next_fcf_index;
2145 struct lpfc_fcf_rec *fcf_rec = NULL;
2146 uint16_t vlan_id;
999d813f
JS
2147 uint32_t seed;
2148 bool select_new_fcf;
0c9ab6f5
JS
2149 int rc;
2150
2151 /* If there is pending FCoE event restart FCF table scan */
a93ff37a 2152 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
0c9ab6f5
JS
2153 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2154 return;
2155 }
2156
2157 /* Parse the FCF record from the non-embedded mailbox command */
2158 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2159 &next_fcf_index);
2160 if (!new_fcf_record) {
a93ff37a 2161 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
0c9ab6f5
JS
2162 "2765 Mailbox command READ_FCF_RECORD "
2163 "failed to retrieve a FCF record.\n");
2164 /* Let next new FCF event trigger fast failover */
2165 spin_lock_irq(&phba->hbalock);
a93ff37a 2166 phba->hba_flag &= ~FCF_TS_INPROG;
0c9ab6f5
JS
2167 spin_unlock_irq(&phba->hbalock);
2168 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2169 return;
2170 }
2171
2172 /* Check the FCF record against the connection list */
ecfd03c6
JS
2173 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2174 &addr_mode, &vlan_id);
0c9ab6f5
JS
2175
2176 /* Log the FCF record information if turned on */
2177 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2178 next_fcf_index);
2179
6fb120a7
JS
2180 /*
2181 * If the fcf record does not match with connect list entries
0c9ab6f5 2182 * read the next entry; otherwise, this is an eligible FCF
a93ff37a 2183 * record for roundrobin FCF failover.
6fb120a7 2184 */
0c9ab6f5 2185 if (!rc) {
7d791df7
JS
2186 lpfc_sli4_fcf_pri_list_del(phba,
2187 bf_get(lpfc_fcf_record_fcf_index,
2188 new_fcf_record));
0c9ab6f5 2189 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
a93ff37a 2190 "2781 FCF (x%x) failed connection "
26979ced 2191 "list check: (x%x/x%x/%x)\n",
0c9ab6f5
JS
2192 bf_get(lpfc_fcf_record_fcf_index,
2193 new_fcf_record),
2194 bf_get(lpfc_fcf_record_fcf_avail,
2195 new_fcf_record),
2196 bf_get(lpfc_fcf_record_fcf_valid,
26979ced
JS
2197 new_fcf_record),
2198 bf_get(lpfc_fcf_record_fcf_sol,
0c9ab6f5 2199 new_fcf_record));
dbb6b3ab
JS
2200 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2201 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2202 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
5ac6b303
JS
2203 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2204 phba->fcf.current_rec.fcf_indx) {
2205 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2206 "2862 FCF (x%x) matches property "
2207 "of in-use FCF (x%x)\n",
2208 bf_get(lpfc_fcf_record_fcf_index,
2209 new_fcf_record),
2210 phba->fcf.current_rec.fcf_indx);
2211 goto read_next_fcf;
2212 }
dbb6b3ab
JS
2213 /*
2214 * In case the current in-use FCF record becomes
2215 * invalid/unavailable during FCF discovery that
2216 * was not triggered by fast FCF failover process,
2217 * treat it as fast FCF failover.
2218 */
2219 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2220 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2221 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2222 "2835 Invalid in-use FCF "
a93ff37a
JS
2223 "(x%x), enter FCF failover "
2224 "table scan.\n",
dbb6b3ab
JS
2225 phba->fcf.current_rec.fcf_indx);
2226 spin_lock_irq(&phba->hbalock);
2227 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2228 spin_unlock_irq(&phba->hbalock);
2229 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2230 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2231 LPFC_FCOE_FCF_GET_FIRST);
2232 return;
2233 }
2234 }
6fb120a7 2235 goto read_next_fcf;
0c9ab6f5
JS
2236 } else {
2237 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
7d791df7
JS
2238 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2239 new_fcf_record);
0c9ab6f5
JS
2240 if (rc)
2241 goto read_next_fcf;
2242 }
2243
6fb120a7
JS
2244 /*
2245 * If this is not the first FCF discovery of the HBA, use last
ecfd03c6
JS
2246 * FCF record for the discovery. The condition that a rescan
2247 * matches the in-use FCF record: fabric name, switch name, mac
2248 * address, and vlan_id.
6fb120a7 2249 */
0c9ab6f5 2250 spin_lock_irq(&phba->hbalock);
6fb120a7 2251 if (phba->fcf.fcf_flag & FCF_IN_USE) {
7d791df7
JS
2252 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2253 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
dbb6b3ab 2254 new_fcf_record, vlan_id)) {
5ac6b303
JS
2255 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2256 phba->fcf.current_rec.fcf_indx) {
2257 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2258 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2259 /* Stop FCF redisc wait timer */
2260 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2261 phba);
2262 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2263 /* Fast failover, mark completed */
2264 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2265 spin_unlock_irq(&phba->hbalock);
2266 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2267 "2836 New FCF matches in-use "
2268 "FCF (x%x)\n",
2269 phba->fcf.current_rec.fcf_indx);
2270 goto out;
2271 } else
2272 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2273 "2863 New FCF (x%x) matches "
2274 "property of in-use FCF (x%x)\n",
dbb6b3ab 2275 bf_get(lpfc_fcf_record_fcf_index,
5ac6b303
JS
2276 new_fcf_record),
2277 phba->fcf.current_rec.fcf_indx);
6fb120a7 2278 }
ecfd03c6
JS
2279 /*
2280 * Read next FCF record from HBA searching for the matching
2281 * with in-use record only if not during the fast failover
2282 * period. In case of fast failover period, it shall try to
2283 * determine whether the FCF record just read should be the
2284 * next candidate.
2285 */
2286 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
0c9ab6f5 2287 spin_unlock_irq(&phba->hbalock);
ecfd03c6
JS
2288 goto read_next_fcf;
2289 }
6fb120a7 2290 }
ecfd03c6
JS
2291 /*
2292 * Update on failover FCF record only if it's in FCF fast-failover
2293 * period; otherwise, update on current FCF record.
2294 */
fc2b989b
JS
2295 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2296 fcf_rec = &phba->fcf.failover_rec;
2297 else
ecfd03c6
JS
2298 fcf_rec = &phba->fcf.current_rec;
2299
6fb120a7
JS
2300 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2301 /*
ecfd03c6
JS
2302 * If the driver FCF record does not have boot flag
2303 * set and new hba fcf record has boot flag set, use
2304 * the new hba fcf record.
6fb120a7 2305 */
ecfd03c6
JS
2306 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2307 /* Choose this FCF record */
dbb6b3ab
JS
2308 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2309 "2837 Update current FCF record "
2310 "(x%x) with new FCF record (x%x)\n",
2311 fcf_rec->fcf_indx,
2312 bf_get(lpfc_fcf_record_fcf_index,
2313 new_fcf_record));
ecfd03c6
JS
2314 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2315 addr_mode, vlan_id, BOOT_ENABLE);
0c9ab6f5 2316 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
2317 goto read_next_fcf;
2318 }
2319 /*
ecfd03c6
JS
2320 * If the driver FCF record has boot flag set and the
2321 * new hba FCF record does not have boot flag, read
2322 * the next FCF record.
6fb120a7 2323 */
ecfd03c6 2324 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
0c9ab6f5 2325 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
2326 goto read_next_fcf;
2327 }
2328 /*
ecfd03c6
JS
2329 * If the new hba FCF record has lower priority value
2330 * than the driver FCF record, use the new record.
6fb120a7 2331 */
fc2b989b 2332 if (new_fcf_record->fip_priority < fcf_rec->priority) {
999d813f 2333 /* Choose the new FCF record with lower priority */
dbb6b3ab
JS
2334 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2335 "2838 Update current FCF record "
2336 "(x%x) with new FCF record (x%x)\n",
2337 fcf_rec->fcf_indx,
2338 bf_get(lpfc_fcf_record_fcf_index,
2339 new_fcf_record));
ecfd03c6
JS
2340 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2341 addr_mode, vlan_id, 0);
999d813f
JS
2342 /* Reset running random FCF selection count */
2343 phba->fcf.eligible_fcf_cnt = 1;
2344 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2345 /* Update running random FCF selection count */
2346 phba->fcf.eligible_fcf_cnt++;
2347 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2348 phba->fcf.eligible_fcf_cnt);
dbb6b3ab
JS
2349 if (select_new_fcf) {
2350 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2351 "2839 Update current FCF record "
2352 "(x%x) with new FCF record (x%x)\n",
2353 fcf_rec->fcf_indx,
2354 bf_get(lpfc_fcf_record_fcf_index,
2355 new_fcf_record));
999d813f
JS
2356 /* Choose the new FCF by random selection */
2357 __lpfc_update_fcf_record(phba, fcf_rec,
2358 new_fcf_record,
2359 addr_mode, vlan_id, 0);
dbb6b3ab 2360 }
6fb120a7 2361 }
0c9ab6f5 2362 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
2363 goto read_next_fcf;
2364 }
2365 /*
ecfd03c6
JS
2366 * This is the first suitable FCF record, choose this record for
2367 * initial best-fit FCF.
6fb120a7 2368 */
ecfd03c6 2369 if (fcf_rec) {
dbb6b3ab 2370 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
2371 "2840 Update initial FCF candidate "
2372 "with FCF (x%x)\n",
dbb6b3ab
JS
2373 bf_get(lpfc_fcf_record_fcf_index,
2374 new_fcf_record));
ecfd03c6
JS
2375 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2376 addr_mode, vlan_id, (boot_flag ?
2377 BOOT_ENABLE : 0));
2378 phba->fcf.fcf_flag |= FCF_AVAILABLE;
999d813f
JS
2379 /* Setup initial running random FCF selection count */
2380 phba->fcf.eligible_fcf_cnt = 1;
2381 /* Seeding the random number generator for random selection */
2382 seed = (uint32_t)(0xFFFFFFFF & jiffies);
3b60a64f 2383 prandom_seed(seed);
6fb120a7 2384 }
0c9ab6f5 2385 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
2386 goto read_next_fcf;
2387
2388read_next_fcf:
2389 lpfc_sli4_mbox_cmd_free(phba, mboxq);
ecfd03c6
JS
2390 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2391 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2392 /*
2393 * Case of FCF fast failover scan
2394 */
2395
2396 /*
2397 * It has not found any suitable FCF record, cancel
2398 * FCF scan inprogress, and do nothing
2399 */
2400 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
0c9ab6f5 2401 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
a93ff37a
JS
2402 "2782 No suitable FCF found: "
2403 "(x%x/x%x)\n",
0c9ab6f5
JS
2404 phba->fcoe_eventtag_at_fcf_scan,
2405 bf_get(lpfc_fcf_record_fcf_index,
2406 new_fcf_record));
a93ff37a
JS
2407 spin_lock_irq(&phba->hbalock);
2408 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2409 phba->hba_flag &= ~FCF_TS_INPROG;
2410 spin_unlock_irq(&phba->hbalock);
2411 /* Unregister in-use FCF and rescan */
2412 lpfc_printf_log(phba, KERN_INFO,
2413 LOG_FIP,
2414 "2864 On devloss tmo "
2415 "unreg in-use FCF and "
2416 "rescan FCF table\n");
2417 lpfc_unregister_fcf_rescan(phba);
2418 return;
2419 }
0c9ab6f5 2420 /*
a93ff37a 2421 * Let next new FCF event trigger fast failover
0c9ab6f5 2422 */
a93ff37a 2423 phba->hba_flag &= ~FCF_TS_INPROG;
0c9ab6f5 2424 spin_unlock_irq(&phba->hbalock);
ecfd03c6
JS
2425 return;
2426 }
2427 /*
2428 * It has found a suitable FCF record that is not
2429 * the same as in-use FCF record, unregister the
2430 * in-use FCF record, replace the in-use FCF record
2431 * with the new FCF record, mark FCF fast failover
2432 * completed, and then start register the new FCF
2433 * record.
2434 */
2435
0c9ab6f5 2436 /* Unregister the current in-use FCF record */
ecfd03c6 2437 lpfc_unregister_fcf(phba);
0c9ab6f5
JS
2438
2439 /* Replace in-use record with the new record */
dbb6b3ab 2440 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
2441 "2842 Replace in-use FCF (x%x) "
2442 "with failover FCF (x%x)\n",
dbb6b3ab
JS
2443 phba->fcf.current_rec.fcf_indx,
2444 phba->fcf.failover_rec.fcf_indx);
ecfd03c6
JS
2445 memcpy(&phba->fcf.current_rec,
2446 &phba->fcf.failover_rec,
2447 sizeof(struct lpfc_fcf_rec));
3804dc84
JS
2448 /*
2449 * Mark the fast FCF failover rediscovery completed
2450 * and the start of the first round of the roundrobin
2451 * FCF failover.
2452 */
0c9ab6f5 2453 spin_lock_irq(&phba->hbalock);
a93ff37a 2454 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
0c9ab6f5 2455 spin_unlock_irq(&phba->hbalock);
ecfd03c6
JS
2456 /* Register to the new FCF record */
2457 lpfc_register_fcf(phba);
2458 } else {
2459 /*
2460 * In case of transaction period to fast FCF failover,
2461 * do nothing when search to the end of the FCF table.
2462 */
2463 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2464 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2465 return;
dbb6b3ab 2466
7d791df7
JS
2467 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2468 phba->fcf.fcf_flag & FCF_IN_USE) {
dbb6b3ab
JS
2469 /*
2470 * In case the current in-use FCF record no
2471 * longer existed during FCF discovery that
2472 * was not triggered by fast FCF failover
2473 * process, treat it as fast FCF failover.
2474 */
2475 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2476 "2841 In-use FCF record (x%x) "
2477 "not reported, entering fast "
2478 "FCF failover mode scanning.\n",
2479 phba->fcf.current_rec.fcf_indx);
2480 spin_lock_irq(&phba->hbalock);
2481 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2482 spin_unlock_irq(&phba->hbalock);
dbb6b3ab
JS
2483 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2484 LPFC_FCOE_FCF_GET_FIRST);
2485 return;
2486 }
0c9ab6f5 2487 /* Register to the new FCF record */
ecfd03c6
JS
2488 lpfc_register_fcf(phba);
2489 }
2490 } else
0c9ab6f5 2491 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
6fb120a7
JS
2492 return;
2493
2494out:
2495 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2496 lpfc_register_fcf(phba);
2497
2498 return;
2499}
2500
0c9ab6f5 2501/**
a93ff37a 2502 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
0c9ab6f5
JS
2503 * @phba: pointer to lpfc hba data structure.
2504 * @mboxq: pointer to mailbox object.
2505 *
a93ff37a 2506 * This is the callback function for FLOGI failure roundrobin FCF failover
0c9ab6f5
JS
2507 * read FCF record mailbox command from the eligible FCF record bmask for
2508 * performing the failover. If the FCF read back is not valid/available, it
2509 * fails through to retrying FLOGI to the currently registered FCF again.
2510 * Otherwise, if the FCF read back is valid and available, it will set the
2511 * newly read FCF record to the failover FCF record, unregister currently
2512 * registered FCF record, copy the failover FCF record to the current
2513 * FCF record, and then register the current FCF record before proceeding
2514 * to trying FLOGI on the new failover FCF.
2515 */
2516void
2517lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2518{
2519 struct fcf_record *new_fcf_record;
2520 uint32_t boot_flag, addr_mode;
a93ff37a 2521 uint16_t next_fcf_index, fcf_index;
0c9ab6f5
JS
2522 uint16_t current_fcf_index;
2523 uint16_t vlan_id;
a93ff37a 2524 int rc;
0c9ab6f5 2525
a93ff37a 2526 /* If link state is not up, stop the roundrobin failover process */
0c9ab6f5
JS
2527 if (phba->link_state < LPFC_LINK_UP) {
2528 spin_lock_irq(&phba->hbalock);
2529 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
a93ff37a 2530 phba->hba_flag &= ~FCF_RR_INPROG;
0c9ab6f5 2531 spin_unlock_irq(&phba->hbalock);
a93ff37a 2532 goto out;
0c9ab6f5
JS
2533 }
2534
2535 /* Parse the FCF record from the non-embedded mailbox command */
2536 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2537 &next_fcf_index);
2538 if (!new_fcf_record) {
2539 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2540 "2766 Mailbox command READ_FCF_RECORD "
2541 "failed to retrieve a FCF record.\n");
a93ff37a 2542 goto error_out;
0c9ab6f5
JS
2543 }
2544
2545 /* Get the needed parameters from FCF record */
a93ff37a
JS
2546 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2547 &addr_mode, &vlan_id);
0c9ab6f5
JS
2548
2549 /* Log the FCF record information if turned on */
2550 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2551 next_fcf_index);
2552
a93ff37a
JS
2553 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2554 if (!rc) {
2555 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2556 "2848 Remove ineligible FCF (x%x) from "
2557 "from roundrobin bmask\n", fcf_index);
2558 /* Clear roundrobin bmask bit for ineligible FCF */
2559 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2560 /* Perform next round of roundrobin FCF failover */
2561 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2562 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2563 if (rc)
2564 goto out;
2565 goto error_out;
2566 }
2567
2568 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2569 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2570 "2760 Perform FLOGI roundrobin FCF failover: "
2571 "FCF (x%x) back to FCF (x%x)\n",
2572 phba->fcf.current_rec.fcf_indx, fcf_index);
2573 /* Wait 500 ms before retrying FLOGI to current FCF */
2574 msleep(500);
76a95d75 2575 lpfc_issue_init_vfi(phba->pport);
a93ff37a
JS
2576 goto out;
2577 }
2578
0c9ab6f5 2579 /* Upload new FCF record to the failover FCF record */
dbb6b3ab 2580 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
2581 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2582 phba->fcf.failover_rec.fcf_indx, fcf_index);
0c9ab6f5
JS
2583 spin_lock_irq(&phba->hbalock);
2584 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2585 new_fcf_record, addr_mode, vlan_id,
2586 (boot_flag ? BOOT_ENABLE : 0));
2587 spin_unlock_irq(&phba->hbalock);
2588
2589 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2590
2591 /* Unregister the current in-use FCF record */
2592 lpfc_unregister_fcf(phba);
2593
2594 /* Replace in-use record with the new record */
2595 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2596 sizeof(struct lpfc_fcf_rec));
2597
2598 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
2599 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2600 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
0c9ab6f5 2601
a93ff37a
JS
2602error_out:
2603 lpfc_register_fcf(phba);
0c9ab6f5
JS
2604out:
2605 lpfc_sli4_mbox_cmd_free(phba, mboxq);
0c9ab6f5
JS
2606}
2607
2608/**
2609 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2610 * @phba: pointer to lpfc hba data structure.
2611 * @mboxq: pointer to mailbox object.
2612 *
2613 * This is the callback function of read FCF record mailbox command for
a93ff37a 2614 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
0c9ab6f5
JS
2615 * failover when a new FCF event happened. If the FCF read back is
2616 * valid/available and it passes the connection list check, it updates
a93ff37a 2617 * the bmask for the eligible FCF record for roundrobin failover.
0c9ab6f5
JS
2618 */
2619void
2620lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2621{
2622 struct fcf_record *new_fcf_record;
2623 uint32_t boot_flag, addr_mode;
2624 uint16_t fcf_index, next_fcf_index;
2625 uint16_t vlan_id;
2626 int rc;
2627
2628 /* If link state is not up, no need to proceed */
2629 if (phba->link_state < LPFC_LINK_UP)
2630 goto out;
2631
2632 /* If FCF discovery period is over, no need to proceed */
3804dc84 2633 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
0c9ab6f5
JS
2634 goto out;
2635
2636 /* Parse the FCF record from the non-embedded mailbox command */
2637 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2638 &next_fcf_index);
2639 if (!new_fcf_record) {
2640 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2641 "2767 Mailbox command READ_FCF_RECORD "
2642 "failed to retrieve a FCF record.\n");
2643 goto out;
2644 }
2645
2646 /* Check the connection list for eligibility */
2647 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2648 &addr_mode, &vlan_id);
2649
2650 /* Log the FCF record information if turned on */
2651 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2652 next_fcf_index);
2653
2654 if (!rc)
2655 goto out;
2656
2657 /* Update the eligible FCF record index bmask */
2658 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
7d791df7
JS
2659
2660 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
0c9ab6f5
JS
2661
2662out:
2663 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2664}
2665
76a95d75
JS
2666/**
2667 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2668 * @phba: pointer to lpfc hba data structure.
2669 * @mboxq: pointer to mailbox data structure.
2670 *
2671 * This function handles completion of init vfi mailbox command.
2672 */
2673void
2674lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2675{
2676 struct lpfc_vport *vport = mboxq->vport;
2677
df9e1b59
JS
2678 /*
2679 * VFI not supported on interface type 0, just do the flogi
2680 * Also continue if the VFI is in use - just use the same one.
2681 */
2682 if (mboxq->u.mb.mbxStatus &&
2683 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2684 LPFC_SLI_INTF_IF_TYPE_0) &&
2685 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
76a95d75
JS
2686 lpfc_printf_vlog(vport, KERN_ERR,
2687 LOG_MBOX,
2688 "2891 Init VFI mailbox failed 0x%x\n",
2689 mboxq->u.mb.mbxStatus);
2690 mempool_free(mboxq, phba->mbox_mem_pool);
2691 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2692 return;
2693 }
73d91e50 2694
76a95d75
JS
2695 lpfc_initial_flogi(vport);
2696 mempool_free(mboxq, phba->mbox_mem_pool);
2697 return;
2698}
2699
2700/**
2701 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2702 * @vport: pointer to lpfc_vport data structure.
2703 *
2704 * This function issue a init_vfi mailbox command to initialize the VFI and
2705 * VPI for the physical port.
2706 */
2707void
2708lpfc_issue_init_vfi(struct lpfc_vport *vport)
2709{
2710 LPFC_MBOXQ_t *mboxq;
2711 int rc;
2712 struct lpfc_hba *phba = vport->phba;
2713
2714 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2715 if (!mboxq) {
2716 lpfc_printf_vlog(vport, KERN_ERR,
2717 LOG_MBOX, "2892 Failed to allocate "
2718 "init_vfi mailbox\n");
2719 return;
2720 }
2721 lpfc_init_vfi(mboxq, vport);
2722 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2723 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2724 if (rc == MBX_NOT_FINISHED) {
2725 lpfc_printf_vlog(vport, KERN_ERR,
2726 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2727 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2728 }
2729}
2730
1c6834a7
JS
2731/**
2732 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2733 * @phba: pointer to lpfc hba data structure.
2734 * @mboxq: pointer to mailbox data structure.
2735 *
2736 * This function handles completion of init vpi mailbox command.
2737 */
695a814e 2738void
1c6834a7
JS
2739lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2740{
2741 struct lpfc_vport *vport = mboxq->vport;
695a814e 2742 struct lpfc_nodelist *ndlp;
72100cc4
JS
2743 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2744
1c6834a7
JS
2745 if (mboxq->u.mb.mbxStatus) {
2746 lpfc_printf_vlog(vport, KERN_ERR,
2747 LOG_MBOX,
2748 "2609 Init VPI mailbox failed 0x%x\n",
2749 mboxq->u.mb.mbxStatus);
2750 mempool_free(mboxq, phba->mbox_mem_pool);
2751 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2752 return;
2753 }
72100cc4 2754 spin_lock_irq(shost->host_lock);
1c6834a7 2755 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
72100cc4 2756 spin_unlock_irq(shost->host_lock);
1c6834a7 2757
695a814e
JS
2758 /* If this port is physical port or FDISC is done, do reg_vpi */
2759 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2760 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2761 if (!ndlp)
2762 lpfc_printf_vlog(vport, KERN_ERR,
2763 LOG_DISCOVERY,
2764 "2731 Cannot find fabric "
2765 "controller node\n");
2766 else
2767 lpfc_register_new_vport(phba, vport, ndlp);
2768 mempool_free(mboxq, phba->mbox_mem_pool);
2769 return;
2770 }
2771
1c6834a7
JS
2772 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2773 lpfc_initial_fdisc(vport);
2774 else {
2775 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
6a9c52cf
JS
2776 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2777 "2606 No NPIV Fabric support\n");
1c6834a7 2778 }
695a814e 2779 mempool_free(mboxq, phba->mbox_mem_pool);
1c6834a7
JS
2780 return;
2781}
2782
ecfd03c6
JS
2783/**
2784 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2785 * @vport: pointer to lpfc_vport data structure.
2786 *
2787 * This function issue a init_vpi mailbox command to initialize
2788 * VPI for the vport.
2789 */
2790void
2791lpfc_issue_init_vpi(struct lpfc_vport *vport)
2792{
2793 LPFC_MBOXQ_t *mboxq;
2794 int rc;
2795
2796 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2797 if (!mboxq) {
2798 lpfc_printf_vlog(vport, KERN_ERR,
2799 LOG_MBOX, "2607 Failed to allocate "
2800 "init_vpi mailbox\n");
2801 return;
2802 }
2803 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2804 mboxq->vport = vport;
2805 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2806 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2807 if (rc == MBX_NOT_FINISHED) {
2808 lpfc_printf_vlog(vport, KERN_ERR,
2809 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2810 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2811 }
2812}
2813
6fb120a7
JS
2814/**
2815 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2816 * @phba: pointer to lpfc hba data structure.
2817 *
2818 * This function loops through the list of vports on the @phba and issues an
2819 * FDISC if possible.
2820 */
2821void
2822lpfc_start_fdiscs(struct lpfc_hba *phba)
2823{
2824 struct lpfc_vport **vports;
2825 int i;
2826
2827 vports = lpfc_create_vport_work_array(phba);
2828 if (vports != NULL) {
2829 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2830 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2831 continue;
2832 /* There are no vpi for this vport */
2833 if (vports[i]->vpi > phba->max_vpi) {
2834 lpfc_vport_set_state(vports[i],
2835 FC_VPORT_FAILED);
2836 continue;
2837 }
76a95d75 2838 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6fb120a7
JS
2839 lpfc_vport_set_state(vports[i],
2840 FC_VPORT_LINKDOWN);
2841 continue;
2842 }
1c6834a7 2843 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
ecfd03c6 2844 lpfc_issue_init_vpi(vports[i]);
1c6834a7
JS
2845 continue;
2846 }
6fb120a7
JS
2847 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2848 lpfc_initial_fdisc(vports[i]);
2849 else {
2850 lpfc_vport_set_state(vports[i],
2851 FC_VPORT_NO_FABRIC_SUPP);
2852 lpfc_printf_vlog(vports[i], KERN_ERR,
2853 LOG_ELS,
2854 "0259 No NPIV "
2855 "Fabric support\n");
2856 }
2857 }
2858 }
2859 lpfc_destroy_vport_work_array(phba, vports);
2860}
2861
2862void
2863lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2864{
2865 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2866 struct lpfc_vport *vport = mboxq->vport;
72100cc4 2867 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6fb120a7 2868
f5eca9be
JS
2869 /*
2870 * VFI not supported for interface type 0, so ignore any mailbox
2871 * error (except VFI in use) and continue with the discovery.
2872 */
2873 if (mboxq->u.mb.mbxStatus &&
2874 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2875 LPFC_SLI_INTF_IF_TYPE_0) &&
2876 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
6fb120a7
JS
2877 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2878 "2018 REG_VFI mbxStatus error x%x "
2879 "HBA state x%x\n",
2880 mboxq->u.mb.mbxStatus, vport->port_state);
76a95d75 2881 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
6fb120a7
JS
2882 /* FLOGI failed, use loop map to make discovery list */
2883 lpfc_disc_list_loopmap(vport);
2884 /* Start discovery */
2885 lpfc_disc_start(vport);
1b51197d 2886 goto out_free_mem;
6fb120a7
JS
2887 }
2888 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1b51197d 2889 goto out_free_mem;
6fb120a7 2890 }
c868595d 2891 /* The VPI is implicitly registered when the VFI is registered */
72100cc4 2892 spin_lock_irq(shost->host_lock);
c868595d 2893 vport->vpi_state |= LPFC_VPI_REGISTERED;
695a814e 2894 vport->fc_flag |= FC_VFI_REGISTERED;
695a814e 2895 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5248a749 2896 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
72100cc4 2897 spin_unlock_irq(shost->host_lock);
6fb120a7 2898
1b51197d
JS
2899 /* In case SLI4 FC loopback test, we are ready */
2900 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2901 (phba->link_flag & LS_LOOPBACK_MODE)) {
2902 phba->link_state = LPFC_HBA_READY;
2903 goto out_free_mem;
2904 }
2905
6fb120a7 2906 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
939723a4
JS
2907 /*
2908 * For private loop or for NPort pt2pt,
2909 * just start discovery and we are done.
2910 */
2911 if ((vport->fc_flag & FC_PT2PT) ||
2912 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2913 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2914
76a95d75
JS
2915 /* Use loop map to make discovery list */
2916 lpfc_disc_list_loopmap(vport);
2917 /* Start discovery */
2918 lpfc_disc_start(vport);
2919 } else {
2920 lpfc_start_fdiscs(phba);
2921 lpfc_do_scr_ns_plogi(phba, vport);
2922 }
6fb120a7
JS
2923 }
2924
1b51197d 2925out_free_mem:
6fb120a7
JS
2926 mempool_free(mboxq, phba->mbox_mem_pool);
2927 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2928 kfree(dmabuf);
2929 return;
2930}
2931
dea3101e 2932static void
2e0fef85 2933lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2934{
6fb120a7 2935 MAILBOX_t *mb = &pmb->u.mb;
dea3101e 2936 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
2e0fef85 2937 struct lpfc_vport *vport = pmb->vport;
dea3101e 2938
2939
2940 /* Check for error */
2941 if (mb->mbxStatus) {
2942 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
e8b62011
JS
2943 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2944 "0319 READ_SPARAM mbxStatus error x%x "
2945 "hba state x%x>\n",
2946 mb->mbxStatus, vport->port_state);
dea3101e 2947 lpfc_linkdown(phba);
dea3101e 2948 goto out;
2949 }
2950
2e0fef85 2951 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea3101e 2952 sizeof (struct serv_parm));
0558056c 2953 lpfc_update_vport_wwn(vport);
92d7f7b0
JS
2954 if (vport->port_type == LPFC_PHYSICAL_PORT) {
2955 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
2956 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
2957 }
2958
dea3101e 2959 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2960 kfree(mp);
2e0fef85 2961 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2962 return;
2963
2964out:
2965 pmb->context1 = NULL;
2966 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2967 kfree(mp);
92d7f7b0
JS
2968 lpfc_issue_clear_la(phba, vport);
2969 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2970 return;
2971}
2972
2973static void
76a95d75 2974lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea3101e 2975{
92d7f7b0 2976 struct lpfc_vport *vport = phba->pport;
6fb120a7 2977 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
df9e1b59 2978 struct Scsi_Host *shost;
2e0fef85 2979 int i;
14691150
JS
2980 struct lpfc_dmabuf *mp;
2981 int rc;
6fb120a7 2982 struct fcf_record *fcf_record;
14691150 2983
92d7f7b0 2984 spin_lock_irq(&phba->hbalock);
76a95d75
JS
2985 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
2986 case LPFC_LINK_SPEED_1GHZ:
2987 case LPFC_LINK_SPEED_2GHZ:
2988 case LPFC_LINK_SPEED_4GHZ:
2989 case LPFC_LINK_SPEED_8GHZ:
2990 case LPFC_LINK_SPEED_10GHZ:
2991 case LPFC_LINK_SPEED_16GHZ:
2992 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
f4b4c68f 2993 break;
92d7f7b0 2994 default:
76a95d75 2995 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
92d7f7b0 2996 break;
dea3101e 2997 }
2998
76a95d75 2999 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
92d7f7b0 3000 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea3101e 3001
df9e1b59 3002 shost = lpfc_shost_from_vport(vport);
76a95d75 3003 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
92d7f7b0 3004 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea3101e 3005
6a9c52cf
JS
3006 /* if npiv is enabled and this adapter supports npiv log
3007 * a message that npiv is not supported in this topology
3008 */
3009 if (phba->cfg_enable_npiv && phba->max_vpi)
495a714c
JS
3010 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3011 "1309 Link Up Event npiv not supported in loop "
3012 "topology\n");
92d7f7b0 3013 /* Get Loop Map information */
df9e1b59 3014 if (bf_get(lpfc_mbx_read_top_il, la)) {
72859909 3015 spin_lock(shost->host_lock);
2e0fef85 3016 vport->fc_flag |= FC_LBIT;
72859909 3017 spin_unlock(shost->host_lock);
df9e1b59 3018 }
dea3101e 3019
76a95d75
JS
3020 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3021 i = la->lilpBde64.tus.f.bdeSize;
dea3101e 3022
3023 if (i == 0) {
3024 phba->alpa_map[0] = 0;
3025 } else {
e8b62011 3026 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea3101e 3027 int numalpa, j, k;
3028 union {
3029 uint8_t pamap[16];
3030 struct {
3031 uint32_t wd1;
3032 uint32_t wd2;
3033 uint32_t wd3;
3034 uint32_t wd4;
3035 } pa;
3036 } un;
3037 numalpa = phba->alpa_map[0];
3038 j = 0;
3039 while (j < numalpa) {
3040 memset(un.pamap, 0, 16);
3041 for (k = 1; j < numalpa; k++) {
3042 un.pamap[k - 1] =
3043 phba->alpa_map[j + 1];
3044 j++;
3045 if (k == 16)
3046 break;
3047 }
3048 /* Link Up Event ALPA map */
3049 lpfc_printf_log(phba,
92d7f7b0
JS
3050 KERN_WARNING,
3051 LOG_LINK_EVENT,
e8b62011 3052 "1304 Link Up Event "
92d7f7b0
JS
3053 "ALPA map Data: x%x "
3054 "x%x x%x x%x\n",
92d7f7b0
JS
3055 un.pa.wd1, un.pa.wd2,
3056 un.pa.wd3, un.pa.wd4);
dea3101e 3057 }
3058 }
3059 }
3060 } else {
92d7f7b0 3061 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
78b2d852 3062 if (phba->max_vpi && phba->cfg_enable_npiv &&
df9e1b59 3063 (phba->sli_rev >= LPFC_SLI_REV3))
92d7f7b0
JS
3064 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3065 }
2e0fef85 3066 vport->fc_myDID = phba->fc_pref_DID;
72859909 3067 spin_lock(shost->host_lock);
2e0fef85 3068 vport->fc_flag |= FC_LBIT;
72859909 3069 spin_unlock(shost->host_lock);
dea3101e 3070 }
92d7f7b0 3071 spin_unlock_irq(&phba->hbalock);
dea3101e 3072
3073 lpfc_linkup(phba);
9f1177a3
JS
3074 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3075 if (!sparam_mbox)
3076 goto out;
3077
3078 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3079 if (rc) {
3080 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3081 goto out;
3082 }
3083 sparam_mbox->vport = vport;
3084 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3085 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3086 if (rc == MBX_NOT_FINISHED) {
3087 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3088 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3089 kfree(mp);
3090 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3091 goto out;
dea3101e 3092 }
3093
76a95d75 3094 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6fb120a7
JS
3095 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3096 if (!cfglink_mbox)
3097 goto out;
2e0fef85 3098 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea3101e 3099 lpfc_config_link(phba, cfglink_mbox);
2e0fef85 3100 cfglink_mbox->vport = vport;
25594c6b 3101 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
0b727fea 3102 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
6fb120a7
JS
3103 if (rc == MBX_NOT_FINISHED) {
3104 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3105 goto out;
3106 }
3107 } else {
32b9793f 3108 vport->port_state = LPFC_VPORT_UNKNOWN;
6fb120a7
JS
3109 /*
3110 * Add the driver's default FCF record at FCF index 0 now. This
3111 * is phase 1 implementation that support FCF index 0 and driver
3112 * defaults.
3113 */
45ed1190 3114 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
6fb120a7
JS
3115 fcf_record = kzalloc(sizeof(struct fcf_record),
3116 GFP_KERNEL);
3117 if (unlikely(!fcf_record)) {
3118 lpfc_printf_log(phba, KERN_ERR,
3119 LOG_MBOX | LOG_SLI,
b595076a 3120 "2554 Could not allocate memory for "
6fb120a7
JS
3121 "fcf record\n");
3122 rc = -ENODEV;
3123 goto out;
3124 }
3125
3126 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3127 LPFC_FCOE_FCF_DEF_INDEX);
3128 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3129 if (unlikely(rc)) {
3130 lpfc_printf_log(phba, KERN_ERR,
3131 LOG_MBOX | LOG_SLI,
3132 "2013 Could not manually add FCF "
3133 "record 0, status %d\n", rc);
3134 rc = -ENODEV;
3135 kfree(fcf_record);
3136 goto out;
3137 }
3138 kfree(fcf_record);
3139 }
3140 /*
3141 * The driver is expected to do FIP/FCF. Call the port
3142 * and get the FCF Table.
3143 */
32b9793f 3144 spin_lock_irq(&phba->hbalock);
a93ff37a 3145 if (phba->hba_flag & FCF_TS_INPROG) {
32b9793f
JS
3146 spin_unlock_irq(&phba->hbalock);
3147 return;
3148 }
0c9ab6f5
JS
3149 /* This is the initial FCF discovery scan */
3150 phba->fcf.fcf_flag |= FCF_INIT_DISC;
32b9793f 3151 spin_unlock_irq(&phba->hbalock);
0c9ab6f5
JS
3152 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3153 "2778 Start FCF table scan at linkup\n");
0c9ab6f5
JS
3154 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3155 LPFC_FCOE_FCF_GET_FIRST);
3156 if (rc) {
3157 spin_lock_irq(&phba->hbalock);
3158 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3159 spin_unlock_irq(&phba->hbalock);
6fb120a7 3160 goto out;
0c9ab6f5 3161 }
38b92ef8 3162 /* Reset FCF roundrobin bmask for new discovery */
7d791df7 3163 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea3101e 3164 }
6fb120a7
JS
3165
3166 return;
92d7f7b0
JS
3167out:
3168 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
3169 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3170 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3171 vport->port_state, sparam_mbox, cfglink_mbox);
92d7f7b0
JS
3172 lpfc_issue_clear_la(phba, vport);
3173 return;
dea3101e 3174}
3175
3176static void
84774a4d 3177lpfc_enable_la(struct lpfc_hba *phba)
2e0fef85 3178{
dea3101e 3179 uint32_t control;
3180 struct lpfc_sli *psli = &phba->sli;
2e0fef85 3181 spin_lock_irq(&phba->hbalock);
dea3101e 3182 psli->sli_flag |= LPFC_PROCESS_LA;
3772a991
JS
3183 if (phba->sli_rev <= LPFC_SLI_REV3) {
3184 control = readl(phba->HCregaddr);
3185 control |= HC_LAINT_ENA;
3186 writel(control, phba->HCregaddr);
3187 readl(phba->HCregaddr); /* flush */
3188 }
2e0fef85 3189 spin_unlock_irq(&phba->hbalock);
dea3101e 3190}
3191
84774a4d
JS
3192static void
3193lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3194{
3195 lpfc_linkdown(phba);
3196 lpfc_enable_la(phba);
6fb120a7 3197 lpfc_unregister_unused_fcf(phba);
84774a4d
JS
3198 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3199}
3200
3201
dea3101e 3202/*
76a95d75 3203 * This routine handles processing a READ_TOPOLOGY mailbox
dea3101e 3204 * command upon completion. It is setup in the LPFC_MBOXQ
3205 * as the completion routine when the command is
3206 * handed off to the SLI layer.
3207 */
3208void
76a95d75 3209lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 3210{
2e0fef85
JS
3211 struct lpfc_vport *vport = pmb->vport;
3212 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
76a95d75 3213 struct lpfc_mbx_read_top *la;
04c68496 3214 MAILBOX_t *mb = &pmb->u.mb;
dea3101e 3215 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3216
0d2b6b83
JS
3217 /* Unblock ELS traffic */
3218 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 3219 /* Check for error */
3220 if (mb->mbxStatus) {
ed957684 3221 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
e8b62011
JS
3222 "1307 READ_LA mbox error x%x state x%x\n",
3223 mb->mbxStatus, vport->port_state);
dea3101e 3224 lpfc_mbx_issue_link_down(phba);
2e0fef85 3225 phba->link_state = LPFC_HBA_ERROR;
76a95d75 3226 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea3101e 3227 }
3228
76a95d75 3229 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea3101e 3230
3231 memcpy(&phba->alpa_map[0], mp->virt, 128);
3232
2e0fef85 3233 spin_lock_irq(shost->host_lock);
76a95d75 3234 if (bf_get(lpfc_mbx_read_top_pb, la))
2e0fef85 3235 vport->fc_flag |= FC_BYPASSED_MODE;
c9f8735b 3236 else
2e0fef85
JS
3237 vport->fc_flag &= ~FC_BYPASSED_MODE;
3238 spin_unlock_irq(shost->host_lock);
c9f8735b 3239
0c287589 3240 if ((phba->fc_eventTag < la->eventTag) ||
92d7f7b0 3241 (phba->fc_eventTag == la->eventTag)) {
dea3101e 3242 phba->fc_stat.LinkMultiEvent++;
76a95d75 3243 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea3101e 3244 if (phba->fc_eventTag != 0)
3245 lpfc_linkdown(phba);
92d7f7b0 3246 }
dea3101e 3247
3248 phba->fc_eventTag = la->eventTag;
72100cc4 3249 spin_lock_irq(&phba->hbalock);
76a95d75 3250 if (bf_get(lpfc_mbx_read_top_mm, la))
84774a4d
JS
3251 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3252 else
3253 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
72100cc4 3254 spin_unlock_irq(&phba->hbalock);
dea3101e 3255
4d9ab994 3256 phba->link_events++;
76a95d75
JS
3257 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
3258 (!bf_get(lpfc_mbx_read_top_mm, la))) {
dea3101e 3259 phba->fc_stat.LinkUp++;
2e0fef85 3260 if (phba->link_flag & LS_LOOPBACK_MODE) {
3163f725 3261 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
e8b62011
JS
3262 "1306 Link Up Event in loop back mode "
3263 "x%x received Data: x%x x%x x%x x%x\n",
3264 la->eventTag, phba->fc_eventTag,
76a95d75
JS
3265 bf_get(lpfc_mbx_read_top_alpa_granted,
3266 la),
3267 bf_get(lpfc_mbx_read_top_link_spd, la),
e8b62011 3268 phba->alpa_map[0]);
5b8bd0c9
JS
3269 } else {
3270 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
e8b62011 3271 "1303 Link Up Event x%x received "
84774a4d 3272 "Data: x%x x%x x%x x%x x%x x%x %d\n",
e8b62011 3273 la->eventTag, phba->fc_eventTag,
76a95d75
JS
3274 bf_get(lpfc_mbx_read_top_alpa_granted,
3275 la),
3276 bf_get(lpfc_mbx_read_top_link_spd, la),
84774a4d 3277 phba->alpa_map[0],
76a95d75
JS
3278 bf_get(lpfc_mbx_read_top_mm, la),
3279 bf_get(lpfc_mbx_read_top_fa, la),
84774a4d 3280 phba->wait_4_mlo_maint_flg);
5b8bd0c9 3281 }
92d7f7b0 3282 lpfc_mbx_process_link_up(phba, la);
76a95d75
JS
3283 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3284 LPFC_ATT_LINK_DOWN) {
dea3101e 3285 phba->fc_stat.LinkDown++;
1b51197d 3286 if (phba->link_flag & LS_LOOPBACK_MODE)
3163f725
JS
3287 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3288 "1308 Link Down Event in loop back mode "
3289 "x%x received "
3290 "Data: x%x x%x x%x\n",
3291 la->eventTag, phba->fc_eventTag,
3292 phba->pport->port_state, vport->fc_flag);
1b51197d 3293 else
3163f725 3294 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
e8b62011 3295 "1305 Link Down Event x%x received "
84774a4d
JS
3296 "Data: x%x x%x x%x x%x x%x\n",
3297 la->eventTag, phba->fc_eventTag,
3298 phba->pport->port_state, vport->fc_flag,
76a95d75
JS
3299 bf_get(lpfc_mbx_read_top_mm, la),
3300 bf_get(lpfc_mbx_read_top_fa, la));
84774a4d
JS
3301 lpfc_mbx_issue_link_down(phba);
3302 }
76a95d75
JS
3303 if ((bf_get(lpfc_mbx_read_top_mm, la)) &&
3304 (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)) {
84774a4d
JS
3305 if (phba->link_state != LPFC_LINK_DOWN) {
3306 phba->fc_stat.LinkDown++;
3307 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3308 "1312 Link Down Event x%x received "
dea3101e 3309 "Data: x%x x%x x%x\n",
e8b62011 3310 la->eventTag, phba->fc_eventTag,
2e0fef85 3311 phba->pport->port_state, vport->fc_flag);
84774a4d
JS
3312 lpfc_mbx_issue_link_down(phba);
3313 } else
3314 lpfc_enable_la(phba);
3315
3316 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3317 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3318 "Data: x%x x%x x%x\n",
3319 la->eventTag, phba->fc_eventTag,
3320 phba->pport->port_state, vport->fc_flag);
3321 /*
3322 * The cmnd that triggered this will be waiting for this
3323 * signal.
3324 */
3325 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3326 if (phba->wait_4_mlo_maint_flg) {
3327 phba->wait_4_mlo_maint_flg = 0;
3328 wake_up_interruptible(&phba->wait_4_mlo_m_q);
3163f725 3329 }
84774a4d
JS
3330 }
3331
76a95d75
JS
3332 if (bf_get(lpfc_mbx_read_top_fa, la)) {
3333 if (bf_get(lpfc_mbx_read_top_mm, la))
84774a4d
JS
3334 lpfc_issue_clear_la(phba, vport);
3335 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
76a95d75
JS
3336 "1311 fa %d\n",
3337 bf_get(lpfc_mbx_read_top_fa, la));
dea3101e 3338 }
3339
76a95d75 3340lpfc_mbx_cmpl_read_topology_free_mbuf:
dea3101e 3341 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3342 kfree(mp);
3343 mempool_free(pmb, phba->mbox_mem_pool);
3344 return;
3345}
3346
3347/*
3348 * This routine handles processing a REG_LOGIN mailbox
3349 * command upon completion. It is setup in the LPFC_MBOXQ
3350 * as the completion routine when the command is
3351 * handed off to the SLI layer.
3352 */
3353void
2e0fef85 3354lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 3355{
2e0fef85 3356 struct lpfc_vport *vport = pmb->vport;
92d7f7b0 3357 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2e0fef85 3358 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
ffc95493 3359 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 3360
dea3101e 3361 pmb->context1 = NULL;
d439d286 3362 pmb->context2 = NULL;
dea3101e 3363
ffc95493
JS
3364 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3365 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3366
4042629e
JS
3367 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3368 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
ffc95493
JS
3369 /* We rcvd a rscn after issuing this
3370 * mbox reg login, we may have cycled
3371 * back through the state and be
3372 * back at reg login state so this
3373 * mbox needs to be ignored becase
3374 * there is another reg login in
25985edc 3375 * process.
ffc95493
JS
3376 */
3377 spin_lock_irq(shost->host_lock);
3378 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3379 spin_unlock_irq(shost->host_lock);
ffc95493
JS
3380 } else
3381 /* Good status, call state machine */
3382 lpfc_disc_state_machine(vport, ndlp, pmb,
3383 NLP_EVT_CMPL_REG_LOGIN);
3384
dea3101e 3385 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3386 kfree(mp);
2e0fef85 3387 mempool_free(pmb, phba->mbox_mem_pool);
fa4066b6
JS
3388 /* decrement the node reference count held for this callback
3389 * function.
3390 */
329f9bc7 3391 lpfc_nlp_put(ndlp);
dea3101e 3392
3393 return;
3394}
3395
92d7f7b0
JS
3396static void
3397lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3398{
04c68496 3399 MAILBOX_t *mb = &pmb->u.mb;
92d7f7b0
JS
3400 struct lpfc_vport *vport = pmb->vport;
3401 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3402
3403 switch (mb->mbxStatus) {
3404 case 0x0011:
3405 case 0x0020:
e8b62011
JS
3406 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3407 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3408 mb->mbxStatus);
92d7f7b0 3409 break;
78730cfe
JS
3410 /* If VPI is busy, reset the HBA */
3411 case 0x9700:
3412 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3413 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3414 vport->vpi, mb->mbxStatus);
3415 if (!(phba->pport->load_flag & FC_UNLOADING))
3416 lpfc_workq_post_event(phba, NULL, NULL,
3417 LPFC_EVT_RESET_HBA);
92d7f7b0 3418 }
72100cc4 3419 spin_lock_irq(shost->host_lock);
c868595d 3420 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
1987807d 3421 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
72100cc4 3422 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
3423 vport->unreg_vpi_cmpl = VPORT_OK;
3424 mempool_free(pmb, phba->mbox_mem_pool);
1151e3ec 3425 lpfc_cleanup_vports_rrqs(vport, NULL);
92d7f7b0
JS
3426 /*
3427 * This shost reference might have been taken at the beginning of
3428 * lpfc_vport_delete()
3429 */
1c6f4ef5 3430 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
92d7f7b0
JS
3431 scsi_host_put(shost);
3432}
3433
d7c255b2 3434int
92d7f7b0
JS
3435lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3436{
3437 struct lpfc_hba *phba = vport->phba;
3438 LPFC_MBOXQ_t *mbox;
3439 int rc;
3440
3441 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3442 if (!mbox)
d7c255b2 3443 return 1;
92d7f7b0
JS
3444
3445 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3446 mbox->vport = vport;
3447 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
0b727fea 3448 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
92d7f7b0 3449 if (rc == MBX_NOT_FINISHED) {
e8b62011
JS
3450 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3451 "1800 Could not issue unreg_vpi\n");
92d7f7b0
JS
3452 mempool_free(mbox, phba->mbox_mem_pool);
3453 vport->unreg_vpi_cmpl = VPORT_ERROR;
d7c255b2 3454 return rc;
92d7f7b0 3455 }
d7c255b2 3456 return 0;
92d7f7b0
JS
3457}
3458
3459static void
3460lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3461{
3462 struct lpfc_vport *vport = pmb->vport;
3463 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
04c68496 3464 MAILBOX_t *mb = &pmb->u.mb;
92d7f7b0
JS
3465
3466 switch (mb->mbxStatus) {
3467 case 0x0011:
3468 case 0x9601:
3469 case 0x9602:
e8b62011
JS
3470 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3471 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3472 mb->mbxStatus);
92d7f7b0
JS
3473 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3474 spin_lock_irq(shost->host_lock);
3475 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3476 spin_unlock_irq(shost->host_lock);
3477 vport->fc_myDID = 0;
3478 goto out;
3479 }
92d7f7b0 3480
72100cc4 3481 spin_lock_irq(shost->host_lock);
c868595d 3482 vport->vpi_state |= LPFC_VPI_REGISTERED;
695a814e 3483 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
72100cc4 3484 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
3485 vport->num_disc_nodes = 0;
3486 /* go thru NPR list and issue ELS PLOGIs */
3487 if (vport->fc_npr_cnt)
3488 lpfc_els_disc_plogi(vport);
3489
3490 if (!vport->num_disc_nodes) {
3491 spin_lock_irq(shost->host_lock);
3492 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3493 spin_unlock_irq(shost->host_lock);
3494 lpfc_can_disctmo(vport);
3495 }
3496 vport->port_state = LPFC_VPORT_READY;
3497
3498out:
3499 mempool_free(pmb, phba->mbox_mem_pool);
3500 return;
3501}
3502
21e9a0a5
JS
3503/**
3504 * lpfc_create_static_vport - Read HBA config region to create static vports.
3505 * @phba: pointer to lpfc hba data structure.
3506 *
3507 * This routine issue a DUMP mailbox command for config region 22 to get
3508 * the list of static vports to be created. The function create vports
3509 * based on the information returned from the HBA.
3510 **/
3511void
3512lpfc_create_static_vport(struct lpfc_hba *phba)
3513{
3514 LPFC_MBOXQ_t *pmb = NULL;
3515 MAILBOX_t *mb;
3516 struct static_vport_info *vport_info;
cdcc2343 3517 int mbx_wait_rc = 0, i;
21e9a0a5
JS
3518 struct fc_vport_identifiers vport_id;
3519 struct fc_vport *new_fc_vport;
3520 struct Scsi_Host *shost;
3521 struct lpfc_vport *vport;
3522 uint16_t offset = 0;
3523 uint8_t *vport_buff;
1c6834a7
JS
3524 struct lpfc_dmabuf *mp;
3525 uint32_t byte_count = 0;
21e9a0a5
JS
3526
3527 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3528 if (!pmb) {
3529 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3530 "0542 lpfc_create_static_vport failed to"
3531 " allocate mailbox memory\n");
3532 return;
3533 }
cdcc2343 3534 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
21e9a0a5
JS
3535 mb = &pmb->u.mb;
3536
3537 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3538 if (!vport_info) {
3539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3540 "0543 lpfc_create_static_vport failed to"
3541 " allocate vport_info\n");
3542 mempool_free(pmb, phba->mbox_mem_pool);
3543 return;
3544 }
3545
3546 vport_buff = (uint8_t *) vport_info;
3547 do {
cdcc2343
JS
3548 /* free dma buffer from previous round */
3549 if (pmb->context1) {
3550 mp = (struct lpfc_dmabuf *)pmb->context1;
3551 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3552 kfree(mp);
3553 }
1c6834a7
JS
3554 if (lpfc_dump_static_vport(phba, pmb, offset))
3555 goto out;
3556
21e9a0a5 3557 pmb->vport = phba->pport;
cdcc2343
JS
3558 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3559 LPFC_MBOX_TMO);
21e9a0a5 3560
cdcc2343 3561 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
21e9a0a5
JS
3562 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3563 "0544 lpfc_create_static_vport failed to"
3564 " issue dump mailbox command ret 0x%x "
3565 "status 0x%x\n",
cdcc2343 3566 mbx_wait_rc, mb->mbxStatus);
21e9a0a5
JS
3567 goto out;
3568 }
3569
1c6834a7
JS
3570 if (phba->sli_rev == LPFC_SLI_REV4) {
3571 byte_count = pmb->u.mqe.un.mb_words[5];
cdcc2343 3572 mp = (struct lpfc_dmabuf *)pmb->context1;
1c6834a7
JS
3573 if (byte_count > sizeof(struct static_vport_info) -
3574 offset)
3575 byte_count = sizeof(struct static_vport_info)
3576 - offset;
3577 memcpy(vport_buff + offset, mp->virt, byte_count);
3578 offset += byte_count;
3579 } else {
3580 if (mb->un.varDmp.word_cnt >
3581 sizeof(struct static_vport_info) - offset)
3582 mb->un.varDmp.word_cnt =
3583 sizeof(struct static_vport_info)
3584 - offset;
3585 byte_count = mb->un.varDmp.word_cnt;
3586 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3587 vport_buff + offset,
3588 byte_count);
3589
3590 offset += byte_count;
3591 }
21e9a0a5 3592
1c6834a7 3593 } while (byte_count &&
21e9a0a5
JS
3594 offset < sizeof(struct static_vport_info));
3595
3596
3597 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3598 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3599 != VPORT_INFO_REV)) {
3600 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3601 "0545 lpfc_create_static_vport bad"
3602 " information header 0x%x 0x%x\n",
3603 le32_to_cpu(vport_info->signature),
3604 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3605
3606 goto out;
3607 }
3608
3609 shost = lpfc_shost_from_vport(phba->pport);
3610
3611 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3612 memset(&vport_id, 0, sizeof(vport_id));
3613 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3614 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3615 if (!vport_id.port_name || !vport_id.node_name)
3616 continue;
3617
3618 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3619 vport_id.vport_type = FC_PORTTYPE_NPIV;
3620 vport_id.disable = false;
3621 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3622
3623 if (!new_fc_vport) {
3624 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3625 "0546 lpfc_create_static_vport failed to"
e4e74273 3626 " create vport\n");
21e9a0a5
JS
3627 continue;
3628 }
3629
3630 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3631 vport->vport_flag |= STATIC_VPORT;
3632 }
3633
3634out:
21e9a0a5 3635 kfree(vport_info);
cdcc2343
JS
3636 if (mbx_wait_rc != MBX_TIMEOUT) {
3637 if (pmb->context1) {
3638 mp = (struct lpfc_dmabuf *)pmb->context1;
1c6834a7
JS
3639 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3640 kfree(mp);
3641 }
21e9a0a5 3642 mempool_free(pmb, phba->mbox_mem_pool);
1c6834a7 3643 }
21e9a0a5
JS
3644
3645 return;
3646}
3647
dea3101e 3648/*
3649 * This routine handles processing a Fabric REG_LOGIN mailbox
3650 * command upon completion. It is setup in the LPFC_MBOXQ
3651 * as the completion routine when the command is
3652 * handed off to the SLI layer.
3653 */
3654void
2e0fef85 3655lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 3656{
92d7f7b0 3657 struct lpfc_vport *vport = pmb->vport;
21e9a0a5 3658 MAILBOX_t *mb = &pmb->u.mb;
2e0fef85 3659 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
92d7f7b0 3660 struct lpfc_nodelist *ndlp;
df9e1b59 3661 struct Scsi_Host *shost;
dea3101e 3662
549e55cd 3663 ndlp = (struct lpfc_nodelist *) pmb->context2;
329f9bc7
JS
3664 pmb->context1 = NULL;
3665 pmb->context2 = NULL;
d439d286 3666
dea3101e 3667 if (mb->mbxStatus) {
21e9a0a5
JS
3668 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3669 "0258 Register Fabric login error: 0x%x\n",
3670 mb->mbxStatus);
dea3101e 3671 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3672 kfree(mp);
329f9bc7 3673 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 3674
76a95d75 3675 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
92d7f7b0
JS
3676 /* FLOGI failed, use loop map to make discovery list */
3677 lpfc_disc_list_loopmap(vport);
3678
3679 /* Start discovery */
3680 lpfc_disc_start(vport);
e47c9093
JS
3681 /* Decrement the reference count to ndlp after the
3682 * reference to the ndlp are done.
3683 */
3684 lpfc_nlp_put(ndlp);
92d7f7b0
JS
3685 return;
3686 }
3687
3688 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e47c9093
JS
3689 /* Decrement the reference count to ndlp after the reference
3690 * to the ndlp are done.
3691 */
3692 lpfc_nlp_put(ndlp);
dea3101e 3693 return;
3694 }
3695
6d368e53
JS
3696 if (phba->sli_rev < LPFC_SLI_REV4)
3697 ndlp->nlp_rpi = mb->un.varWords[0];
4042629e 3698 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea3101e 3699 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 3700 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 3701
2e0fef85 3702 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
4b40c59e
JS
3703 /* when physical port receive logo donot start
3704 * vport discovery */
3705 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3706 lpfc_start_fdiscs(phba);
df9e1b59
JS
3707 else {
3708 shost = lpfc_shost_from_vport(vport);
3709 spin_lock_irq(shost->host_lock);
4b40c59e 3710 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
df9e1b59
JS
3711 spin_unlock_irq(shost->host_lock);
3712 }
92d7f7b0 3713 lpfc_do_scr_ns_plogi(phba, vport);
dea3101e 3714 }
3715
3716 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3717 kfree(mp);
329f9bc7 3718 mempool_free(pmb, phba->mbox_mem_pool);
e47c9093
JS
3719
3720 /* Drop the reference count from the mbox at the end after
3721 * all the current reference to the ndlp have been done.
3722 */
3723 lpfc_nlp_put(ndlp);
dea3101e 3724 return;
3725}
3726
3727/*
3728 * This routine handles processing a NameServer REG_LOGIN mailbox
3729 * command upon completion. It is setup in the LPFC_MBOXQ
3730 * as the completion routine when the command is
3731 * handed off to the SLI layer.
3732 */
3733void
2e0fef85 3734lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 3735{
21e9a0a5 3736 MAILBOX_t *mb = &pmb->u.mb;
2e0fef85
JS
3737 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3738 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3739 struct lpfc_vport *vport = pmb->vport;
dea3101e 3740
d439d286
JS
3741 pmb->context1 = NULL;
3742 pmb->context2 = NULL;
3743
dea3101e 3744 if (mb->mbxStatus) {
92d7f7b0 3745out:
21e9a0a5
JS
3746 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3747 "0260 Register NameServer error: 0x%x\n",
3748 mb->mbxStatus);
fa4066b6
JS
3749 /* decrement the node reference count held for this
3750 * callback function.
3751 */
329f9bc7 3752 lpfc_nlp_put(ndlp);
dea3101e 3753 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3754 kfree(mp);
de0c5b32 3755 mempool_free(pmb, phba->mbox_mem_pool);
87af33fe
JS
3756
3757 /* If no other thread is using the ndlp, free it */
3758 lpfc_nlp_not_used(ndlp);
dea3101e 3759
76a95d75 3760 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
92d7f7b0
JS
3761 /*
3762 * RegLogin failed, use loop map to make discovery
3763 * list
3764 */
3765 lpfc_disc_list_loopmap(vport);
dea3101e 3766
92d7f7b0
JS
3767 /* Start discovery */
3768 lpfc_disc_start(vport);
3769 return;
3770 }
3771 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea3101e 3772 return;
3773 }
3774
6d368e53
JS
3775 if (phba->sli_rev < LPFC_SLI_REV4)
3776 ndlp->nlp_rpi = mb->un.varWords[0];
4042629e 3777 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea3101e 3778 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 3779 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 3780
2e0fef85
JS
3781 if (vport->port_state < LPFC_VPORT_READY) {
3782 /* Link up discovery requires Fabric registration. */
92d7f7b0
JS
3783 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
3784 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3785 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3786 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3787 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
3788
3789 /* Issue SCR just before NameServer GID_FT Query */
3790 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea3101e 3791 }
3792
2e0fef85 3793 vport->fc_ns_retry = 0;
dea3101e 3794 /* Good status, issue CT Request to NameServer */
92d7f7b0 3795 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea3101e 3796 /* Cannot issue NameServer Query, so finish up discovery */
92d7f7b0 3797 goto out;
dea3101e 3798 }
3799
fa4066b6
JS
3800 /* decrement the node reference count held for this
3801 * callback function.
3802 */
329f9bc7 3803 lpfc_nlp_put(ndlp);
dea3101e 3804 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3805 kfree(mp);
2e0fef85 3806 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 3807
3808 return;
3809}
3810
3811static void
2e0fef85 3812lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 3813{
2e0fef85
JS
3814 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3815 struct fc_rport *rport;
dea3101e 3816 struct lpfc_rport_data *rdata;
3817 struct fc_rport_identifiers rport_ids;
2e0fef85 3818 struct lpfc_hba *phba = vport->phba;
dea3101e 3819
3820 /* Remote port has reappeared. Re-register w/ FC transport */
68ce1eb5
AM
3821 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3822 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea3101e 3823 rport_ids.port_id = ndlp->nlp_DID;
3824 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea3101e 3825
329f9bc7
JS
3826 /*
3827 * We leave our node pointer in rport->dd_data when we unregister a
3828 * FCP target port. But fc_remote_port_add zeros the space to which
3829 * rport->dd_data points. So, if we're reusing a previously
3830 * registered port, drop the reference that we took the last time we
3831 * registered the port.
3832 */
3833 if (ndlp->rport && ndlp->rport->dd_data &&
e47c9093 3834 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
329f9bc7 3835 lpfc_nlp_put(ndlp);
858c9f6c
JS
3836
3837 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
3838 "rport add: did:x%x flg:x%x type x%x",
3839 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3840
9589b062
JS
3841 /* Don't add the remote port if unloading. */
3842 if (vport->load_flag & FC_UNLOADING)
3843 return;
3844
2e0fef85 3845 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
329f9bc7 3846 if (!rport || !get_device(&rport->dev)) {
dea3101e 3847 dev_printk(KERN_WARNING, &phba->pcidev->dev,
3848 "Warning: fc_remote_port_add failed\n");
3849 return;
3850 }
3851
3852 /* initialize static port data */
3853 rport->maxframe_size = ndlp->nlp_maxframe;
3854 rport->supported_classes = ndlp->nlp_class_sup;
dea3101e 3855 rdata = rport->dd_data;
329f9bc7 3856 rdata->pnode = lpfc_nlp_get(ndlp);
23dc04f1
JSEC
3857
3858 if (ndlp->nlp_type & NLP_FCP_TARGET)
3859 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
3860 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
3861 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
3862
23dc04f1
JSEC
3863 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
3864 fc_remote_port_rolechg(rport, rport_ids.roles);
3865
34f5ad8b
JS
3866 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3867 "3183 rport register x%06x, rport %p role x%x\n",
3868 ndlp->nlp_DID, rport, rport_ids.roles);
3869
071fbd3d 3870 if ((rport->scsi_target_id != -1) &&
92d7f7b0 3871 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
071fbd3d
JS
3872 ndlp->nlp_sid = rport->scsi_target_id;
3873 }
19a7b4ae
JSEC
3874 return;
3875}
3876
3877static void
2e0fef85 3878lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
19a7b4ae
JSEC
3879{
3880 struct fc_rport *rport = ndlp->rport;
c01f3208 3881
858c9f6c
JS
3882 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
3883 "rport delete: did:x%x flg:x%x type x%x",
3884 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3885
34f5ad8b
JS
3886 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3887 "3184 rport unregister x%06x, rport %p\n",
3888 ndlp->nlp_DID, rport);
3889
19a7b4ae 3890 fc_remote_port_delete(rport);
dea3101e 3891
3892 return;
3893}
3894
de0c5b32 3895static void
2e0fef85 3896lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
dea3101e 3897{
2e0fef85
JS
3898 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3899
3900 spin_lock_irq(shost->host_lock);
de0c5b32
JS
3901 switch (state) {
3902 case NLP_STE_UNUSED_NODE:
2e0fef85 3903 vport->fc_unused_cnt += count;
de0c5b32
JS
3904 break;
3905 case NLP_STE_PLOGI_ISSUE:
2e0fef85 3906 vport->fc_plogi_cnt += count;
de0c5b32
JS
3907 break;
3908 case NLP_STE_ADISC_ISSUE:
2e0fef85 3909 vport->fc_adisc_cnt += count;
dea3101e 3910 break;
de0c5b32 3911 case NLP_STE_REG_LOGIN_ISSUE:
2e0fef85 3912 vport->fc_reglogin_cnt += count;
de0c5b32
JS
3913 break;
3914 case NLP_STE_PRLI_ISSUE:
2e0fef85 3915 vport->fc_prli_cnt += count;
de0c5b32
JS
3916 break;
3917 case NLP_STE_UNMAPPED_NODE:
2e0fef85 3918 vport->fc_unmap_cnt += count;
de0c5b32
JS
3919 break;
3920 case NLP_STE_MAPPED_NODE:
2e0fef85 3921 vport->fc_map_cnt += count;
de0c5b32
JS
3922 break;
3923 case NLP_STE_NPR_NODE:
2e0fef85 3924 vport->fc_npr_cnt += count;
de0c5b32
JS
3925 break;
3926 }
2e0fef85 3927 spin_unlock_irq(shost->host_lock);
de0c5b32 3928}
66a9ed66 3929
de0c5b32 3930static void
2e0fef85 3931lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
de0c5b32
JS
3932 int old_state, int new_state)
3933{
2e0fef85
JS
3934 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3935
de0c5b32 3936 if (new_state == NLP_STE_UNMAPPED_NODE) {
de0c5b32
JS
3937 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3938 ndlp->nlp_type |= NLP_FC_NODE;
3939 }
3940 if (new_state == NLP_STE_MAPPED_NODE)
3941 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
3942 if (new_state == NLP_STE_NPR_NODE)
3943 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
3944
3945 /* Transport interface */
3946 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
3947 old_state == NLP_STE_UNMAPPED_NODE)) {
2e0fef85
JS
3948 vport->phba->nport_event_cnt++;
3949 lpfc_unregister_remote_port(ndlp);
de0c5b32 3950 }
dea3101e 3951
de0c5b32
JS
3952 if (new_state == NLP_STE_MAPPED_NODE ||
3953 new_state == NLP_STE_UNMAPPED_NODE) {
2e0fef85 3954 vport->phba->nport_event_cnt++;
858c9f6c
JS
3955 /*
3956 * Tell the fc transport about the port, if we haven't
3957 * already. If we have, and it's a scsi entity, be
3958 * sure to unblock any attached scsi devices
3959 */
3960 lpfc_register_remote_port(vport, ndlp);
de0c5b32 3961 }
ea2151b4
JS
3962 if ((new_state == NLP_STE_MAPPED_NODE) &&
3963 (vport->stat_data_enabled)) {
3964 /*
3965 * A new target is discovered, if there is no buffer for
3966 * statistical data collection allocate buffer.
3967 */
3968 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
3969 sizeof(struct lpfc_scsicmd_bkt),
3970 GFP_KERNEL);
3971
3972 if (!ndlp->lat_data)
3973 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3974 "0286 lpfc_nlp_state_cleanup failed to "
3975 "allocate statistical data buffer DID "
3976 "0x%x\n", ndlp->nlp_DID);
3977 }
858c9f6c
JS
3978 /*
3979 * if we added to Mapped list, but the remote port
3980 * registration failed or assigned a target id outside
3981 * our presentable range - move the node to the
3982 * Unmapped List
3983 */
de0c5b32
JS
3984 if (new_state == NLP_STE_MAPPED_NODE &&
3985 (!ndlp->rport ||
3986 ndlp->rport->scsi_target_id == -1 ||
3987 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2e0fef85 3988 spin_lock_irq(shost->host_lock);
de0c5b32 3989 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2e0fef85
JS
3990 spin_unlock_irq(shost->host_lock);
3991 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 3992 }
de0c5b32
JS
3993}
3994
685f0bf7
JS
3995static char *
3996lpfc_nlp_state_name(char *buffer, size_t size, int state)
3997{
3998 static char *states[] = {
3999 [NLP_STE_UNUSED_NODE] = "UNUSED",
4000 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4001 [NLP_STE_ADISC_ISSUE] = "ADISC",
4002 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4003 [NLP_STE_PRLI_ISSUE] = "PRLI",
086a345f 4004 [NLP_STE_LOGO_ISSUE] = "LOGO",
685f0bf7
JS
4005 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4006 [NLP_STE_MAPPED_NODE] = "MAPPED",
4007 [NLP_STE_NPR_NODE] = "NPR",
4008 };
4009
311464ec 4010 if (state < NLP_STE_MAX_STATE && states[state])
685f0bf7
JS
4011 strlcpy(buffer, states[state], size);
4012 else
4013 snprintf(buffer, size, "unknown (%d)", state);
4014 return buffer;
4015}
4016
de0c5b32 4017void
2e0fef85
JS
4018lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4019 int state)
de0c5b32 4020{
2e0fef85 4021 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
de0c5b32 4022 int old_state = ndlp->nlp_state;
685f0bf7 4023 char name1[16], name2[16];
de0c5b32 4024
e8b62011
JS
4025 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4026 "0904 NPort state transition x%06x, %s -> %s\n",
4027 ndlp->nlp_DID,
4028 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4029 lpfc_nlp_state_name(name2, sizeof(name2), state));
858c9f6c
JS
4030
4031 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4032 "node statechg did:x%x old:%d ste:%d",
4033 ndlp->nlp_DID, old_state, state);
4034
de0c5b32 4035 if (old_state == NLP_STE_NPR_NODE &&
de0c5b32 4036 state != NLP_STE_NPR_NODE)
2e0fef85 4037 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32
JS
4038 if (old_state == NLP_STE_UNMAPPED_NODE) {
4039 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4040 ndlp->nlp_type &= ~NLP_FC_NODE;
4041 }
4042
685f0bf7 4043 if (list_empty(&ndlp->nlp_listp)) {
2e0fef85
JS
4044 spin_lock_irq(shost->host_lock);
4045 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4046 spin_unlock_irq(shost->host_lock);
685f0bf7 4047 } else if (old_state)
2e0fef85 4048 lpfc_nlp_counters(vport, old_state, -1);
de0c5b32
JS
4049
4050 ndlp->nlp_state = state;
2e0fef85
JS
4051 lpfc_nlp_counters(vport, state, 1);
4052 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
de0c5b32
JS
4053}
4054
e47c9093
JS
4055void
4056lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4057{
4058 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4059
4060 if (list_empty(&ndlp->nlp_listp)) {
4061 spin_lock_irq(shost->host_lock);
4062 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4063 spin_unlock_irq(shost->host_lock);
4064 }
4065}
4066
de0c5b32 4067void
2e0fef85 4068lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 4069{
2e0fef85
JS
4070 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4071
0d2b6b83 4072 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32 4073 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2e0fef85
JS
4074 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4075 spin_lock_irq(shost->host_lock);
685f0bf7 4076 list_del_init(&ndlp->nlp_listp);
2e0fef85 4077 spin_unlock_irq(shost->host_lock);
858c9f6c 4078 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
e47c9093
JS
4079 NLP_STE_UNUSED_NODE);
4080}
4081
4d9db01e 4082static void
e47c9093
JS
4083lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4084{
0d2b6b83 4085 lpfc_cancel_retry_delay_tmo(vport, ndlp);
e47c9093
JS
4086 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4087 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4088 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4089 NLP_STE_UNUSED_NODE);
4090}
109f6ed0 4091/**
3621a710 4092 * lpfc_initialize_node - Initialize all fields of node object
109f6ed0
JS
4093 * @vport: Pointer to Virtual Port object.
4094 * @ndlp: Pointer to FC node object.
4095 * @did: FC_ID of the node.
a257bf90
JS
4096 *
4097 * This function is always called when node object need to be initialized.
4098 * It initializes all the fields of the node object. Although the reference
4099 * to phba from @ndlp can be obtained indirectly through it's reference to
4100 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4101 * to the life-span of the @ndlp might go beyond the existence of @vport as
4102 * the final release of ndlp is determined by its reference count. And, the
4103 * operation on @ndlp needs the reference to phba.
109f6ed0
JS
4104 **/
4105static inline void
4106lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4107 uint32_t did)
4108{
4109 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4110 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4111 init_timer(&ndlp->nlp_delayfunc);
4112 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4113 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4114 ndlp->nlp_DID = did;
4115 ndlp->vport = vport;
a257bf90 4116 ndlp->phba = vport->phba;
109f6ed0
JS
4117 ndlp->nlp_sid = NLP_NO_SID;
4118 kref_init(&ndlp->kref);
4119 NLP_INT_NODE_ACT(ndlp);
4120 atomic_set(&ndlp->cmd_pending, 0);
7dc517df 4121 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4042629e
JS
4122 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4123 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
109f6ed0 4124}
e47c9093
JS
4125
4126struct lpfc_nodelist *
4127lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4128 int state)
4129{
4130 struct lpfc_hba *phba = vport->phba;
4131 uint32_t did;
4132 unsigned long flags;
4133
4134 if (!ndlp)
4135 return NULL;
4136
4137 spin_lock_irqsave(&phba->ndlp_lock, flags);
4138 /* The ndlp should not be in memory free mode */
4139 if (NLP_CHK_FREE_REQ(ndlp)) {
4140 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4141 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4142 "0277 lpfc_enable_node: ndlp:x%p "
4143 "usgmap:x%x refcnt:%d\n",
4144 (void *)ndlp, ndlp->nlp_usg_map,
4145 atomic_read(&ndlp->kref.refcount));
4146 return NULL;
4147 }
4148 /* The ndlp should not already be in active mode */
4149 if (NLP_CHK_NODE_ACT(ndlp)) {
4150 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4151 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4152 "0278 lpfc_enable_node: ndlp:x%p "
4153 "usgmap:x%x refcnt:%d\n",
4154 (void *)ndlp, ndlp->nlp_usg_map,
4155 atomic_read(&ndlp->kref.refcount));
4156 return NULL;
4157 }
4158
4159 /* Keep the original DID */
4160 did = ndlp->nlp_DID;
4161
4162 /* re-initialize ndlp except of ndlp linked list pointer */
4163 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4164 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
109f6ed0 4165 lpfc_initialize_node(vport, ndlp, did);
e47c9093
JS
4166
4167 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4168
4169 if (state != NLP_STE_UNUSED_NODE)
4170 lpfc_nlp_set_state(vport, ndlp, state);
4171
4172 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4173 "node enable: did:x%x",
4174 ndlp->nlp_DID, 0, 0);
4175 return ndlp;
de0c5b32
JS
4176}
4177
4178void
2e0fef85 4179lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 4180{
87af33fe 4181 /*
fa4066b6 4182 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
87af33fe 4183 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
fa4066b6
JS
4184 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4185 * until ALL other outstanding threads have completed. We check
4186 * that the ndlp not already in the UNUSED state before we proceed.
87af33fe 4187 */
fa4066b6
JS
4188 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4189 return;
51ef4c26 4190 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
1151e3ec
JS
4191 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4192 lpfc_cleanup_vports_rrqs(vport, ndlp);
87af33fe 4193 lpfc_nlp_put(ndlp);
98c9ea5c 4194 return;
dea3101e 4195}
4196
4197/*
4198 * Start / ReStart rescue timer for Discovery / RSCN handling
4199 */
4200void
2e0fef85 4201lpfc_set_disctmo(struct lpfc_vport *vport)
dea3101e 4202{
2e0fef85
JS
4203 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4204 struct lpfc_hba *phba = vport->phba;
dea3101e 4205 uint32_t tmo;
4206
2e0fef85 4207 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
025dfdaf 4208 /* For FAN, timeout should be greater than edtov */
c9f8735b
JW
4209 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4210 } else {
025dfdaf 4211 /* Normal discovery timeout should be > than ELS/CT timeout
c9f8735b
JW
4212 * FC spec states we need 3 * ratov for CT requests
4213 */
4214 tmo = ((phba->fc_ratov * 3) + 3);
4215 }
dea3101e 4216
858c9f6c
JS
4217
4218 if (!timer_pending(&vport->fc_disctmo)) {
4219 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4220 "set disc timer: tmo:x%x state:x%x flg:x%x",
4221 tmo, vport->port_state, vport->fc_flag);
4222 }
4223
2e0fef85
JS
4224 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
4225 spin_lock_irq(shost->host_lock);
4226 vport->fc_flag |= FC_DISC_TMO;
4227 spin_unlock_irq(shost->host_lock);
dea3101e 4228
4229 /* Start Discovery Timer state <hba_state> */
e8b62011
JS
4230 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4231 "0247 Start Discovery Timer state x%x "
4232 "Data: x%x x%lx x%x x%x\n",
4233 vport->port_state, tmo,
4234 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4235 vport->fc_adisc_cnt);
dea3101e 4236
4237 return;
4238}
4239
4240/*
4241 * Cancel rescue timer for Discovery / RSCN handling
4242 */
4243int
2e0fef85 4244lpfc_can_disctmo(struct lpfc_vport *vport)
dea3101e 4245{
2e0fef85 4246 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2e0fef85
JS
4247 unsigned long iflags;
4248
858c9f6c
JS
4249 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4250 "can disc timer: state:x%x rtry:x%x flg:x%x",
4251 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4252
dea3101e 4253 /* Turn off discovery timer if its running */
2e0fef85
JS
4254 if (vport->fc_flag & FC_DISC_TMO) {
4255 spin_lock_irqsave(shost->host_lock, iflags);
4256 vport->fc_flag &= ~FC_DISC_TMO;
4257 spin_unlock_irqrestore(shost->host_lock, iflags);
4258 del_timer_sync(&vport->fc_disctmo);
4259 spin_lock_irqsave(&vport->work_port_lock, iflags);
4260 vport->work_port_events &= ~WORKER_DISC_TMO;
4261 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea3101e 4262 }
4263
4264 /* Cancel Discovery Timer state <hba_state> */
e8b62011
JS
4265 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4266 "0248 Cancel Discovery Timer state x%x "
4267 "Data: x%x x%x x%x\n",
4268 vport->port_state, vport->fc_flag,
4269 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
2fe165b6 4270 return 0;
dea3101e 4271}
4272
4273/*
4274 * Check specified ring for outstanding IOCB on the SLI queue
4275 * Return true if iocb matches the specified nport
4276 */
4277int
2e0fef85
JS
4278lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4279 struct lpfc_sli_ring *pring,
4280 struct lpfc_iocbq *iocb,
4281 struct lpfc_nodelist *ndlp)
dea3101e 4282{
2e0fef85
JS
4283 struct lpfc_sli *psli = &phba->sli;
4284 IOCB_t *icmd = &iocb->iocb;
92d7f7b0
JS
4285 struct lpfc_vport *vport = ndlp->vport;
4286
4287 if (iocb->vport != vport)
4288 return 0;
4289
dea3101e 4290 if (pring->ringno == LPFC_ELS_RING) {
4291 switch (icmd->ulpCommand) {
4292 case CMD_GEN_REQUEST64_CR:
21e9a0a5 4293 if (iocb->context_un.ndlp == ndlp)
2fe165b6 4294 return 1;
dea3101e 4295 case CMD_ELS_REQUEST64_CR:
10d4e957
JS
4296 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4297 return 1;
dea3101e 4298 case CMD_XMIT_ELS_RSP64_CX:
4299 if (iocb->context1 == (uint8_t *) ndlp)
2fe165b6 4300 return 1;
dea3101e 4301 }
a4bc3379 4302 } else if (pring->ringno == psli->extra_ring) {
dea3101e 4303
4304 } else if (pring->ringno == psli->fcp_ring) {
4305 /* Skip match check if waiting to relogin to FCP target */
4306 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
92d7f7b0 4307 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
2fe165b6 4308 return 0;
dea3101e 4309 }
4310 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
2fe165b6 4311 return 1;
dea3101e 4312 }
4313 } else if (pring->ringno == psli->next_ring) {
4314
4315 }
2fe165b6 4316 return 0;
dea3101e 4317}
4318
4319/*
4320 * Free resources / clean up outstanding I/Os
4321 * associated with nlp_rpi in the LPFC_NODELIST entry.
4322 */
4323static int
2e0fef85 4324lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 4325{
2534ba75 4326 LIST_HEAD(completions);
dea3101e 4327 struct lpfc_sli *psli;
4328 struct lpfc_sli_ring *pring;
4329 struct lpfc_iocbq *iocb, *next_iocb;
6a9c52cf 4330 uint32_t i;
dea3101e 4331
92d7f7b0
JS
4332 lpfc_fabric_abort_nport(ndlp);
4333
dea3101e 4334 /*
4335 * Everything that matches on txcmplq will be returned
4336 * by firmware with a no rpi error.
4337 */
4338 psli = &phba->sli;
4042629e 4339 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
dea3101e 4340 /* Now process each ring */
4341 for (i = 0; i < psli->num_rings; i++) {
4342 pring = &psli->ring[i];
4343
2e0fef85 4344 spin_lock_irq(&phba->hbalock);
dea3101e 4345 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
2e0fef85 4346 list) {
dea3101e 4347 /*
4348 * Check to see if iocb matches the nport we are
4349 * looking for
4350 */
92d7f7b0
JS
4351 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
4352 ndlp))) {
dea3101e 4353 /* It matches, so deque and call compl
4354 with an error */
2534ba75
JS
4355 list_move_tail(&iocb->list,
4356 &completions);
dea3101e 4357 pring->txq_cnt--;
dea3101e 4358 }
4359 }
2e0fef85 4360 spin_unlock_irq(&phba->hbalock);
dea3101e 4361 }
4362 }
2534ba75 4363
a257bf90
JS
4364 /* Cancel all the IOCBs from the completions list */
4365 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4366 IOERR_SLI_ABORTED);
2534ba75 4367
2fe165b6 4368 return 0;
dea3101e 4369}
4370
086a345f
JS
4371/**
4372 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4373 * @phba: Pointer to HBA context object.
4374 * @pmb: Pointer to mailbox object.
4375 *
4376 * This function will issue an ELS LOGO command after completing
4377 * the UNREG_RPI.
4378 **/
4379void
4380lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4381{
4382 struct lpfc_vport *vport = pmb->vport;
4383 struct lpfc_nodelist *ndlp;
4384
4385 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4386 if (!ndlp)
4387 return;
4388 lpfc_issue_els_logo(vport, ndlp, 0);
4389}
4390
dea3101e 4391/*
4392 * Free rpi associated with LPFC_NODELIST entry.
4393 * This routine is called from lpfc_freenode(), when we are removing
4394 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4395 * LOGO that completes successfully, and we are waiting to PLOGI back
4396 * to the remote NPort. In addition, it is called after we receive
4397 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4398 * we are waiting to PLOGI back to the remote NPort.
4399 */
4400int
2e0fef85 4401lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 4402{
2e0fef85
JS
4403 struct lpfc_hba *phba = vport->phba;
4404 LPFC_MBOXQ_t *mbox;
dea3101e 4405 int rc;
6d368e53 4406 uint16_t rpi;
dea3101e 4407
4042629e 4408 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
2e0fef85
JS
4409 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4410 if (mbox) {
6d368e53
JS
4411 /* SLI4 ports require the physical rpi value. */
4412 rpi = ndlp->nlp_rpi;
4413 if (phba->sli_rev == LPFC_SLI_REV4)
4414 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
086a345f 4415
6d368e53 4416 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
ed957684 4417 mbox->vport = vport;
086a345f
JS
4418 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4419 mbox->context1 = ndlp;
4420 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4421 } else {
4422 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4423 }
4424
0b727fea 4425 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea3101e 4426 if (rc == MBX_NOT_FINISHED)
2e0fef85 4427 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 4428 }
dea3101e 4429 lpfc_no_rpi(phba, ndlp);
d7c47992 4430
4042629e
JS
4431 if (phba->sli_rev != LPFC_SLI_REV4)
4432 ndlp->nlp_rpi = 0;
4433 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
0c287589 4434 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea3101e 4435 return 1;
4436 }
4437 return 0;
4438}
4439
ecfd03c6
JS
4440/**
4441 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4442 * @phba: pointer to lpfc hba data structure.
4443 *
4444 * This routine is invoked to unregister all the currently registered RPIs
4445 * to the HBA.
4446 **/
4447void
4448lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4449{
4450 struct lpfc_vport **vports;
4451 struct lpfc_nodelist *ndlp;
4452 struct Scsi_Host *shost;
4453 int i;
4454
4455 vports = lpfc_create_vport_work_array(phba);
63e801ce
JS
4456 if (!vports) {
4457 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4458 "2884 Vport array allocation failed \n");
4459 return;
4460 }
ecfd03c6
JS
4461 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4462 shost = lpfc_shost_from_vport(vports[i]);
4463 spin_lock_irq(shost->host_lock);
4464 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4042629e 4465 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
0c9ab6f5
JS
4466 /* The mempool_alloc might sleep */
4467 spin_unlock_irq(shost->host_lock);
ecfd03c6 4468 lpfc_unreg_rpi(vports[i], ndlp);
0c9ab6f5
JS
4469 spin_lock_irq(shost->host_lock);
4470 }
ecfd03c6
JS
4471 }
4472 spin_unlock_irq(shost->host_lock);
4473 }
4474 lpfc_destroy_vport_work_array(phba, vports);
4475}
4476
92d7f7b0
JS
4477void
4478lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4479{
4480 struct lpfc_hba *phba = vport->phba;
4481 LPFC_MBOXQ_t *mbox;
4482 int rc;
4483
5af5eee7
JS
4484 if (phba->sli_rev == LPFC_SLI_REV4) {
4485 lpfc_sli4_unreg_all_rpis(vport);
4486 return;
4487 }
4488
92d7f7b0
JS
4489 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4490 if (mbox) {
6d368e53
JS
4491 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4492 mbox);
92d7f7b0
JS
4493 mbox->vport = vport;
4494 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
09372820
JS
4495 mbox->context1 = NULL;
4496 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
a257bf90 4497 if (rc != MBX_TIMEOUT)
92d7f7b0 4498 mempool_free(mbox, phba->mbox_mem_pool);
a257bf90
JS
4499
4500 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4501 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4502 "1836 Could not issue "
4503 "unreg_login(all_rpis) status %d\n", rc);
92d7f7b0
JS
4504 }
4505}
4506
4507void
4508lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4509{
4510 struct lpfc_hba *phba = vport->phba;
4511 LPFC_MBOXQ_t *mbox;
4512 int rc;
4513
4514 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4515 if (mbox) {
6d368e53
JS
4516 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4517 mbox);
92d7f7b0
JS
4518 mbox->vport = vport;
4519 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
09372820
JS
4520 mbox->context1 = NULL;
4521 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
a257bf90
JS
4522 if (rc != MBX_TIMEOUT)
4523 mempool_free(mbox, phba->mbox_mem_pool);
4524
4525 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
e8b62011
JS
4526 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4527 "1815 Could not issue "
a257bf90
JS
4528 "unreg_did (default rpis) status %d\n",
4529 rc);
92d7f7b0
JS
4530 }
4531}
4532
dea3101e 4533/*
4534 * Free resources associated with LPFC_NODELIST entry
4535 * so it can be freed.
4536 */
4537static int
2e0fef85 4538lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 4539{
2e0fef85
JS
4540 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4541 struct lpfc_hba *phba = vport->phba;
4542 LPFC_MBOXQ_t *mb, *nextmb;
dea3101e 4543 struct lpfc_dmabuf *mp;
dea3101e 4544
4545 /* Cleanup node for NPort <nlp_DID> */
e8b62011
JS
4546 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4547 "0900 Cleanup node for NPort x%x "
4548 "Data: x%x x%x x%x\n",
4549 ndlp->nlp_DID, ndlp->nlp_flag,
4550 ndlp->nlp_state, ndlp->nlp_rpi);
e47c9093
JS
4551 if (NLP_CHK_FREE_REQ(ndlp)) {
4552 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4553 "0280 lpfc_cleanup_node: ndlp:x%p "
4554 "usgmap:x%x refcnt:%d\n",
4555 (void *)ndlp, ndlp->nlp_usg_map,
4556 atomic_read(&ndlp->kref.refcount));
4557 lpfc_dequeue_node(vport, ndlp);
4558 } else {
4559 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4560 "0281 lpfc_cleanup_node: ndlp:x%p "
4561 "usgmap:x%x refcnt:%d\n",
4562 (void *)ndlp, ndlp->nlp_usg_map,
4563 atomic_read(&ndlp->kref.refcount));
4564 lpfc_disable_node(vport, ndlp);
4565 }
dea3101e 4566
086a345f
JS
4567
4568 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4569
dea3101e 4570 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4571 if ((mb = phba->sli.mbox_active)) {
04c68496 4572 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
086a345f 4573 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea3101e 4574 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4575 mb->context2 = NULL;
4576 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4577 }
4578 }
33ccf8d1 4579
2e0fef85 4580 spin_lock_irq(&phba->hbalock);
5ac6b303
JS
4581 /* Cleanup REG_LOGIN completions which are not yet processed */
4582 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4583 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
086a345f 4584 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5ac6b303
JS
4585 (ndlp != (struct lpfc_nodelist *) mb->context2))
4586 continue;
4587
4588 mb->context2 = NULL;
4589 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4590 }
4591
dea3101e 4592 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
04c68496 4593 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
086a345f 4594 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
92d7f7b0 4595 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea3101e 4596 mp = (struct lpfc_dmabuf *) (mb->context1);
4597 if (mp) {
2e0fef85 4598 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e 4599 kfree(mp);
4600 }
4601 list_del(&mb->list);
4602 mempool_free(mb, phba->mbox_mem_pool);
e47c9093
JS
4603 /* We shall not invoke the lpfc_nlp_put to decrement
4604 * the ndlp reference count as we are in the process
4605 * of lpfc_nlp_release.
4606 */
dea3101e 4607 }
4608 }
2e0fef85 4609 spin_unlock_irq(&phba->hbalock);
dea3101e 4610
e47c9093
JS
4611 lpfc_els_abort(phba, ndlp);
4612
2e0fef85 4613 spin_lock_irq(shost->host_lock);
c01f3208 4614 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 4615 spin_unlock_irq(shost->host_lock);
dea3101e 4616
5024ab17 4617 ndlp->nlp_last_elscmd = 0;
dea3101e 4618 del_timer_sync(&ndlp->nlp_delayfunc);
4619
0d2b6b83
JS
4620 list_del_init(&ndlp->els_retry_evt.evt_listp);
4621 list_del_init(&ndlp->dev_loss_evt.evt_listp);
1151e3ec 4622 lpfc_cleanup_vports_rrqs(vport, ndlp);
2e0fef85 4623 lpfc_unreg_rpi(vport, ndlp);
dea3101e 4624
2fe165b6 4625 return 0;
dea3101e 4626}
4627
4628/*
4629 * Check to see if we can free the nlp back to the freelist.
4630 * If we are in the middle of using the nlp in the discovery state
4631 * machine, defer the free till we reach the end of the state machine.
4632 */
329f9bc7 4633static void
2e0fef85 4634lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 4635{
a8adb832 4636 struct lpfc_hba *phba = vport->phba;
1dcb58e5 4637 struct lpfc_rport_data *rdata;
a8adb832
JS
4638 LPFC_MBOXQ_t *mbox;
4639 int rc;
dea3101e 4640
0d2b6b83 4641 lpfc_cancel_retry_delay_tmo(vport, ndlp);
21e9a0a5 4642 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
4042629e
JS
4643 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4644 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
a8adb832
JS
4645 /* For this case we need to cleanup the default rpi
4646 * allocated by the firmware.
4647 */
4648 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4649 != NULL) {
21e9a0a5 4650 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
4042629e 4651 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
a8adb832
JS
4652 if (rc) {
4653 mempool_free(mbox, phba->mbox_mem_pool);
4654 }
4655 else {
4656 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4657 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4658 mbox->vport = vport;
086a345f 4659 mbox->context2 = ndlp;
a8adb832
JS
4660 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4661 if (rc == MBX_NOT_FINISHED) {
4662 mempool_free(mbox, phba->mbox_mem_pool);
4663 }
4664 }
4665 }
4666 }
2e0fef85 4667 lpfc_cleanup_node(vport, ndlp);
1dcb58e5 4668
2e0fef85 4669 /*
92d7f7b0
JS
4670 * We can get here with a non-NULL ndlp->rport because when we
4671 * unregister a rport we don't break the rport/node linkage. So if we
4672 * do, make sure we don't leaving any dangling pointers behind.
2e0fef85 4673 */
92d7f7b0 4674 if (ndlp->rport) {
329f9bc7
JS
4675 rdata = ndlp->rport->dd_data;
4676 rdata->pnode = NULL;
4677 ndlp->rport = NULL;
dea3101e 4678 }
dea3101e 4679}
4680
4681static int
2e0fef85
JS
4682lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4683 uint32_t did)
dea3101e 4684{
2e0fef85 4685 D_ID mydid, ndlpdid, matchdid;
dea3101e 4686
4687 if (did == Bcast_DID)
2fe165b6 4688 return 0;
dea3101e 4689
dea3101e 4690 /* First check for Direct match */
4691 if (ndlp->nlp_DID == did)
2fe165b6 4692 return 1;
dea3101e 4693
4694 /* Next check for area/domain identically equals 0 match */
2e0fef85 4695 mydid.un.word = vport->fc_myDID;
dea3101e 4696 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
2fe165b6 4697 return 0;
dea3101e 4698 }
4699
4700 matchdid.un.word = did;
4701 ndlpdid.un.word = ndlp->nlp_DID;
4702 if (matchdid.un.b.id == ndlpdid.un.b.id) {
4703 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
4704 (mydid.un.b.area == matchdid.un.b.area)) {
4705 if ((ndlpdid.un.b.domain == 0) &&
4706 (ndlpdid.un.b.area == 0)) {
4707 if (ndlpdid.un.b.id)
2fe165b6 4708 return 1;
dea3101e 4709 }
2fe165b6 4710 return 0;
dea3101e 4711 }
4712
4713 matchdid.un.word = ndlp->nlp_DID;
4714 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
4715 (mydid.un.b.area == ndlpdid.un.b.area)) {
4716 if ((matchdid.un.b.domain == 0) &&
4717 (matchdid.un.b.area == 0)) {
4718 if (matchdid.un.b.id)
2fe165b6 4719 return 1;
dea3101e 4720 }
4721 }
4722 }
2fe165b6 4723 return 0;
dea3101e 4724}
4725
685f0bf7 4726/* Search for a nodelist entry */
2e0fef85
JS
4727static struct lpfc_nodelist *
4728__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea3101e 4729{
2fb9bd8b 4730 struct lpfc_nodelist *ndlp;
dea3101e 4731 uint32_t data1;
4732
2e0fef85
JS
4733 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4734 if (lpfc_matchdid(vport, ndlp, did)) {
685f0bf7
JS
4735 data1 = (((uint32_t) ndlp->nlp_state << 24) |
4736 ((uint32_t) ndlp->nlp_xri << 16) |
4737 ((uint32_t) ndlp->nlp_type << 8) |
4738 ((uint32_t) ndlp->nlp_rpi & 0xff));
e8b62011
JS
4739 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4740 "0929 FIND node DID "
4741 "Data: x%p x%x x%x x%x\n",
4742 ndlp, ndlp->nlp_DID,
4743 ndlp->nlp_flag, data1);
685f0bf7 4744 return ndlp;
dea3101e 4745 }
4746 }
66a9ed66 4747
dea3101e 4748 /* FIND node did <did> NOT FOUND */
e8b62011
JS
4749 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4750 "0932 FIND node did x%x NOT FOUND.\n", did);
dea3101e 4751 return NULL;
4752}
4753
4754struct lpfc_nodelist *
2e0fef85
JS
4755lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
4756{
4757 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4758 struct lpfc_nodelist *ndlp;
fedd3b7b 4759 unsigned long iflags;
2e0fef85 4760
fedd3b7b 4761 spin_lock_irqsave(shost->host_lock, iflags);
2e0fef85 4762 ndlp = __lpfc_findnode_did(vport, did);
fedd3b7b 4763 spin_unlock_irqrestore(shost->host_lock, iflags);
2e0fef85
JS
4764 return ndlp;
4765}
4766
4767struct lpfc_nodelist *
4768lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
dea3101e 4769{
2e0fef85 4770 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 4771 struct lpfc_nodelist *ndlp;
dea3101e 4772
2e0fef85 4773 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 4774 if (!ndlp) {
2e0fef85
JS
4775 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
4776 lpfc_rscn_payload_check(vport, did) == 0)
dea3101e 4777 return NULL;
4778 ndlp = (struct lpfc_nodelist *)
2e0fef85 4779 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea3101e 4780 if (!ndlp)
4781 return NULL;
2e0fef85
JS
4782 lpfc_nlp_init(vport, ndlp, did);
4783 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4784 spin_lock_irq(shost->host_lock);
dea3101e 4785 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 4786 spin_unlock_irq(shost->host_lock);
dea3101e 4787 return ndlp;
e47c9093
JS
4788 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
4789 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
4790 if (!ndlp)
4791 return NULL;
4792 spin_lock_irq(shost->host_lock);
4793 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4794 spin_unlock_irq(shost->host_lock);
4795 return ndlp;
dea3101e 4796 }
e47c9093 4797
58da1ffb
JS
4798 if ((vport->fc_flag & FC_RSCN_MODE) &&
4799 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
2e0fef85 4800 if (lpfc_rscn_payload_check(vport, did)) {
25985edc 4801 /* If we've already received a PLOGI from this NPort
87af33fe
JS
4802 * we don't need to try to discover it again.
4803 */
4804 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
4805 return NULL;
4806
c9f8735b
JW
4807 /* Since this node is marked for discovery,
4808 * delay timeout is not needed.
4809 */
0d2b6b83 4810 lpfc_cancel_retry_delay_tmo(vport, ndlp);
a257bf90
JS
4811 spin_lock_irq(shost->host_lock);
4812 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
4813 spin_unlock_irq(shost->host_lock);
071fbd3d 4814 } else
dea3101e 4815 ndlp = NULL;
2fe165b6 4816 } else {
25985edc 4817 /* If we've already received a PLOGI from this NPort,
87af33fe
JS
4818 * or we are already in the process of discovery on it,
4819 * we don't need to try to discover it again.
4820 */
685f0bf7 4821 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
87af33fe
JS
4822 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4823 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea3101e 4824 return NULL;
2e0fef85
JS
4825 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
4826 spin_lock_irq(shost->host_lock);
dea3101e 4827 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 4828 spin_unlock_irq(shost->host_lock);
dea3101e 4829 }
4830 return ndlp;
4831}
4832
4833/* Build a list of nodes to discover based on the loopmap */
4834void
2e0fef85 4835lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea3101e 4836{
2e0fef85 4837 struct lpfc_hba *phba = vport->phba;
dea3101e 4838 int j;
4839 uint32_t alpa, index;
4840
2e0fef85 4841 if (!lpfc_is_link_up(phba))
dea3101e 4842 return;
2e0fef85 4843
76a95d75 4844 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea3101e 4845 return;
dea3101e 4846
4847 /* Check for loop map present or not */
4848 if (phba->alpa_map[0]) {
4849 for (j = 1; j <= phba->alpa_map[0]; j++) {
4850 alpa = phba->alpa_map[j];
2e0fef85 4851 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea3101e 4852 continue;
2e0fef85 4853 lpfc_setup_disc_node(vport, alpa);
dea3101e 4854 }
4855 } else {
4856 /* No alpamap, so try all alpa's */
4857 for (j = 0; j < FC_MAXLOOP; j++) {
4858 /* If cfg_scan_down is set, start from highest
4859 * ALPA (0xef) to lowest (0x1).
4860 */
3de2a653 4861 if (vport->cfg_scan_down)
dea3101e 4862 index = j;
4863 else
4864 index = FC_MAXLOOP - j - 1;
4865 alpa = lpfcAlpaArray[index];
2e0fef85 4866 if ((vport->fc_myDID & 0xff) == alpa)
dea3101e 4867 continue;
2e0fef85 4868 lpfc_setup_disc_node(vport, alpa);
dea3101e 4869 }
4870 }
4871 return;
4872}
4873
dea3101e 4874void
2e0fef85 4875lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea3101e 4876{
dea3101e 4877 LPFC_MBOXQ_t *mbox;
2e0fef85
JS
4878 struct lpfc_sli *psli = &phba->sli;
4879 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
4880 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
4881 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
4882 int rc;
4883
92d7f7b0
JS
4884 /*
4885 * if it's not a physical port or if we already send
4886 * clear_la then don't send it.
4887 */
4888 if ((phba->link_state >= LPFC_CLEAR_LA) ||
da0436e9
JS
4889 (vport->port_type != LPFC_PHYSICAL_PORT) ||
4890 (phba->sli_rev == LPFC_SLI_REV4))
92d7f7b0
JS
4891 return;
4892
2e0fef85
JS
4893 /* Link up discovery */
4894 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
4895 phba->link_state = LPFC_CLEAR_LA;
4896 lpfc_clear_la(phba, mbox);
4897 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
4898 mbox->vport = vport;
0b727fea 4899 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2e0fef85
JS
4900 if (rc == MBX_NOT_FINISHED) {
4901 mempool_free(mbox, phba->mbox_mem_pool);
4902 lpfc_disc_flush_list(vport);
4903 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4904 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
4905 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
92d7f7b0
JS
4906 phba->link_state = LPFC_HBA_ERROR;
4907 }
4908 }
4909}
4910
4911/* Reg_vpi to tell firmware to resume normal operations */
4912void
4913lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
4914{
4915 LPFC_MBOXQ_t *regvpimbox;
4916
4917 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4918 if (regvpimbox) {
da0436e9 4919 lpfc_reg_vpi(vport, regvpimbox);
92d7f7b0
JS
4920 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
4921 regvpimbox->vport = vport;
0b727fea 4922 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
92d7f7b0
JS
4923 == MBX_NOT_FINISHED) {
4924 mempool_free(regvpimbox, phba->mbox_mem_pool);
2e0fef85
JS
4925 }
4926 }
4927}
4928
4929/* Start Link up / RSCN discovery on NPR nodes */
4930void
4931lpfc_disc_start(struct lpfc_vport *vport)
4932{
4933 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4934 struct lpfc_hba *phba = vport->phba;
685f0bf7 4935 uint32_t num_sent;
dea3101e 4936 uint32_t clear_la_pending;
685f0bf7 4937 int did_changed;
dea3101e 4938
2e0fef85 4939 if (!lpfc_is_link_up(phba))
dea3101e 4940 return;
2e0fef85
JS
4941
4942 if (phba->link_state == LPFC_CLEAR_LA)
dea3101e 4943 clear_la_pending = 1;
4944 else
4945 clear_la_pending = 0;
4946
2e0fef85
JS
4947 if (vport->port_state < LPFC_VPORT_READY)
4948 vport->port_state = LPFC_DISC_AUTH;
dea3101e 4949
2e0fef85
JS
4950 lpfc_set_disctmo(vport);
4951
4952 if (vport->fc_prevDID == vport->fc_myDID)
dea3101e 4953 did_changed = 0;
2e0fef85 4954 else
dea3101e 4955 did_changed = 1;
2e0fef85
JS
4956
4957 vport->fc_prevDID = vport->fc_myDID;
4958 vport->num_disc_nodes = 0;
dea3101e 4959
4960 /* Start Discovery state <hba_state> */
e8b62011
JS
4961 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4962 "0202 Start Discovery hba state x%x "
4963 "Data: x%x x%x x%x\n",
4964 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
4965 vport->fc_adisc_cnt);
dea3101e 4966
4967 /* First do ADISCs - if any */
2e0fef85 4968 num_sent = lpfc_els_disc_adisc(vport);
dea3101e 4969
4970 if (num_sent)
4971 return;
4972
6d368e53 4973 /* Register the VPI for SLI3, NON-NPIV only. */
92d7f7b0 4974 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1b32f6aa 4975 !(vport->fc_flag & FC_PT2PT) &&
da0436e9
JS
4976 !(vport->fc_flag & FC_RSCN_MODE) &&
4977 (phba->sli_rev < LPFC_SLI_REV4)) {
92d7f7b0
JS
4978 lpfc_issue_reg_vpi(phba, vport);
4979 return;
4980 }
4981
4982 /*
4983 * For SLI2, we need to set port_state to READY and continue
4984 * discovery.
4985 */
2e0fef85 4986 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
dea3101e 4987 /* If we get here, there is nothing to ADISC */
92d7f7b0 4988 if (vport->port_type == LPFC_PHYSICAL_PORT)
2e0fef85 4989 lpfc_issue_clear_la(phba, vport);
2e0fef85 4990
92d7f7b0 4991 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2e0fef85
JS
4992 vport->num_disc_nodes = 0;
4993 /* go thru NPR nodes and issue ELS PLOGIs */
4994 if (vport->fc_npr_cnt)
4995 lpfc_els_disc_plogi(vport);
4996
4997 if (!vport->num_disc_nodes) {
4998 spin_lock_irq(shost->host_lock);
4999 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5000 spin_unlock_irq(shost->host_lock);
92d7f7b0 5001 lpfc_can_disctmo(vport);
dea3101e 5002 }
5003 }
92d7f7b0 5004 vport->port_state = LPFC_VPORT_READY;
dea3101e 5005 } else {
5006 /* Next do PLOGIs - if any */
2e0fef85 5007 num_sent = lpfc_els_disc_plogi(vport);
dea3101e 5008
5009 if (num_sent)
5010 return;
5011
2e0fef85 5012 if (vport->fc_flag & FC_RSCN_MODE) {
dea3101e 5013 /* Check to see if more RSCNs came in while we
5014 * were processing this one.
5015 */
2e0fef85
JS
5016 if ((vport->fc_rscn_id_cnt == 0) &&
5017 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5018 spin_lock_irq(shost->host_lock);
5019 vport->fc_flag &= ~FC_RSCN_MODE;
5020 spin_unlock_irq(shost->host_lock);
92d7f7b0 5021 lpfc_can_disctmo(vport);
2fe165b6 5022 } else
2e0fef85 5023 lpfc_els_handle_rscn(vport);
dea3101e 5024 }
5025 }
5026 return;
5027}
5028
5029/*
5030 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5031 * ring the match the sppecified nodelist.
5032 */
5033static void
2e0fef85 5034lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 5035{
2534ba75 5036 LIST_HEAD(completions);
dea3101e 5037 struct lpfc_sli *psli;
5038 IOCB_t *icmd;
5039 struct lpfc_iocbq *iocb, *next_iocb;
5040 struct lpfc_sli_ring *pring;
dea3101e 5041
5042 psli = &phba->sli;
5043 pring = &psli->ring[LPFC_ELS_RING];
5044
5045 /* Error matching iocb on txq or txcmplq
5046 * First check the txq.
5047 */
2e0fef85 5048 spin_lock_irq(&phba->hbalock);
dea3101e 5049 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5050 if (iocb->context1 != ndlp) {
5051 continue;
5052 }
5053 icmd = &iocb->iocb;
5054 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5055 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5056
2534ba75 5057 list_move_tail(&iocb->list, &completions);
dea3101e 5058 pring->txq_cnt--;
dea3101e 5059 }
5060 }
5061
5062 /* Next check the txcmplq */
5063 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5064 if (iocb->context1 != ndlp) {
5065 continue;
5066 }
5067 icmd = &iocb->iocb;
2e0fef85
JS
5068 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5069 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
2534ba75
JS
5070 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
5071 }
5072 }
2e0fef85 5073 spin_unlock_irq(&phba->hbalock);
dea3101e 5074
a257bf90
JS
5075 /* Cancel all the IOCBs from the completions list */
5076 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5077 IOERR_SLI_ABORTED);
dea3101e 5078}
5079
a6ababd2 5080static void
2e0fef85 5081lpfc_disc_flush_list(struct lpfc_vport *vport)
dea3101e 5082{
5083 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 5084 struct lpfc_hba *phba = vport->phba;
dea3101e 5085
2e0fef85
JS
5086 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5087 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7 5088 nlp_listp) {
e47c9093
JS
5089 if (!NLP_CHK_NODE_ACT(ndlp))
5090 continue;
685f0bf7
JS
5091 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5092 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5093 lpfc_free_tx(phba, ndlp);
685f0bf7 5094 }
dea3101e 5095 }
5096 }
dea3101e 5097}
5098
92d7f7b0
JS
5099void
5100lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5101{
5102 lpfc_els_flush_rscn(vport);
5103 lpfc_els_flush_cmd(vport);
5104 lpfc_disc_flush_list(vport);
5105}
5106
dea3101e 5107/*****************************************************************************/
5108/*
5109 * NAME: lpfc_disc_timeout
5110 *
5111 * FUNCTION: Fibre Channel driver discovery timeout routine.
5112 *
5113 * EXECUTION ENVIRONMENT: interrupt only
5114 *
5115 * CALLED FROM:
5116 * Timer function
5117 *
5118 * RETURNS:
5119 * none
5120 */
5121/*****************************************************************************/
5122void
5123lpfc_disc_timeout(unsigned long ptr)
5124{
2e0fef85
JS
5125 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5126 struct lpfc_hba *phba = vport->phba;
5e9d9b82 5127 uint32_t tmo_posted;
dea3101e 5128 unsigned long flags = 0;
5129
5130 if (unlikely(!phba))
5131 return;
5132
5e9d9b82
JS
5133 spin_lock_irqsave(&vport->work_port_lock, flags);
5134 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5135 if (!tmo_posted)
2e0fef85 5136 vport->work_port_events |= WORKER_DISC_TMO;
5e9d9b82 5137 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2e0fef85 5138
5e9d9b82
JS
5139 if (!tmo_posted)
5140 lpfc_worker_wake_up(phba);
dea3101e 5141 return;
5142}
5143
5144static void
2e0fef85 5145lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea3101e 5146{
2e0fef85
JS
5147 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5148 struct lpfc_hba *phba = vport->phba;
5149 struct lpfc_sli *psli = &phba->sli;
c9f8735b 5150 struct lpfc_nodelist *ndlp, *next_ndlp;
92d7f7b0 5151 LPFC_MBOXQ_t *initlinkmbox;
dea3101e 5152 int rc, clrlaerr = 0;
5153
2e0fef85 5154 if (!(vport->fc_flag & FC_DISC_TMO))
dea3101e 5155 return;
5156
2e0fef85
JS
5157 spin_lock_irq(shost->host_lock);
5158 vport->fc_flag &= ~FC_DISC_TMO;
5159 spin_unlock_irq(shost->host_lock);
dea3101e 5160
858c9f6c
JS
5161 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5162 "disc timeout: state:x%x rtry:x%x flg:x%x",
5163 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5164
2e0fef85 5165 switch (vport->port_state) {
dea3101e 5166
5167 case LPFC_LOCAL_CFG_LINK:
2e0fef85
JS
5168 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
5169 * FAN
5170 */
5171 /* FAN timeout */
e8b62011
JS
5172 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5173 "0221 FAN timeout\n");
c9f8735b 5174 /* Start discovery by sending FLOGI, clean up old rpis */
2e0fef85 5175 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7 5176 nlp_listp) {
e47c9093
JS
5177 if (!NLP_CHK_NODE_ACT(ndlp))
5178 continue;
685f0bf7
JS
5179 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5180 continue;
c9f8735b
JW
5181 if (ndlp->nlp_type & NLP_FABRIC) {
5182 /* Clean up the ndlp on Fabric connections */
2e0fef85 5183 lpfc_drop_node(vport, ndlp);
87af33fe 5184
2fe165b6 5185 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
c9f8735b
JW
5186 /* Fail outstanding IO now since device
5187 * is marked for PLOGI.
5188 */
2e0fef85 5189 lpfc_unreg_rpi(vport, ndlp);
c9f8735b
JW
5190 }
5191 }
92d7f7b0 5192 if (vport->port_state != LPFC_FLOGI) {
76a95d75
JS
5193 if (phba->sli_rev <= LPFC_SLI_REV3)
5194 lpfc_initial_flogi(vport);
5195 else
5196 lpfc_issue_init_vfi(vport);
0ff10d46 5197 return;
92d7f7b0 5198 }
dea3101e 5199 break;
5200
92d7f7b0 5201 case LPFC_FDISC:
dea3101e 5202 case LPFC_FLOGI:
2e0fef85 5203 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea3101e 5204 /* Initial FLOGI timeout */
e8b62011
JS
5205 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5206 "0222 Initial %s timeout\n",
87af33fe 5207 vport->vpi ? "FDISC" : "FLOGI");
dea3101e 5208
5209 /* Assume no Fabric and go on with discovery.
5210 * Check for outstanding ELS FLOGI to abort.
5211 */
5212
5213 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 5214 lpfc_disc_list_loopmap(vport);
dea3101e 5215
5216 /* Start discovery */
2e0fef85 5217 lpfc_disc_start(vport);
dea3101e 5218 break;
5219
5220 case LPFC_FABRIC_CFG_LINK:
5221 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5222 NameServer login */
e8b62011
JS
5223 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5224 "0223 Timeout while waiting for "
5225 "NameServer login\n");
dea3101e 5226 /* Next look for NameServer ndlp */
2e0fef85 5227 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093 5228 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
87af33fe
JS
5229 lpfc_els_abort(phba, ndlp);
5230
5231 /* ReStart discovery */
5232 goto restart_disc;
dea3101e 5233
5234 case LPFC_NS_QRY:
5235 /* Check for wait for NameServer Rsp timeout */
e8b62011
JS
5236 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5237 "0224 NameServer Query timeout "
5238 "Data: x%x x%x\n",
5239 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 5240
92d7f7b0
JS
5241 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5242 /* Try it one more time */
5243 vport->fc_ns_retry++;
5244 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
5245 vport->fc_ns_retry, 0);
5246 if (rc == 0)
5247 break;
dea3101e 5248 }
92d7f7b0 5249 vport->fc_ns_retry = 0;
dea3101e 5250
87af33fe 5251restart_disc:
92d7f7b0
JS
5252 /*
5253 * Discovery is over.
5254 * set port_state to PORT_READY if SLI2.
5255 * cmpl_reg_vpi will set port_state to READY for SLI3.
5256 */
3772a991
JS
5257 if (phba->sli_rev < LPFC_SLI_REV4) {
5258 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5259 lpfc_issue_reg_vpi(phba, vport);
6d368e53 5260 else {
3772a991
JS
5261 lpfc_issue_clear_la(phba, vport);
5262 vport->port_state = LPFC_VPORT_READY;
5263 }
dea3101e 5264 }
5265
5266 /* Setup and issue mailbox INITIALIZE LINK command */
5267 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5268 if (!initlinkmbox) {
e8b62011
JS
5269 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5270 "0206 Device Discovery "
5271 "completion error\n");
2e0fef85 5272 phba->link_state = LPFC_HBA_ERROR;
dea3101e 5273 break;
5274 }
5275
5276 lpfc_linkdown(phba);
5277 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5278 phba->cfg_link_speed);
04c68496 5279 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
ed957684 5280 initlinkmbox->vport = vport;
92d7f7b0 5281 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0b727fea 5282 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
5b8bd0c9 5283 lpfc_set_loopback_flag(phba);
dea3101e 5284 if (rc == MBX_NOT_FINISHED)
5285 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5286
5287 break;
5288
5289 case LPFC_DISC_AUTH:
5290 /* Node Authentication timeout */
e8b62011
JS
5291 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5292 "0227 Node Authentication timeout\n");
2e0fef85
JS
5293 lpfc_disc_flush_list(vport);
5294
92d7f7b0
JS
5295 /*
5296 * set port_state to PORT_READY if SLI2.
5297 * cmpl_reg_vpi will set port_state to READY for SLI3.
5298 */
3772a991
JS
5299 if (phba->sli_rev < LPFC_SLI_REV4) {
5300 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5301 lpfc_issue_reg_vpi(phba, vport);
5302 else { /* NPIV Not enabled */
5303 lpfc_issue_clear_la(phba, vport);
5304 vport->port_state = LPFC_VPORT_READY;
5305 }
dea3101e 5306 }
5307 break;
5308
2e0fef85
JS
5309 case LPFC_VPORT_READY:
5310 if (vport->fc_flag & FC_RSCN_MODE) {
e8b62011
JS
5311 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5312 "0231 RSCN timeout Data: x%x "
5313 "x%x\n",
5314 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 5315
5316 /* Cleanup any outstanding ELS commands */
2e0fef85 5317 lpfc_els_flush_cmd(vport);
dea3101e 5318
2e0fef85
JS
5319 lpfc_els_flush_rscn(vport);
5320 lpfc_disc_flush_list(vport);
dea3101e 5321 }
5322 break;
2e0fef85 5323
92d7f7b0 5324 default:
e8b62011 5325 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
d7c255b2 5326 "0273 Unexpected discovery timeout, "
e8b62011 5327 "vport State x%x\n", vport->port_state);
2e0fef85
JS
5328 break;
5329 }
5330
5331 switch (phba->link_state) {
5332 case LPFC_CLEAR_LA:
92d7f7b0 5333 /* CLEAR LA timeout */
e8b62011
JS
5334 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5335 "0228 CLEAR LA timeout\n");
2e0fef85
JS
5336 clrlaerr = 1;
5337 break;
5338
09372820
JS
5339 case LPFC_LINK_UP:
5340 lpfc_issue_clear_la(phba, vport);
5341 /* Drop thru */
2e0fef85
JS
5342 case LPFC_LINK_UNKNOWN:
5343 case LPFC_WARM_START:
5344 case LPFC_INIT_START:
5345 case LPFC_INIT_MBX_CMDS:
5346 case LPFC_LINK_DOWN:
2e0fef85 5347 case LPFC_HBA_ERROR:
e8b62011
JS
5348 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5349 "0230 Unexpected timeout, hba link "
5350 "state x%x\n", phba->link_state);
2e0fef85
JS
5351 clrlaerr = 1;
5352 break;
92d7f7b0
JS
5353
5354 case LPFC_HBA_READY:
5355 break;
dea3101e 5356 }
5357
5358 if (clrlaerr) {
2e0fef85 5359 lpfc_disc_flush_list(vport);
a4bc3379 5360 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 5361 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
5362 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2e0fef85 5363 vport->port_state = LPFC_VPORT_READY;
dea3101e 5364 }
5365
5366 return;
5367}
5368
dea3101e 5369/*
5370 * This routine handles processing a NameServer REG_LOGIN mailbox
5371 * command upon completion. It is setup in the LPFC_MBOXQ
5372 * as the completion routine when the command is
5373 * handed off to the SLI layer.
5374 */
5375void
2e0fef85 5376lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 5377{
04c68496 5378 MAILBOX_t *mb = &pmb->u.mb;
2e0fef85
JS
5379 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5380 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5381 struct lpfc_vport *vport = pmb->vport;
dea3101e 5382
5383 pmb->context1 = NULL;
d439d286 5384 pmb->context2 = NULL;
dea3101e 5385
6d368e53
JS
5386 if (phba->sli_rev < LPFC_SLI_REV4)
5387 ndlp->nlp_rpi = mb->un.varWords[0];
4042629e 5388 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea3101e 5389 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 5390 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 5391
2e0fef85
JS
5392 /*
5393 * Start issuing Fabric-Device Management Interface (FDMI) command to
5394 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
5395 * fdmi-on=2 (supporting RPA/hostnmae)
dea3101e 5396 */
2e0fef85 5397
3de2a653 5398 if (vport->cfg_fdmi_on == 1)
2e0fef85
JS
5399 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
5400 else
5401 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea3101e 5402
fa4066b6
JS
5403 /* decrement the node reference count held for this callback
5404 * function.
5405 */
329f9bc7 5406 lpfc_nlp_put(ndlp);
dea3101e 5407 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5408 kfree(mp);
329f9bc7 5409 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 5410
5411 return;
5412}
5413
685f0bf7
JS
5414static int
5415lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5416{
5417 uint16_t *rpi = param;
5418
eff4a01b
JS
5419 /* check for active node */
5420 if (!NLP_CHK_NODE_ACT(ndlp))
5421 return 0;
5422
685f0bf7
JS
5423 return ndlp->nlp_rpi == *rpi;
5424}
5425
5426static int
5427lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5428{
5429 return memcmp(&ndlp->nlp_portname, param,
5430 sizeof(ndlp->nlp_portname)) == 0;
5431}
5432
a6ababd2 5433static struct lpfc_nodelist *
2e0fef85 5434__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea3101e 5435{
21568f53 5436 struct lpfc_nodelist *ndlp;
dea3101e 5437
2e0fef85 5438 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
34f5ad8b
JS
5439 if (filter(ndlp, param)) {
5440 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5441 "3185 FIND node filter %p DID "
5442 "Data: x%p x%x x%x\n",
5443 filter, ndlp, ndlp->nlp_DID,
5444 ndlp->nlp_flag);
685f0bf7 5445 return ndlp;
34f5ad8b 5446 }
685f0bf7 5447 }
34f5ad8b
JS
5448 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5449 "3186 FIND node filter %p NOT FOUND.\n", filter);
21568f53 5450 return NULL;
dea3101e 5451}
5452
685f0bf7
JS
5453/*
5454 * This routine looks up the ndlp lists for the given RPI. If rpi found it
2e0fef85 5455 * returns the node list element pointer else return NULL.
685f0bf7
JS
5456 */
5457struct lpfc_nodelist *
2e0fef85 5458__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
685f0bf7 5459{
2e0fef85 5460 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
685f0bf7
JS
5461}
5462
488d1469 5463/*
685f0bf7 5464 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
2e0fef85 5465 * returns the node element list pointer else return NULL.
488d1469
JS
5466 */
5467struct lpfc_nodelist *
2e0fef85 5468lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
488d1469 5469{
2e0fef85 5470 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
488d1469 5471 struct lpfc_nodelist *ndlp;
488d1469 5472
2e0fef85
JS
5473 spin_lock_irq(shost->host_lock);
5474 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5475 spin_unlock_irq(shost->host_lock);
858c9f6c 5476 return ndlp;
488d1469
JS
5477}
5478
cb69f7de
JS
5479/*
5480 * This routine looks up the ndlp lists for the given RPI. If the rpi
5481 * is found, the routine returns the node element list pointer else
5482 * return NULL.
5483 */
5484struct lpfc_nodelist *
5485lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5486{
5487 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5488 struct lpfc_nodelist *ndlp;
5489
5490 spin_lock_irq(shost->host_lock);
5491 ndlp = __lpfc_findnode_rpi(vport, rpi);
5492 spin_unlock_irq(shost->host_lock);
5493 return ndlp;
5494}
5495
5496/**
5497 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5498 * @phba: pointer to lpfc hba data structure.
5499 * @vpi: the physical host virtual N_Port identifier.
5500 *
5501 * This routine finds a vport on a HBA (referred by @phba) through a
5502 * @vpi. The function walks the HBA's vport list and returns the address
5503 * of the vport with the matching @vpi.
5504 *
5505 * Return code
5506 * NULL - No vport with the matching @vpi found
5507 * Otherwise - Address to the vport with the matching @vpi.
5508 **/
5509struct lpfc_vport *
5510lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5511{
5512 struct lpfc_vport *vport;
5513 unsigned long flags;
5514 int i = 0;
5515
5516 /* The physical ports are always vpi 0 - translate is unnecessary. */
5517 if (vpi > 0) {
5518 /*
5519 * Translate the physical vpi to the logical vpi. The
5520 * vport stores the logical vpi.
5521 */
5522 for (i = 0; i < phba->max_vpi; i++) {
5523 if (vpi == phba->vpi_ids[i])
5524 break;
5525 }
5526
5527 if (i >= phba->max_vpi) {
5528 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5529 "2936 Could not find Vport mapped "
5530 "to vpi %d\n", vpi);
5531 return NULL;
5532 }
5533 }
5534
5535 spin_lock_irqsave(&phba->hbalock, flags);
5536 list_for_each_entry(vport, &phba->port_list, listentry) {
5537 if (vport->vpi == i) {
5538 spin_unlock_irqrestore(&phba->hbalock, flags);
5539 return vport;
5540 }
5541 }
5542 spin_unlock_irqrestore(&phba->hbalock, flags);
5543 return NULL;
5544}
5545
dea3101e 5546void
2e0fef85
JS
5547lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5548 uint32_t did)
dea3101e 5549{
5550 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
109f6ed0
JS
5551
5552 lpfc_initialize_node(vport, ndlp, did);
685f0bf7 5553 INIT_LIST_HEAD(&ndlp->nlp_listp);
858c9f6c
JS
5554
5555 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5556 "node init: did:x%x",
5557 ndlp->nlp_DID, 0, 0);
5558
dea3101e 5559 return;
5560}
329f9bc7 5561
98c9ea5c
JS
5562/* This routine releases all resources associated with a specifc NPort's ndlp
5563 * and mempool_free's the nodelist.
5564 */
311464ec 5565static void
329f9bc7
JS
5566lpfc_nlp_release(struct kref *kref)
5567{
e47c9093
JS
5568 struct lpfc_hba *phba;
5569 unsigned long flags;
329f9bc7
JS
5570 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5571 kref);
858c9f6c
JS
5572
5573 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5574 "node release: did:x%x flg:x%x type:x%x",
5575 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5576
e47c9093 5577 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
939723a4 5578 "0279 lpfc_nlp_release: ndlp:x%p did %x "
e47c9093 5579 "usgmap:x%x refcnt:%d\n",
939723a4 5580 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
e47c9093
JS
5581 atomic_read(&ndlp->kref.refcount));
5582
5583 /* remove ndlp from action. */
2e0fef85 5584 lpfc_nlp_remove(ndlp->vport, ndlp);
e47c9093
JS
5585
5586 /* clear the ndlp active flag for all release cases */
a257bf90 5587 phba = ndlp->phba;
e47c9093
JS
5588 spin_lock_irqsave(&phba->ndlp_lock, flags);
5589 NLP_CLR_NODE_ACT(ndlp);
5590 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4042629e
JS
5591 if (phba->sli_rev == LPFC_SLI_REV4)
5592 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
e47c9093
JS
5593
5594 /* free ndlp memory for final ndlp release */
ea2151b4
JS
5595 if (NLP_CHK_FREE_REQ(ndlp)) {
5596 kfree(ndlp->lat_data);
a257bf90 5597 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
ea2151b4 5598 }
329f9bc7
JS
5599}
5600
98c9ea5c
JS
5601/* This routine bumps the reference count for a ndlp structure to ensure
5602 * that one discovery thread won't free a ndlp while another discovery thread
5603 * is using it.
5604 */
329f9bc7
JS
5605struct lpfc_nodelist *
5606lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5607{
e47c9093
JS
5608 struct lpfc_hba *phba;
5609 unsigned long flags;
5610
98c9ea5c
JS
5611 if (ndlp) {
5612 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5613 "node get: did:x%x flg:x%x refcnt:x%x",
5614 ndlp->nlp_DID, ndlp->nlp_flag,
5615 atomic_read(&ndlp->kref.refcount));
e47c9093
JS
5616 /* The check of ndlp usage to prevent incrementing the
5617 * ndlp reference count that is in the process of being
5618 * released.
5619 */
a257bf90 5620 phba = ndlp->phba;
e47c9093
JS
5621 spin_lock_irqsave(&phba->ndlp_lock, flags);
5622 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5623 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5624 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5625 "0276 lpfc_nlp_get: ndlp:x%p "
5626 "usgmap:x%x refcnt:%d\n",
5627 (void *)ndlp, ndlp->nlp_usg_map,
5628 atomic_read(&ndlp->kref.refcount));
5629 return NULL;
5630 } else
5631 kref_get(&ndlp->kref);
5632 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
98c9ea5c 5633 }
329f9bc7
JS
5634 return ndlp;
5635}
5636
98c9ea5c 5637/* This routine decrements the reference count for a ndlp structure. If the
e47c9093
JS
5638 * count goes to 0, this indicates the the associated nodelist should be
5639 * freed. Returning 1 indicates the ndlp resource has been released; on the
5640 * other hand, returning 0 indicates the ndlp resource has not been released
5641 * yet.
98c9ea5c 5642 */
329f9bc7
JS
5643int
5644lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5645{
e47c9093
JS
5646 struct lpfc_hba *phba;
5647 unsigned long flags;
5648
5649 if (!ndlp)
5650 return 1;
5651
5652 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5653 "node put: did:x%x flg:x%x refcnt:x%x",
5654 ndlp->nlp_DID, ndlp->nlp_flag,
5655 atomic_read(&ndlp->kref.refcount));
a257bf90 5656 phba = ndlp->phba;
e47c9093
JS
5657 spin_lock_irqsave(&phba->ndlp_lock, flags);
5658 /* Check the ndlp memory free acknowledge flag to avoid the
5659 * possible race condition that kref_put got invoked again
5660 * after previous one has done ndlp memory free.
5661 */
5662 if (NLP_CHK_FREE_ACK(ndlp)) {
5663 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5664 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5665 "0274 lpfc_nlp_put: ndlp:x%p "
5666 "usgmap:x%x refcnt:%d\n",
5667 (void *)ndlp, ndlp->nlp_usg_map,
5668 atomic_read(&ndlp->kref.refcount));
5669 return 1;
5670 }
5671 /* Check the ndlp inactivate log flag to avoid the possible
5672 * race condition that kref_put got invoked again after ndlp
5673 * is already in inactivating state.
5674 */
5675 if (NLP_CHK_IACT_REQ(ndlp)) {
5676 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5677 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5678 "0275 lpfc_nlp_put: ndlp:x%p "
5679 "usgmap:x%x refcnt:%d\n",
5680 (void *)ndlp, ndlp->nlp_usg_map,
5681 atomic_read(&ndlp->kref.refcount));
5682 return 1;
98c9ea5c 5683 }
e47c9093
JS
5684 /* For last put, mark the ndlp usage flags to make sure no
5685 * other kref_get and kref_put on the same ndlp shall get
5686 * in between the process when the final kref_put has been
5687 * invoked on this ndlp.
5688 */
5689 if (atomic_read(&ndlp->kref.refcount) == 1) {
5690 /* Indicate ndlp is put to inactive state. */
5691 NLP_SET_IACT_REQ(ndlp);
5692 /* Acknowledge ndlp memory free has been seen. */
5693 if (NLP_CHK_FREE_REQ(ndlp))
5694 NLP_SET_FREE_ACK(ndlp);
5695 }
5696 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5697 /* Note, the kref_put returns 1 when decrementing a reference
5698 * count that was 1, it invokes the release callback function,
5699 * but it still left the reference count as 1 (not actually
5700 * performs the last decrementation). Otherwise, it actually
5701 * decrements the reference count and returns 0.
5702 */
5703 return kref_put(&ndlp->kref, lpfc_nlp_release);
329f9bc7 5704}
98c9ea5c
JS
5705
5706/* This routine free's the specified nodelist if it is not in use
e47c9093
JS
5707 * by any other discovery thread. This routine returns 1 if the
5708 * ndlp has been freed. A return value of 0 indicates the ndlp is
5709 * not yet been released.
98c9ea5c
JS
5710 */
5711int
5712lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
5713{
5714 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5715 "node not used: did:x%x flg:x%x refcnt:x%x",
5716 ndlp->nlp_DID, ndlp->nlp_flag,
5717 atomic_read(&ndlp->kref.refcount));
e47c9093
JS
5718 if (atomic_read(&ndlp->kref.refcount) == 1)
5719 if (lpfc_nlp_put(ndlp))
5720 return 1;
98c9ea5c
JS
5721 return 0;
5722}
6fb120a7
JS
5723
5724/**
5725 * lpfc_fcf_inuse - Check if FCF can be unregistered.
5726 * @phba: Pointer to hba context object.
5727 *
5728 * This function iterate through all FC nodes associated
5729 * will all vports to check if there is any node with
5730 * fc_rports associated with it. If there is an fc_rport
5731 * associated with the node, then the node is either in
5732 * discovered state or its devloss_timer is pending.
5733 */
5734static int
5735lpfc_fcf_inuse(struct lpfc_hba *phba)
5736{
5737 struct lpfc_vport **vports;
5738 int i, ret = 0;
5739 struct lpfc_nodelist *ndlp;
5740 struct Scsi_Host *shost;
5741
5742 vports = lpfc_create_vport_work_array(phba);
5743
63e801ce
JS
5744 /* If driver cannot allocate memory, indicate fcf is in use */
5745 if (!vports)
5746 return 1;
5747
6fb120a7
JS
5748 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5749 shost = lpfc_shost_from_vport(vports[i]);
5750 spin_lock_irq(shost->host_lock);
0558056c
JS
5751 /*
5752 * IF the CVL_RCVD bit is not set then we have sent the
5753 * flogi.
5754 * If dev_loss fires while we are waiting we do not want to
5755 * unreg the fcf.
5756 */
5757 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
5758 spin_unlock_irq(shost->host_lock);
5759 ret = 1;
5760 goto out;
5761 }
6fb120a7
JS
5762 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5763 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
5764 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
5765 ret = 1;
5766 spin_unlock_irq(shost->host_lock);
5767 goto out;
80c17849
JS
5768 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5769 ret = 1;
891478a2 5770 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
80c17849
JS
5771 "2624 RPI %x DID %x flag %x "
5772 "still logged in\n",
5773 ndlp->nlp_rpi, ndlp->nlp_DID,
5774 ndlp->nlp_flag);
6fb120a7
JS
5775 }
5776 }
5777 spin_unlock_irq(shost->host_lock);
5778 }
5779out:
5780 lpfc_destroy_vport_work_array(phba, vports);
5781 return ret;
5782}
5783
5784/**
5785 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
5786 * @phba: Pointer to hba context object.
5787 * @mboxq: Pointer to mailbox object.
5788 *
5789 * This function frees memory associated with the mailbox command.
5790 */
1b51197d 5791void
6fb120a7
JS
5792lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5793{
5794 struct lpfc_vport *vport = mboxq->vport;
38b92ef8 5795 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6fb120a7
JS
5796
5797 if (mboxq->u.mb.mbxStatus) {
5798 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5799 "2555 UNREG_VFI mbxStatus error x%x "
5800 "HBA state x%x\n",
5801 mboxq->u.mb.mbxStatus, vport->port_state);
5802 }
38b92ef8
JS
5803 spin_lock_irq(shost->host_lock);
5804 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
5805 spin_unlock_irq(shost->host_lock);
6fb120a7
JS
5806 mempool_free(mboxq, phba->mbox_mem_pool);
5807 return;
5808}
5809
5810/**
5811 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
5812 * @phba: Pointer to hba context object.
5813 * @mboxq: Pointer to mailbox object.
5814 *
5815 * This function frees memory associated with the mailbox command.
5816 */
5817static void
5818lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5819{
5820 struct lpfc_vport *vport = mboxq->vport;
5821
5822 if (mboxq->u.mb.mbxStatus) {
5823 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
5824 "2550 UNREG_FCFI mbxStatus error x%x "
5825 "HBA state x%x\n",
5826 mboxq->u.mb.mbxStatus, vport->port_state);
5827 }
5828 mempool_free(mboxq, phba->mbox_mem_pool);
5829 return;
5830}
5831
5832/**
ecfd03c6 5833 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6fb120a7
JS
5834 * @phba: Pointer to hba context object.
5835 *
ecfd03c6
JS
5836 * This function prepare the HBA for unregistering the currently registered
5837 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
5838 * VFIs.
6fb120a7 5839 */
ecfd03c6
JS
5840int
5841lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6fb120a7 5842{
6fb120a7 5843 struct lpfc_vport **vports;
695a814e 5844 struct lpfc_nodelist *ndlp;
72100cc4 5845 struct Scsi_Host *shost;
ecfd03c6 5846 int i, rc;
6fb120a7 5847
ecfd03c6 5848 /* Unregister RPIs */
6fb120a7 5849 if (lpfc_fcf_inuse(phba))
ecfd03c6 5850 lpfc_unreg_hba_rpis(phba);
6fb120a7 5851
4d9ab994
JS
5852 /* At this point, all discovery is aborted */
5853 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6fb120a7
JS
5854
5855 /* Unregister VPIs */
5856 vports = lpfc_create_vport_work_array(phba);
ecfd03c6 5857 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6fb120a7 5858 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
695a814e
JS
5859 /* Stop FLOGI/FDISC retries */
5860 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
5861 if (ndlp)
5862 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
78730cfe 5863 lpfc_cleanup_pending_mbox(vports[i]);
5af5eee7
JS
5864 if (phba->sli_rev == LPFC_SLI_REV4)
5865 lpfc_sli4_unreg_all_rpis(vports[i]);
6fb120a7 5866 lpfc_mbx_unreg_vpi(vports[i]);
72100cc4
JS
5867 shost = lpfc_shost_from_vport(vports[i]);
5868 spin_lock_irq(shost->host_lock);
891478a2 5869 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
c868595d 5870 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
72100cc4 5871 spin_unlock_irq(shost->host_lock);
6fb120a7
JS
5872 }
5873 lpfc_destroy_vport_work_array(phba, vports);
5874
695a814e
JS
5875 /* Cleanup any outstanding ELS commands */
5876 lpfc_els_flush_all_cmd(phba);
5877
1b51197d
JS
5878 /* Unregister the physical port VFI */
5879 rc = lpfc_issue_unreg_vfi(phba->pport);
5880 return rc;
ecfd03c6
JS
5881}
5882
5883/**
5884 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
5885 * @phba: Pointer to hba context object.
5886 *
5887 * This function issues synchronous unregister FCF mailbox command to HBA to
5888 * unregister the currently registered FCF record. The driver does not reset
5889 * the driver FCF usage state flags.
5890 *
5891 * Return 0 if successfully issued, none-zero otherwise.
5892 */
5893int
5894lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
5895{
5896 LPFC_MBOXQ_t *mbox;
5897 int rc;
5898
6fb120a7
JS
5899 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5900 if (!mbox) {
5901 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
ecfd03c6
JS
5902 "2551 UNREG_FCFI mbox allocation failed"
5903 "HBA state x%x\n", phba->pport->port_state);
5904 return -ENOMEM;
6fb120a7 5905 }
6fb120a7
JS
5906 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
5907 mbox->vport = phba->pport;
5908 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
5909 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5910
5911 if (rc == MBX_NOT_FINISHED) {
ecfd03c6
JS
5912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5913 "2552 Unregister FCFI command failed rc x%x "
5914 "HBA state x%x\n",
5915 rc, phba->pport->port_state);
5916 return -EINVAL;
5917 }
5918 return 0;
5919}
5920
5921/**
5922 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
5923 * @phba: Pointer to hba context object.
5924 *
5925 * This function unregisters the currently reigstered FCF. This function
5926 * also tries to find another FCF for discovery by rescan the HBA FCF table.
5927 */
5928void
5929lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
5930{
5931 int rc;
5932
5933 /* Preparation for unregistering fcf */
5934 rc = lpfc_unregister_fcf_prep(phba);
5935 if (rc) {
5936 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5937 "2748 Failed to prepare for unregistering "
5938 "HBA's FCF record: rc=%d\n", rc);
6fb120a7
JS
5939 return;
5940 }
5941
ecfd03c6
JS
5942 /* Now, unregister FCF record and reset HBA FCF state */
5943 rc = lpfc_sli4_unregister_fcf(phba);
5944 if (rc)
5945 return;
5946 /* Reset HBA FCF states after successful unregister FCF */
5947 phba->fcf.fcf_flag = 0;
fc2b989b 5948 phba->fcf.current_rec.flag = 0;
6fb120a7
JS
5949
5950 /*
5951 * If driver is not unloading, check if there is any other
5952 * FCF record that can be used for discovery.
5953 */
5954 if ((phba->pport->load_flag & FC_UNLOADING) ||
ecfd03c6 5955 (phba->link_state < LPFC_LINK_UP))
6fb120a7
JS
5956 return;
5957
0c9ab6f5
JS
5958 /* This is considered as the initial FCF discovery scan */
5959 spin_lock_irq(&phba->hbalock);
5960 phba->fcf.fcf_flag |= FCF_INIT_DISC;
5961 spin_unlock_irq(&phba->hbalock);
38b92ef8
JS
5962
5963 /* Reset FCF roundrobin bmask for new discovery */
7d791df7 5964 lpfc_sli4_clear_fcf_rr_bmask(phba);
38b92ef8 5965
0c9ab6f5 5966 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6fb120a7 5967
0c9ab6f5
JS
5968 if (rc) {
5969 spin_lock_irq(&phba->hbalock);
5970 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
5971 spin_unlock_irq(&phba->hbalock);
6fb120a7 5972 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
ecfd03c6
JS
5973 "2553 lpfc_unregister_unused_fcf failed "
5974 "to read FCF record HBA state x%x\n",
5975 phba->pport->port_state);
0c9ab6f5 5976 }
ecfd03c6
JS
5977}
5978
5979/**
5980 * lpfc_unregister_fcf - Unregister the currently registered fcf record
5981 * @phba: Pointer to hba context object.
5982 *
5983 * This function just unregisters the currently reigstered FCF. It does not
5984 * try to find another FCF for discovery.
5985 */
5986void
5987lpfc_unregister_fcf(struct lpfc_hba *phba)
5988{
5989 int rc;
5990
5991 /* Preparation for unregistering fcf */
5992 rc = lpfc_unregister_fcf_prep(phba);
5993 if (rc) {
5994 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
5995 "2749 Failed to prepare for unregistering "
5996 "HBA's FCF record: rc=%d\n", rc);
5997 return;
5998 }
5999
6000 /* Now, unregister FCF record and reset HBA FCF state */
6001 rc = lpfc_sli4_unregister_fcf(phba);
6002 if (rc)
6003 return;
6004 /* Set proper HBA FCF states after successful unregister FCF */
6005 spin_lock_irq(&phba->hbalock);
6006 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6007 spin_unlock_irq(&phba->hbalock);
6008}
6009
6010/**
6011 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6012 * @phba: Pointer to hba context object.
6013 *
6014 * This function check if there are any connected remote port for the FCF and
6015 * if all the devices are disconnected, this function unregister FCFI.
6016 * This function also tries to use another FCF for discovery.
6017 */
6018void
6019lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6020{
6021 /*
3804dc84
JS
6022 * If HBA is not running in FIP mode, if HBA does not support
6023 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6024 * registered, do nothing.
ecfd03c6
JS
6025 */
6026 spin_lock_irq(&phba->hbalock);
76a95d75 6027 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
ecfd03c6 6028 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
dbb6b3ab 6029 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
3804dc84 6030 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
dbb6b3ab 6031 (phba->pport->port_state == LPFC_FLOGI)) {
ecfd03c6
JS
6032 spin_unlock_irq(&phba->hbalock);
6033 return;
6034 }
6035 spin_unlock_irq(&phba->hbalock);
6036
6037 if (lpfc_fcf_inuse(phba))
6038 return;
6039
6040 lpfc_unregister_fcf_rescan(phba);
6fb120a7
JS
6041}
6042
6043/**
6044 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6045 * @phba: Pointer to hba context object.
6046 * @buff: Buffer containing the FCF connection table as in the config
6047 * region.
6048 * This function create driver data structure for the FCF connection
6049 * record table read from config region 23.
6050 */
6051static void
6052lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6053 uint8_t *buff)
6054{
6055 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6056 struct lpfc_fcf_conn_hdr *conn_hdr;
6057 struct lpfc_fcf_conn_rec *conn_rec;
6058 uint32_t record_count;
6059 int i;
6060
6061 /* Free the current connect table */
6062 list_for_each_entry_safe(conn_entry, next_conn_entry,
4d9ab994
JS
6063 &phba->fcf_conn_rec_list, list) {
6064 list_del_init(&conn_entry->list);
6fb120a7 6065 kfree(conn_entry);
4d9ab994 6066 }
6fb120a7
JS
6067
6068 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6069 record_count = conn_hdr->length * sizeof(uint32_t)/
6070 sizeof(struct lpfc_fcf_conn_rec);
6071
6072 conn_rec = (struct lpfc_fcf_conn_rec *)
6073 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6074
6075 for (i = 0; i < record_count; i++) {
6076 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6077 continue;
6078 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6079 GFP_KERNEL);
6080 if (!conn_entry) {
6081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6082 "2566 Failed to allocate connection"
6083 " table entry\n");
6084 return;
6085 }
6086
6087 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6088 sizeof(struct lpfc_fcf_conn_rec));
6089 conn_entry->conn_rec.vlan_tag =
6090 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
6091 conn_entry->conn_rec.flags =
6092 le16_to_cpu(conn_entry->conn_rec.flags);
6093 list_add_tail(&conn_entry->list,
6094 &phba->fcf_conn_rec_list);
6095 }
6096}
6097
6098/**
6099 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6100 * @phba: Pointer to hba context object.
6101 * @buff: Buffer containing the FCoE parameter data structure.
6102 *
6103 * This function update driver data structure with config
6104 * parameters read from config region 23.
6105 */
6106static void
6107lpfc_read_fcoe_param(struct lpfc_hba *phba,
6108 uint8_t *buff)
6109{
6110 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6111 struct lpfc_fcoe_params *fcoe_param;
6112
6113 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6114 buff;
6115 fcoe_param = (struct lpfc_fcoe_params *)
32b9793f 6116 (buff + sizeof(struct lpfc_fip_param_hdr));
6fb120a7
JS
6117
6118 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6119 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6120 return;
6121
6fb120a7
JS
6122 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6123 phba->valid_vlan = 1;
6124 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6125 0xFFF;
6126 }
6127
6128 phba->fc_map[0] = fcoe_param->fc_map[0];
6129 phba->fc_map[1] = fcoe_param->fc_map[1];
6130 phba->fc_map[2] = fcoe_param->fc_map[2];
6131 return;
6132}
6133
6134/**
6135 * lpfc_get_rec_conf23 - Get a record type in config region data.
6136 * @buff: Buffer containing config region 23 data.
6137 * @size: Size of the data buffer.
6138 * @rec_type: Record type to be searched.
6139 *
25985edc 6140 * This function searches config region data to find the beginning
6fb120a7
JS
6141 * of the record specified by record_type. If record found, this
6142 * function return pointer to the record else return NULL.
6143 */
6144static uint8_t *
6145lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6146{
6147 uint32_t offset = 0, rec_length;
6148
6149 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6150 (size < sizeof(uint32_t)))
6151 return NULL;
6152
6153 rec_length = buff[offset + 1];
6154
6155 /*
6156 * One TLV record has one word header and number of data words
6157 * specified in the rec_length field of the record header.
6158 */
6159 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6160 <= size) {
6161 if (buff[offset] == rec_type)
6162 return &buff[offset];
6163
6164 if (buff[offset] == LPFC_REGION23_LAST_REC)
6165 return NULL;
6166
6167 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6168 rec_length = buff[offset + 1];
6169 }
6170 return NULL;
6171}
6172
6173/**
6174 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6175 * @phba: Pointer to lpfc_hba data structure.
6176 * @buff: Buffer containing config region 23 data.
6177 * @size: Size of the data buffer.
6178 *
eef35c2d 6179 * This function parses the FCoE config parameters in config region 23 and
6fb120a7
JS
6180 * populate driver data structure with the parameters.
6181 */
6182void
6183lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6184 uint8_t *buff,
6185 uint32_t size)
6186{
6187 uint32_t offset = 0, rec_length;
6188 uint8_t *rec_ptr;
6189
6190 /*
6191 * If data size is less than 2 words signature and version cannot be
6192 * verified.
6193 */
6194 if (size < 2*sizeof(uint32_t))
6195 return;
6196
6197 /* Check the region signature first */
6198 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6200 "2567 Config region 23 has bad signature\n");
6201 return;
6202 }
6203
6204 offset += 4;
6205
6206 /* Check the data structure version */
6207 if (buff[offset] != LPFC_REGION23_VERSION) {
6208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6209 "2568 Config region 23 has bad version\n");
6210 return;
6211 }
6212 offset += 4;
6213
6214 rec_length = buff[offset + 1];
6215
6216 /* Read FCoE param record */
6217 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6218 size - offset, FCOE_PARAM_TYPE);
6219 if (rec_ptr)
6220 lpfc_read_fcoe_param(phba, rec_ptr);
6221
6222 /* Read FCF connection table */
6223 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6224 size - offset, FCOE_CONN_TBL_TYPE);
6225 if (rec_ptr)
6226 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6227
6228}
This page took 1.588567 seconds and 5 git commands to generate.