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