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