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