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