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