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