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