Merge branch 'tracing/core' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
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 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26
91886523 27#include <scsi/scsi.h>
dea3101e 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 35#include "lpfc_disc.h"
36#include "lpfc_sli.h"
da0436e9 37#include "lpfc_sli4.h"
dea3101e 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 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 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 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 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 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 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 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 483
484}
485
311464ec 486static void
2e0fef85 487lpfc_work_done(struct lpfc_hba *phba)
dea3101e 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 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 506 lpfc_handle_eratt(phba);
507
2fe165b6 508 if (ha_copy & HA_MBATT)
dea3101e 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 }
da0436e9
JS
528 }
529
549e55cd
JS
530 vports = lpfc_create_vport_work_array(phba);
531 if (vports != NULL)
da0436e9 532 for (i = 0; i <= phba->max_vports; i++) {
51ef4c26
JS
533 /*
534 * We could have no vports in array if unloading, so if
535 * this happens then just use the pport
536 */
537 if (vports[i] == NULL && i == 0)
538 vport = phba->pport;
539 else
540 vport = vports[i];
541 if (vport == NULL)
542 break;
58da1ffb 543 spin_lock_irq(&vport->work_port_lock);
51ef4c26 544 work_port_events = vport->work_port_events;
58da1ffb
JS
545 vport->work_port_events &= ~work_port_events;
546 spin_unlock_irq(&vport->work_port_lock);
549e55cd 547 if (work_port_events & WORKER_DISC_TMO)
51ef4c26 548 lpfc_disc_timeout_handler(vport);
549e55cd 549 if (work_port_events & WORKER_ELS_TMO)
51ef4c26 550 lpfc_els_timeout_handler(vport);
549e55cd
JS
551 if (work_port_events & WORKER_HB_TMO)
552 lpfc_hb_timeout_handler(phba);
553 if (work_port_events & WORKER_MBOX_TMO)
554 lpfc_mbox_timeout_handler(phba);
555 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
556 lpfc_unblock_fabric_iocbs(phba);
557 if (work_port_events & WORKER_FDMI_TMO)
51ef4c26 558 lpfc_fdmi_timeout_handler(vport);
549e55cd
JS
559 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
560 lpfc_ramp_down_queue_handler(phba);
561 if (work_port_events & WORKER_RAMP_UP_QUEUE)
562 lpfc_ramp_up_queue_handler(phba);
92d7f7b0 563 }
09372820 564 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 565
858c9f6c
JS
566 pring = &phba->sli.ring[LPFC_ELS_RING];
567 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
568 status >>= (4*LPFC_ELS_RING);
4d9ab994
JS
569 if ((status & HA_RXMASK) ||
570 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
45ed1190 571 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
0b727fea 572 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
858c9f6c 573 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
574 /* Set the lpfc data pending flag */
575 set_bit(LPFC_DATA_READY, &phba->data_flags);
858c9f6c 576 } else {
58da1ffb 577 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
858c9f6c
JS
578 lpfc_sli_handle_slow_ring_event(phba, pring,
579 (status &
580 HA_RXMASK));
858c9f6c
JS
581 }
582 /*
583 * Turn on Ring interrupts
584 */
3772a991
JS
585 if (phba->sli_rev <= LPFC_SLI_REV3) {
586 spin_lock_irq(&phba->hbalock);
587 control = readl(phba->HCregaddr);
588 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
589 lpfc_debugfs_slow_ring_trc(phba,
590 "WRK Enable ring: cntl:x%x hacopy:x%x",
591 control, ha_copy, 0);
592
593 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
594 writel(control, phba->HCregaddr);
595 readl(phba->HCregaddr); /* flush */
596 } else {
597 lpfc_debugfs_slow_ring_trc(phba,
598 "WRK Ring ok: cntl:x%x hacopy:x%x",
599 control, ha_copy, 0);
600 }
601 spin_unlock_irq(&phba->hbalock);
a58cbd52 602 }
dea3101e 603 }
2e0fef85 604 lpfc_work_list_done(phba);
dea3101e 605}
606
dea3101e 607int
608lpfc_do_work(void *p)
609{
610 struct lpfc_hba *phba = p;
611 int rc;
dea3101e 612
613 set_user_nice(current, -20);
5e9d9b82 614 phba->data_flags = 0;
dea3101e 615
3a55b532 616 while (!kthread_should_stop()) {
5e9d9b82
JS
617 /* wait and check worker queue activities */
618 rc = wait_event_interruptible(phba->work_waitq,
619 (test_and_clear_bit(LPFC_DATA_READY,
620 &phba->data_flags)
621 || kthread_should_stop()));
3a55b532
JS
622 /* Signal wakeup shall terminate the worker thread */
623 if (rc) {
624 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
625 "0433 Wakeup on signal: rc=x%x\n", rc);
dea3101e 626 break;
3a55b532 627 }
dea3101e 628
5e9d9b82 629 /* Attend pending lpfc data processing */
dea3101e 630 lpfc_work_done(phba);
dea3101e 631 }
3a55b532
JS
632 phba->worker_thread = NULL;
633 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
634 "0432 Worker thread stopped.\n");
dea3101e 635 return 0;
636}
637
638/*
639 * This is only called to handle FC worker events. Since this a rare
640 * occurance, we allocate a struct lpfc_work_evt structure here instead of
641 * embedding it in the IOCB.
642 */
643int
2e0fef85 644lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea3101e 645 uint32_t evt)
646{
647 struct lpfc_work_evt *evtp;
ed957684 648 unsigned long flags;
dea3101e 649
650 /*
651 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
652 * be queued to worker thread for processing
653 */
92d7f7b0 654 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea3101e 655 if (!evtp)
656 return 0;
657
658 evtp->evt_arg1 = arg1;
659 evtp->evt_arg2 = arg2;
660 evtp->evt = evt;
661
ed957684 662 spin_lock_irqsave(&phba->hbalock, flags);
071fbd3d 663 list_add_tail(&evtp->evt_listp, &phba->work_list);
ed957684 664 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 665
5e9d9b82
JS
666 lpfc_worker_wake_up(phba);
667
dea3101e 668 return 1;
669}
670
92d7f7b0
JS
671void
672lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
673{
09372820 674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
675 struct lpfc_hba *phba = vport->phba;
676 struct lpfc_nodelist *ndlp, *next_ndlp;
677 int rc;
678
679 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
680 if (!NLP_CHK_NODE_ACT(ndlp))
681 continue;
92d7f7b0
JS
682 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
683 continue;
98c9ea5c
JS
684 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
685 ((vport->port_type == LPFC_NPIV_PORT) &&
686 (ndlp->nlp_DID == NameServer_DID)))
92d7f7b0
JS
687 lpfc_unreg_rpi(vport, ndlp);
688
689 /* Leave Fabric nodes alone on link down */
4d9ab994
JS
690 if ((phba->sli_rev < LPFC_SLI_REV4) &&
691 (!remove && ndlp->nlp_type & NLP_FABRIC))
92d7f7b0
JS
692 continue;
693 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
694 remove
695 ? NLP_EVT_DEVICE_RM
696 : NLP_EVT_DEVICE_RECOVERY);
697 }
698 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
699 lpfc_mbx_unreg_vpi(vport);
09372820 700 spin_lock_irq(shost->host_lock);
92d7f7b0 701 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
09372820 702 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
703 }
704}
705
87af33fe 706void
98c9ea5c 707lpfc_port_link_failure(struct lpfc_vport *vport)
92d7f7b0 708{
45ed1190
JS
709 /* Cleanup any outstanding received buffers */
710 lpfc_cleanup_rcv_buffers(vport);
711
92d7f7b0
JS
712 /* Cleanup any outstanding RSCN activity */
713 lpfc_els_flush_rscn(vport);
714
715 /* Cleanup any outstanding ELS commands */
716 lpfc_els_flush_cmd(vport);
717
718 lpfc_cleanup_rpis(vport, 0);
719
92d7f7b0
JS
720 /* Turn off discovery timer if its running */
721 lpfc_can_disctmo(vport);
722}
723
3772a991 724void
98c9ea5c
JS
725lpfc_linkdown_port(struct lpfc_vport *vport)
726{
727 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
728
729 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
730
731 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
732 "Link Down: state:x%x rtry:x%x flg:x%x",
733 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
734
735 lpfc_port_link_failure(vport);
736
737}
738
dea3101e 739int
685f0bf7 740lpfc_linkdown(struct lpfc_hba *phba)
dea3101e 741{
2e0fef85
JS
742 struct lpfc_vport *vport = phba->pport;
743 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
549e55cd 744 struct lpfc_vport **vports;
685f0bf7 745 LPFC_MBOXQ_t *mb;
549e55cd 746 int i;
dea3101e 747
3163f725 748 if (phba->link_state == LPFC_LINK_DOWN)
2e0fef85 749 return 0;
aacc20e3
JS
750
751 /* Block all SCSI stack I/Os */
752 lpfc_scsi_dev_block(phba);
753
2e0fef85 754 spin_lock_irq(&phba->hbalock);
6fb120a7 755 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
92d7f7b0 756 if (phba->link_state > LPFC_LINK_DOWN) {
2e0fef85 757 phba->link_state = LPFC_LINK_DOWN;
92d7f7b0
JS
758 phba->pport->fc_flag &= ~FC_LBIT;
759 }
2e0fef85 760 spin_unlock_irq(&phba->hbalock);
549e55cd
JS
761 vports = lpfc_create_vport_work_array(phba);
762 if (vports != NULL)
6fb120a7 763 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
764 /* Issue a LINK DOWN event to all nodes */
765 lpfc_linkdown_port(vports[i]);
766 }
09372820 767 lpfc_destroy_vport_work_array(phba, vports);
dea3101e 768 /* Clean up any firmware default rpi's */
2e0fef85
JS
769 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
770 if (mb) {
92d7f7b0 771 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
ed957684 772 mb->vport = vport;
2e0fef85 773 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0b727fea 774 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea3101e 775 == MBX_NOT_FINISHED) {
2e0fef85 776 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 777 }
778 }
779
dea3101e 780 /* Setup myDID for link up if we are in pt2pt mode */
92d7f7b0
JS
781 if (phba->pport->fc_flag & FC_PT2PT) {
782 phba->pport->fc_myDID = 0;
2e0fef85
JS
783 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
784 if (mb) {
dea3101e 785 lpfc_config_link(phba, mb);
92d7f7b0 786 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
ed957684 787 mb->vport = vport;
0b727fea 788 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea3101e 789 == MBX_NOT_FINISHED) {
2e0fef85 790 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 791 }
792 }
2e0fef85 793 spin_lock_irq(shost->host_lock);
92d7f7b0 794 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
2e0fef85 795 spin_unlock_irq(shost->host_lock);
dea3101e 796 }
2e0fef85 797
92d7f7b0
JS
798 return 0;
799}
dea3101e 800
92d7f7b0
JS
801static void
802lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
803{
804 struct lpfc_nodelist *ndlp;
dea3101e 805
92d7f7b0 806 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
807 if (!NLP_CHK_NODE_ACT(ndlp))
808 continue;
92d7f7b0
JS
809 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
810 continue;
92d7f7b0 811 if (ndlp->nlp_type & NLP_FABRIC) {
e47c9093
JS
812 /* On Linkup its safe to clean up the ndlp
813 * from Fabric connections.
814 */
92d7f7b0
JS
815 if (ndlp->nlp_DID != Fabric_DID)
816 lpfc_unreg_rpi(vport, ndlp);
817 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
818 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
e47c9093
JS
819 /* Fail outstanding IO now since device is
820 * marked for PLOGI.
821 */
92d7f7b0
JS
822 lpfc_unreg_rpi(vport, ndlp);
823 }
824 }
dea3101e 825}
826
92d7f7b0
JS
827static void
828lpfc_linkup_port(struct lpfc_vport *vport)
dea3101e 829{
92d7f7b0 830 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
831 struct lpfc_hba *phba = vport->phba;
832
833 if ((vport->load_flag & FC_UNLOADING) != 0)
834 return;
835
858c9f6c
JS
836 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
837 "Link Up: top:x%x speed:x%x flg:x%x",
838 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
839
92d7f7b0
JS
840 /* If NPIV is not enabled, only bring the physical port up */
841 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
842 (vport != phba->pport))
843 return;
dea3101e 844
2e0fef85 845 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
d2873e4c 846
2e0fef85 847 spin_lock_irq(shost->host_lock);
2e0fef85
JS
848 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
849 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
850 vport->fc_flag |= FC_NDISC_ACTIVE;
851 vport->fc_ns_retry = 0;
852 spin_unlock_irq(shost->host_lock);
dea3101e 853
92d7f7b0
JS
854 if (vport->fc_flag & FC_LBIT)
855 lpfc_linkup_cleanup_nodes(vport);
dea3101e 856
92d7f7b0
JS
857}
858
859static int
860lpfc_linkup(struct lpfc_hba *phba)
861{
549e55cd
JS
862 struct lpfc_vport **vports;
863 int i;
92d7f7b0
JS
864
865 phba->link_state = LPFC_LINK_UP;
866
867 /* Unblock fabric iocbs if they are blocked */
868 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
869 del_timer_sync(&phba->fabric_block_timer);
870
549e55cd
JS
871 vports = lpfc_create_vport_work_array(phba);
872 if (vports != NULL)
6fb120a7 873 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
549e55cd 874 lpfc_linkup_port(vports[i]);
09372820 875 lpfc_destroy_vport_work_array(phba, vports);
6fb120a7
JS
876 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
877 (phba->sli_rev < LPFC_SLI_REV4))
92d7f7b0 878 lpfc_issue_clear_la(phba, phba->pport);
dea3101e 879
880 return 0;
881}
882
883/*
884 * This routine handles processing a CLEAR_LA mailbox
885 * command upon completion. It is setup in the LPFC_MBOXQ
886 * as the completion routine when the command is
887 * handed off to the SLI layer.
888 */
a6ababd2 889static void
2e0fef85 890lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 891{
2e0fef85
JS
892 struct lpfc_vport *vport = pmb->vport;
893 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
894 struct lpfc_sli *psli = &phba->sli;
04c68496 895 MAILBOX_t *mb = &pmb->u.mb;
dea3101e 896 uint32_t control;
897
dea3101e 898 /* Since we don't do discovery right now, turn these off here */
a4bc3379 899 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 900 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
901 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
902
903 /* Check for error */
904 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
92d7f7b0 905 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
e8b62011
JS
906 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
907 "0320 CLEAR_LA mbxStatus error x%x hba "
908 "state x%x\n",
909 mb->mbxStatus, vport->port_state);
2e0fef85 910 phba->link_state = LPFC_HBA_ERROR;
dea3101e 911 goto out;
912 }
913
92d7f7b0
JS
914 if (vport->port_type == LPFC_PHYSICAL_PORT)
915 phba->link_state = LPFC_HBA_READY;
916
917 spin_lock_irq(&phba->hbalock);
918 psli->sli_flag |= LPFC_PROCESS_LA;
919 control = readl(phba->HCregaddr);
920 control |= HC_LAINT_ENA;
921 writel(control, phba->HCregaddr);
922 readl(phba->HCregaddr); /* flush */
923 spin_unlock_irq(&phba->hbalock);
1b32f6aa 924 mempool_free(pmb, phba->mbox_mem_pool);
92d7f7b0 925 return;
dea3101e 926
dea3101e 927out:
928 /* Device Discovery completes */
e8b62011
JS
929 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
930 "0225 Device Discovery completes\n");
2e0fef85 931 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 932
2e0fef85 933 spin_lock_irq(shost->host_lock);
58da1ffb 934 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
2e0fef85 935 spin_unlock_irq(shost->host_lock);
dea3101e 936
2e0fef85 937 lpfc_can_disctmo(vport);
dea3101e 938
939 /* turn on Link Attention interrupts */
2e0fef85
JS
940
941 spin_lock_irq(&phba->hbalock);
dea3101e 942 psli->sli_flag |= LPFC_PROCESS_LA;
943 control = readl(phba->HCregaddr);
944 control |= HC_LAINT_ENA;
945 writel(control, phba->HCregaddr);
946 readl(phba->HCregaddr); /* flush */
2e0fef85 947 spin_unlock_irq(&phba->hbalock);
dea3101e 948
949 return;
950}
951
2e0fef85 952
dea3101e 953static void
25594c6b 954lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 955{
2e0fef85 956 struct lpfc_vport *vport = pmb->vport;
dea3101e 957
04c68496 958 if (pmb->u.mb.mbxStatus)
dea3101e 959 goto out;
dea3101e 960
25594c6b
JW
961 mempool_free(pmb, phba->mbox_mem_pool);
962
963 if (phba->fc_topology == TOPOLOGY_LOOP &&
2e0fef85
JS
964 vport->fc_flag & FC_PUBLIC_LOOP &&
965 !(vport->fc_flag & FC_LBIT)) {
25594c6b 966 /* Need to wait for FAN - use discovery timer
2e0fef85 967 * for timeout. port_state is identically
25594c6b
JW
968 * LPFC_LOCAL_CFG_LINK while waiting for FAN
969 */
2e0fef85 970 lpfc_set_disctmo(vport);
25594c6b 971 return;
92d7f7b0 972 }
dea3101e 973
2e0fef85 974 /* Start discovery by sending a FLOGI. port_state is identically
25594c6b
JW
975 * LPFC_FLOGI while waiting for FLOGI cmpl
976 */
92d7f7b0 977 if (vport->port_state != LPFC_FLOGI) {
92d7f7b0
JS
978 lpfc_initial_flogi(vport);
979 }
25594c6b 980 return;
dea3101e 981
982out:
e8b62011
JS
983 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
984 "0306 CONFIG_LINK mbxStatus error x%x "
985 "HBA state x%x\n",
04c68496 986 pmb->u.mb.mbxStatus, vport->port_state);
92d7f7b0 987 mempool_free(pmb, phba->mbox_mem_pool);
25594c6b 988
92d7f7b0 989 lpfc_linkdown(phba);
25594c6b 990
e8b62011
JS
991 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
992 "0200 CONFIG_LINK bad hba state x%x\n",
993 vport->port_state);
dea3101e 994
92d7f7b0 995 lpfc_issue_clear_la(phba, vport);
dea3101e 996 return;
997}
998
6fb120a7
JS
999static void
1000lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1001{
1002 struct lpfc_vport *vport = mboxq->vport;
1003 unsigned long flags;
1004
1005 if (mboxq->u.mb.mbxStatus) {
1006 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1007 "2017 REG_FCFI mbxStatus error x%x "
1008 "HBA state x%x\n",
1009 mboxq->u.mb.mbxStatus, vport->port_state);
1010 mempool_free(mboxq, phba->mbox_mem_pool);
1011 return;
1012 }
1013
1014 /* Start FCoE discovery by sending a FLOGI. */
1015 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1016 /* Set the FCFI registered flag */
1017 spin_lock_irqsave(&phba->hbalock, flags);
1018 phba->fcf.fcf_flag |= FCF_REGISTERED;
1019 spin_unlock_irqrestore(&phba->hbalock, flags);
32b9793f
JS
1020 /* If there is a pending FCoE event, restart FCF table scan. */
1021 if (lpfc_check_pending_fcoe_event(phba, 1)) {
1022 mempool_free(mboxq, phba->mbox_mem_pool);
1023 return;
1024 }
1c6f4ef5 1025 spin_lock_irqsave(&phba->hbalock, flags);
4d9ab994
JS
1026 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
1027 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1c6f4ef5
JS
1028 spin_unlock_irqrestore(&phba->hbalock, flags);
1029 if (vport->port_state != LPFC_FLOGI)
6fb120a7 1030 lpfc_initial_flogi(vport);
6fb120a7
JS
1031
1032 mempool_free(mboxq, phba->mbox_mem_pool);
1033 return;
1034}
1035
1036/**
1037 * lpfc_fab_name_match - Check if the fcf fabric name match.
1038 * @fab_name: pointer to fabric name.
1039 * @new_fcf_record: pointer to fcf record.
1040 *
1041 * This routine compare the fcf record's fabric name with provided
1042 * fabric name. If the fabric name are identical this function
1043 * returns 1 else return 0.
1044 **/
1045static uint32_t
1046lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1047{
1048 if ((fab_name[0] ==
1049 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
1050 (fab_name[1] ==
1051 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
1052 (fab_name[2] ==
1053 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
1054 (fab_name[3] ==
1055 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
1056 (fab_name[4] ==
1057 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
1058 (fab_name[5] ==
1059 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
1060 (fab_name[6] ==
1061 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
1062 (fab_name[7] ==
1063 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
1064 return 1;
1065 else
1066 return 0;
1067}
1068
8fa38513
JS
1069/**
1070 * lpfc_sw_name_match - Check if the fcf switch name match.
1071 * @fab_name: pointer to fabric name.
1072 * @new_fcf_record: pointer to fcf record.
1073 *
1074 * This routine compare the fcf record's switch name with provided
1075 * switch name. If the switch name are identical this function
1076 * returns 1 else return 0.
1077 **/
1078static uint32_t
1079lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1080{
1081 if ((sw_name[0] ==
1082 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record)) &&
1083 (sw_name[1] ==
1084 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record)) &&
1085 (sw_name[2] ==
1086 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record)) &&
1087 (sw_name[3] ==
1088 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record)) &&
1089 (sw_name[4] ==
1090 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record)) &&
1091 (sw_name[5] ==
1092 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record)) &&
1093 (sw_name[6] ==
1094 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record)) &&
1095 (sw_name[7] ==
1096 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record)))
1097 return 1;
1098 else
1099 return 0;
1100}
1101
6fb120a7
JS
1102/**
1103 * lpfc_mac_addr_match - Check if the fcf mac address match.
1104 * @phba: pointer to lpfc hba data structure.
1105 * @new_fcf_record: pointer to fcf record.
1106 *
1107 * This routine compare the fcf record's mac address with HBA's
1108 * FCF mac address. If the mac addresses are identical this function
1109 * returns 1 else return 0.
1110 **/
1111static uint32_t
1112lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1113{
1114 if ((phba->fcf.mac_addr[0] ==
1115 bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
1116 (phba->fcf.mac_addr[1] ==
1117 bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
1118 (phba->fcf.mac_addr[2] ==
1119 bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
1120 (phba->fcf.mac_addr[3] ==
1121 bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
1122 (phba->fcf.mac_addr[4] ==
1123 bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
1124 (phba->fcf.mac_addr[5] ==
1125 bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
1126 return 1;
1127 else
1128 return 0;
1129}
1130
1131/**
1132 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1133 * @phba: pointer to lpfc hba data structure.
1134 * @new_fcf_record: pointer to fcf record.
1135 *
1136 * This routine copies the FCF information from the FCF
1137 * record to lpfc_hba data structure.
1138 **/
1139static void
1140lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
1141{
1142 phba->fcf.fabric_name[0] =
1143 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1144 phba->fcf.fabric_name[1] =
1145 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1146 phba->fcf.fabric_name[2] =
1147 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1148 phba->fcf.fabric_name[3] =
1149 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1150 phba->fcf.fabric_name[4] =
1151 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1152 phba->fcf.fabric_name[5] =
1153 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1154 phba->fcf.fabric_name[6] =
1155 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1156 phba->fcf.fabric_name[7] =
1157 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1158 phba->fcf.mac_addr[0] =
1159 bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1160 phba->fcf.mac_addr[1] =
1161 bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1162 phba->fcf.mac_addr[2] =
1163 bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1164 phba->fcf.mac_addr[3] =
1165 bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1166 phba->fcf.mac_addr[4] =
1167 bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1168 phba->fcf.mac_addr[5] =
1169 bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1170 phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1171 phba->fcf.priority = new_fcf_record->fip_priority;
8fa38513
JS
1172 phba->fcf.switch_name[0] =
1173 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1174 phba->fcf.switch_name[1] =
1175 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1176 phba->fcf.switch_name[2] =
1177 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1178 phba->fcf.switch_name[3] =
1179 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1180 phba->fcf.switch_name[4] =
1181 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1182 phba->fcf.switch_name[5] =
1183 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1184 phba->fcf.switch_name[6] =
1185 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1186 phba->fcf.switch_name[7] =
1187 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
6fb120a7
JS
1188}
1189
1190/**
1191 * lpfc_register_fcf - Register the FCF with hba.
1192 * @phba: pointer to lpfc hba data structure.
1193 *
1194 * This routine issues a register fcfi mailbox command to register
1195 * the fcf with HBA.
1196 **/
1197static void
1198lpfc_register_fcf(struct lpfc_hba *phba)
1199{
1200 LPFC_MBOXQ_t *fcf_mbxq;
1201 int rc;
1202 unsigned long flags;
1203
1204 spin_lock_irqsave(&phba->hbalock, flags);
1205
1206 /* If the FCF is not availabe do nothing. */
1207 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
4d9ab994 1208 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
6fb120a7
JS
1209 spin_unlock_irqrestore(&phba->hbalock, flags);
1210 return;
1211 }
1212
1213 /* The FCF is already registered, start discovery */
1214 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1215 phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
32b9793f 1216 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
6fb120a7
JS
1217 spin_unlock_irqrestore(&phba->hbalock, flags);
1218 if (phba->pport->port_state != LPFC_FLOGI)
1219 lpfc_initial_flogi(phba->pport);
1220 return;
1221 }
1222 spin_unlock_irqrestore(&phba->hbalock, flags);
1223
1224 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
1225 GFP_KERNEL);
4d9ab994
JS
1226 if (!fcf_mbxq) {
1227 spin_lock_irqsave(&phba->hbalock, flags);
1228 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1229 spin_unlock_irqrestore(&phba->hbalock, flags);
6fb120a7 1230 return;
4d9ab994 1231 }
6fb120a7
JS
1232
1233 lpfc_reg_fcfi(phba, fcf_mbxq);
1234 fcf_mbxq->vport = phba->pport;
1235 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1236 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
4d9ab994
JS
1237 if (rc == MBX_NOT_FINISHED) {
1238 spin_lock_irqsave(&phba->hbalock, flags);
1239 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1240 spin_unlock_irqrestore(&phba->hbalock, flags);
6fb120a7 1241 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
4d9ab994 1242 }
6fb120a7
JS
1243
1244 return;
1245}
1246
1247/**
1248 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1249 * @phba: pointer to lpfc hba data structure.
1250 * @new_fcf_record: pointer to fcf record.
1251 * @boot_flag: Indicates if this record used by boot bios.
1252 * @addr_mode: The address mode to be used by this FCF
1253 *
1254 * This routine compare the fcf record with connect list obtained from the
1255 * config region to decide if this FCF can be used for SAN discovery. It returns
1256 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1257 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1258 * is used by boot bios and addr_mode will indicate the addressing mode to be
1259 * used for this FCF when the function returns.
1260 * If the FCF record need to be used with a particular vlan id, the vlan is
1261 * set in the vlan_id on return of the function. If not VLAN tagging need to
1262 * be used with the FCF vlan_id will be set to 0xFFFF;
1263 **/
1264static int
1265lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1266 struct fcf_record *new_fcf_record,
1267 uint32_t *boot_flag, uint32_t *addr_mode,
1268 uint16_t *vlan_id)
1269{
1270 struct lpfc_fcf_conn_entry *conn_entry;
4d9ab994
JS
1271 int i, j, fcf_vlan_id = 0;
1272
1273 /* Find the lowest VLAN id in the FCF record */
1274 for (i = 0; i < 512; i++) {
1275 if (new_fcf_record->vlan_bitmap[i]) {
1276 fcf_vlan_id = i * 8;
1277 j = 0;
1278 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1279 j++;
1280 fcf_vlan_id++;
1281 }
1282 break;
1283 }
1284 }
6fb120a7 1285
0c287589
JS
1286 /* If FCF not available return 0 */
1287 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1288 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
1289 return 0;
1290
45ed1190 1291 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
6fb120a7
JS
1292 *boot_flag = 0;
1293 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1294 new_fcf_record);
1295 if (phba->valid_vlan)
1296 *vlan_id = phba->vlan_id;
1297 else
1298 *vlan_id = 0xFFFF;
1299 return 1;
1300 }
1301
1302 /*
1303 * If there are no FCF connection table entry, driver connect to all
1304 * FCFs.
1305 */
1306 if (list_empty(&phba->fcf_conn_rec_list)) {
1307 *boot_flag = 0;
1308 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1309 new_fcf_record);
0c287589
JS
1310
1311 /*
1312 * When there are no FCF connect entries, use driver's default
1313 * addressing mode - FPMA.
1314 */
1315 if (*addr_mode & LPFC_FCF_FPMA)
1316 *addr_mode = LPFC_FCF_FPMA;
1317
4d9ab994
JS
1318 /* If FCF record report a vlan id use that vlan id */
1319 if (fcf_vlan_id)
1320 *vlan_id = fcf_vlan_id;
1321 else
1322 *vlan_id = 0xFFFF;
6fb120a7
JS
1323 return 1;
1324 }
1325
1326 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
1327 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1328 continue;
1329
1330 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1331 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
8fa38513
JS
1332 new_fcf_record))
1333 continue;
1334 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1335 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1336 new_fcf_record))
6fb120a7 1337 continue;
6fb120a7
JS
1338 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1339 /*
1340 * If the vlan bit map does not have the bit set for the
1341 * vlan id to be used, then it is not a match.
1342 */
1343 if (!(new_fcf_record->vlan_bitmap
1344 [conn_entry->conn_rec.vlan_tag / 8] &
1345 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1346 continue;
1347 }
1348
0c287589
JS
1349 /*
1350 * If connection record does not support any addressing mode,
1351 * skip the FCF record.
1352 */
1353 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1354 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1355 continue;
1356
6fb120a7
JS
1357 /*
1358 * Check if the connection record specifies a required
1359 * addressing mode.
1360 */
1361 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1362 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1363
1364 /*
1365 * If SPMA required but FCF not support this continue.
1366 */
1367 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1368 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1369 new_fcf_record) & LPFC_FCF_SPMA))
1370 continue;
1371
1372 /*
1373 * If FPMA required but FCF not support this continue.
1374 */
1375 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1376 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1377 new_fcf_record) & LPFC_FCF_FPMA))
1378 continue;
1379 }
1380
1381 /*
1382 * This fcf record matches filtering criteria.
1383 */
1384 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1385 *boot_flag = 1;
1386 else
1387 *boot_flag = 0;
1388
0c287589
JS
1389 /*
1390 * If user did not specify any addressing mode, or if the
1391 * prefered addressing mode specified by user is not supported
1392 * by FCF, allow fabric to pick the addressing mode.
1393 */
6fb120a7
JS
1394 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1395 new_fcf_record);
1396 /*
1397 * If the user specified a required address mode, assign that
1398 * address mode
1399 */
1400 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1401 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1402 *addr_mode = (conn_entry->conn_rec.flags &
1403 FCFCNCT_AM_SPMA) ?
1404 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1405 /*
1406 * If the user specified a prefered address mode, use the
1407 * addr mode only if FCF support the addr_mode.
1408 */
1409 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1410 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1411 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1412 (*addr_mode & LPFC_FCF_SPMA))
1413 *addr_mode = LPFC_FCF_SPMA;
1414 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1415 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1416 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1417 (*addr_mode & LPFC_FCF_FPMA))
1418 *addr_mode = LPFC_FCF_FPMA;
6fb120a7 1419
4d9ab994 1420 /* If matching connect list has a vlan id, use it */
6fb120a7
JS
1421 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1422 *vlan_id = conn_entry->conn_rec.vlan_tag;
4d9ab994
JS
1423 /*
1424 * If no vlan id is specified in connect list, use the vlan id
1425 * in the FCF record
1426 */
1427 else if (fcf_vlan_id)
1428 *vlan_id = fcf_vlan_id;
6fb120a7
JS
1429 else
1430 *vlan_id = 0xFFFF;
1431
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
32b9793f
JS
1438/**
1439 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1440 * @phba: pointer to lpfc hba data structure.
1441 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1442 *
1443 * This function check if there is any fcoe event pending while driver
1444 * scan FCF entries. If there is any pending event, it will restart the
1445 * FCF saning and return 1 else return 0.
1446 */
1447int
1448lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1449{
1450 LPFC_MBOXQ_t *mbox;
1451 int rc;
1452 /*
1453 * If the Link is up and no FCoE events while in the
1454 * FCF discovery, no need to restart FCF discovery.
1455 */
1456 if ((phba->link_state >= LPFC_LINK_UP) &&
1457 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
1458 return 0;
1459
1460 spin_lock_irq(&phba->hbalock);
1461 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1462 spin_unlock_irq(&phba->hbalock);
1463
1464 if (phba->link_state >= LPFC_LINK_UP)
1465 lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
1c6f4ef5 1466 else {
4d9ab994
JS
1467 /*
1468 * Do not continue FCF discovery and clear FCF_DISC_INPROGRESS
1469 * flag
1470 */
1c6f4ef5 1471 spin_lock_irq(&phba->hbalock);
4d9ab994 1472 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
1c6f4ef5
JS
1473 spin_unlock_irq(&phba->hbalock);
1474 }
32b9793f
JS
1475
1476 if (unreg_fcf) {
1477 spin_lock_irq(&phba->hbalock);
1478 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1479 spin_unlock_irq(&phba->hbalock);
1480 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1481 if (!mbox) {
1482 lpfc_printf_log(phba, KERN_ERR,
1483 LOG_DISCOVERY|LOG_MBOX,
1484 "2610 UNREG_FCFI mbox allocation failed\n");
1485 return 1;
1486 }
1487 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
1488 mbox->vport = phba->pport;
1489 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
1490 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1491 if (rc == MBX_NOT_FINISHED) {
1492 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
1493 "2611 UNREG_FCFI issue mbox failed\n");
1494 mempool_free(mbox, phba->mbox_mem_pool);
1495 }
1496 }
1497
1498 return 1;
1499}
1500
6fb120a7
JS
1501/**
1502 * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
1503 * @phba: pointer to lpfc hba data structure.
1504 * @mboxq: pointer to mailbox object.
1505 *
1506 * This function iterate through all the fcf records available in
1507 * HBA and choose the optimal FCF record for discovery. After finding
1508 * the FCF for discovery it register the FCF record and kick start
1509 * discovery.
1510 * If FCF_IN_USE flag is set in currently used FCF, the routine try to
1511 * use a FCF record which match fabric name and mac address of the
1512 * currently used FCF record.
1513 * If the driver support only one FCF, it will try to use the FCF record
1514 * used by BOOT_BIOS.
1515 */
1516void
1517lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1518{
1519 void *virt_addr;
1520 dma_addr_t phys_addr;
1521 uint8_t *bytep;
1522 struct lpfc_mbx_sge sge;
1523 struct lpfc_mbx_read_fcf_tbl *read_fcf;
1524 uint32_t shdr_status, shdr_add_status;
1525 union lpfc_sli4_cfg_shdr *shdr;
1526 struct fcf_record *new_fcf_record;
1527 int rc;
1528 uint32_t boot_flag, addr_mode;
1529 uint32_t next_fcf_index;
1530 unsigned long flags;
1531 uint16_t vlan_id;
1532
32b9793f
JS
1533 /* If there is pending FCoE event restart FCF table scan */
1534 if (lpfc_check_pending_fcoe_event(phba, 0)) {
1535 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1536 return;
1537 }
1538
6fb120a7
JS
1539 /* Get the first SGE entry from the non-embedded DMA memory. This
1540 * routine only uses a single SGE.
1541 */
1542 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
1543 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
1544 if (unlikely(!mboxq->sge_array)) {
1545 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1546 "2524 Failed to get the non-embedded SGE "
1547 "virtual address\n");
1548 goto out;
1549 }
1550 virt_addr = mboxq->sge_array->addr[0];
1551
1552 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
1553 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
1554 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
1555 &shdr->response);
1556 /*
1557 * The FCF Record was read and there is no reason for the driver
1558 * to maintain the FCF record data or memory. Instead, just need
1559 * to book keeping the FCFIs can be used.
1560 */
1561 if (shdr_status || shdr_add_status) {
def9c7a9
JS
1562 if (shdr_status == STATUS_FCF_TABLE_EMPTY) {
1563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1564 "2726 READ_FCF_RECORD Indicates empty "
1565 "FCF table.\n");
1566 } else {
1567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1568 "2521 READ_FCF_RECORD mailbox failed "
1569 "with status x%x add_status x%x, mbx\n",
1570 shdr_status, shdr_add_status);
1571 }
6fb120a7
JS
1572 goto out;
1573 }
1574 /* Interpreting the returned information of FCF records */
1575 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1576 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1577 sizeof(struct lpfc_mbx_read_fcf_tbl));
1578 next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
1579
1580 new_fcf_record = (struct fcf_record *)(virt_addr +
1581 sizeof(struct lpfc_mbx_read_fcf_tbl));
1582 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
1583 sizeof(struct fcf_record));
1584 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
1585
1586 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
1587 &boot_flag, &addr_mode,
1588 &vlan_id);
1589 /*
1590 * If the fcf record does not match with connect list entries
1591 * read the next entry.
1592 */
1593 if (!rc)
1594 goto read_next_fcf;
1595 /*
1596 * If this is not the first FCF discovery of the HBA, use last
1597 * FCF record for the discovery.
1598 */
1599 spin_lock_irqsave(&phba->hbalock, flags);
1600 if (phba->fcf.fcf_flag & FCF_IN_USE) {
1601 if (lpfc_fab_name_match(phba->fcf.fabric_name,
8fa38513
JS
1602 new_fcf_record) &&
1603 lpfc_sw_name_match(phba->fcf.switch_name,
1604 new_fcf_record) &&
6fb120a7
JS
1605 lpfc_mac_addr_match(phba, new_fcf_record)) {
1606 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1607 spin_unlock_irqrestore(&phba->hbalock, flags);
1608 goto out;
1609 }
1610 spin_unlock_irqrestore(&phba->hbalock, flags);
1611 goto read_next_fcf;
1612 }
1613 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
1614 /*
1615 * If the current FCF record does not have boot flag
1616 * set and new fcf record has boot flag set, use the
1617 * new fcf record.
1618 */
1619 if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1620 /* Use this FCF record */
1621 lpfc_copy_fcf_record(phba, new_fcf_record);
1622 phba->fcf.addr_mode = addr_mode;
1623 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1624 if (vlan_id != 0xFFFF) {
1625 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1626 phba->fcf.vlan_id = vlan_id;
1627 }
1628 spin_unlock_irqrestore(&phba->hbalock, flags);
1629 goto read_next_fcf;
1630 }
1631 /*
1632 * If the current FCF record has boot flag set and the
1633 * new FCF record does not have boot flag, read the next
1634 * FCF record.
1635 */
1636 if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
1637 spin_unlock_irqrestore(&phba->hbalock, flags);
1638 goto read_next_fcf;
1639 }
1640 /*
1641 * If there is a record with lower priority value for
1642 * the current FCF, use that record.
1643 */
8fa38513
JS
1644 if (lpfc_fab_name_match(phba->fcf.fabric_name,
1645 new_fcf_record) &&
1646 (new_fcf_record->fip_priority < phba->fcf.priority)) {
6fb120a7
JS
1647 /* Use this FCF record */
1648 lpfc_copy_fcf_record(phba, new_fcf_record);
1649 phba->fcf.addr_mode = addr_mode;
1650 if (vlan_id != 0xFFFF) {
1651 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1652 phba->fcf.vlan_id = vlan_id;
1653 }
1654 spin_unlock_irqrestore(&phba->hbalock, flags);
1655 goto read_next_fcf;
1656 }
1657 spin_unlock_irqrestore(&phba->hbalock, flags);
1658 goto read_next_fcf;
1659 }
1660 /*
1661 * This is the first available FCF record, use this
1662 * record.
1663 */
1664 lpfc_copy_fcf_record(phba, new_fcf_record);
1665 phba->fcf.addr_mode = addr_mode;
1666 if (boot_flag)
1667 phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
1668 phba->fcf.fcf_flag |= FCF_AVAILABLE;
1669 if (vlan_id != 0xFFFF) {
1670 phba->fcf.fcf_flag |= FCF_VALID_VLAN;
1671 phba->fcf.vlan_id = vlan_id;
1672 }
1673 spin_unlock_irqrestore(&phba->hbalock, flags);
1674 goto read_next_fcf;
1675
1676read_next_fcf:
1677 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1678 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
1679 lpfc_register_fcf(phba);
1680 else
1681 lpfc_sli4_read_fcf_record(phba, next_fcf_index);
1682 return;
1683
1684out:
1685 lpfc_sli4_mbox_cmd_free(phba, mboxq);
1686 lpfc_register_fcf(phba);
1687
1688 return;
1689}
1690
1c6834a7
JS
1691/**
1692 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
1693 * @phba: pointer to lpfc hba data structure.
1694 * @mboxq: pointer to mailbox data structure.
1695 *
1696 * This function handles completion of init vpi mailbox command.
1697 */
1698static void
1699lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1700{
1701 struct lpfc_vport *vport = mboxq->vport;
1702 if (mboxq->u.mb.mbxStatus) {
1703 lpfc_printf_vlog(vport, KERN_ERR,
1704 LOG_MBOX,
1705 "2609 Init VPI mailbox failed 0x%x\n",
1706 mboxq->u.mb.mbxStatus);
1707 mempool_free(mboxq, phba->mbox_mem_pool);
1708 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1709 return;
1710 }
97957244 1711 spin_lock_irq(&phba->hbalock);
1c6834a7 1712 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
97957244 1713 spin_unlock_irq(&phba->hbalock);
1c6834a7
JS
1714
1715 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1716 lpfc_initial_fdisc(vport);
1717 else {
1718 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
6a9c52cf
JS
1719 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1720 "2606 No NPIV Fabric support\n");
1c6834a7
JS
1721 }
1722 return;
1723}
1724
6fb120a7
JS
1725/**
1726 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
1727 * @phba: pointer to lpfc hba data structure.
1728 *
1729 * This function loops through the list of vports on the @phba and issues an
1730 * FDISC if possible.
1731 */
1732void
1733lpfc_start_fdiscs(struct lpfc_hba *phba)
1734{
1735 struct lpfc_vport **vports;
1736 int i;
1c6834a7
JS
1737 LPFC_MBOXQ_t *mboxq;
1738 int rc;
6fb120a7
JS
1739
1740 vports = lpfc_create_vport_work_array(phba);
1741 if (vports != NULL) {
1742 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1743 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1744 continue;
1745 /* There are no vpi for this vport */
1746 if (vports[i]->vpi > phba->max_vpi) {
1747 lpfc_vport_set_state(vports[i],
1748 FC_VPORT_FAILED);
1749 continue;
1750 }
1751 if (phba->fc_topology == TOPOLOGY_LOOP) {
1752 lpfc_vport_set_state(vports[i],
1753 FC_VPORT_LINKDOWN);
1754 continue;
1755 }
1c6834a7
JS
1756 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
1757 mboxq = mempool_alloc(phba->mbox_mem_pool,
1758 GFP_KERNEL);
1759 if (!mboxq) {
1760 lpfc_printf_vlog(vports[i], KERN_ERR,
1761 LOG_MBOX, "2607 Failed to allocate "
1762 "init_vpi mailbox\n");
1763 continue;
1764 }
1765 lpfc_init_vpi(phba, mboxq, vports[i]->vpi);
1766 mboxq->vport = vports[i];
1767 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
1768 rc = lpfc_sli_issue_mbox(phba, mboxq,
1769 MBX_NOWAIT);
1770 if (rc == MBX_NOT_FINISHED) {
1771 lpfc_printf_vlog(vports[i], KERN_ERR,
1772 LOG_MBOX, "2608 Failed to issue "
1773 "init_vpi mailbox\n");
1774 mempool_free(mboxq,
1775 phba->mbox_mem_pool);
1776 }
1777 continue;
1778 }
6fb120a7
JS
1779 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1780 lpfc_initial_fdisc(vports[i]);
1781 else {
1782 lpfc_vport_set_state(vports[i],
1783 FC_VPORT_NO_FABRIC_SUPP);
1784 lpfc_printf_vlog(vports[i], KERN_ERR,
1785 LOG_ELS,
1786 "0259 No NPIV "
1787 "Fabric support\n");
1788 }
1789 }
1790 }
1791 lpfc_destroy_vport_work_array(phba, vports);
1792}
1793
1794void
1795lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1796{
1797 struct lpfc_dmabuf *dmabuf = mboxq->context1;
1798 struct lpfc_vport *vport = mboxq->vport;
1799
1800 if (mboxq->u.mb.mbxStatus) {
1801 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1802 "2018 REG_VFI mbxStatus error x%x "
1803 "HBA state x%x\n",
1804 mboxq->u.mb.mbxStatus, vport->port_state);
1805 if (phba->fc_topology == TOPOLOGY_LOOP) {
1806 /* FLOGI failed, use loop map to make discovery list */
1807 lpfc_disc_list_loopmap(vport);
1808 /* Start discovery */
1809 lpfc_disc_start(vport);
1810 goto fail_free_mem;
1811 }
1812 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1813 goto fail_free_mem;
1814 }
c868595d
JS
1815 /* The VPI is implicitly registered when the VFI is registered */
1816 vport->vpi_state |= LPFC_VPI_REGISTERED;
6fb120a7
JS
1817
1818 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1819 lpfc_start_fdiscs(phba);
1820 lpfc_do_scr_ns_plogi(phba, vport);
1821 }
1822
1823fail_free_mem:
1824 mempool_free(mboxq, phba->mbox_mem_pool);
1825 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
1826 kfree(dmabuf);
1827 return;
1828}
1829
dea3101e 1830static void
2e0fef85 1831lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1832{
6fb120a7 1833 MAILBOX_t *mb = &pmb->u.mb;
dea3101e 1834 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
2e0fef85 1835 struct lpfc_vport *vport = pmb->vport;
dea3101e 1836
1837
1838 /* Check for error */
1839 if (mb->mbxStatus) {
1840 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
e8b62011
JS
1841 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1842 "0319 READ_SPARAM mbxStatus error x%x "
1843 "hba state x%x>\n",
1844 mb->mbxStatus, vport->port_state);
dea3101e 1845 lpfc_linkdown(phba);
dea3101e 1846 goto out;
1847 }
1848
2e0fef85 1849 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea3101e 1850 sizeof (struct serv_parm));
a12e07bc 1851 if (phba->cfg_soft_wwnn)
2e0fef85
JS
1852 u64_to_wwn(phba->cfg_soft_wwnn,
1853 vport->fc_sparam.nodeName.u.wwn);
c3f28afa 1854 if (phba->cfg_soft_wwpn)
2e0fef85
JS
1855 u64_to_wwn(phba->cfg_soft_wwpn,
1856 vport->fc_sparam.portName.u.wwn);
92d7f7b0
JS
1857 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1858 sizeof(vport->fc_nodename));
1859 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1860 sizeof(vport->fc_portname));
1861 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1862 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1863 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1864 }
1865
dea3101e 1866 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1867 kfree(mp);
2e0fef85 1868 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1869 return;
1870
1871out:
1872 pmb->context1 = NULL;
1873 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1874 kfree(mp);
92d7f7b0
JS
1875 lpfc_issue_clear_la(phba, vport);
1876 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1877 return;
1878}
1879
1880static void
92d7f7b0 1881lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea3101e 1882{
92d7f7b0 1883 struct lpfc_vport *vport = phba->pport;
6fb120a7 1884 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
2e0fef85 1885 int i;
14691150
JS
1886 struct lpfc_dmabuf *mp;
1887 int rc;
6fb120a7 1888 struct fcf_record *fcf_record;
14691150 1889
dea3101e 1890 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
dea3101e 1891
92d7f7b0 1892 spin_lock_irq(&phba->hbalock);
2fe165b6 1893 switch (la->UlnkSpeed) {
92d7f7b0
JS
1894 case LA_1GHZ_LINK:
1895 phba->fc_linkspeed = LA_1GHZ_LINK;
1896 break;
1897 case LA_2GHZ_LINK:
1898 phba->fc_linkspeed = LA_2GHZ_LINK;
1899 break;
1900 case LA_4GHZ_LINK:
1901 phba->fc_linkspeed = LA_4GHZ_LINK;
1902 break;
1903 case LA_8GHZ_LINK:
1904 phba->fc_linkspeed = LA_8GHZ_LINK;
1905 break;
f4b4c68f
JS
1906 case LA_10GHZ_LINK:
1907 phba->fc_linkspeed = LA_10GHZ_LINK;
1908 break;
92d7f7b0
JS
1909 default:
1910 phba->fc_linkspeed = LA_UNKNW_LINK;
1911 break;
dea3101e 1912 }
1913
1914 phba->fc_topology = la->topology;
92d7f7b0 1915 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea3101e 1916
1917 if (phba->fc_topology == TOPOLOGY_LOOP) {
92d7f7b0 1918 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea3101e 1919
6a9c52cf
JS
1920 /* if npiv is enabled and this adapter supports npiv log
1921 * a message that npiv is not supported in this topology
1922 */
1923 if (phba->cfg_enable_npiv && phba->max_vpi)
495a714c
JS
1924 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1925 "1309 Link Up Event npiv not supported in loop "
1926 "topology\n");
92d7f7b0 1927 /* Get Loop Map information */
dea3101e 1928 if (la->il)
2e0fef85 1929 vport->fc_flag |= FC_LBIT;
dea3101e 1930
2e0fef85 1931 vport->fc_myDID = la->granted_AL_PA;
dea3101e 1932 i = la->un.lilpBde64.tus.f.bdeSize;
1933
1934 if (i == 0) {
1935 phba->alpa_map[0] = 0;
1936 } else {
e8b62011 1937 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea3101e 1938 int numalpa, j, k;
1939 union {
1940 uint8_t pamap[16];
1941 struct {
1942 uint32_t wd1;
1943 uint32_t wd2;
1944 uint32_t wd3;
1945 uint32_t wd4;
1946 } pa;
1947 } un;
1948 numalpa = phba->alpa_map[0];
1949 j = 0;
1950 while (j < numalpa) {
1951 memset(un.pamap, 0, 16);
1952 for (k = 1; j < numalpa; k++) {
1953 un.pamap[k - 1] =
1954 phba->alpa_map[j + 1];
1955 j++;
1956 if (k == 16)
1957 break;
1958 }
1959 /* Link Up Event ALPA map */
1960 lpfc_printf_log(phba,
92d7f7b0
JS
1961 KERN_WARNING,
1962 LOG_LINK_EVENT,
e8b62011 1963 "1304 Link Up Event "
92d7f7b0
JS
1964 "ALPA map Data: x%x "
1965 "x%x x%x x%x\n",
92d7f7b0
JS
1966 un.pa.wd1, un.pa.wd2,
1967 un.pa.wd3, un.pa.wd4);
dea3101e 1968 }
1969 }
1970 }
1971 } else {
92d7f7b0 1972 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
78b2d852 1973 if (phba->max_vpi && phba->cfg_enable_npiv &&
92d7f7b0
JS
1974 (phba->sli_rev == 3))
1975 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1976 }
2e0fef85
JS
1977 vport->fc_myDID = phba->fc_pref_DID;
1978 vport->fc_flag |= FC_LBIT;
dea3101e 1979 }
92d7f7b0 1980 spin_unlock_irq(&phba->hbalock);
dea3101e 1981
1982 lpfc_linkup(phba);
1983 if (sparam_mbox) {
92d7f7b0 1984 lpfc_read_sparam(phba, sparam_mbox, 0);
2e0fef85 1985 sparam_mbox->vport = vport;
dea3101e 1986 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
0b727fea 1987 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
14691150
JS
1988 if (rc == MBX_NOT_FINISHED) {
1989 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1990 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1991 kfree(mp);
1992 mempool_free(sparam_mbox, phba->mbox_mem_pool);
92d7f7b0 1993 goto out;
14691150 1994 }
dea3101e 1995 }
1996
6fb120a7
JS
1997 if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
1998 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1999 if (!cfglink_mbox)
2000 goto out;
2e0fef85 2001 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea3101e 2002 lpfc_config_link(phba, cfglink_mbox);
2e0fef85 2003 cfglink_mbox->vport = vport;
25594c6b 2004 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
0b727fea 2005 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
6fb120a7
JS
2006 if (rc == MBX_NOT_FINISHED) {
2007 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
2008 goto out;
2009 }
2010 } else {
32b9793f 2011 vport->port_state = LPFC_VPORT_UNKNOWN;
6fb120a7
JS
2012 /*
2013 * Add the driver's default FCF record at FCF index 0 now. This
2014 * is phase 1 implementation that support FCF index 0 and driver
2015 * defaults.
2016 */
45ed1190 2017 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
6fb120a7
JS
2018 fcf_record = kzalloc(sizeof(struct fcf_record),
2019 GFP_KERNEL);
2020 if (unlikely(!fcf_record)) {
2021 lpfc_printf_log(phba, KERN_ERR,
2022 LOG_MBOX | LOG_SLI,
2023 "2554 Could not allocate memmory for "
2024 "fcf record\n");
2025 rc = -ENODEV;
2026 goto out;
2027 }
2028
2029 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
2030 LPFC_FCOE_FCF_DEF_INDEX);
2031 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
2032 if (unlikely(rc)) {
2033 lpfc_printf_log(phba, KERN_ERR,
2034 LOG_MBOX | LOG_SLI,
2035 "2013 Could not manually add FCF "
2036 "record 0, status %d\n", rc);
2037 rc = -ENODEV;
2038 kfree(fcf_record);
2039 goto out;
2040 }
2041 kfree(fcf_record);
2042 }
2043 /*
2044 * The driver is expected to do FIP/FCF. Call the port
2045 * and get the FCF Table.
2046 */
32b9793f
JS
2047 spin_lock_irq(&phba->hbalock);
2048 if (phba->hba_flag & FCF_DISC_INPROGRESS) {
2049 spin_unlock_irq(&phba->hbalock);
2050 return;
2051 }
2052 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
2053 rc = lpfc_sli4_read_fcf_record(phba,
2054 LPFC_FCOE_FCF_GET_FIRST);
2055 if (rc)
2056 goto out;
dea3101e 2057 }
6fb120a7
JS
2058
2059 return;
92d7f7b0
JS
2060out:
2061 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
2062 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2063 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
2064 vport->port_state, sparam_mbox, cfglink_mbox);
92d7f7b0
JS
2065 lpfc_issue_clear_la(phba, vport);
2066 return;
dea3101e 2067}
2068
2069static void
84774a4d 2070lpfc_enable_la(struct lpfc_hba *phba)
2e0fef85 2071{
dea3101e 2072 uint32_t control;
2073 struct lpfc_sli *psli = &phba->sli;
2e0fef85 2074 spin_lock_irq(&phba->hbalock);
dea3101e 2075 psli->sli_flag |= LPFC_PROCESS_LA;
3772a991
JS
2076 if (phba->sli_rev <= LPFC_SLI_REV3) {
2077 control = readl(phba->HCregaddr);
2078 control |= HC_LAINT_ENA;
2079 writel(control, phba->HCregaddr);
2080 readl(phba->HCregaddr); /* flush */
2081 }
2e0fef85 2082 spin_unlock_irq(&phba->hbalock);
dea3101e 2083}
2084
84774a4d
JS
2085static void
2086lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
2087{
2088 lpfc_linkdown(phba);
2089 lpfc_enable_la(phba);
6fb120a7 2090 lpfc_unregister_unused_fcf(phba);
84774a4d
JS
2091 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2092}
2093
2094
dea3101e 2095/*
2096 * This routine handles processing a READ_LA mailbox
2097 * command upon completion. It is setup in the LPFC_MBOXQ
2098 * as the completion routine when the command is
2099 * handed off to the SLI layer.
2100 */
2101void
2e0fef85 2102lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2103{
2e0fef85
JS
2104 struct lpfc_vport *vport = pmb->vport;
2105 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2106 READ_LA_VAR *la;
04c68496 2107 MAILBOX_t *mb = &pmb->u.mb;
dea3101e 2108 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2109
0d2b6b83
JS
2110 /* Unblock ELS traffic */
2111 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 2112 /* Check for error */
2113 if (mb->mbxStatus) {
ed957684 2114 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
e8b62011
JS
2115 "1307 READ_LA mbox error x%x state x%x\n",
2116 mb->mbxStatus, vport->port_state);
dea3101e 2117 lpfc_mbx_issue_link_down(phba);
2e0fef85 2118 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2119 goto lpfc_mbx_cmpl_read_la_free_mbuf;
2120 }
2121
04c68496 2122 la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
dea3101e 2123
2124 memcpy(&phba->alpa_map[0], mp->virt, 128);
2125
2e0fef85 2126 spin_lock_irq(shost->host_lock);
c9f8735b 2127 if (la->pb)
2e0fef85 2128 vport->fc_flag |= FC_BYPASSED_MODE;
c9f8735b 2129 else
2e0fef85
JS
2130 vport->fc_flag &= ~FC_BYPASSED_MODE;
2131 spin_unlock_irq(shost->host_lock);
c9f8735b 2132
0c287589 2133 if ((phba->fc_eventTag < la->eventTag) ||
92d7f7b0 2134 (phba->fc_eventTag == la->eventTag)) {
dea3101e 2135 phba->fc_stat.LinkMultiEvent++;
2e0fef85 2136 if (la->attType == AT_LINK_UP)
dea3101e 2137 if (phba->fc_eventTag != 0)
2138 lpfc_linkdown(phba);
92d7f7b0 2139 }
dea3101e 2140
2141 phba->fc_eventTag = la->eventTag;
84774a4d
JS
2142 if (la->mm)
2143 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
2144 else
2145 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
dea3101e 2146
4d9ab994 2147 phba->link_events++;
84774a4d 2148 if (la->attType == AT_LINK_UP && (!la->mm)) {
dea3101e 2149 phba->fc_stat.LinkUp++;
2e0fef85 2150 if (phba->link_flag & LS_LOOPBACK_MODE) {
3163f725 2151 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
e8b62011
JS
2152 "1306 Link Up Event in loop back mode "
2153 "x%x received Data: x%x x%x x%x x%x\n",
2154 la->eventTag, phba->fc_eventTag,
2155 la->granted_AL_PA, la->UlnkSpeed,
2156 phba->alpa_map[0]);
5b8bd0c9
JS
2157 } else {
2158 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
e8b62011 2159 "1303 Link Up Event x%x received "
84774a4d 2160 "Data: x%x x%x x%x x%x x%x x%x %d\n",
e8b62011
JS
2161 la->eventTag, phba->fc_eventTag,
2162 la->granted_AL_PA, la->UlnkSpeed,
84774a4d
JS
2163 phba->alpa_map[0],
2164 la->mm, la->fa,
2165 phba->wait_4_mlo_maint_flg);
5b8bd0c9 2166 }
92d7f7b0 2167 lpfc_mbx_process_link_up(phba, la);
84774a4d 2168 } else if (la->attType == AT_LINK_DOWN) {
dea3101e 2169 phba->fc_stat.LinkDown++;
3163f725
JS
2170 if (phba->link_flag & LS_LOOPBACK_MODE) {
2171 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2172 "1308 Link Down Event in loop back mode "
2173 "x%x received "
2174 "Data: x%x x%x x%x\n",
2175 la->eventTag, phba->fc_eventTag,
2176 phba->pport->port_state, vport->fc_flag);
2177 }
2178 else {
2179 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
e8b62011 2180 "1305 Link Down Event x%x received "
84774a4d
JS
2181 "Data: x%x x%x x%x x%x x%x\n",
2182 la->eventTag, phba->fc_eventTag,
2183 phba->pport->port_state, vport->fc_flag,
2184 la->mm, la->fa);
2185 }
2186 lpfc_mbx_issue_link_down(phba);
2187 }
2188 if (la->mm && la->attType == AT_LINK_UP) {
2189 if (phba->link_state != LPFC_LINK_DOWN) {
2190 phba->fc_stat.LinkDown++;
2191 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2192 "1312 Link Down Event x%x received "
dea3101e 2193 "Data: x%x x%x x%x\n",
e8b62011 2194 la->eventTag, phba->fc_eventTag,
2e0fef85 2195 phba->pport->port_state, vport->fc_flag);
84774a4d
JS
2196 lpfc_mbx_issue_link_down(phba);
2197 } else
2198 lpfc_enable_la(phba);
2199
2200 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
2201 "1310 Menlo Maint Mode Link up Event x%x rcvd "
2202 "Data: x%x x%x x%x\n",
2203 la->eventTag, phba->fc_eventTag,
2204 phba->pport->port_state, vport->fc_flag);
2205 /*
2206 * The cmnd that triggered this will be waiting for this
2207 * signal.
2208 */
2209 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
2210 if (phba->wait_4_mlo_maint_flg) {
2211 phba->wait_4_mlo_maint_flg = 0;
2212 wake_up_interruptible(&phba->wait_4_mlo_m_q);
3163f725 2213 }
84774a4d
JS
2214 }
2215
2216 if (la->fa) {
2217 if (la->mm)
2218 lpfc_issue_clear_la(phba, vport);
2219 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
2220 "1311 fa %d\n", la->fa);
dea3101e 2221 }
2222
2223lpfc_mbx_cmpl_read_la_free_mbuf:
2224 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2225 kfree(mp);
2226 mempool_free(pmb, phba->mbox_mem_pool);
2227 return;
2228}
2229
2230/*
2231 * This routine handles processing a REG_LOGIN mailbox
2232 * command upon completion. It is setup in the LPFC_MBOXQ
2233 * as the completion routine when the command is
2234 * handed off to the SLI layer.
2235 */
2236void
2e0fef85 2237lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2238{
2e0fef85 2239 struct lpfc_vport *vport = pmb->vport;
92d7f7b0 2240 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2e0fef85 2241 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea3101e 2242
dea3101e 2243 pmb->context1 = NULL;
2244
2245 /* Good status, call state machine */
2e0fef85 2246 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea3101e 2247 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2248 kfree(mp);
2e0fef85 2249 mempool_free(pmb, phba->mbox_mem_pool);
fa4066b6
JS
2250 /* decrement the node reference count held for this callback
2251 * function.
2252 */
329f9bc7 2253 lpfc_nlp_put(ndlp);
dea3101e 2254
2255 return;
2256}
2257
92d7f7b0
JS
2258static void
2259lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2260{
04c68496 2261 MAILBOX_t *mb = &pmb->u.mb;
92d7f7b0
JS
2262 struct lpfc_vport *vport = pmb->vport;
2263 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2264
2265 switch (mb->mbxStatus) {
2266 case 0x0011:
2267 case 0x0020:
2268 case 0x9700:
e8b62011
JS
2269 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2270 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
2271 mb->mbxStatus);
92d7f7b0 2272 break;
92d7f7b0 2273 }
97957244 2274 spin_lock_irq(&phba->hbalock);
c868595d 2275 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
1987807d 2276 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
97957244 2277 spin_unlock_irq(&phba->hbalock);
92d7f7b0
JS
2278 vport->unreg_vpi_cmpl = VPORT_OK;
2279 mempool_free(pmb, phba->mbox_mem_pool);
2280 /*
2281 * This shost reference might have been taken at the beginning of
2282 * lpfc_vport_delete()
2283 */
1c6f4ef5 2284 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
92d7f7b0
JS
2285 scsi_host_put(shost);
2286}
2287
d7c255b2 2288int
92d7f7b0
JS
2289lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
2290{
2291 struct lpfc_hba *phba = vport->phba;
2292 LPFC_MBOXQ_t *mbox;
2293 int rc;
2294
2295 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2296 if (!mbox)
d7c255b2 2297 return 1;
92d7f7b0
JS
2298
2299 lpfc_unreg_vpi(phba, vport->vpi, mbox);
2300 mbox->vport = vport;
2301 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
0b727fea 2302 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
92d7f7b0 2303 if (rc == MBX_NOT_FINISHED) {
e8b62011
JS
2304 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2305 "1800 Could not issue unreg_vpi\n");
92d7f7b0
JS
2306 mempool_free(mbox, phba->mbox_mem_pool);
2307 vport->unreg_vpi_cmpl = VPORT_ERROR;
d7c255b2 2308 return rc;
92d7f7b0 2309 }
d7c255b2 2310 return 0;
92d7f7b0
JS
2311}
2312
2313static void
2314lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2315{
2316 struct lpfc_vport *vport = pmb->vport;
2317 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
04c68496 2318 MAILBOX_t *mb = &pmb->u.mb;
92d7f7b0
JS
2319
2320 switch (mb->mbxStatus) {
2321 case 0x0011:
2322 case 0x9601:
2323 case 0x9602:
e8b62011
JS
2324 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2325 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
2326 mb->mbxStatus);
92d7f7b0
JS
2327 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2328 spin_lock_irq(shost->host_lock);
2329 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2330 spin_unlock_irq(shost->host_lock);
2331 vport->fc_myDID = 0;
2332 goto out;
2333 }
92d7f7b0 2334
c868595d 2335 vport->vpi_state |= LPFC_VPI_REGISTERED;
92d7f7b0
JS
2336 vport->num_disc_nodes = 0;
2337 /* go thru NPR list and issue ELS PLOGIs */
2338 if (vport->fc_npr_cnt)
2339 lpfc_els_disc_plogi(vport);
2340
2341 if (!vport->num_disc_nodes) {
2342 spin_lock_irq(shost->host_lock);
2343 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2344 spin_unlock_irq(shost->host_lock);
2345 lpfc_can_disctmo(vport);
2346 }
2347 vport->port_state = LPFC_VPORT_READY;
2348
2349out:
2350 mempool_free(pmb, phba->mbox_mem_pool);
2351 return;
2352}
2353
21e9a0a5
JS
2354/**
2355 * lpfc_create_static_vport - Read HBA config region to create static vports.
2356 * @phba: pointer to lpfc hba data structure.
2357 *
2358 * This routine issue a DUMP mailbox command for config region 22 to get
2359 * the list of static vports to be created. The function create vports
2360 * based on the information returned from the HBA.
2361 **/
2362void
2363lpfc_create_static_vport(struct lpfc_hba *phba)
2364{
2365 LPFC_MBOXQ_t *pmb = NULL;
2366 MAILBOX_t *mb;
2367 struct static_vport_info *vport_info;
1c6834a7 2368 int rc = 0, i;
21e9a0a5
JS
2369 struct fc_vport_identifiers vport_id;
2370 struct fc_vport *new_fc_vport;
2371 struct Scsi_Host *shost;
2372 struct lpfc_vport *vport;
2373 uint16_t offset = 0;
2374 uint8_t *vport_buff;
1c6834a7
JS
2375 struct lpfc_dmabuf *mp;
2376 uint32_t byte_count = 0;
21e9a0a5
JS
2377
2378 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2379 if (!pmb) {
2380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2381 "0542 lpfc_create_static_vport failed to"
2382 " allocate mailbox memory\n");
2383 return;
2384 }
2385
2386 mb = &pmb->u.mb;
2387
2388 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
2389 if (!vport_info) {
2390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2391 "0543 lpfc_create_static_vport failed to"
2392 " allocate vport_info\n");
2393 mempool_free(pmb, phba->mbox_mem_pool);
2394 return;
2395 }
2396
2397 vport_buff = (uint8_t *) vport_info;
2398 do {
1c6834a7
JS
2399 if (lpfc_dump_static_vport(phba, pmb, offset))
2400 goto out;
2401
21e9a0a5
JS
2402 pmb->vport = phba->pport;
2403 rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
2404
2405 if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
2406 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2407 "0544 lpfc_create_static_vport failed to"
2408 " issue dump mailbox command ret 0x%x "
2409 "status 0x%x\n",
2410 rc, mb->mbxStatus);
2411 goto out;
2412 }
2413
1c6834a7
JS
2414 if (phba->sli_rev == LPFC_SLI_REV4) {
2415 byte_count = pmb->u.mqe.un.mb_words[5];
2416 mp = (struct lpfc_dmabuf *) pmb->context2;
2417 if (byte_count > sizeof(struct static_vport_info) -
2418 offset)
2419 byte_count = sizeof(struct static_vport_info)
2420 - offset;
2421 memcpy(vport_buff + offset, mp->virt, byte_count);
2422 offset += byte_count;
2423 } else {
2424 if (mb->un.varDmp.word_cnt >
2425 sizeof(struct static_vport_info) - offset)
2426 mb->un.varDmp.word_cnt =
2427 sizeof(struct static_vport_info)
2428 - offset;
2429 byte_count = mb->un.varDmp.word_cnt;
2430 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
2431 vport_buff + offset,
2432 byte_count);
2433
2434 offset += byte_count;
2435 }
21e9a0a5 2436
1c6834a7 2437 } while (byte_count &&
21e9a0a5
JS
2438 offset < sizeof(struct static_vport_info));
2439
2440
2441 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
2442 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
2443 != VPORT_INFO_REV)) {
2444 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2445 "0545 lpfc_create_static_vport bad"
2446 " information header 0x%x 0x%x\n",
2447 le32_to_cpu(vport_info->signature),
2448 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
2449
2450 goto out;
2451 }
2452
2453 shost = lpfc_shost_from_vport(phba->pport);
2454
2455 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
2456 memset(&vport_id, 0, sizeof(vport_id));
2457 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
2458 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
2459 if (!vport_id.port_name || !vport_id.node_name)
2460 continue;
2461
2462 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
2463 vport_id.vport_type = FC_PORTTYPE_NPIV;
2464 vport_id.disable = false;
2465 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
2466
2467 if (!new_fc_vport) {
2468 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2469 "0546 lpfc_create_static_vport failed to"
e4e74273 2470 " create vport\n");
21e9a0a5
JS
2471 continue;
2472 }
2473
2474 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
2475 vport->vport_flag |= STATIC_VPORT;
2476 }
2477
2478out:
21e9a0a5 2479 kfree(vport_info);
1c6834a7
JS
2480 if (rc != MBX_TIMEOUT) {
2481 if (pmb->context2) {
2482 mp = (struct lpfc_dmabuf *) pmb->context2;
2483 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2484 kfree(mp);
2485 }
21e9a0a5 2486 mempool_free(pmb, phba->mbox_mem_pool);
1c6834a7 2487 }
21e9a0a5
JS
2488
2489 return;
2490}
2491
dea3101e 2492/*
2493 * This routine handles processing a Fabric REG_LOGIN mailbox
2494 * command upon completion. It is setup in the LPFC_MBOXQ
2495 * as the completion routine when the command is
2496 * handed off to the SLI layer.
2497 */
2498void
2e0fef85 2499lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2500{
92d7f7b0 2501 struct lpfc_vport *vport = pmb->vport;
21e9a0a5 2502 MAILBOX_t *mb = &pmb->u.mb;
2e0fef85 2503 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
92d7f7b0 2504 struct lpfc_nodelist *ndlp;
dea3101e 2505
549e55cd 2506 ndlp = (struct lpfc_nodelist *) pmb->context2;
329f9bc7
JS
2507 pmb->context1 = NULL;
2508 pmb->context2 = NULL;
dea3101e 2509 if (mb->mbxStatus) {
21e9a0a5
JS
2510 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2511 "0258 Register Fabric login error: 0x%x\n",
2512 mb->mbxStatus);
dea3101e 2513 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2514 kfree(mp);
329f9bc7 2515 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2516
92d7f7b0
JS
2517 if (phba->fc_topology == TOPOLOGY_LOOP) {
2518 /* FLOGI failed, use loop map to make discovery list */
2519 lpfc_disc_list_loopmap(vport);
2520
2521 /* Start discovery */
2522 lpfc_disc_start(vport);
e47c9093
JS
2523 /* Decrement the reference count to ndlp after the
2524 * reference to the ndlp are done.
2525 */
2526 lpfc_nlp_put(ndlp);
92d7f7b0
JS
2527 return;
2528 }
2529
2530 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e47c9093
JS
2531 /* Decrement the reference count to ndlp after the reference
2532 * to the ndlp are done.
2533 */
2534 lpfc_nlp_put(ndlp);
dea3101e 2535 return;
2536 }
2537
dea3101e 2538 ndlp->nlp_rpi = mb->un.varWords[0];
21e9a0a5 2539 ndlp->nlp_flag |= NLP_RPI_VALID;
dea3101e 2540 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 2541 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 2542
2e0fef85 2543 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
21e9a0a5 2544 lpfc_start_fdiscs(phba);
92d7f7b0 2545 lpfc_do_scr_ns_plogi(phba, vport);
dea3101e 2546 }
2547
2548 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2549 kfree(mp);
329f9bc7 2550 mempool_free(pmb, phba->mbox_mem_pool);
e47c9093
JS
2551
2552 /* Drop the reference count from the mbox at the end after
2553 * all the current reference to the ndlp have been done.
2554 */
2555 lpfc_nlp_put(ndlp);
dea3101e 2556 return;
2557}
2558
2559/*
2560 * This routine handles processing a NameServer REG_LOGIN mailbox
2561 * command upon completion. It is setup in the LPFC_MBOXQ
2562 * as the completion routine when the command is
2563 * handed off to the SLI layer.
2564 */
2565void
2e0fef85 2566lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2567{
21e9a0a5 2568 MAILBOX_t *mb = &pmb->u.mb;
2e0fef85
JS
2569 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2570 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2571 struct lpfc_vport *vport = pmb->vport;
dea3101e 2572
2573 if (mb->mbxStatus) {
92d7f7b0 2574out:
21e9a0a5
JS
2575 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2576 "0260 Register NameServer error: 0x%x\n",
2577 mb->mbxStatus);
fa4066b6
JS
2578 /* decrement the node reference count held for this
2579 * callback function.
2580 */
329f9bc7 2581 lpfc_nlp_put(ndlp);
dea3101e 2582 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2583 kfree(mp);
de0c5b32 2584 mempool_free(pmb, phba->mbox_mem_pool);
87af33fe
JS
2585
2586 /* If no other thread is using the ndlp, free it */
2587 lpfc_nlp_not_used(ndlp);
dea3101e 2588
92d7f7b0
JS
2589 if (phba->fc_topology == TOPOLOGY_LOOP) {
2590 /*
2591 * RegLogin failed, use loop map to make discovery
2592 * list
2593 */
2594 lpfc_disc_list_loopmap(vport);
dea3101e 2595
92d7f7b0
JS
2596 /* Start discovery */
2597 lpfc_disc_start(vport);
2598 return;
2599 }
2600 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea3101e 2601 return;
2602 }
2603
2604 pmb->context1 = NULL;
2605
dea3101e 2606 ndlp->nlp_rpi = mb->un.varWords[0];
21e9a0a5 2607 ndlp->nlp_flag |= NLP_RPI_VALID;
dea3101e 2608 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 2609 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 2610
2e0fef85
JS
2611 if (vport->port_state < LPFC_VPORT_READY) {
2612 /* Link up discovery requires Fabric registration. */
92d7f7b0
JS
2613 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
2614 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
2615 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
2616 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
2617 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
2618
2619 /* Issue SCR just before NameServer GID_FT Query */
2620 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea3101e 2621 }
2622
2e0fef85 2623 vport->fc_ns_retry = 0;
dea3101e 2624 /* Good status, issue CT Request to NameServer */
92d7f7b0 2625 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea3101e 2626 /* Cannot issue NameServer Query, so finish up discovery */
92d7f7b0 2627 goto out;
dea3101e 2628 }
2629
fa4066b6
JS
2630 /* decrement the node reference count held for this
2631 * callback function.
2632 */
329f9bc7 2633 lpfc_nlp_put(ndlp);
dea3101e 2634 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2635 kfree(mp);
2e0fef85 2636 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2637
2638 return;
2639}
2640
2641static void
2e0fef85 2642lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 2643{
2e0fef85
JS
2644 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2645 struct fc_rport *rport;
dea3101e 2646 struct lpfc_rport_data *rdata;
2647 struct fc_rport_identifiers rport_ids;
2e0fef85 2648 struct lpfc_hba *phba = vport->phba;
dea3101e 2649
2650 /* Remote port has reappeared. Re-register w/ FC transport */
68ce1eb5
AM
2651 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2652 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea3101e 2653 rport_ids.port_id = ndlp->nlp_DID;
2654 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea3101e 2655
329f9bc7
JS
2656 /*
2657 * We leave our node pointer in rport->dd_data when we unregister a
2658 * FCP target port. But fc_remote_port_add zeros the space to which
2659 * rport->dd_data points. So, if we're reusing a previously
2660 * registered port, drop the reference that we took the last time we
2661 * registered the port.
2662 */
2663 if (ndlp->rport && ndlp->rport->dd_data &&
e47c9093 2664 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
329f9bc7 2665 lpfc_nlp_put(ndlp);
858c9f6c
JS
2666
2667 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
2668 "rport add: did:x%x flg:x%x type x%x",
2669 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2670
2e0fef85 2671 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
329f9bc7 2672 if (!rport || !get_device(&rport->dev)) {
dea3101e 2673 dev_printk(KERN_WARNING, &phba->pcidev->dev,
2674 "Warning: fc_remote_port_add failed\n");
2675 return;
2676 }
2677
2678 /* initialize static port data */
2679 rport->maxframe_size = ndlp->nlp_maxframe;
2680 rport->supported_classes = ndlp->nlp_class_sup;
dea3101e 2681 rdata = rport->dd_data;
329f9bc7 2682 rdata->pnode = lpfc_nlp_get(ndlp);
23dc04f1
JSEC
2683
2684 if (ndlp->nlp_type & NLP_FCP_TARGET)
2685 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
2686 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
2687 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
2688
2689
2690 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
2691 fc_remote_port_rolechg(rport, rport_ids.roles);
2692
071fbd3d 2693 if ((rport->scsi_target_id != -1) &&
92d7f7b0 2694 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
071fbd3d
JS
2695 ndlp->nlp_sid = rport->scsi_target_id;
2696 }
19a7b4ae
JSEC
2697 return;
2698}
2699
2700static void
2e0fef85 2701lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
19a7b4ae
JSEC
2702{
2703 struct fc_rport *rport = ndlp->rport;
c01f3208 2704
858c9f6c
JS
2705 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
2706 "rport delete: did:x%x flg:x%x type x%x",
2707 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
2708
19a7b4ae 2709 fc_remote_port_delete(rport);
dea3101e 2710
2711 return;
2712}
2713
de0c5b32 2714static void
2e0fef85 2715lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
dea3101e 2716{
2e0fef85
JS
2717 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2718
2719 spin_lock_irq(shost->host_lock);
de0c5b32
JS
2720 switch (state) {
2721 case NLP_STE_UNUSED_NODE:
2e0fef85 2722 vport->fc_unused_cnt += count;
de0c5b32
JS
2723 break;
2724 case NLP_STE_PLOGI_ISSUE:
2e0fef85 2725 vport->fc_plogi_cnt += count;
de0c5b32
JS
2726 break;
2727 case NLP_STE_ADISC_ISSUE:
2e0fef85 2728 vport->fc_adisc_cnt += count;
dea3101e 2729 break;
de0c5b32 2730 case NLP_STE_REG_LOGIN_ISSUE:
2e0fef85 2731 vport->fc_reglogin_cnt += count;
de0c5b32
JS
2732 break;
2733 case NLP_STE_PRLI_ISSUE:
2e0fef85 2734 vport->fc_prli_cnt += count;
de0c5b32
JS
2735 break;
2736 case NLP_STE_UNMAPPED_NODE:
2e0fef85 2737 vport->fc_unmap_cnt += count;
de0c5b32
JS
2738 break;
2739 case NLP_STE_MAPPED_NODE:
2e0fef85 2740 vport->fc_map_cnt += count;
de0c5b32
JS
2741 break;
2742 case NLP_STE_NPR_NODE:
2e0fef85 2743 vport->fc_npr_cnt += count;
de0c5b32
JS
2744 break;
2745 }
2e0fef85 2746 spin_unlock_irq(shost->host_lock);
de0c5b32 2747}
66a9ed66 2748
de0c5b32 2749static void
2e0fef85 2750lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
de0c5b32
JS
2751 int old_state, int new_state)
2752{
2e0fef85
JS
2753 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2754
de0c5b32
JS
2755 if (new_state == NLP_STE_UNMAPPED_NODE) {
2756 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2757 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2758 ndlp->nlp_type |= NLP_FC_NODE;
2759 }
2760 if (new_state == NLP_STE_MAPPED_NODE)
2761 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
2762 if (new_state == NLP_STE_NPR_NODE)
2763 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
2764
2765 /* Transport interface */
2766 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
2767 old_state == NLP_STE_UNMAPPED_NODE)) {
2e0fef85
JS
2768 vport->phba->nport_event_cnt++;
2769 lpfc_unregister_remote_port(ndlp);
de0c5b32 2770 }
dea3101e 2771
de0c5b32
JS
2772 if (new_state == NLP_STE_MAPPED_NODE ||
2773 new_state == NLP_STE_UNMAPPED_NODE) {
2e0fef85 2774 vport->phba->nport_event_cnt++;
858c9f6c
JS
2775 /*
2776 * Tell the fc transport about the port, if we haven't
2777 * already. If we have, and it's a scsi entity, be
2778 * sure to unblock any attached scsi devices
2779 */
2780 lpfc_register_remote_port(vport, ndlp);
de0c5b32 2781 }
ea2151b4
JS
2782 if ((new_state == NLP_STE_MAPPED_NODE) &&
2783 (vport->stat_data_enabled)) {
2784 /*
2785 * A new target is discovered, if there is no buffer for
2786 * statistical data collection allocate buffer.
2787 */
2788 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
2789 sizeof(struct lpfc_scsicmd_bkt),
2790 GFP_KERNEL);
2791
2792 if (!ndlp->lat_data)
2793 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
2794 "0286 lpfc_nlp_state_cleanup failed to "
2795 "allocate statistical data buffer DID "
2796 "0x%x\n", ndlp->nlp_DID);
2797 }
858c9f6c
JS
2798 /*
2799 * if we added to Mapped list, but the remote port
2800 * registration failed or assigned a target id outside
2801 * our presentable range - move the node to the
2802 * Unmapped List
2803 */
de0c5b32
JS
2804 if (new_state == NLP_STE_MAPPED_NODE &&
2805 (!ndlp->rport ||
2806 ndlp->rport->scsi_target_id == -1 ||
2807 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2e0fef85 2808 spin_lock_irq(shost->host_lock);
de0c5b32 2809 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2e0fef85
JS
2810 spin_unlock_irq(shost->host_lock);
2811 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 2812 }
de0c5b32
JS
2813}
2814
685f0bf7
JS
2815static char *
2816lpfc_nlp_state_name(char *buffer, size_t size, int state)
2817{
2818 static char *states[] = {
2819 [NLP_STE_UNUSED_NODE] = "UNUSED",
2820 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
2821 [NLP_STE_ADISC_ISSUE] = "ADISC",
2822 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
2823 [NLP_STE_PRLI_ISSUE] = "PRLI",
2824 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
2825 [NLP_STE_MAPPED_NODE] = "MAPPED",
2826 [NLP_STE_NPR_NODE] = "NPR",
2827 };
2828
311464ec 2829 if (state < NLP_STE_MAX_STATE && states[state])
685f0bf7
JS
2830 strlcpy(buffer, states[state], size);
2831 else
2832 snprintf(buffer, size, "unknown (%d)", state);
2833 return buffer;
2834}
2835
de0c5b32 2836void
2e0fef85
JS
2837lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2838 int state)
de0c5b32 2839{
2e0fef85 2840 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
de0c5b32 2841 int old_state = ndlp->nlp_state;
685f0bf7 2842 char name1[16], name2[16];
de0c5b32 2843
e8b62011
JS
2844 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2845 "0904 NPort state transition x%06x, %s -> %s\n",
2846 ndlp->nlp_DID,
2847 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
2848 lpfc_nlp_state_name(name2, sizeof(name2), state));
858c9f6c
JS
2849
2850 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2851 "node statechg did:x%x old:%d ste:%d",
2852 ndlp->nlp_DID, old_state, state);
2853
de0c5b32 2854 if (old_state == NLP_STE_NPR_NODE &&
de0c5b32 2855 state != NLP_STE_NPR_NODE)
2e0fef85 2856 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32
JS
2857 if (old_state == NLP_STE_UNMAPPED_NODE) {
2858 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
2859 ndlp->nlp_type &= ~NLP_FC_NODE;
2860 }
2861
685f0bf7 2862 if (list_empty(&ndlp->nlp_listp)) {
2e0fef85
JS
2863 spin_lock_irq(shost->host_lock);
2864 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2865 spin_unlock_irq(shost->host_lock);
685f0bf7 2866 } else if (old_state)
2e0fef85 2867 lpfc_nlp_counters(vport, old_state, -1);
de0c5b32
JS
2868
2869 ndlp->nlp_state = state;
2e0fef85
JS
2870 lpfc_nlp_counters(vport, state, 1);
2871 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
de0c5b32
JS
2872}
2873
e47c9093
JS
2874void
2875lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2876{
2877 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2878
2879 if (list_empty(&ndlp->nlp_listp)) {
2880 spin_lock_irq(shost->host_lock);
2881 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
2882 spin_unlock_irq(shost->host_lock);
2883 }
2884}
2885
de0c5b32 2886void
2e0fef85 2887lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 2888{
2e0fef85
JS
2889 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2890
0d2b6b83 2891 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32 2892 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2e0fef85
JS
2893 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2894 spin_lock_irq(shost->host_lock);
685f0bf7 2895 list_del_init(&ndlp->nlp_listp);
2e0fef85 2896 spin_unlock_irq(shost->host_lock);
858c9f6c 2897 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
e47c9093
JS
2898 NLP_STE_UNUSED_NODE);
2899}
2900
4d9db01e 2901static void
e47c9093
JS
2902lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2903{
0d2b6b83 2904 lpfc_cancel_retry_delay_tmo(vport, ndlp);
e47c9093
JS
2905 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2906 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
2907 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
2908 NLP_STE_UNUSED_NODE);
2909}
109f6ed0 2910/**
3621a710 2911 * lpfc_initialize_node - Initialize all fields of node object
109f6ed0
JS
2912 * @vport: Pointer to Virtual Port object.
2913 * @ndlp: Pointer to FC node object.
2914 * @did: FC_ID of the node.
a257bf90
JS
2915 *
2916 * This function is always called when node object need to be initialized.
2917 * It initializes all the fields of the node object. Although the reference
2918 * to phba from @ndlp can be obtained indirectly through it's reference to
2919 * @vport, a direct reference to phba is taken here by @ndlp. This is due
2920 * to the life-span of the @ndlp might go beyond the existence of @vport as
2921 * the final release of ndlp is determined by its reference count. And, the
2922 * operation on @ndlp needs the reference to phba.
109f6ed0
JS
2923 **/
2924static inline void
2925lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2926 uint32_t did)
2927{
2928 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2929 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2930 init_timer(&ndlp->nlp_delayfunc);
2931 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2932 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2933 ndlp->nlp_DID = did;
2934 ndlp->vport = vport;
a257bf90 2935 ndlp->phba = vport->phba;
109f6ed0
JS
2936 ndlp->nlp_sid = NLP_NO_SID;
2937 kref_init(&ndlp->kref);
2938 NLP_INT_NODE_ACT(ndlp);
2939 atomic_set(&ndlp->cmd_pending, 0);
2940 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2941}
e47c9093
JS
2942
2943struct lpfc_nodelist *
2944lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2945 int state)
2946{
2947 struct lpfc_hba *phba = vport->phba;
2948 uint32_t did;
2949 unsigned long flags;
2950
2951 if (!ndlp)
2952 return NULL;
2953
2954 spin_lock_irqsave(&phba->ndlp_lock, flags);
2955 /* The ndlp should not be in memory free mode */
2956 if (NLP_CHK_FREE_REQ(ndlp)) {
2957 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2958 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2959 "0277 lpfc_enable_node: ndlp:x%p "
2960 "usgmap:x%x refcnt:%d\n",
2961 (void *)ndlp, ndlp->nlp_usg_map,
2962 atomic_read(&ndlp->kref.refcount));
2963 return NULL;
2964 }
2965 /* The ndlp should not already be in active mode */
2966 if (NLP_CHK_NODE_ACT(ndlp)) {
2967 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2968 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2969 "0278 lpfc_enable_node: ndlp:x%p "
2970 "usgmap:x%x refcnt:%d\n",
2971 (void *)ndlp, ndlp->nlp_usg_map,
2972 atomic_read(&ndlp->kref.refcount));
2973 return NULL;
2974 }
2975
2976 /* Keep the original DID */
2977 did = ndlp->nlp_DID;
2978
2979 /* re-initialize ndlp except of ndlp linked list pointer */
2980 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
2981 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
109f6ed0 2982 lpfc_initialize_node(vport, ndlp, did);
e47c9093
JS
2983
2984 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
2985
2986 if (state != NLP_STE_UNUSED_NODE)
2987 lpfc_nlp_set_state(vport, ndlp, state);
2988
2989 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
2990 "node enable: did:x%x",
2991 ndlp->nlp_DID, 0, 0);
2992 return ndlp;
de0c5b32
JS
2993}
2994
2995void
2e0fef85 2996lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 2997{
87af33fe 2998 /*
fa4066b6 2999 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
87af33fe 3000 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
fa4066b6
JS
3001 * the ndlp from the vport. The ndlp marked as UNUSED on the list
3002 * until ALL other outstanding threads have completed. We check
3003 * that the ndlp not already in the UNUSED state before we proceed.
87af33fe 3004 */
fa4066b6
JS
3005 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
3006 return;
51ef4c26 3007 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
87af33fe 3008 lpfc_nlp_put(ndlp);
98c9ea5c 3009 return;
dea3101e 3010}
3011
3012/*
3013 * Start / ReStart rescue timer for Discovery / RSCN handling
3014 */
3015void
2e0fef85 3016lpfc_set_disctmo(struct lpfc_vport *vport)
dea3101e 3017{
2e0fef85
JS
3018 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3019 struct lpfc_hba *phba = vport->phba;
dea3101e 3020 uint32_t tmo;
3021
2e0fef85 3022 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
025dfdaf 3023 /* For FAN, timeout should be greater than edtov */
c9f8735b
JW
3024 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
3025 } else {
025dfdaf 3026 /* Normal discovery timeout should be > than ELS/CT timeout
c9f8735b
JW
3027 * FC spec states we need 3 * ratov for CT requests
3028 */
3029 tmo = ((phba->fc_ratov * 3) + 3);
3030 }
dea3101e 3031
858c9f6c
JS
3032
3033 if (!timer_pending(&vport->fc_disctmo)) {
3034 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3035 "set disc timer: tmo:x%x state:x%x flg:x%x",
3036 tmo, vport->port_state, vport->fc_flag);
3037 }
3038
2e0fef85
JS
3039 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
3040 spin_lock_irq(shost->host_lock);
3041 vport->fc_flag |= FC_DISC_TMO;
3042 spin_unlock_irq(shost->host_lock);
dea3101e 3043
3044 /* Start Discovery Timer state <hba_state> */
e8b62011
JS
3045 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3046 "0247 Start Discovery Timer state x%x "
3047 "Data: x%x x%lx x%x x%x\n",
3048 vport->port_state, tmo,
3049 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
3050 vport->fc_adisc_cnt);
dea3101e 3051
3052 return;
3053}
3054
3055/*
3056 * Cancel rescue timer for Discovery / RSCN handling
3057 */
3058int
2e0fef85 3059lpfc_can_disctmo(struct lpfc_vport *vport)
dea3101e 3060{
2e0fef85 3061 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2e0fef85
JS
3062 unsigned long iflags;
3063
858c9f6c
JS
3064 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3065 "can disc timer: state:x%x rtry:x%x flg:x%x",
3066 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3067
dea3101e 3068 /* Turn off discovery timer if its running */
2e0fef85
JS
3069 if (vport->fc_flag & FC_DISC_TMO) {
3070 spin_lock_irqsave(shost->host_lock, iflags);
3071 vport->fc_flag &= ~FC_DISC_TMO;
3072 spin_unlock_irqrestore(shost->host_lock, iflags);
3073 del_timer_sync(&vport->fc_disctmo);
3074 spin_lock_irqsave(&vport->work_port_lock, iflags);
3075 vport->work_port_events &= ~WORKER_DISC_TMO;
3076 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea3101e 3077 }
3078
3079 /* Cancel Discovery Timer state <hba_state> */
e8b62011
JS
3080 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3081 "0248 Cancel Discovery Timer state x%x "
3082 "Data: x%x x%x x%x\n",
3083 vport->port_state, vport->fc_flag,
3084 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
2fe165b6 3085 return 0;
dea3101e 3086}
3087
3088/*
3089 * Check specified ring for outstanding IOCB on the SLI queue
3090 * Return true if iocb matches the specified nport
3091 */
3092int
2e0fef85
JS
3093lpfc_check_sli_ndlp(struct lpfc_hba *phba,
3094 struct lpfc_sli_ring *pring,
3095 struct lpfc_iocbq *iocb,
3096 struct lpfc_nodelist *ndlp)
dea3101e 3097{
2e0fef85
JS
3098 struct lpfc_sli *psli = &phba->sli;
3099 IOCB_t *icmd = &iocb->iocb;
92d7f7b0
JS
3100 struct lpfc_vport *vport = ndlp->vport;
3101
3102 if (iocb->vport != vport)
3103 return 0;
3104
dea3101e 3105 if (pring->ringno == LPFC_ELS_RING) {
3106 switch (icmd->ulpCommand) {
3107 case CMD_GEN_REQUEST64_CR:
21e9a0a5 3108 if (iocb->context_un.ndlp == ndlp)
2fe165b6 3109 return 1;
dea3101e 3110 case CMD_ELS_REQUEST64_CR:
10d4e957
JS
3111 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
3112 return 1;
dea3101e 3113 case CMD_XMIT_ELS_RSP64_CX:
3114 if (iocb->context1 == (uint8_t *) ndlp)
2fe165b6 3115 return 1;
dea3101e 3116 }
a4bc3379 3117 } else if (pring->ringno == psli->extra_ring) {
dea3101e 3118
3119 } else if (pring->ringno == psli->fcp_ring) {
3120 /* Skip match check if waiting to relogin to FCP target */
3121 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
92d7f7b0 3122 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
2fe165b6 3123 return 0;
dea3101e 3124 }
3125 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
2fe165b6 3126 return 1;
dea3101e 3127 }
3128 } else if (pring->ringno == psli->next_ring) {
3129
3130 }
2fe165b6 3131 return 0;
dea3101e 3132}
3133
3134/*
3135 * Free resources / clean up outstanding I/Os
3136 * associated with nlp_rpi in the LPFC_NODELIST entry.
3137 */
3138static int
2e0fef85 3139lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 3140{
2534ba75 3141 LIST_HEAD(completions);
dea3101e 3142 struct lpfc_sli *psli;
3143 struct lpfc_sli_ring *pring;
3144 struct lpfc_iocbq *iocb, *next_iocb;
6a9c52cf 3145 uint32_t i;
dea3101e 3146
92d7f7b0
JS
3147 lpfc_fabric_abort_nport(ndlp);
3148
dea3101e 3149 /*
3150 * Everything that matches on txcmplq will be returned
3151 * by firmware with a no rpi error.
3152 */
3153 psli = &phba->sli;
21e9a0a5 3154 if (ndlp->nlp_flag & NLP_RPI_VALID) {
dea3101e 3155 /* Now process each ring */
3156 for (i = 0; i < psli->num_rings; i++) {
3157 pring = &psli->ring[i];
3158
2e0fef85 3159 spin_lock_irq(&phba->hbalock);
dea3101e 3160 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
2e0fef85 3161 list) {
dea3101e 3162 /*
3163 * Check to see if iocb matches the nport we are
3164 * looking for
3165 */
92d7f7b0
JS
3166 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
3167 ndlp))) {
dea3101e 3168 /* It matches, so deque and call compl
3169 with an error */
2534ba75
JS
3170 list_move_tail(&iocb->list,
3171 &completions);
dea3101e 3172 pring->txq_cnt--;
dea3101e 3173 }
3174 }
2e0fef85 3175 spin_unlock_irq(&phba->hbalock);
dea3101e 3176 }
3177 }
2534ba75 3178
a257bf90
JS
3179 /* Cancel all the IOCBs from the completions list */
3180 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3181 IOERR_SLI_ABORTED);
2534ba75 3182
2fe165b6 3183 return 0;
dea3101e 3184}
3185
3186/*
3187 * Free rpi associated with LPFC_NODELIST entry.
3188 * This routine is called from lpfc_freenode(), when we are removing
3189 * a LPFC_NODELIST entry. It is also called if the driver initiates a
3190 * LOGO that completes successfully, and we are waiting to PLOGI back
3191 * to the remote NPort. In addition, it is called after we receive
3192 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
3193 * we are waiting to PLOGI back to the remote NPort.
3194 */
3195int
2e0fef85 3196lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 3197{
2e0fef85
JS
3198 struct lpfc_hba *phba = vport->phba;
3199 LPFC_MBOXQ_t *mbox;
dea3101e 3200 int rc;
3201
21e9a0a5 3202 if (ndlp->nlp_flag & NLP_RPI_VALID) {
2e0fef85
JS
3203 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3204 if (mbox) {
92d7f7b0 3205 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
ed957684 3206 mbox->vport = vport;
92d7f7b0 3207 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0b727fea 3208 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea3101e 3209 if (rc == MBX_NOT_FINISHED)
2e0fef85 3210 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 3211 }
dea3101e 3212 lpfc_no_rpi(phba, ndlp);
3213 ndlp->nlp_rpi = 0;
21e9a0a5 3214 ndlp->nlp_flag &= ~NLP_RPI_VALID;
0c287589 3215 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
dea3101e 3216 return 1;
3217 }
3218 return 0;
3219}
3220
92d7f7b0
JS
3221void
3222lpfc_unreg_all_rpis(struct lpfc_vport *vport)
3223{
3224 struct lpfc_hba *phba = vport->phba;
3225 LPFC_MBOXQ_t *mbox;
3226 int rc;
3227
3228 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3229 if (mbox) {
3230 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
3231 mbox->vport = vport;
3232 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
09372820
JS
3233 mbox->context1 = NULL;
3234 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
a257bf90 3235 if (rc != MBX_TIMEOUT)
92d7f7b0 3236 mempool_free(mbox, phba->mbox_mem_pool);
a257bf90
JS
3237
3238 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
3239 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3240 "1836 Could not issue "
3241 "unreg_login(all_rpis) status %d\n", rc);
92d7f7b0
JS
3242 }
3243}
3244
3245void
3246lpfc_unreg_default_rpis(struct lpfc_vport *vport)
3247{
3248 struct lpfc_hba *phba = vport->phba;
3249 LPFC_MBOXQ_t *mbox;
3250 int rc;
3251
3252 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3253 if (mbox) {
3254 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
3255 mbox->vport = vport;
3256 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
09372820
JS
3257 mbox->context1 = NULL;
3258 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
a257bf90
JS
3259 if (rc != MBX_TIMEOUT)
3260 mempool_free(mbox, phba->mbox_mem_pool);
3261
3262 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
e8b62011
JS
3263 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3264 "1815 Could not issue "
a257bf90
JS
3265 "unreg_did (default rpis) status %d\n",
3266 rc);
92d7f7b0
JS
3267 }
3268}
3269
dea3101e 3270/*
3271 * Free resources associated with LPFC_NODELIST entry
3272 * so it can be freed.
3273 */
3274static int
2e0fef85 3275lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 3276{
2e0fef85
JS
3277 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3278 struct lpfc_hba *phba = vport->phba;
3279 LPFC_MBOXQ_t *mb, *nextmb;
dea3101e 3280 struct lpfc_dmabuf *mp;
dea3101e 3281
3282 /* Cleanup node for NPort <nlp_DID> */
e8b62011
JS
3283 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3284 "0900 Cleanup node for NPort x%x "
3285 "Data: x%x x%x x%x\n",
3286 ndlp->nlp_DID, ndlp->nlp_flag,
3287 ndlp->nlp_state, ndlp->nlp_rpi);
e47c9093
JS
3288 if (NLP_CHK_FREE_REQ(ndlp)) {
3289 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3290 "0280 lpfc_cleanup_node: ndlp:x%p "
3291 "usgmap:x%x refcnt:%d\n",
3292 (void *)ndlp, ndlp->nlp_usg_map,
3293 atomic_read(&ndlp->kref.refcount));
3294 lpfc_dequeue_node(vport, ndlp);
3295 } else {
3296 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
3297 "0281 lpfc_cleanup_node: ndlp:x%p "
3298 "usgmap:x%x refcnt:%d\n",
3299 (void *)ndlp, ndlp->nlp_usg_map,
3300 atomic_read(&ndlp->kref.refcount));
3301 lpfc_disable_node(vport, ndlp);
3302 }
dea3101e 3303
dea3101e 3304 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
3305 if ((mb = phba->sli.mbox_active)) {
04c68496 3306 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
dea3101e 3307 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
3308 mb->context2 = NULL;
3309 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3310 }
3311 }
33ccf8d1 3312
2e0fef85 3313 spin_lock_irq(&phba->hbalock);
dea3101e 3314 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
04c68496 3315 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
92d7f7b0 3316 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea3101e 3317 mp = (struct lpfc_dmabuf *) (mb->context1);
3318 if (mp) {
2e0fef85 3319 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e 3320 kfree(mp);
3321 }
3322 list_del(&mb->list);
3323 mempool_free(mb, phba->mbox_mem_pool);
e47c9093
JS
3324 /* We shall not invoke the lpfc_nlp_put to decrement
3325 * the ndlp reference count as we are in the process
3326 * of lpfc_nlp_release.
3327 */
dea3101e 3328 }
3329 }
2e0fef85 3330 spin_unlock_irq(&phba->hbalock);
dea3101e 3331
e47c9093
JS
3332 lpfc_els_abort(phba, ndlp);
3333
2e0fef85 3334 spin_lock_irq(shost->host_lock);
c01f3208 3335 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 3336 spin_unlock_irq(shost->host_lock);
dea3101e 3337
5024ab17 3338 ndlp->nlp_last_elscmd = 0;
dea3101e 3339 del_timer_sync(&ndlp->nlp_delayfunc);
3340
0d2b6b83
JS
3341 list_del_init(&ndlp->els_retry_evt.evt_listp);
3342 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea3101e 3343
2e0fef85 3344 lpfc_unreg_rpi(vport, ndlp);
dea3101e 3345
2fe165b6 3346 return 0;
dea3101e 3347}
3348
3349/*
3350 * Check to see if we can free the nlp back to the freelist.
3351 * If we are in the middle of using the nlp in the discovery state
3352 * machine, defer the free till we reach the end of the state machine.
3353 */
329f9bc7 3354static void
2e0fef85 3355lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 3356{
a8adb832 3357 struct lpfc_hba *phba = vport->phba;
1dcb58e5 3358 struct lpfc_rport_data *rdata;
a8adb832
JS
3359 LPFC_MBOXQ_t *mbox;
3360 int rc;
dea3101e 3361
0d2b6b83 3362 lpfc_cancel_retry_delay_tmo(vport, ndlp);
21e9a0a5
JS
3363 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
3364 !(ndlp->nlp_flag & NLP_RPI_VALID)) {
a8adb832
JS
3365 /* For this case we need to cleanup the default rpi
3366 * allocated by the firmware.
3367 */
3368 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
3369 != NULL) {
21e9a0a5 3370 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
a8adb832
JS
3371 (uint8_t *) &vport->fc_sparam, mbox, 0);
3372 if (rc) {
3373 mempool_free(mbox, phba->mbox_mem_pool);
3374 }
3375 else {
3376 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3377 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3378 mbox->vport = vport;
09372820 3379 mbox->context2 = NULL;
a8adb832
JS
3380 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3381 if (rc == MBX_NOT_FINISHED) {
3382 mempool_free(mbox, phba->mbox_mem_pool);
3383 }
3384 }
3385 }
3386 }
2e0fef85 3387 lpfc_cleanup_node(vport, ndlp);
1dcb58e5 3388
2e0fef85 3389 /*
92d7f7b0
JS
3390 * We can get here with a non-NULL ndlp->rport because when we
3391 * unregister a rport we don't break the rport/node linkage. So if we
3392 * do, make sure we don't leaving any dangling pointers behind.
2e0fef85 3393 */
92d7f7b0 3394 if (ndlp->rport) {
329f9bc7
JS
3395 rdata = ndlp->rport->dd_data;
3396 rdata->pnode = NULL;
3397 ndlp->rport = NULL;
dea3101e 3398 }
dea3101e 3399}
3400
3401static int
2e0fef85
JS
3402lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3403 uint32_t did)
dea3101e 3404{
2e0fef85 3405 D_ID mydid, ndlpdid, matchdid;
dea3101e 3406
3407 if (did == Bcast_DID)
2fe165b6 3408 return 0;
dea3101e 3409
dea3101e 3410 /* First check for Direct match */
3411 if (ndlp->nlp_DID == did)
2fe165b6 3412 return 1;
dea3101e 3413
3414 /* Next check for area/domain identically equals 0 match */
2e0fef85 3415 mydid.un.word = vport->fc_myDID;
dea3101e 3416 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
2fe165b6 3417 return 0;
dea3101e 3418 }
3419
3420 matchdid.un.word = did;
3421 ndlpdid.un.word = ndlp->nlp_DID;
3422 if (matchdid.un.b.id == ndlpdid.un.b.id) {
3423 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
3424 (mydid.un.b.area == matchdid.un.b.area)) {
3425 if ((ndlpdid.un.b.domain == 0) &&
3426 (ndlpdid.un.b.area == 0)) {
3427 if (ndlpdid.un.b.id)
2fe165b6 3428 return 1;
dea3101e 3429 }
2fe165b6 3430 return 0;
dea3101e 3431 }
3432
3433 matchdid.un.word = ndlp->nlp_DID;
3434 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
3435 (mydid.un.b.area == ndlpdid.un.b.area)) {
3436 if ((matchdid.un.b.domain == 0) &&
3437 (matchdid.un.b.area == 0)) {
3438 if (matchdid.un.b.id)
2fe165b6 3439 return 1;
dea3101e 3440 }
3441 }
3442 }
2fe165b6 3443 return 0;
dea3101e 3444}
3445
685f0bf7 3446/* Search for a nodelist entry */
2e0fef85
JS
3447static struct lpfc_nodelist *
3448__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea3101e 3449{
2fb9bd8b 3450 struct lpfc_nodelist *ndlp;
dea3101e 3451 uint32_t data1;
3452
2e0fef85
JS
3453 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3454 if (lpfc_matchdid(vport, ndlp, did)) {
685f0bf7
JS
3455 data1 = (((uint32_t) ndlp->nlp_state << 24) |
3456 ((uint32_t) ndlp->nlp_xri << 16) |
3457 ((uint32_t) ndlp->nlp_type << 8) |
3458 ((uint32_t) ndlp->nlp_rpi & 0xff));
e8b62011
JS
3459 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3460 "0929 FIND node DID "
3461 "Data: x%p x%x x%x x%x\n",
3462 ndlp, ndlp->nlp_DID,
3463 ndlp->nlp_flag, data1);
685f0bf7 3464 return ndlp;
dea3101e 3465 }
3466 }
66a9ed66 3467
dea3101e 3468 /* FIND node did <did> NOT FOUND */
e8b62011
JS
3469 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3470 "0932 FIND node did x%x NOT FOUND.\n", did);
dea3101e 3471 return NULL;
3472}
3473
3474struct lpfc_nodelist *
2e0fef85
JS
3475lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
3476{
3477 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3478 struct lpfc_nodelist *ndlp;
3479
3480 spin_lock_irq(shost->host_lock);
3481 ndlp = __lpfc_findnode_did(vport, did);
3482 spin_unlock_irq(shost->host_lock);
3483 return ndlp;
3484}
3485
3486struct lpfc_nodelist *
3487lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
dea3101e 3488{
2e0fef85 3489 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 3490 struct lpfc_nodelist *ndlp;
dea3101e 3491
2e0fef85 3492 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 3493 if (!ndlp) {
2e0fef85
JS
3494 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
3495 lpfc_rscn_payload_check(vport, did) == 0)
dea3101e 3496 return NULL;
3497 ndlp = (struct lpfc_nodelist *)
2e0fef85 3498 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea3101e 3499 if (!ndlp)
3500 return NULL;
2e0fef85
JS
3501 lpfc_nlp_init(vport, ndlp, did);
3502 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3503 spin_lock_irq(shost->host_lock);
dea3101e 3504 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 3505 spin_unlock_irq(shost->host_lock);
dea3101e 3506 return ndlp;
e47c9093
JS
3507 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3508 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
3509 if (!ndlp)
3510 return NULL;
3511 spin_lock_irq(shost->host_lock);
3512 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3513 spin_unlock_irq(shost->host_lock);
3514 return ndlp;
dea3101e 3515 }
e47c9093 3516
58da1ffb
JS
3517 if ((vport->fc_flag & FC_RSCN_MODE) &&
3518 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
2e0fef85 3519 if (lpfc_rscn_payload_check(vport, did)) {
87af33fe
JS
3520 /* If we've already recieved a PLOGI from this NPort
3521 * we don't need to try to discover it again.
3522 */
3523 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
3524 return NULL;
3525
c9f8735b
JW
3526 /* Since this node is marked for discovery,
3527 * delay timeout is not needed.
3528 */
0d2b6b83 3529 lpfc_cancel_retry_delay_tmo(vport, ndlp);
a257bf90
JS
3530 spin_lock_irq(shost->host_lock);
3531 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
3532 spin_unlock_irq(shost->host_lock);
071fbd3d 3533 } else
dea3101e 3534 ndlp = NULL;
2fe165b6 3535 } else {
87af33fe
JS
3536 /* If we've already recieved a PLOGI from this NPort,
3537 * or we are already in the process of discovery on it,
3538 * we don't need to try to discover it again.
3539 */
685f0bf7 3540 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
87af33fe
JS
3541 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3542 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea3101e 3543 return NULL;
2e0fef85
JS
3544 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3545 spin_lock_irq(shost->host_lock);
dea3101e 3546 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 3547 spin_unlock_irq(shost->host_lock);
dea3101e 3548 }
3549 return ndlp;
3550}
3551
3552/* Build a list of nodes to discover based on the loopmap */
3553void
2e0fef85 3554lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea3101e 3555{
2e0fef85 3556 struct lpfc_hba *phba = vport->phba;
dea3101e 3557 int j;
3558 uint32_t alpa, index;
3559
2e0fef85 3560 if (!lpfc_is_link_up(phba))
dea3101e 3561 return;
2e0fef85
JS
3562
3563 if (phba->fc_topology != TOPOLOGY_LOOP)
dea3101e 3564 return;
dea3101e 3565
3566 /* Check for loop map present or not */
3567 if (phba->alpa_map[0]) {
3568 for (j = 1; j <= phba->alpa_map[0]; j++) {
3569 alpa = phba->alpa_map[j];
2e0fef85 3570 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea3101e 3571 continue;
2e0fef85 3572 lpfc_setup_disc_node(vport, alpa);
dea3101e 3573 }
3574 } else {
3575 /* No alpamap, so try all alpa's */
3576 for (j = 0; j < FC_MAXLOOP; j++) {
3577 /* If cfg_scan_down is set, start from highest
3578 * ALPA (0xef) to lowest (0x1).
3579 */
3de2a653 3580 if (vport->cfg_scan_down)
dea3101e 3581 index = j;
3582 else
3583 index = FC_MAXLOOP - j - 1;
3584 alpa = lpfcAlpaArray[index];
2e0fef85 3585 if ((vport->fc_myDID & 0xff) == alpa)
dea3101e 3586 continue;
2e0fef85 3587 lpfc_setup_disc_node(vport, alpa);
dea3101e 3588 }
3589 }
3590 return;
3591}
3592
dea3101e 3593void
2e0fef85 3594lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea3101e 3595{
dea3101e 3596 LPFC_MBOXQ_t *mbox;
2e0fef85
JS
3597 struct lpfc_sli *psli = &phba->sli;
3598 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
3599 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
3600 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
3601 int rc;
3602
92d7f7b0
JS
3603 /*
3604 * if it's not a physical port or if we already send
3605 * clear_la then don't send it.
3606 */
3607 if ((phba->link_state >= LPFC_CLEAR_LA) ||
da0436e9
JS
3608 (vport->port_type != LPFC_PHYSICAL_PORT) ||
3609 (phba->sli_rev == LPFC_SLI_REV4))
92d7f7b0
JS
3610 return;
3611
2e0fef85
JS
3612 /* Link up discovery */
3613 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
3614 phba->link_state = LPFC_CLEAR_LA;
3615 lpfc_clear_la(phba, mbox);
3616 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
3617 mbox->vport = vport;
0b727fea 3618 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2e0fef85
JS
3619 if (rc == MBX_NOT_FINISHED) {
3620 mempool_free(mbox, phba->mbox_mem_pool);
3621 lpfc_disc_flush_list(vport);
3622 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3623 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
3624 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
92d7f7b0
JS
3625 phba->link_state = LPFC_HBA_ERROR;
3626 }
3627 }
3628}
3629
3630/* Reg_vpi to tell firmware to resume normal operations */
3631void
3632lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
3633{
3634 LPFC_MBOXQ_t *regvpimbox;
3635
3636 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3637 if (regvpimbox) {
da0436e9 3638 lpfc_reg_vpi(vport, regvpimbox);
92d7f7b0
JS
3639 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
3640 regvpimbox->vport = vport;
0b727fea 3641 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
92d7f7b0
JS
3642 == MBX_NOT_FINISHED) {
3643 mempool_free(regvpimbox, phba->mbox_mem_pool);
2e0fef85
JS
3644 }
3645 }
3646}
3647
3648/* Start Link up / RSCN discovery on NPR nodes */
3649void
3650lpfc_disc_start(struct lpfc_vport *vport)
3651{
3652 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3653 struct lpfc_hba *phba = vport->phba;
685f0bf7 3654 uint32_t num_sent;
dea3101e 3655 uint32_t clear_la_pending;
685f0bf7 3656 int did_changed;
dea3101e 3657
2e0fef85 3658 if (!lpfc_is_link_up(phba))
dea3101e 3659 return;
2e0fef85
JS
3660
3661 if (phba->link_state == LPFC_CLEAR_LA)
dea3101e 3662 clear_la_pending = 1;
3663 else
3664 clear_la_pending = 0;
3665
2e0fef85
JS
3666 if (vport->port_state < LPFC_VPORT_READY)
3667 vport->port_state = LPFC_DISC_AUTH;
dea3101e 3668
2e0fef85
JS
3669 lpfc_set_disctmo(vport);
3670
3671 if (vport->fc_prevDID == vport->fc_myDID)
dea3101e 3672 did_changed = 0;
2e0fef85 3673 else
dea3101e 3674 did_changed = 1;
2e0fef85
JS
3675
3676 vport->fc_prevDID = vport->fc_myDID;
3677 vport->num_disc_nodes = 0;
dea3101e 3678
3679 /* Start Discovery state <hba_state> */
e8b62011
JS
3680 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3681 "0202 Start Discovery hba state x%x "
3682 "Data: x%x x%x x%x\n",
3683 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
3684 vport->fc_adisc_cnt);
dea3101e 3685
3686 /* First do ADISCs - if any */
2e0fef85 3687 num_sent = lpfc_els_disc_adisc(vport);
dea3101e 3688
3689 if (num_sent)
3690 return;
3691
92d7f7b0
JS
3692 /*
3693 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
3694 * continue discovery.
3695 */
3696 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1b32f6aa 3697 !(vport->fc_flag & FC_PT2PT) &&
da0436e9
JS
3698 !(vport->fc_flag & FC_RSCN_MODE) &&
3699 (phba->sli_rev < LPFC_SLI_REV4)) {
92d7f7b0
JS
3700 lpfc_issue_reg_vpi(phba, vport);
3701 return;
3702 }
3703
3704 /*
3705 * For SLI2, we need to set port_state to READY and continue
3706 * discovery.
3707 */
2e0fef85 3708 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
dea3101e 3709 /* If we get here, there is nothing to ADISC */
92d7f7b0 3710 if (vport->port_type == LPFC_PHYSICAL_PORT)
2e0fef85 3711 lpfc_issue_clear_la(phba, vport);
2e0fef85 3712
92d7f7b0 3713 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2e0fef85
JS
3714 vport->num_disc_nodes = 0;
3715 /* go thru NPR nodes and issue ELS PLOGIs */
3716 if (vport->fc_npr_cnt)
3717 lpfc_els_disc_plogi(vport);
3718
3719 if (!vport->num_disc_nodes) {
3720 spin_lock_irq(shost->host_lock);
3721 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3722 spin_unlock_irq(shost->host_lock);
92d7f7b0 3723 lpfc_can_disctmo(vport);
dea3101e 3724 }
3725 }
92d7f7b0 3726 vport->port_state = LPFC_VPORT_READY;
dea3101e 3727 } else {
3728 /* Next do PLOGIs - if any */
2e0fef85 3729 num_sent = lpfc_els_disc_plogi(vport);
dea3101e 3730
3731 if (num_sent)
3732 return;
3733
2e0fef85 3734 if (vport->fc_flag & FC_RSCN_MODE) {
dea3101e 3735 /* Check to see if more RSCNs came in while we
3736 * were processing this one.
3737 */
2e0fef85
JS
3738 if ((vport->fc_rscn_id_cnt == 0) &&
3739 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
3740 spin_lock_irq(shost->host_lock);
3741 vport->fc_flag &= ~FC_RSCN_MODE;
3742 spin_unlock_irq(shost->host_lock);
92d7f7b0 3743 lpfc_can_disctmo(vport);
2fe165b6 3744 } else
2e0fef85 3745 lpfc_els_handle_rscn(vport);
dea3101e 3746 }
3747 }
3748 return;
3749}
3750
3751/*
3752 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
3753 * ring the match the sppecified nodelist.
3754 */
3755static void
2e0fef85 3756lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 3757{
2534ba75 3758 LIST_HEAD(completions);
dea3101e 3759 struct lpfc_sli *psli;
3760 IOCB_t *icmd;
3761 struct lpfc_iocbq *iocb, *next_iocb;
3762 struct lpfc_sli_ring *pring;
dea3101e 3763
3764 psli = &phba->sli;
3765 pring = &psli->ring[LPFC_ELS_RING];
3766
3767 /* Error matching iocb on txq or txcmplq
3768 * First check the txq.
3769 */
2e0fef85 3770 spin_lock_irq(&phba->hbalock);
dea3101e 3771 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3772 if (iocb->context1 != ndlp) {
3773 continue;
3774 }
3775 icmd = &iocb->iocb;
3776 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
3777 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
3778
2534ba75 3779 list_move_tail(&iocb->list, &completions);
dea3101e 3780 pring->txq_cnt--;
dea3101e 3781 }
3782 }
3783
3784 /* Next check the txcmplq */
3785 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
3786 if (iocb->context1 != ndlp) {
3787 continue;
3788 }
3789 icmd = &iocb->iocb;
2e0fef85
JS
3790 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
3791 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
2534ba75
JS
3792 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3793 }
3794 }
2e0fef85 3795 spin_unlock_irq(&phba->hbalock);
dea3101e 3796
a257bf90
JS
3797 /* Cancel all the IOCBs from the completions list */
3798 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3799 IOERR_SLI_ABORTED);
dea3101e 3800}
3801
a6ababd2 3802static void
2e0fef85 3803lpfc_disc_flush_list(struct lpfc_vport *vport)
dea3101e 3804{
3805 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 3806 struct lpfc_hba *phba = vport->phba;
dea3101e 3807
2e0fef85
JS
3808 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
3809 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7 3810 nlp_listp) {
e47c9093
JS
3811 if (!NLP_CHK_NODE_ACT(ndlp))
3812 continue;
685f0bf7
JS
3813 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
3814 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
3815 lpfc_free_tx(phba, ndlp);
685f0bf7 3816 }
dea3101e 3817 }
3818 }
dea3101e 3819}
3820
92d7f7b0
JS
3821void
3822lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
3823{
3824 lpfc_els_flush_rscn(vport);
3825 lpfc_els_flush_cmd(vport);
3826 lpfc_disc_flush_list(vport);
3827}
3828
dea3101e 3829/*****************************************************************************/
3830/*
3831 * NAME: lpfc_disc_timeout
3832 *
3833 * FUNCTION: Fibre Channel driver discovery timeout routine.
3834 *
3835 * EXECUTION ENVIRONMENT: interrupt only
3836 *
3837 * CALLED FROM:
3838 * Timer function
3839 *
3840 * RETURNS:
3841 * none
3842 */
3843/*****************************************************************************/
3844void
3845lpfc_disc_timeout(unsigned long ptr)
3846{
2e0fef85
JS
3847 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
3848 struct lpfc_hba *phba = vport->phba;
5e9d9b82 3849 uint32_t tmo_posted;
dea3101e 3850 unsigned long flags = 0;
3851
3852 if (unlikely(!phba))
3853 return;
3854
5e9d9b82
JS
3855 spin_lock_irqsave(&vport->work_port_lock, flags);
3856 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
3857 if (!tmo_posted)
2e0fef85 3858 vport->work_port_events |= WORKER_DISC_TMO;
5e9d9b82 3859 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2e0fef85 3860
5e9d9b82
JS
3861 if (!tmo_posted)
3862 lpfc_worker_wake_up(phba);
dea3101e 3863 return;
3864}
3865
3866static void
2e0fef85 3867lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea3101e 3868{
2e0fef85
JS
3869 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3870 struct lpfc_hba *phba = vport->phba;
3871 struct lpfc_sli *psli = &phba->sli;
c9f8735b 3872 struct lpfc_nodelist *ndlp, *next_ndlp;
92d7f7b0 3873 LPFC_MBOXQ_t *initlinkmbox;
dea3101e 3874 int rc, clrlaerr = 0;
3875
2e0fef85 3876 if (!(vport->fc_flag & FC_DISC_TMO))
dea3101e 3877 return;
3878
2e0fef85
JS
3879 spin_lock_irq(shost->host_lock);
3880 vport->fc_flag &= ~FC_DISC_TMO;
3881 spin_unlock_irq(shost->host_lock);
dea3101e 3882
858c9f6c
JS
3883 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3884 "disc timeout: state:x%x rtry:x%x flg:x%x",
3885 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
3886
2e0fef85 3887 switch (vport->port_state) {
dea3101e 3888
3889 case LPFC_LOCAL_CFG_LINK:
2e0fef85
JS
3890 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
3891 * FAN
3892 */
3893 /* FAN timeout */
e8b62011
JS
3894 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
3895 "0221 FAN timeout\n");
c9f8735b 3896 /* Start discovery by sending FLOGI, clean up old rpis */
2e0fef85 3897 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7 3898 nlp_listp) {
e47c9093
JS
3899 if (!NLP_CHK_NODE_ACT(ndlp))
3900 continue;
685f0bf7
JS
3901 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
3902 continue;
c9f8735b
JW
3903 if (ndlp->nlp_type & NLP_FABRIC) {
3904 /* Clean up the ndlp on Fabric connections */
2e0fef85 3905 lpfc_drop_node(vport, ndlp);
87af33fe 3906
2fe165b6 3907 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
c9f8735b
JW
3908 /* Fail outstanding IO now since device
3909 * is marked for PLOGI.
3910 */
2e0fef85 3911 lpfc_unreg_rpi(vport, ndlp);
c9f8735b
JW
3912 }
3913 }
92d7f7b0 3914 if (vport->port_state != LPFC_FLOGI) {
92d7f7b0 3915 lpfc_initial_flogi(vport);
0ff10d46 3916 return;
92d7f7b0 3917 }
dea3101e 3918 break;
3919
92d7f7b0 3920 case LPFC_FDISC:
dea3101e 3921 case LPFC_FLOGI:
2e0fef85 3922 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea3101e 3923 /* Initial FLOGI timeout */
e8b62011
JS
3924 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3925 "0222 Initial %s timeout\n",
87af33fe 3926 vport->vpi ? "FDISC" : "FLOGI");
dea3101e 3927
3928 /* Assume no Fabric and go on with discovery.
3929 * Check for outstanding ELS FLOGI to abort.
3930 */
3931
3932 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 3933 lpfc_disc_list_loopmap(vport);
dea3101e 3934
3935 /* Start discovery */
2e0fef85 3936 lpfc_disc_start(vport);
dea3101e 3937 break;
3938
3939 case LPFC_FABRIC_CFG_LINK:
3940 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
3941 NameServer login */
e8b62011
JS
3942 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3943 "0223 Timeout while waiting for "
3944 "NameServer login\n");
dea3101e 3945 /* Next look for NameServer ndlp */
2e0fef85 3946 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093 3947 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
87af33fe
JS
3948 lpfc_els_abort(phba, ndlp);
3949
3950 /* ReStart discovery */
3951 goto restart_disc;
dea3101e 3952
3953 case LPFC_NS_QRY:
3954 /* Check for wait for NameServer Rsp timeout */
e8b62011
JS
3955 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3956 "0224 NameServer Query timeout "
3957 "Data: x%x x%x\n",
3958 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 3959
92d7f7b0
JS
3960 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
3961 /* Try it one more time */
3962 vport->fc_ns_retry++;
3963 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
3964 vport->fc_ns_retry, 0);
3965 if (rc == 0)
3966 break;
dea3101e 3967 }
92d7f7b0 3968 vport->fc_ns_retry = 0;
dea3101e 3969
87af33fe 3970restart_disc:
92d7f7b0
JS
3971 /*
3972 * Discovery is over.
3973 * set port_state to PORT_READY if SLI2.
3974 * cmpl_reg_vpi will set port_state to READY for SLI3.
3975 */
3772a991
JS
3976 if (phba->sli_rev < LPFC_SLI_REV4) {
3977 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
3978 lpfc_issue_reg_vpi(phba, vport);
3979 else { /* NPIV Not enabled */
3980 lpfc_issue_clear_la(phba, vport);
3981 vport->port_state = LPFC_VPORT_READY;
3982 }
dea3101e 3983 }
3984
3985 /* Setup and issue mailbox INITIALIZE LINK command */
3986 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3987 if (!initlinkmbox) {
e8b62011
JS
3988 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3989 "0206 Device Discovery "
3990 "completion error\n");
2e0fef85 3991 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3992 break;
3993 }
3994
3995 lpfc_linkdown(phba);
3996 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
3997 phba->cfg_link_speed);
04c68496 3998 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
ed957684 3999 initlinkmbox->vport = vport;
92d7f7b0 4000 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
0b727fea 4001 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
5b8bd0c9 4002 lpfc_set_loopback_flag(phba);
dea3101e 4003 if (rc == MBX_NOT_FINISHED)
4004 mempool_free(initlinkmbox, phba->mbox_mem_pool);
4005
4006 break;
4007
4008 case LPFC_DISC_AUTH:
4009 /* Node Authentication timeout */
e8b62011
JS
4010 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4011 "0227 Node Authentication timeout\n");
2e0fef85
JS
4012 lpfc_disc_flush_list(vport);
4013
92d7f7b0
JS
4014 /*
4015 * set port_state to PORT_READY if SLI2.
4016 * cmpl_reg_vpi will set port_state to READY for SLI3.
4017 */
3772a991
JS
4018 if (phba->sli_rev < LPFC_SLI_REV4) {
4019 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
4020 lpfc_issue_reg_vpi(phba, vport);
4021 else { /* NPIV Not enabled */
4022 lpfc_issue_clear_la(phba, vport);
4023 vport->port_state = LPFC_VPORT_READY;
4024 }
dea3101e 4025 }
4026 break;
4027
2e0fef85
JS
4028 case LPFC_VPORT_READY:
4029 if (vport->fc_flag & FC_RSCN_MODE) {
e8b62011
JS
4030 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4031 "0231 RSCN timeout Data: x%x "
4032 "x%x\n",
4033 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 4034
4035 /* Cleanup any outstanding ELS commands */
2e0fef85 4036 lpfc_els_flush_cmd(vport);
dea3101e 4037
2e0fef85
JS
4038 lpfc_els_flush_rscn(vport);
4039 lpfc_disc_flush_list(vport);
dea3101e 4040 }
4041 break;
2e0fef85 4042
92d7f7b0 4043 default:
e8b62011 4044 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
d7c255b2 4045 "0273 Unexpected discovery timeout, "
e8b62011 4046 "vport State x%x\n", vport->port_state);
2e0fef85
JS
4047 break;
4048 }
4049
4050 switch (phba->link_state) {
4051 case LPFC_CLEAR_LA:
92d7f7b0 4052 /* CLEAR LA timeout */
e8b62011
JS
4053 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4054 "0228 CLEAR LA timeout\n");
2e0fef85
JS
4055 clrlaerr = 1;
4056 break;
4057
09372820
JS
4058 case LPFC_LINK_UP:
4059 lpfc_issue_clear_la(phba, vport);
4060 /* Drop thru */
2e0fef85
JS
4061 case LPFC_LINK_UNKNOWN:
4062 case LPFC_WARM_START:
4063 case LPFC_INIT_START:
4064 case LPFC_INIT_MBX_CMDS:
4065 case LPFC_LINK_DOWN:
2e0fef85 4066 case LPFC_HBA_ERROR:
e8b62011
JS
4067 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
4068 "0230 Unexpected timeout, hba link "
4069 "state x%x\n", phba->link_state);
2e0fef85
JS
4070 clrlaerr = 1;
4071 break;
92d7f7b0
JS
4072
4073 case LPFC_HBA_READY:
4074 break;
dea3101e 4075 }
4076
4077 if (clrlaerr) {
2e0fef85 4078 lpfc_disc_flush_list(vport);
a4bc3379 4079 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 4080 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
4081 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2e0fef85 4082 vport->port_state = LPFC_VPORT_READY;
dea3101e 4083 }
4084
4085 return;
4086}
4087
dea3101e 4088/*
4089 * This routine handles processing a NameServer REG_LOGIN mailbox
4090 * command upon completion. It is setup in the LPFC_MBOXQ
4091 * as the completion routine when the command is
4092 * handed off to the SLI layer.
4093 */
4094void
2e0fef85 4095lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 4096{
04c68496 4097 MAILBOX_t *mb = &pmb->u.mb;
2e0fef85
JS
4098 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
4099 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
4100 struct lpfc_vport *vport = pmb->vport;
dea3101e 4101
4102 pmb->context1 = NULL;
4103
dea3101e 4104 ndlp->nlp_rpi = mb->un.varWords[0];
21e9a0a5 4105 ndlp->nlp_flag |= NLP_RPI_VALID;
dea3101e 4106 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 4107 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 4108
2e0fef85
JS
4109 /*
4110 * Start issuing Fabric-Device Management Interface (FDMI) command to
4111 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
4112 * fdmi-on=2 (supporting RPA/hostnmae)
dea3101e 4113 */
2e0fef85 4114
3de2a653 4115 if (vport->cfg_fdmi_on == 1)
2e0fef85
JS
4116 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
4117 else
4118 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea3101e 4119
fa4066b6
JS
4120 /* decrement the node reference count held for this callback
4121 * function.
4122 */
329f9bc7 4123 lpfc_nlp_put(ndlp);
dea3101e 4124 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4125 kfree(mp);
329f9bc7 4126 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 4127
4128 return;
4129}
4130
685f0bf7
JS
4131static int
4132lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
4133{
4134 uint16_t *rpi = param;
4135
4136 return ndlp->nlp_rpi == *rpi;
4137}
4138
4139static int
4140lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
4141{
4142 return memcmp(&ndlp->nlp_portname, param,
4143 sizeof(ndlp->nlp_portname)) == 0;
4144}
4145
a6ababd2 4146static struct lpfc_nodelist *
2e0fef85 4147__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea3101e 4148{
21568f53 4149 struct lpfc_nodelist *ndlp;
dea3101e 4150
2e0fef85 4151 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
87af33fe 4152 if (filter(ndlp, param))
685f0bf7
JS
4153 return ndlp;
4154 }
21568f53 4155 return NULL;
dea3101e 4156}
4157
685f0bf7
JS
4158/*
4159 * This routine looks up the ndlp lists for the given RPI. If rpi found it
2e0fef85 4160 * returns the node list element pointer else return NULL.
685f0bf7
JS
4161 */
4162struct lpfc_nodelist *
2e0fef85 4163__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
685f0bf7 4164{
2e0fef85 4165 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
685f0bf7
JS
4166}
4167
488d1469 4168/*
685f0bf7 4169 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
2e0fef85 4170 * returns the node element list pointer else return NULL.
488d1469
JS
4171 */
4172struct lpfc_nodelist *
2e0fef85 4173lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
488d1469 4174{
2e0fef85 4175 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
488d1469 4176 struct lpfc_nodelist *ndlp;
488d1469 4177
2e0fef85
JS
4178 spin_lock_irq(shost->host_lock);
4179 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
4180 spin_unlock_irq(shost->host_lock);
858c9f6c 4181 return ndlp;
488d1469
JS
4182}
4183
dea3101e 4184void
2e0fef85
JS
4185lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4186 uint32_t did)
dea3101e 4187{
4188 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
109f6ed0
JS
4189
4190 lpfc_initialize_node(vport, ndlp, did);
685f0bf7 4191 INIT_LIST_HEAD(&ndlp->nlp_listp);
858c9f6c
JS
4192
4193 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4194 "node init: did:x%x",
4195 ndlp->nlp_DID, 0, 0);
4196
dea3101e 4197 return;
4198}
329f9bc7 4199
98c9ea5c
JS
4200/* This routine releases all resources associated with a specifc NPort's ndlp
4201 * and mempool_free's the nodelist.
4202 */
311464ec 4203static void
329f9bc7
JS
4204lpfc_nlp_release(struct kref *kref)
4205{
e47c9093
JS
4206 struct lpfc_hba *phba;
4207 unsigned long flags;
329f9bc7
JS
4208 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
4209 kref);
858c9f6c
JS
4210
4211 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4212 "node release: did:x%x flg:x%x type:x%x",
4213 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4214
e47c9093
JS
4215 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4216 "0279 lpfc_nlp_release: ndlp:x%p "
4217 "usgmap:x%x refcnt:%d\n",
4218 (void *)ndlp, ndlp->nlp_usg_map,
4219 atomic_read(&ndlp->kref.refcount));
4220
4221 /* remove ndlp from action. */
2e0fef85 4222 lpfc_nlp_remove(ndlp->vport, ndlp);
e47c9093
JS
4223
4224 /* clear the ndlp active flag for all release cases */
a257bf90 4225 phba = ndlp->phba;
e47c9093
JS
4226 spin_lock_irqsave(&phba->ndlp_lock, flags);
4227 NLP_CLR_NODE_ACT(ndlp);
4228 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4229
4230 /* free ndlp memory for final ndlp release */
ea2151b4
JS
4231 if (NLP_CHK_FREE_REQ(ndlp)) {
4232 kfree(ndlp->lat_data);
a257bf90 4233 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
ea2151b4 4234 }
329f9bc7
JS
4235}
4236
98c9ea5c
JS
4237/* This routine bumps the reference count for a ndlp structure to ensure
4238 * that one discovery thread won't free a ndlp while another discovery thread
4239 * is using it.
4240 */
329f9bc7
JS
4241struct lpfc_nodelist *
4242lpfc_nlp_get(struct lpfc_nodelist *ndlp)
4243{
e47c9093
JS
4244 struct lpfc_hba *phba;
4245 unsigned long flags;
4246
98c9ea5c
JS
4247 if (ndlp) {
4248 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4249 "node get: did:x%x flg:x%x refcnt:x%x",
4250 ndlp->nlp_DID, ndlp->nlp_flag,
4251 atomic_read(&ndlp->kref.refcount));
e47c9093
JS
4252 /* The check of ndlp usage to prevent incrementing the
4253 * ndlp reference count that is in the process of being
4254 * released.
4255 */
a257bf90 4256 phba = ndlp->phba;
e47c9093
JS
4257 spin_lock_irqsave(&phba->ndlp_lock, flags);
4258 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
4259 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4260 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4261 "0276 lpfc_nlp_get: ndlp:x%p "
4262 "usgmap:x%x refcnt:%d\n",
4263 (void *)ndlp, ndlp->nlp_usg_map,
4264 atomic_read(&ndlp->kref.refcount));
4265 return NULL;
4266 } else
4267 kref_get(&ndlp->kref);
4268 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
98c9ea5c 4269 }
329f9bc7
JS
4270 return ndlp;
4271}
4272
98c9ea5c 4273/* This routine decrements the reference count for a ndlp structure. If the
e47c9093
JS
4274 * count goes to 0, this indicates the the associated nodelist should be
4275 * freed. Returning 1 indicates the ndlp resource has been released; on the
4276 * other hand, returning 0 indicates the ndlp resource has not been released
4277 * yet.
98c9ea5c 4278 */
329f9bc7
JS
4279int
4280lpfc_nlp_put(struct lpfc_nodelist *ndlp)
4281{
e47c9093
JS
4282 struct lpfc_hba *phba;
4283 unsigned long flags;
4284
4285 if (!ndlp)
4286 return 1;
4287
4288 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4289 "node put: did:x%x flg:x%x refcnt:x%x",
4290 ndlp->nlp_DID, ndlp->nlp_flag,
4291 atomic_read(&ndlp->kref.refcount));
a257bf90 4292 phba = ndlp->phba;
e47c9093
JS
4293 spin_lock_irqsave(&phba->ndlp_lock, flags);
4294 /* Check the ndlp memory free acknowledge flag to avoid the
4295 * possible race condition that kref_put got invoked again
4296 * after previous one has done ndlp memory free.
4297 */
4298 if (NLP_CHK_FREE_ACK(ndlp)) {
4299 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4300 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4301 "0274 lpfc_nlp_put: ndlp:x%p "
4302 "usgmap:x%x refcnt:%d\n",
4303 (void *)ndlp, ndlp->nlp_usg_map,
4304 atomic_read(&ndlp->kref.refcount));
4305 return 1;
4306 }
4307 /* Check the ndlp inactivate log flag to avoid the possible
4308 * race condition that kref_put got invoked again after ndlp
4309 * is already in inactivating state.
4310 */
4311 if (NLP_CHK_IACT_REQ(ndlp)) {
4312 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4313 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
4314 "0275 lpfc_nlp_put: ndlp:x%p "
4315 "usgmap:x%x refcnt:%d\n",
4316 (void *)ndlp, ndlp->nlp_usg_map,
4317 atomic_read(&ndlp->kref.refcount));
4318 return 1;
98c9ea5c 4319 }
e47c9093
JS
4320 /* For last put, mark the ndlp usage flags to make sure no
4321 * other kref_get and kref_put on the same ndlp shall get
4322 * in between the process when the final kref_put has been
4323 * invoked on this ndlp.
4324 */
4325 if (atomic_read(&ndlp->kref.refcount) == 1) {
4326 /* Indicate ndlp is put to inactive state. */
4327 NLP_SET_IACT_REQ(ndlp);
4328 /* Acknowledge ndlp memory free has been seen. */
4329 if (NLP_CHK_FREE_REQ(ndlp))
4330 NLP_SET_FREE_ACK(ndlp);
4331 }
4332 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4333 /* Note, the kref_put returns 1 when decrementing a reference
4334 * count that was 1, it invokes the release callback function,
4335 * but it still left the reference count as 1 (not actually
4336 * performs the last decrementation). Otherwise, it actually
4337 * decrements the reference count and returns 0.
4338 */
4339 return kref_put(&ndlp->kref, lpfc_nlp_release);
329f9bc7 4340}
98c9ea5c
JS
4341
4342/* This routine free's the specified nodelist if it is not in use
e47c9093
JS
4343 * by any other discovery thread. This routine returns 1 if the
4344 * ndlp has been freed. A return value of 0 indicates the ndlp is
4345 * not yet been released.
98c9ea5c
JS
4346 */
4347int
4348lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
4349{
4350 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
4351 "node not used: did:x%x flg:x%x refcnt:x%x",
4352 ndlp->nlp_DID, ndlp->nlp_flag,
4353 atomic_read(&ndlp->kref.refcount));
e47c9093
JS
4354 if (atomic_read(&ndlp->kref.refcount) == 1)
4355 if (lpfc_nlp_put(ndlp))
4356 return 1;
98c9ea5c
JS
4357 return 0;
4358}
6fb120a7
JS
4359
4360/**
4361 * lpfc_fcf_inuse - Check if FCF can be unregistered.
4362 * @phba: Pointer to hba context object.
4363 *
4364 * This function iterate through all FC nodes associated
4365 * will all vports to check if there is any node with
4366 * fc_rports associated with it. If there is an fc_rport
4367 * associated with the node, then the node is either in
4368 * discovered state or its devloss_timer is pending.
4369 */
4370static int
4371lpfc_fcf_inuse(struct lpfc_hba *phba)
4372{
4373 struct lpfc_vport **vports;
4374 int i, ret = 0;
4375 struct lpfc_nodelist *ndlp;
4376 struct Scsi_Host *shost;
4377
4378 vports = lpfc_create_vport_work_array(phba);
4379
4380 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4381 shost = lpfc_shost_from_vport(vports[i]);
4382 spin_lock_irq(shost->host_lock);
4383 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
4384 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
4385 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
4386 ret = 1;
4387 spin_unlock_irq(shost->host_lock);
4388 goto out;
891478a2
JS
4389 } else {
4390 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4391 "2624 RPI %x DID %x flg %x still "
4392 "logged in\n",
4393 ndlp->nlp_rpi, ndlp->nlp_DID,
4394 ndlp->nlp_flag);
4395 if (ndlp->nlp_flag & NLP_RPI_VALID)
4396 ret = 1;
6fb120a7
JS
4397 }
4398 }
4399 spin_unlock_irq(shost->host_lock);
4400 }
4401out:
4402 lpfc_destroy_vport_work_array(phba, vports);
4403 return ret;
4404}
4405
4406/**
4407 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
4408 * @phba: Pointer to hba context object.
4409 * @mboxq: Pointer to mailbox object.
4410 *
4411 * This function frees memory associated with the mailbox command.
4412 */
4413static void
4414lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4415{
4416 struct lpfc_vport *vport = mboxq->vport;
4417
4418 if (mboxq->u.mb.mbxStatus) {
4419 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4420 "2555 UNREG_VFI mbxStatus error x%x "
4421 "HBA state x%x\n",
4422 mboxq->u.mb.mbxStatus, vport->port_state);
4423 }
4424 mempool_free(mboxq, phba->mbox_mem_pool);
4425 return;
4426}
4427
4428/**
4429 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
4430 * @phba: Pointer to hba context object.
4431 * @mboxq: Pointer to mailbox object.
4432 *
4433 * This function frees memory associated with the mailbox command.
4434 */
4435static void
4436lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
4437{
4438 struct lpfc_vport *vport = mboxq->vport;
4439
4440 if (mboxq->u.mb.mbxStatus) {
4441 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4442 "2550 UNREG_FCFI mbxStatus error x%x "
4443 "HBA state x%x\n",
4444 mboxq->u.mb.mbxStatus, vport->port_state);
4445 }
4446 mempool_free(mboxq, phba->mbox_mem_pool);
4447 return;
4448}
4449
4450/**
4451 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
4452 * @phba: Pointer to hba context object.
4453 *
4454 * This function check if there are any connected remote port for the FCF and
4455 * if all the devices are disconnected, this function unregister FCFI.
4456 * This function also tries to use another FCF for discovery.
4457 */
4458void
4459lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
4460{
4461 LPFC_MBOXQ_t *mbox;
4462 int rc;
4463 struct lpfc_vport **vports;
4464 int i;
4465
4466 spin_lock_irq(&phba->hbalock);
4467 /*
4468 * If HBA is not running in FIP mode or
4469 * If HBA does not support FCoE or
4470 * If FCF is not registered.
4471 * do nothing.
4472 */
4473 if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
4474 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
45ed1190 4475 (!(phba->hba_flag & HBA_FIP_SUPPORT))) {
6fb120a7
JS
4476 spin_unlock_irq(&phba->hbalock);
4477 return;
4478 }
4479 spin_unlock_irq(&phba->hbalock);
4480
4481 if (lpfc_fcf_inuse(phba))
4482 return;
4483
4d9ab994
JS
4484 /* At this point, all discovery is aborted */
4485 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6fb120a7
JS
4486
4487 /* Unregister VPIs */
4488 vports = lpfc_create_vport_work_array(phba);
4489 if (vports &&
4490 (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
4491 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4492 lpfc_mbx_unreg_vpi(vports[i]);
97957244 4493 spin_lock_irq(&phba->hbalock);
891478a2 4494 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
c868595d 4495 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
97957244 4496 spin_unlock_irq(&phba->hbalock);
6fb120a7
JS
4497 }
4498 lpfc_destroy_vport_work_array(phba, vports);
4499
4500 /* Unregister VFI */
4501 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4502 if (!mbox) {
4503 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4504 "2556 UNREG_VFI mbox allocation failed"
4505 "HBA state x%x\n",
4506 phba->pport->port_state);
4507 return;
4508 }
4509
6669f9bb 4510 lpfc_unreg_vfi(mbox, phba->pport);
6fb120a7
JS
4511 mbox->vport = phba->pport;
4512 mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
4513
4514 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4515 if (rc == MBX_NOT_FINISHED) {
4516 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4517 "2557 UNREG_VFI issue mbox failed rc x%x "
4518 "HBA state x%x\n",
4519 rc, phba->pport->port_state);
4520 mempool_free(mbox, phba->mbox_mem_pool);
4521 return;
4522 }
4523
4524 /* Unregister FCF */
4525 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4526 if (!mbox) {
4527 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4528 "2551 UNREG_FCFI mbox allocation failed"
4529 "HBA state x%x\n",
4530 phba->pport->port_state);
4531 return;
4532 }
4533
4534 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
4535 mbox->vport = phba->pport;
4536 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
4537 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4538
4539 if (rc == MBX_NOT_FINISHED) {
4540 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4541 "2552 UNREG_FCFI issue mbox failed rc x%x "
4542 "HBA state x%x\n",
4543 rc, phba->pport->port_state);
4544 mempool_free(mbox, phba->mbox_mem_pool);
4545 return;
4546 }
4547
4548 spin_lock_irq(&phba->hbalock);
4549 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
4550 FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
4551 FCF_VALID_VLAN);
4552 spin_unlock_irq(&phba->hbalock);
4553
4554 /*
4555 * If driver is not unloading, check if there is any other
4556 * FCF record that can be used for discovery.
4557 */
4558 if ((phba->pport->load_flag & FC_UNLOADING) ||
4559 (phba->link_state < LPFC_LINK_UP))
4560 return;
4561
4562 rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
4563
4564 if (rc)
4565 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
4566 "2553 lpfc_unregister_unused_fcf failed to read FCF"
4567 " record HBA state x%x\n",
4568 phba->pport->port_state);
4569}
4570
4571/**
4572 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
4573 * @phba: Pointer to hba context object.
4574 * @buff: Buffer containing the FCF connection table as in the config
4575 * region.
4576 * This function create driver data structure for the FCF connection
4577 * record table read from config region 23.
4578 */
4579static void
4580lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
4581 uint8_t *buff)
4582{
4583 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4584 struct lpfc_fcf_conn_hdr *conn_hdr;
4585 struct lpfc_fcf_conn_rec *conn_rec;
4586 uint32_t record_count;
4587 int i;
4588
4589 /* Free the current connect table */
4590 list_for_each_entry_safe(conn_entry, next_conn_entry,
4d9ab994
JS
4591 &phba->fcf_conn_rec_list, list) {
4592 list_del_init(&conn_entry->list);
6fb120a7 4593 kfree(conn_entry);
4d9ab994 4594 }
6fb120a7
JS
4595
4596 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
4597 record_count = conn_hdr->length * sizeof(uint32_t)/
4598 sizeof(struct lpfc_fcf_conn_rec);
4599
4600 conn_rec = (struct lpfc_fcf_conn_rec *)
4601 (buff + sizeof(struct lpfc_fcf_conn_hdr));
4602
4603 for (i = 0; i < record_count; i++) {
4604 if (!(conn_rec[i].flags & FCFCNCT_VALID))
4605 continue;
4606 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
4607 GFP_KERNEL);
4608 if (!conn_entry) {
4609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4610 "2566 Failed to allocate connection"
4611 " table entry\n");
4612 return;
4613 }
4614
4615 memcpy(&conn_entry->conn_rec, &conn_rec[i],
4616 sizeof(struct lpfc_fcf_conn_rec));
4617 conn_entry->conn_rec.vlan_tag =
4618 le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
4619 conn_entry->conn_rec.flags =
4620 le16_to_cpu(conn_entry->conn_rec.flags);
4621 list_add_tail(&conn_entry->list,
4622 &phba->fcf_conn_rec_list);
4623 }
4624}
4625
4626/**
4627 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
4628 * @phba: Pointer to hba context object.
4629 * @buff: Buffer containing the FCoE parameter data structure.
4630 *
4631 * This function update driver data structure with config
4632 * parameters read from config region 23.
4633 */
4634static void
4635lpfc_read_fcoe_param(struct lpfc_hba *phba,
4636 uint8_t *buff)
4637{
4638 struct lpfc_fip_param_hdr *fcoe_param_hdr;
4639 struct lpfc_fcoe_params *fcoe_param;
4640
4641 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
4642 buff;
4643 fcoe_param = (struct lpfc_fcoe_params *)
32b9793f 4644 (buff + sizeof(struct lpfc_fip_param_hdr));
6fb120a7
JS
4645
4646 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
4647 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
4648 return;
4649
6fb120a7
JS
4650 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
4651 phba->valid_vlan = 1;
4652 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
4653 0xFFF;
4654 }
4655
4656 phba->fc_map[0] = fcoe_param->fc_map[0];
4657 phba->fc_map[1] = fcoe_param->fc_map[1];
4658 phba->fc_map[2] = fcoe_param->fc_map[2];
4659 return;
4660}
4661
4662/**
4663 * lpfc_get_rec_conf23 - Get a record type in config region data.
4664 * @buff: Buffer containing config region 23 data.
4665 * @size: Size of the data buffer.
4666 * @rec_type: Record type to be searched.
4667 *
4668 * This function searches config region data to find the begining
4669 * of the record specified by record_type. If record found, this
4670 * function return pointer to the record else return NULL.
4671 */
4672static uint8_t *
4673lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
4674{
4675 uint32_t offset = 0, rec_length;
4676
4677 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
4678 (size < sizeof(uint32_t)))
4679 return NULL;
4680
4681 rec_length = buff[offset + 1];
4682
4683 /*
4684 * One TLV record has one word header and number of data words
4685 * specified in the rec_length field of the record header.
4686 */
4687 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
4688 <= size) {
4689 if (buff[offset] == rec_type)
4690 return &buff[offset];
4691
4692 if (buff[offset] == LPFC_REGION23_LAST_REC)
4693 return NULL;
4694
4695 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
4696 rec_length = buff[offset + 1];
4697 }
4698 return NULL;
4699}
4700
4701/**
4702 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
4703 * @phba: Pointer to lpfc_hba data structure.
4704 * @buff: Buffer containing config region 23 data.
4705 * @size: Size of the data buffer.
4706 *
4707 * This fuction parse the FCoE config parameters in config region 23 and
4708 * populate driver data structure with the parameters.
4709 */
4710void
4711lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
4712 uint8_t *buff,
4713 uint32_t size)
4714{
4715 uint32_t offset = 0, rec_length;
4716 uint8_t *rec_ptr;
4717
4718 /*
4719 * If data size is less than 2 words signature and version cannot be
4720 * verified.
4721 */
4722 if (size < 2*sizeof(uint32_t))
4723 return;
4724
4725 /* Check the region signature first */
4726 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
4727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4728 "2567 Config region 23 has bad signature\n");
4729 return;
4730 }
4731
4732 offset += 4;
4733
4734 /* Check the data structure version */
4735 if (buff[offset] != LPFC_REGION23_VERSION) {
4736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4737 "2568 Config region 23 has bad version\n");
4738 return;
4739 }
4740 offset += 4;
4741
4742 rec_length = buff[offset + 1];
4743
4744 /* Read FCoE param record */
4745 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4746 size - offset, FCOE_PARAM_TYPE);
4747 if (rec_ptr)
4748 lpfc_read_fcoe_param(phba, rec_ptr);
4749
4750 /* Read FCF connection table */
4751 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
4752 size - offset, FCOE_CONN_TBL_TYPE);
4753 if (rec_ptr)
4754 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
4755
4756}