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