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