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