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