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