[SCSI] lpfc: NPIV: add NPIV support on top of SLI-3
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
9413afff 4 * Copyright (C) 2004-2007 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
32#include "lpfc_hw.h"
33#include "lpfc_disc.h"
34#include "lpfc_sli.h"
35#include "lpfc_scsi.h"
36#include "lpfc.h"
37#include "lpfc_logmsg.h"
38#include "lpfc_crtn.h"
92d7f7b0 39#include "lpfc_vport.h"
dea3101e 40
41/* AlpaArray for assignment of scsid for scan-down and bind_method */
42static uint8_t lpfcAlpaArray[] = {
43 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
44 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
45 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
46 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
47 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
48 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
49 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
50 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
51 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
52 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
53 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
54 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
55 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
56};
57
2e0fef85 58static void lpfc_disc_timeout_handler(struct lpfc_vport *);
dea3101e 59
c01f3208
JS
60void
61lpfc_terminate_rport_io(struct fc_rport *rport)
dea3101e 62{
c01f3208
JS
63 struct lpfc_rport_data *rdata;
64 struct lpfc_nodelist * ndlp;
65 struct lpfc_hba *phba;
dea3101e 66
c01f3208
JS
67 rdata = rport->dd_data;
68 ndlp = rdata->pnode;
69
70 if (!ndlp) {
71 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
72 printk(KERN_ERR "Cannot find remote node"
73 " to terminate I/O Data x%x\n",
74 rport->port_id);
dea3101e 75 return;
76 }
77
2e0fef85 78 phba = ndlp->vport->phba;
c01f3208 79
c01f3208
JS
80 if (ndlp->nlp_sid != NLP_NO_SID) {
81 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
82 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
83 }
c01f3208
JS
84
85 return;
86}
87
88/*
89 * This function will be called when dev_loss_tmo fire.
90 */
91void
92lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
93{
94 struct lpfc_rport_data *rdata;
95 struct lpfc_nodelist * ndlp;
96 uint8_t *name;
97 int warn_on = 0;
98 struct lpfc_hba *phba;
2e0fef85 99 struct lpfc_vport *vport;
92d7f7b0
JS
100 int put_node;
101 int put_rport;
c01f3208
JS
102
103 rdata = rport->dd_data;
104 ndlp = rdata->pnode;
1a169689 105
c01f3208 106 if (!ndlp) {
92d7f7b0 107 if (rport->scsi_target_id != -1) {
c01f3208 108 printk(KERN_ERR "Cannot find remote node"
92d7f7b0
JS
109 " for rport in dev_loss_tmo_callbk x%x\n",
110 rport->port_id);
111 }
c01f3208
JS
112 return;
113 }
114
92d7f7b0
JS
115 if (ndlp->nlp_type & NLP_FABRIC) {
116 /* We will clean up these Nodes in linkup */
117 put_node = rdata->pnode != NULL;
118 put_rport = ndlp->rport != NULL;
119 rdata->pnode = NULL;
120 ndlp->rport = NULL;
121 if (put_node)
122 lpfc_nlp_put(ndlp);
123 if (put_rport)
124 put_device(&rport->dev);
82085718 125 return;
92d7f7b0 126 }
82085718 127
c01f3208 128 name = (uint8_t *)&ndlp->nlp_portname;
2e0fef85
JS
129 vport = ndlp->vport;
130 phba = vport->phba;
dea3101e 131
92d7f7b0
JS
132 if (!(vport->load_flag & FC_UNLOADING) &&
133 ndlp->nlp_state == NLP_STE_MAPPED_NODE)
134 return;
135
136
dea3101e 137 if (ndlp->nlp_sid != NLP_NO_SID) {
6e8215e4 138 warn_on = 1;
dea3101e 139 /* flush the target */
140 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
92d7f7b0 141 ndlp->nlp_sid, 0, 0, LPFC_CTX_TGT);
dea3101e 142 }
2e0fef85 143 if (vport->load_flag & FC_UNLOADING)
c01f3208
JS
144 warn_on = 0;
145
6e8215e4
JSEC
146 if (warn_on) {
147 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0 148 "%d (%d):0203 Devloss timeout on "
488d1469
JS
149 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
150 "NPort x%x Data: x%x x%x x%x\n",
92d7f7b0 151 phba->brd_no, vport->vpi,
488d1469
JS
152 *name, *(name+1), *(name+2), *(name+3),
153 *(name+4), *(name+5), *(name+6), *(name+7),
154 ndlp->nlp_DID, ndlp->nlp_flag,
6e8215e4
JSEC
155 ndlp->nlp_state, ndlp->nlp_rpi);
156 } else {
157 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
92d7f7b0 158 "%d (%d):0204 Devloss timeout on "
488d1469
JS
159 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
160 "NPort x%x Data: x%x x%x x%x\n",
92d7f7b0 161 phba->brd_no, vport->vpi,
488d1469
JS
162 *name, *(name+1), *(name+2), *(name+3),
163 *(name+4), *(name+5), *(name+6), *(name+7),
164 ndlp->nlp_DID, ndlp->nlp_flag,
6e8215e4
JSEC
165 ndlp->nlp_state, ndlp->nlp_rpi);
166 }
167
2e0fef85 168 if (!(vport->load_flag & FC_UNLOADING) &&
1dcb58e5 169 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
82085718
JS
170 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
171 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
2e0fef85 172 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
1dcb58e5 173 else {
92d7f7b0
JS
174 put_node = rdata->pnode != NULL;
175 put_rport = ndlp->rport != NULL;
1dcb58e5
JS
176 rdata->pnode = NULL;
177 ndlp->rport = NULL;
92d7f7b0
JS
178 if (put_node)
179 lpfc_nlp_put(ndlp);
180 if (put_rport)
181 put_device(&rport->dev);
1dcb58e5 182 }
92d7f7b0
JS
183 return;
184}
c01f3208 185
92d7f7b0
JS
186
187void
188lpfc_worker_wake_up(struct lpfc_hba *phba)
189{
190 wake_up(phba->work_wait);
dea3101e 191 return;
192}
193
194static void
2e0fef85 195lpfc_work_list_done(struct lpfc_hba *phba)
dea3101e 196{
197 struct lpfc_work_evt *evtp = NULL;
198 struct lpfc_nodelist *ndlp;
92d7f7b0 199 struct lpfc_vport *vport;
dea3101e 200 int free_evt;
201
2e0fef85
JS
202 spin_lock_irq(&phba->hbalock);
203 while (!list_empty(&phba->work_list)) {
dea3101e 204 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
205 evt_listp);
2e0fef85 206 spin_unlock_irq(&phba->hbalock);
dea3101e 207 free_evt = 1;
2fe165b6 208 switch (evtp->evt) {
92d7f7b0
JS
209 case LPFC_EVT_DEV_LOSS:
210 free_evt = 0; /* evt is part of ndlp */
211 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
212 vport = ndlp->vport;
213 if (!vport)
214 break;
215 if (!(vport->load_flag & FC_UNLOADING) &&
216 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
217 !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
218 lpfc_disc_state_machine(vport, ndlp, NULL,
219 NLP_EVT_DEVICE_RM);
220 }
221 break;
dea3101e 222 case LPFC_EVT_ELS_RETRY:
2e0fef85 223 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea3101e 224 lpfc_els_retry_delay_handler(ndlp);
92d7f7b0 225 free_evt = 0; /* evt is part of ndlp */
dea3101e 226 break;
227 case LPFC_EVT_ONLINE:
2e0fef85
JS
228 if (phba->link_state < LPFC_LINK_DOWN)
229 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
41415862 230 else
2e0fef85 231 *(int *) (evtp->evt_arg1) = 0;
dea3101e 232 complete((struct completion *)(evtp->evt_arg2));
233 break;
46fa311e 234 case LPFC_EVT_OFFLINE_PREP:
2e0fef85 235 if (phba->link_state >= LPFC_LINK_DOWN)
46fa311e
JS
236 lpfc_offline_prep(phba);
237 *(int *)(evtp->evt_arg1) = 0;
238 complete((struct completion *)(evtp->evt_arg2));
239 break;
240 case LPFC_EVT_OFFLINE:
241 lpfc_offline(phba);
41415862
JW
242 lpfc_sli_brdrestart(phba);
243 *(int *)(evtp->evt_arg1) =
46fa311e
JS
244 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
245 lpfc_unblock_mgmt_io(phba);
41415862
JW
246 complete((struct completion *)(evtp->evt_arg2));
247 break;
248 case LPFC_EVT_WARM_START:
46fa311e 249 lpfc_offline(phba);
9290831f 250 lpfc_reset_barrier(phba);
41415862
JW
251 lpfc_sli_brdreset(phba);
252 lpfc_hba_down_post(phba);
253 *(int *)(evtp->evt_arg1) =
254 lpfc_sli_brdready(phba, HS_MBRDY);
46fa311e 255 lpfc_unblock_mgmt_io(phba);
41415862
JW
256 complete((struct completion *)(evtp->evt_arg2));
257 break;
258 case LPFC_EVT_KILL:
46fa311e 259 lpfc_offline(phba);
9290831f 260 *(int *)(evtp->evt_arg1)
2e0fef85
JS
261 = (phba->pport->stopped)
262 ? 0 : lpfc_sli_brdkill(phba);
46fa311e 263 lpfc_unblock_mgmt_io(phba);
dea3101e 264 complete((struct completion *)(evtp->evt_arg2));
265 break;
266 }
267 if (free_evt)
268 kfree(evtp);
2e0fef85 269 spin_lock_irq(&phba->hbalock);
dea3101e 270 }
2e0fef85 271 spin_unlock_irq(&phba->hbalock);
dea3101e 272
273}
274
275static void
2e0fef85 276lpfc_work_done(struct lpfc_hba *phba)
dea3101e 277{
278 struct lpfc_sli_ring *pring;
2e0fef85
JS
279 uint32_t ha_copy, control, work_port_events;
280 struct lpfc_vport *vport;
ed957684 281 int i;
dea3101e 282
2e0fef85 283 spin_lock_irq(&phba->hbalock);
dea3101e 284 ha_copy = phba->work_ha;
285 phba->work_ha = 0;
2e0fef85 286 spin_unlock_irq(&phba->hbalock);
dea3101e 287
2fe165b6 288 if (ha_copy & HA_ERATT)
dea3101e 289 lpfc_handle_eratt(phba);
290
2fe165b6 291 if (ha_copy & HA_MBATT)
dea3101e 292 lpfc_sli_handle_mb_event(phba);
293
2fe165b6 294 if (ha_copy & HA_LATT)
dea3101e 295 lpfc_handle_latt(phba);
296
92d7f7b0
JS
297 spin_lock_irq(&phba->hbalock);
298 list_for_each_entry(vport, &phba->port_list, listentry) {
299 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 300
92d7f7b0
JS
301 if (!scsi_host_get(shost)) {
302 continue;
303 }
304 spin_unlock_irq(&phba->hbalock);
305 work_port_events = vport->work_port_events;
dea3101e 306
92d7f7b0
JS
307 if (work_port_events & WORKER_DISC_TMO)
308 lpfc_disc_timeout_handler(vport);
2e0fef85 309
92d7f7b0
JS
310 if (work_port_events & WORKER_ELS_TMO)
311 lpfc_els_timeout_handler(vport);
2e0fef85 312
92d7f7b0
JS
313 if (work_port_events & WORKER_MBOX_TMO)
314 lpfc_mbox_timeout_handler(phba);
dea3101e 315
92d7f7b0
JS
316 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
317 lpfc_unblock_fabric_iocbs(phba);
dea3101e 318
92d7f7b0
JS
319 if (work_port_events & WORKER_FDMI_TMO)
320 lpfc_fdmi_timeout_handler(vport);
321
322 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
323 lpfc_ramp_down_queue_handler(phba);
324
325 if (work_port_events & WORKER_RAMP_UP_QUEUE)
326 lpfc_ramp_up_queue_handler(phba);
327
328 spin_lock_irq(&vport->work_port_lock);
329 vport->work_port_events &= ~work_port_events;
330 spin_unlock_irq(&vport->work_port_lock);
331 scsi_host_put(shost);
332 spin_lock_irq(&phba->hbalock);
333 }
2e0fef85 334 spin_unlock_irq(&phba->hbalock);
dea3101e 335
336 for (i = 0; i < phba->sli.num_rings; i++, ha_copy >>= 4) {
337 pring = &phba->sli.ring[i];
338 if ((ha_copy & HA_RXATT)
339 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
340 if (pring->flag & LPFC_STOP_IOCB_MASK) {
341 pring->flag |= LPFC_DEFERRED_RING_EVENT;
342 } else {
343 lpfc_sli_handle_slow_ring_event(phba, pring,
344 (ha_copy &
345 HA_RXMASK));
346 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
347 }
348 /*
349 * Turn on Ring interrupts
350 */
2e0fef85 351 spin_lock_irq(&phba->hbalock);
dea3101e 352 control = readl(phba->HCregaddr);
353 control |= (HC_R0INT_ENA << i);
354 writel(control, phba->HCregaddr);
355 readl(phba->HCregaddr); /* flush */
2e0fef85 356 spin_unlock_irq(&phba->hbalock);
dea3101e 357 }
358 }
359
2e0fef85 360 lpfc_work_list_done(phba);
dea3101e 361}
362
363static int
2e0fef85
JS
364check_work_wait_done(struct lpfc_hba *phba)
365{
92d7f7b0
JS
366 struct lpfc_vport *vport;
367 struct lpfc_sli_ring *pring;
368 int i, rc = 0;
ed957684 369
2e0fef85 370 spin_lock_irq(&phba->hbalock);
92d7f7b0
JS
371 list_for_each_entry(vport, &phba->port_list, listentry) {
372 if (vport->work_port_events) {
373 rc = 1;
374 goto exit;
375 }
376 }
dea3101e 377
92d7f7b0
JS
378 if (phba->work_ha || (!list_empty(&phba->work_list)) ||
379 kthread_should_stop()) {
2e0fef85 380 rc = 1;
92d7f7b0
JS
381 goto exit;
382 }
383 for (i = 0; i < phba->sli.num_rings; i++) {
384 pring = &phba->sli.ring[i];
385 if (pring->flag & LPFC_DEFERRED_RING_EVENT) {
386 rc = 1;
387 goto exit;
388 }
389 }
390exit:
391 if (rc)
392 phba->work_found++;
393 else
394 phba->work_found = 0;
2e0fef85
JS
395
396 spin_unlock_irq(&phba->hbalock);
397 return rc;
dea3101e 398}
399
92d7f7b0 400
dea3101e 401int
402lpfc_do_work(void *p)
403{
404 struct lpfc_hba *phba = p;
405 int rc;
7259f0d0 406 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(work_waitq);
dea3101e 407
408 set_user_nice(current, -20);
409 phba->work_wait = &work_waitq;
92d7f7b0 410 phba->work_found = 0;
dea3101e 411
412 while (1) {
413
414 rc = wait_event_interruptible(work_waitq,
92d7f7b0
JS
415 check_work_wait_done(phba));
416
dea3101e 417 BUG_ON(rc);
418
419 if (kthread_should_stop())
420 break;
421
422 lpfc_work_done(phba);
423
92d7f7b0
JS
424 /* If there is alot of slow ring work, like during link up
425 * check_work_wait_done() may cause this thread to not give
426 * up the CPU for very long periods of time. This may cause
427 * soft lockups or other problems. To avoid these situations
428 * give up the CPU here after LPFC_MAX_WORKER_ITERATION
429 * consecutive iterations.
430 */
431 if (phba->work_found >= LPFC_MAX_WORKER_ITERATION) {
432 phba->work_found = 0;
433 schedule();
434 }
dea3101e 435 }
436 phba->work_wait = NULL;
437 return 0;
438}
439
440/*
441 * This is only called to handle FC worker events. Since this a rare
442 * occurance, we allocate a struct lpfc_work_evt structure here instead of
443 * embedding it in the IOCB.
444 */
445int
2e0fef85 446lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea3101e 447 uint32_t evt)
448{
449 struct lpfc_work_evt *evtp;
ed957684 450 unsigned long flags;
dea3101e 451
452 /*
453 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
454 * be queued to worker thread for processing
455 */
92d7f7b0 456 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea3101e 457 if (!evtp)
458 return 0;
459
460 evtp->evt_arg1 = arg1;
461 evtp->evt_arg2 = arg2;
462 evtp->evt = evt;
463
ed957684 464 spin_lock_irqsave(&phba->hbalock, flags);
071fbd3d 465 list_add_tail(&evtp->evt_listp, &phba->work_list);
dea3101e 466 if (phba->work_wait)
92d7f7b0 467 lpfc_worker_wake_up(phba);
ed957684 468 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 469
470 return 1;
471}
472
92d7f7b0
JS
473void
474lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
475{
476 struct lpfc_hba *phba = vport->phba;
477 struct lpfc_nodelist *ndlp, *next_ndlp;
478 int rc;
479
480 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
481 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
482 continue;
483
484 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN)
485 lpfc_unreg_rpi(vport, ndlp);
486
487 /* Leave Fabric nodes alone on link down */
488 if (!remove && ndlp->nlp_type & NLP_FABRIC)
489 continue;
490 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
491 remove
492 ? NLP_EVT_DEVICE_RM
493 : NLP_EVT_DEVICE_RECOVERY);
494 }
495 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
496 lpfc_mbx_unreg_vpi(vport);
497 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
498 }
499}
500
501static void
502lpfc_linkdown_port(struct lpfc_vport *vport)
503{
504 struct lpfc_nodelist *ndlp, *next_ndlp;
505 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
506
507 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
508
509 /* Cleanup any outstanding RSCN activity */
510 lpfc_els_flush_rscn(vport);
511
512 /* Cleanup any outstanding ELS commands */
513 lpfc_els_flush_cmd(vport);
514
515 lpfc_cleanup_rpis(vport, 0);
516
517 /* free any ndlp's on unused list */
518 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp)
519 /* free any ndlp's in unused state */
520 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
521 lpfc_drop_node(vport, ndlp);
522
523 /* Turn off discovery timer if its running */
524 lpfc_can_disctmo(vport);
525}
526
dea3101e 527int
685f0bf7 528lpfc_linkdown(struct lpfc_hba *phba)
dea3101e 529{
2e0fef85
JS
530 struct lpfc_vport *vport = phba->pport;
531 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0 532 struct lpfc_vport *port_iterator;
685f0bf7 533 LPFC_MBOXQ_t *mb;
dea3101e 534
2e0fef85
JS
535 if (phba->link_state == LPFC_LINK_DOWN) {
536 return 0;
c9f8735b 537 }
2e0fef85 538 spin_lock_irq(&phba->hbalock);
92d7f7b0 539 if (phba->link_state > LPFC_LINK_DOWN) {
2e0fef85 540 phba->link_state = LPFC_LINK_DOWN;
92d7f7b0
JS
541 phba->pport->fc_flag &= ~FC_LBIT;
542 }
2e0fef85 543 spin_unlock_irq(&phba->hbalock);
dea3101e 544
92d7f7b0
JS
545 list_for_each_entry(port_iterator, &phba->port_list, listentry) {
546
547 /* Issue a LINK DOWN event to all nodes */
548 lpfc_linkdown_port(port_iterator);
549 }
d2873e4c 550
dea3101e 551 /* Clean up any firmware default rpi's */
2e0fef85
JS
552 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
553 if (mb) {
92d7f7b0 554 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
ed957684 555 mb->vport = vport;
2e0fef85 556 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
dea3101e 557 if (lpfc_sli_issue_mbox(phba, mb, (MBX_NOWAIT | MBX_STOP_IOCB))
558 == MBX_NOT_FINISHED) {
2e0fef85 559 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 560 }
561 }
562
dea3101e 563 /* Setup myDID for link up if we are in pt2pt mode */
92d7f7b0
JS
564 if (phba->pport->fc_flag & FC_PT2PT) {
565 phba->pport->fc_myDID = 0;
2e0fef85
JS
566 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
567 if (mb) {
dea3101e 568 lpfc_config_link(phba, mb);
92d7f7b0 569 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
ed957684 570 mb->vport = vport;
2e0fef85
JS
571 if (lpfc_sli_issue_mbox(phba, mb,
572 (MBX_NOWAIT | MBX_STOP_IOCB))
dea3101e 573 == MBX_NOT_FINISHED) {
2e0fef85 574 mempool_free(mb, phba->mbox_mem_pool);
dea3101e 575 }
576 }
2e0fef85 577 spin_lock_irq(shost->host_lock);
92d7f7b0 578 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
2e0fef85 579 spin_unlock_irq(shost->host_lock);
dea3101e 580 }
2e0fef85 581
92d7f7b0
JS
582 return 0;
583}
dea3101e 584
92d7f7b0
JS
585static void
586lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
587{
588 struct lpfc_nodelist *ndlp;
dea3101e 589
92d7f7b0
JS
590 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
591 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
592 continue;
593
594 if (ndlp->nlp_type & NLP_FABRIC) {
595 /* On Linkup its safe to clean up the ndlp
596 * from Fabric connections.
597 */
598 if (ndlp->nlp_DID != Fabric_DID)
599 lpfc_unreg_rpi(vport, ndlp);
600 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
601 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
602 /* Fail outstanding IO now since device is
603 * marked for PLOGI.
604 */
605 lpfc_unreg_rpi(vport, ndlp);
606 }
607 }
dea3101e 608}
609
92d7f7b0
JS
610static void
611lpfc_linkup_port(struct lpfc_vport *vport)
dea3101e 612{
92d7f7b0 613 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 614 struct lpfc_nodelist *ndlp, *next_ndlp;
92d7f7b0
JS
615 struct lpfc_hba *phba = vport->phba;
616
617 if ((vport->load_flag & FC_UNLOADING) != 0)
618 return;
619
620 /* If NPIV is not enabled, only bring the physical port up */
621 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
622 (vport != phba->pport))
623 return;
dea3101e 624
2e0fef85 625 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
d2873e4c 626
2e0fef85 627 spin_lock_irq(shost->host_lock);
2e0fef85
JS
628 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
629 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
630 vport->fc_flag |= FC_NDISC_ACTIVE;
631 vport->fc_ns_retry = 0;
632 spin_unlock_irq(shost->host_lock);
dea3101e 633
92d7f7b0
JS
634 if (vport->fc_flag & FC_LBIT)
635 lpfc_linkup_cleanup_nodes(vport);
dea3101e 636
92d7f7b0 637 /* free any ndlp's in unused state */
2e0fef85 638 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
92d7f7b0 639 nlp_listp)
685f0bf7 640 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2e0fef85 641 lpfc_drop_node(vport, ndlp);
92d7f7b0
JS
642}
643
644static int
645lpfc_linkup(struct lpfc_hba *phba)
646{
647 struct lpfc_vport *vport;
648
649 phba->link_state = LPFC_LINK_UP;
650
651 /* Unblock fabric iocbs if they are blocked */
652 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
653 del_timer_sync(&phba->fabric_block_timer);
654
655 list_for_each_entry(vport, &phba->port_list, listentry) {
656 lpfc_linkup_port(vport);
685f0bf7 657 }
92d7f7b0
JS
658 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
659 lpfc_issue_clear_la(phba, phba->pport);
dea3101e 660
661 return 0;
662}
663
664/*
665 * This routine handles processing a CLEAR_LA mailbox
666 * command upon completion. It is setup in the LPFC_MBOXQ
667 * as the completion routine when the command is
668 * handed off to the SLI layer.
669 */
670void
2e0fef85 671lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 672{
2e0fef85
JS
673 struct lpfc_vport *vport = pmb->vport;
674 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
675 struct lpfc_sli *psli = &phba->sli;
676 MAILBOX_t *mb = &pmb->mb;
dea3101e 677 uint32_t control;
678
dea3101e 679 /* Since we don't do discovery right now, turn these off here */
a4bc3379 680 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 681 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
682 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
683
684 /* Check for error */
685 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
92d7f7b0 686 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
dea3101e 687 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
92d7f7b0 688 "%d (%d):0320 CLEAR_LA mbxStatus error x%x hba "
dea3101e 689 "state x%x\n",
92d7f7b0
JS
690 phba->brd_no, vport->vpi, mb->mbxStatus,
691 vport->port_state);
dea3101e 692
2e0fef85 693 phba->link_state = LPFC_HBA_ERROR;
dea3101e 694 goto out;
695 }
696
92d7f7b0
JS
697 if (vport->port_type == LPFC_PHYSICAL_PORT)
698 phba->link_state = LPFC_HBA_READY;
699
700 spin_lock_irq(&phba->hbalock);
701 psli->sli_flag |= LPFC_PROCESS_LA;
702 control = readl(phba->HCregaddr);
703 control |= HC_LAINT_ENA;
704 writel(control, phba->HCregaddr);
705 readl(phba->HCregaddr); /* flush */
706 spin_unlock_irq(&phba->hbalock);
707 return;
dea3101e 708
2e0fef85
JS
709 vport->num_disc_nodes = 0;
710 /* go thru NPR nodes and issue ELS PLOGIs */
711 if (vport->fc_npr_cnt)
712 lpfc_els_disc_plogi(vport);
dea3101e 713
2e0fef85
JS
714 if (!vport->num_disc_nodes) {
715 spin_lock_irq(shost->host_lock);
716 vport->fc_flag &= ~FC_NDISC_ACTIVE;
717 spin_unlock_irq(shost->host_lock);
dea3101e 718 }
719
2e0fef85 720 vport->port_state = LPFC_VPORT_READY;
dea3101e 721
722out:
723 /* Device Discovery completes */
ed957684 724 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
92d7f7b0
JS
725 "%d (%d):0225 Device Discovery completes\n",
726 phba->brd_no, vport->vpi);
dea3101e 727
2e0fef85 728 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 729
2e0fef85
JS
730 spin_lock_irq(shost->host_lock);
731 vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_ESTABLISH_LINK);
732 spin_unlock_irq(shost->host_lock);
dea3101e 733
734 del_timer_sync(&phba->fc_estabtmo);
735
2e0fef85 736 lpfc_can_disctmo(vport);
dea3101e 737
738 /* turn on Link Attention interrupts */
2e0fef85
JS
739
740 spin_lock_irq(&phba->hbalock);
dea3101e 741 psli->sli_flag |= LPFC_PROCESS_LA;
742 control = readl(phba->HCregaddr);
743 control |= HC_LAINT_ENA;
744 writel(control, phba->HCregaddr);
745 readl(phba->HCregaddr); /* flush */
2e0fef85 746 spin_unlock_irq(&phba->hbalock);
dea3101e 747
748 return;
749}
750
2e0fef85 751
dea3101e 752static void
25594c6b 753lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 754{
2e0fef85 755 struct lpfc_vport *vport = pmb->vport;
dea3101e 756
25594c6b 757 if (pmb->mb.mbxStatus)
dea3101e 758 goto out;
dea3101e 759
25594c6b
JW
760 mempool_free(pmb, phba->mbox_mem_pool);
761
762 if (phba->fc_topology == TOPOLOGY_LOOP &&
2e0fef85
JS
763 vport->fc_flag & FC_PUBLIC_LOOP &&
764 !(vport->fc_flag & FC_LBIT)) {
25594c6b 765 /* Need to wait for FAN - use discovery timer
2e0fef85 766 * for timeout. port_state is identically
25594c6b
JW
767 * LPFC_LOCAL_CFG_LINK while waiting for FAN
768 */
2e0fef85 769 lpfc_set_disctmo(vport);
25594c6b 770 return;
92d7f7b0 771 }
dea3101e 772
2e0fef85 773 /* Start discovery by sending a FLOGI. port_state is identically
25594c6b
JW
774 * LPFC_FLOGI while waiting for FLOGI cmpl
775 */
92d7f7b0
JS
776 if (vport->port_state != LPFC_FLOGI) {
777 vport->port_state = LPFC_FLOGI;
778 lpfc_set_disctmo(vport);
779 lpfc_initial_flogi(vport);
780 }
25594c6b 781 return;
dea3101e 782
783out:
25594c6b 784 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
92d7f7b0 785 "%d (%d):0306 CONFIG_LINK mbxStatus error x%x "
25594c6b 786 "HBA state x%x\n",
92d7f7b0
JS
787 phba->brd_no, vport->vpi, pmb->mb.mbxStatus,
788 vport->port_state);
25594c6b 789
92d7f7b0 790 mempool_free(pmb, phba->mbox_mem_pool);
25594c6b 791
92d7f7b0 792 lpfc_linkdown(phba);
25594c6b
JW
793
794 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
795 "%d (%d):0200 CONFIG_LINK bad hba state x%x\n",
796 phba->brd_no, vport->vpi, vport->port_state);
dea3101e 797
92d7f7b0 798 lpfc_issue_clear_la(phba, vport);
dea3101e 799 return;
800}
801
802static void
2e0fef85 803lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 804{
dea3101e 805 MAILBOX_t *mb = &pmb->mb;
806 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
2e0fef85 807 struct lpfc_vport *vport = pmb->vport;
dea3101e 808
809
810 /* Check for error */
811 if (mb->mbxStatus) {
812 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
813 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
92d7f7b0 814 "%d (%d):0319 READ_SPARAM mbxStatus error x%x "
dea3101e 815 "hba state x%x>\n",
92d7f7b0
JS
816 phba->brd_no, vport->vpi, mb->mbxStatus,
817 vport->port_state);
dea3101e 818
819 lpfc_linkdown(phba);
dea3101e 820 goto out;
821 }
822
2e0fef85 823 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea3101e 824 sizeof (struct serv_parm));
a12e07bc 825 if (phba->cfg_soft_wwnn)
2e0fef85
JS
826 u64_to_wwn(phba->cfg_soft_wwnn,
827 vport->fc_sparam.nodeName.u.wwn);
c3f28afa 828 if (phba->cfg_soft_wwpn)
2e0fef85
JS
829 u64_to_wwn(phba->cfg_soft_wwpn,
830 vport->fc_sparam.portName.u.wwn);
92d7f7b0
JS
831 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
832 sizeof(vport->fc_nodename));
833 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
834 sizeof(vport->fc_portname));
835 if (vport->port_type == LPFC_PHYSICAL_PORT) {
836 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
837 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
838 }
839
dea3101e 840 lpfc_mbuf_free(phba, mp->virt, mp->phys);
841 kfree(mp);
2e0fef85 842 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 843 return;
844
845out:
846 pmb->context1 = NULL;
847 lpfc_mbuf_free(phba, mp->virt, mp->phys);
848 kfree(mp);
92d7f7b0
JS
849 lpfc_issue_clear_la(phba, vport);
850 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 851 return;
852}
853
854static void
92d7f7b0 855lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
dea3101e 856{
92d7f7b0 857 struct lpfc_vport *vport = phba->pport;
dea3101e 858 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
2e0fef85 859 int i;
14691150
JS
860 struct lpfc_dmabuf *mp;
861 int rc;
862
dea3101e 863 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
864 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
865
92d7f7b0 866 spin_lock_irq(&phba->hbalock);
2fe165b6 867 switch (la->UlnkSpeed) {
92d7f7b0
JS
868 case LA_1GHZ_LINK:
869 phba->fc_linkspeed = LA_1GHZ_LINK;
870 break;
871 case LA_2GHZ_LINK:
872 phba->fc_linkspeed = LA_2GHZ_LINK;
873 break;
874 case LA_4GHZ_LINK:
875 phba->fc_linkspeed = LA_4GHZ_LINK;
876 break;
877 case LA_8GHZ_LINK:
878 phba->fc_linkspeed = LA_8GHZ_LINK;
879 break;
880 default:
881 phba->fc_linkspeed = LA_UNKNW_LINK;
882 break;
dea3101e 883 }
884
885 phba->fc_topology = la->topology;
92d7f7b0 886 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea3101e 887
888 if (phba->fc_topology == TOPOLOGY_LOOP) {
92d7f7b0 889 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea3101e 890
92d7f7b0 891 /* Get Loop Map information */
dea3101e 892 if (la->il)
2e0fef85 893 vport->fc_flag |= FC_LBIT;
dea3101e 894
2e0fef85 895 vport->fc_myDID = la->granted_AL_PA;
dea3101e 896 i = la->un.lilpBde64.tus.f.bdeSize;
897
898 if (i == 0) {
899 phba->alpa_map[0] = 0;
900 } else {
901 if (phba->cfg_log_verbose & LOG_LINK_EVENT) {
902 int numalpa, j, k;
903 union {
904 uint8_t pamap[16];
905 struct {
906 uint32_t wd1;
907 uint32_t wd2;
908 uint32_t wd3;
909 uint32_t wd4;
910 } pa;
911 } un;
912 numalpa = phba->alpa_map[0];
913 j = 0;
914 while (j < numalpa) {
915 memset(un.pamap, 0, 16);
916 for (k = 1; j < numalpa; k++) {
917 un.pamap[k - 1] =
918 phba->alpa_map[j + 1];
919 j++;
920 if (k == 16)
921 break;
922 }
923 /* Link Up Event ALPA map */
924 lpfc_printf_log(phba,
92d7f7b0
JS
925 KERN_WARNING,
926 LOG_LINK_EVENT,
927 "%d:1304 Link Up Event "
928 "ALPA map Data: x%x "
929 "x%x x%x x%x\n",
930 phba->brd_no,
931 un.pa.wd1, un.pa.wd2,
932 un.pa.wd3, un.pa.wd4);
dea3101e 933 }
934 }
935 }
936 } else {
92d7f7b0
JS
937 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
938 if (phba->max_vpi && lpfc_npiv_enable &&
939 (phba->sli_rev == 3))
940 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
941 }
2e0fef85
JS
942 vport->fc_myDID = phba->fc_pref_DID;
943 vport->fc_flag |= FC_LBIT;
dea3101e 944 }
92d7f7b0 945 spin_unlock_irq(&phba->hbalock);
dea3101e 946
947 lpfc_linkup(phba);
948 if (sparam_mbox) {
92d7f7b0 949 lpfc_read_sparam(phba, sparam_mbox, 0);
2e0fef85 950 sparam_mbox->vport = vport;
dea3101e 951 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
14691150 952 rc = lpfc_sli_issue_mbox(phba, sparam_mbox,
92d7f7b0 953 (MBX_NOWAIT | MBX_STOP_IOCB));
14691150
JS
954 if (rc == MBX_NOT_FINISHED) {
955 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
956 lpfc_mbuf_free(phba, mp->virt, mp->phys);
957 kfree(mp);
958 mempool_free(sparam_mbox, phba->mbox_mem_pool);
959 if (cfglink_mbox)
960 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
92d7f7b0 961 goto out;
14691150 962 }
dea3101e 963 }
964
965 if (cfglink_mbox) {
2e0fef85 966 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea3101e 967 lpfc_config_link(phba, cfglink_mbox);
2e0fef85 968 cfglink_mbox->vport = vport;
25594c6b 969 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
14691150 970 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox,
92d7f7b0
JS
971 (MBX_NOWAIT | MBX_STOP_IOCB));
972 if (rc != MBX_NOT_FINISHED)
973 return;
974 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
dea3101e 975 }
92d7f7b0
JS
976out:
977 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
978 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
979 "%d (%d):0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
980 phba->brd_no, vport->vpi,
981 vport->port_state, sparam_mbox, cfglink_mbox);
982
983 lpfc_issue_clear_la(phba, vport);
984 return;
dea3101e 985}
986
987static void
2e0fef85
JS
988lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
989{
dea3101e 990 uint32_t control;
991 struct lpfc_sli *psli = &phba->sli;
992
993 lpfc_linkdown(phba);
994
995 /* turn on Link Attention interrupts - no CLEAR_LA needed */
2e0fef85 996 spin_lock_irq(&phba->hbalock);
dea3101e 997 psli->sli_flag |= LPFC_PROCESS_LA;
998 control = readl(phba->HCregaddr);
999 control |= HC_LAINT_ENA;
1000 writel(control, phba->HCregaddr);
1001 readl(phba->HCregaddr); /* flush */
2e0fef85 1002 spin_unlock_irq(&phba->hbalock);
dea3101e 1003}
1004
1005/*
1006 * This routine handles processing a READ_LA mailbox
1007 * command upon completion. It is setup in the LPFC_MBOXQ
1008 * as the completion routine when the command is
1009 * handed off to the SLI layer.
1010 */
1011void
2e0fef85 1012lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1013{
2e0fef85
JS
1014 struct lpfc_vport *vport = pmb->vport;
1015 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 1016 READ_LA_VAR *la;
1017 MAILBOX_t *mb = &pmb->mb;
1018 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1019
1020 /* Check for error */
1021 if (mb->mbxStatus) {
ed957684 1022 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
dea3101e 1023 "%d:1307 READ_LA mbox error x%x state x%x\n",
ed957684 1024 phba->brd_no, mb->mbxStatus, vport->port_state);
dea3101e 1025 lpfc_mbx_issue_link_down(phba);
2e0fef85 1026 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1027 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1028 }
1029
1030 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1031
1032 memcpy(&phba->alpa_map[0], mp->virt, 128);
1033
2e0fef85 1034 spin_lock_irq(shost->host_lock);
c9f8735b 1035 if (la->pb)
2e0fef85 1036 vport->fc_flag |= FC_BYPASSED_MODE;
c9f8735b 1037 else
2e0fef85
JS
1038 vport->fc_flag &= ~FC_BYPASSED_MODE;
1039 spin_unlock_irq(shost->host_lock);
c9f8735b 1040
dea3101e 1041 if (((phba->fc_eventTag + 1) < la->eventTag) ||
92d7f7b0 1042 (phba->fc_eventTag == la->eventTag)) {
dea3101e 1043 phba->fc_stat.LinkMultiEvent++;
2e0fef85 1044 if (la->attType == AT_LINK_UP)
dea3101e 1045 if (phba->fc_eventTag != 0)
1046 lpfc_linkdown(phba);
92d7f7b0 1047 }
dea3101e 1048
1049 phba->fc_eventTag = la->eventTag;
1050
1051 if (la->attType == AT_LINK_UP) {
1052 phba->fc_stat.LinkUp++;
2e0fef85 1053 if (phba->link_flag & LS_LOOPBACK_MODE) {
5b8bd0c9
JS
1054 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1055 "%d:1306 Link Up Event in loop back mode "
1056 "x%x received Data: x%x x%x x%x x%x\n",
1057 phba->brd_no, la->eventTag, phba->fc_eventTag,
1058 la->granted_AL_PA, la->UlnkSpeed,
1059 phba->alpa_map[0]);
1060 } else {
1061 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
dea3101e 1062 "%d:1303 Link Up Event x%x received "
1063 "Data: x%x x%x x%x x%x\n",
1064 phba->brd_no, la->eventTag, phba->fc_eventTag,
1065 la->granted_AL_PA, la->UlnkSpeed,
1066 phba->alpa_map[0]);
5b8bd0c9 1067 }
92d7f7b0 1068 lpfc_mbx_process_link_up(phba, la);
dea3101e 1069 } else {
1070 phba->fc_stat.LinkDown++;
1071 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1072 "%d:1305 Link Down Event x%x received "
1073 "Data: x%x x%x x%x\n",
1074 phba->brd_no, la->eventTag, phba->fc_eventTag,
2e0fef85 1075 phba->pport->port_state, vport->fc_flag);
dea3101e 1076 lpfc_mbx_issue_link_down(phba);
1077 }
1078
1079lpfc_mbx_cmpl_read_la_free_mbuf:
1080 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1081 kfree(mp);
1082 mempool_free(pmb, phba->mbox_mem_pool);
1083 return;
1084}
1085
1086/*
1087 * This routine handles processing a REG_LOGIN mailbox
1088 * command upon completion. It is setup in the LPFC_MBOXQ
1089 * as the completion routine when the command is
1090 * handed off to the SLI layer.
1091 */
1092void
2e0fef85 1093lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1094{
2e0fef85 1095 struct lpfc_vport *vport = pmb->vport;
92d7f7b0 1096 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2e0fef85 1097 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
dea3101e 1098
dea3101e 1099 pmb->context1 = NULL;
1100
1101 /* Good status, call state machine */
2e0fef85 1102 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
dea3101e 1103 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1104 kfree(mp);
2e0fef85 1105 mempool_free(pmb, phba->mbox_mem_pool);
329f9bc7 1106 lpfc_nlp_put(ndlp);
dea3101e 1107
1108 return;
1109}
1110
92d7f7b0
JS
1111static void
1112lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1113{
1114 MAILBOX_t *mb = &pmb->mb;
1115 struct lpfc_vport *vport = pmb->vport;
1116 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1117
1118 switch (mb->mbxStatus) {
1119 case 0x0011:
1120 case 0x0020:
1121 case 0x9700:
1122 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1123 "%d (%d):0911 cmpl_unreg_vpi, "
1124 "mb status = 0x%x\n",
1125 phba->brd_no, vport->vpi, mb->mbxStatus);
1126 break;
1127 default:
1128 phba->vpi_cnt--;
1129 }
1130 vport->unreg_vpi_cmpl = VPORT_OK;
1131 mempool_free(pmb, phba->mbox_mem_pool);
1132 /*
1133 * This shost reference might have been taken at the beginning of
1134 * lpfc_vport_delete()
1135 */
1136 if (vport->load_flag & FC_UNLOADING)
1137 scsi_host_put(shost);
1138}
1139
1140void
1141lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1142{
1143 struct lpfc_hba *phba = vport->phba;
1144 LPFC_MBOXQ_t *mbox;
1145 int rc;
1146
1147 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1148 if (!mbox)
1149 return;
1150
1151 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1152 mbox->vport = vport;
1153 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
1154 rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1155 if (rc == MBX_NOT_FINISHED) {
1156 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1157 "%d (%d):1800 Could not issue unreg_vpi\n",
1158 phba->brd_no, vport->vpi);
1159 mempool_free(mbox, phba->mbox_mem_pool);
1160 vport->unreg_vpi_cmpl = VPORT_ERROR;
1161 }
1162}
1163
1164static void
1165lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1166{
1167 struct lpfc_vport *vport = pmb->vport;
1168 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1169 MAILBOX_t *mb = &pmb->mb;
1170
1171 switch (mb->mbxStatus) {
1172 case 0x0011:
1173 case 0x9601:
1174 case 0x9602:
1175 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1176 "%d (%d):0912 cmpl_reg_vpi, mb status = 0x%x\n",
1177 phba->brd_no, vport->vpi, mb->mbxStatus);
1178 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1179 spin_lock_irq(shost->host_lock);
1180 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1181 spin_unlock_irq(shost->host_lock);
1182 vport->fc_myDID = 0;
1183 goto out;
1184 }
1185 phba->vpi_cnt++;
1186
1187 vport->num_disc_nodes = 0;
1188 /* go thru NPR list and issue ELS PLOGIs */
1189 if (vport->fc_npr_cnt)
1190 lpfc_els_disc_plogi(vport);
1191
1192 if (!vport->num_disc_nodes) {
1193 spin_lock_irq(shost->host_lock);
1194 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1195 spin_unlock_irq(shost->host_lock);
1196 lpfc_can_disctmo(vport);
1197 }
1198 vport->port_state = LPFC_VPORT_READY;
1199
1200out:
1201 mempool_free(pmb, phba->mbox_mem_pool);
1202 return;
1203}
1204
dea3101e 1205/*
1206 * This routine handles processing a Fabric REG_LOGIN mailbox
1207 * command upon completion. It is setup in the LPFC_MBOXQ
1208 * as the completion routine when the command is
1209 * handed off to the SLI layer.
1210 */
1211void
2e0fef85 1212lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1213{
92d7f7b0
JS
1214 struct lpfc_vport *vport = pmb->vport;
1215 struct lpfc_vport *next_vport;
2e0fef85
JS
1216 MAILBOX_t *mb = &pmb->mb;
1217 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
92d7f7b0 1218 struct lpfc_nodelist *ndlp;
dea3101e 1219 ndlp = (struct lpfc_nodelist *) pmb->context2;
dea3101e 1220
329f9bc7
JS
1221 pmb->context1 = NULL;
1222 pmb->context2 = NULL;
1223
dea3101e 1224 if (mb->mbxStatus) {
1225 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1226 kfree(mp);
329f9bc7
JS
1227 mempool_free(pmb, phba->mbox_mem_pool);
1228 lpfc_nlp_put(ndlp);
dea3101e 1229
92d7f7b0
JS
1230 if (phba->fc_topology == TOPOLOGY_LOOP) {
1231 /* FLOGI failed, use loop map to make discovery list */
1232 lpfc_disc_list_loopmap(vport);
1233
1234 /* Start discovery */
1235 lpfc_disc_start(vport);
1236 return;
1237 }
1238
1239 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1241 "%d (%d):0258 Register Fabric login error: 0x%x\n",
1242 phba->brd_no, vport->vpi, mb->mbxStatus);
dea3101e 1243
dea3101e 1244 return;
1245 }
1246
dea3101e 1247 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e 1248 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 1249 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 1250
329f9bc7
JS
1251 lpfc_nlp_put(ndlp); /* Drop the reference from the mbox */
1252
2e0fef85 1253 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
92d7f7b0
JS
1254 list_for_each_entry(next_vport, &phba->port_list, listentry) {
1255 if (next_vport->port_type == LPFC_PHYSICAL_PORT)
1256 continue;
2e0fef85 1257
92d7f7b0
JS
1258 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1259 lpfc_initial_fdisc(next_vport);
1260 else {
1261 if (phba->sli3_options &
1262 LPFC_SLI3_NPIV_ENABLED) {
1263 lpfc_vport_set_state(vport,
1264 FC_VPORT_NO_FABRIC_SUPP);
1265 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
1266 "%d (%d):0259 No NPIV Fabric "
1267 "support\n",
1268 phba->brd_no, vport->vpi);
1269 }
dea3101e 1270 }
1271 }
92d7f7b0 1272 lpfc_do_scr_ns_plogi(phba, vport);
dea3101e 1273 }
1274
1275 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1276 kfree(mp);
329f9bc7 1277 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1278 return;
1279}
1280
1281/*
1282 * This routine handles processing a NameServer REG_LOGIN mailbox
1283 * command upon completion. It is setup in the LPFC_MBOXQ
1284 * as the completion routine when the command is
1285 * handed off to the SLI layer.
1286 */
1287void
2e0fef85 1288lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1289{
2e0fef85
JS
1290 MAILBOX_t *mb = &pmb->mb;
1291 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1292 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1293 struct lpfc_vport *vport = pmb->vport;
dea3101e 1294
1295 if (mb->mbxStatus) {
92d7f7b0 1296out:
329f9bc7 1297 lpfc_nlp_put(ndlp);
dea3101e 1298 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1299 kfree(mp);
de0c5b32 1300 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85 1301 lpfc_drop_node(vport, ndlp);
dea3101e 1302
92d7f7b0
JS
1303 if (phba->fc_topology == TOPOLOGY_LOOP) {
1304 /*
1305 * RegLogin failed, use loop map to make discovery
1306 * list
1307 */
1308 lpfc_disc_list_loopmap(vport);
dea3101e 1309
92d7f7b0
JS
1310 /* Start discovery */
1311 lpfc_disc_start(vport);
1312 return;
1313 }
1314 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1315 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
1316 "%d (%d):0260 Register NameServer error: 0x%x\n",
1317 phba->brd_no, vport->vpi, mb->mbxStatus);
dea3101e 1318 return;
1319 }
1320
1321 pmb->context1 = NULL;
1322
dea3101e 1323 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e 1324 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 1325 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 1326
2e0fef85
JS
1327 if (vport->port_state < LPFC_VPORT_READY) {
1328 /* Link up discovery requires Fabric registration. */
92d7f7b0
JS
1329 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1330 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1331 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1332 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1333 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1334
1335 /* Issue SCR just before NameServer GID_FT Query */
1336 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea3101e 1337 }
1338
2e0fef85 1339 vport->fc_ns_retry = 0;
dea3101e 1340 /* Good status, issue CT Request to NameServer */
92d7f7b0 1341 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
dea3101e 1342 /* Cannot issue NameServer Query, so finish up discovery */
92d7f7b0 1343 goto out;
dea3101e 1344 }
1345
329f9bc7 1346 lpfc_nlp_put(ndlp);
dea3101e 1347 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1348 kfree(mp);
2e0fef85 1349 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1350
1351 return;
1352}
1353
1354static void
2e0fef85 1355lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1356{
2e0fef85
JS
1357 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1358 struct fc_rport *rport;
dea3101e 1359 struct lpfc_rport_data *rdata;
1360 struct fc_rport_identifiers rport_ids;
2e0fef85 1361 struct lpfc_hba *phba = vport->phba;
dea3101e 1362
1363 /* Remote port has reappeared. Re-register w/ FC transport */
68ce1eb5
AM
1364 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1365 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea3101e 1366 rport_ids.port_id = ndlp->nlp_DID;
1367 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea3101e 1368
329f9bc7
JS
1369 /*
1370 * We leave our node pointer in rport->dd_data when we unregister a
1371 * FCP target port. But fc_remote_port_add zeros the space to which
1372 * rport->dd_data points. So, if we're reusing a previously
1373 * registered port, drop the reference that we took the last time we
1374 * registered the port.
1375 */
1376 if (ndlp->rport && ndlp->rport->dd_data &&
92d7f7b0 1377 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp) {
329f9bc7
JS
1378 lpfc_nlp_put(ndlp);
1379 }
2e0fef85 1380 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
329f9bc7 1381 if (!rport || !get_device(&rport->dev)) {
dea3101e 1382 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1383 "Warning: fc_remote_port_add failed\n");
1384 return;
1385 }
1386
1387 /* initialize static port data */
1388 rport->maxframe_size = ndlp->nlp_maxframe;
1389 rport->supported_classes = ndlp->nlp_class_sup;
dea3101e 1390 rdata = rport->dd_data;
329f9bc7 1391 rdata->pnode = lpfc_nlp_get(ndlp);
23dc04f1
JSEC
1392
1393 if (ndlp->nlp_type & NLP_FCP_TARGET)
1394 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1395 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1396 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
92d7f7b0 1397 del_timer_sync(&ndlp->nlp_initiator_tmr);
23dc04f1
JSEC
1398
1399
1400 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1401 fc_remote_port_rolechg(rport, rport_ids.roles);
1402
071fbd3d 1403 if ((rport->scsi_target_id != -1) &&
92d7f7b0 1404 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
071fbd3d
JS
1405 ndlp->nlp_sid = rport->scsi_target_id;
1406 }
19a7b4ae
JSEC
1407 return;
1408}
1409
1410static void
2e0fef85 1411lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
19a7b4ae
JSEC
1412{
1413 struct fc_rport *rport = ndlp->rport;
c01f3208 1414
19a7b4ae 1415 fc_remote_port_delete(rport);
dea3101e 1416
1417 return;
1418}
1419
de0c5b32 1420static void
2e0fef85 1421lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
dea3101e 1422{
2e0fef85
JS
1423 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1424
1425 spin_lock_irq(shost->host_lock);
de0c5b32
JS
1426 switch (state) {
1427 case NLP_STE_UNUSED_NODE:
2e0fef85 1428 vport->fc_unused_cnt += count;
de0c5b32
JS
1429 break;
1430 case NLP_STE_PLOGI_ISSUE:
2e0fef85 1431 vport->fc_plogi_cnt += count;
de0c5b32
JS
1432 break;
1433 case NLP_STE_ADISC_ISSUE:
2e0fef85 1434 vport->fc_adisc_cnt += count;
dea3101e 1435 break;
de0c5b32 1436 case NLP_STE_REG_LOGIN_ISSUE:
2e0fef85 1437 vport->fc_reglogin_cnt += count;
de0c5b32
JS
1438 break;
1439 case NLP_STE_PRLI_ISSUE:
2e0fef85 1440 vport->fc_prli_cnt += count;
de0c5b32
JS
1441 break;
1442 case NLP_STE_UNMAPPED_NODE:
2e0fef85 1443 vport->fc_unmap_cnt += count;
de0c5b32
JS
1444 break;
1445 case NLP_STE_MAPPED_NODE:
2e0fef85 1446 vport->fc_map_cnt += count;
de0c5b32
JS
1447 break;
1448 case NLP_STE_NPR_NODE:
2e0fef85 1449 vport->fc_npr_cnt += count;
de0c5b32
JS
1450 break;
1451 }
2e0fef85 1452 spin_unlock_irq(shost->host_lock);
de0c5b32 1453}
66a9ed66 1454
de0c5b32 1455static void
2e0fef85 1456lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
de0c5b32
JS
1457 int old_state, int new_state)
1458{
2e0fef85
JS
1459 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1460
de0c5b32
JS
1461 if (new_state == NLP_STE_UNMAPPED_NODE) {
1462 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1463 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1464 ndlp->nlp_type |= NLP_FC_NODE;
1465 }
1466 if (new_state == NLP_STE_MAPPED_NODE)
1467 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1468 if (new_state == NLP_STE_NPR_NODE)
1469 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
1470
1471 /* Transport interface */
1472 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1473 old_state == NLP_STE_UNMAPPED_NODE)) {
2e0fef85
JS
1474 vport->phba->nport_event_cnt++;
1475 lpfc_unregister_remote_port(ndlp);
de0c5b32 1476 }
dea3101e 1477
de0c5b32
JS
1478 if (new_state == NLP_STE_MAPPED_NODE ||
1479 new_state == NLP_STE_UNMAPPED_NODE) {
2e0fef85 1480 vport->phba->nport_event_cnt++;
dea3101e 1481 /*
1482 * Tell the fc transport about the port, if we haven't
1483 * already. If we have, and it's a scsi entity, be
1484 * sure to unblock any attached scsi devices
1485 */
2e0fef85 1486 lpfc_register_remote_port(vport, ndlp);
de0c5b32 1487 }
dea3101e 1488
1489 /*
1490 * if we added to Mapped list, but the remote port
1491 * registration failed or assigned a target id outside
1492 * our presentable range - move the node to the
1493 * Unmapped List
1494 */
de0c5b32
JS
1495 if (new_state == NLP_STE_MAPPED_NODE &&
1496 (!ndlp->rport ||
1497 ndlp->rport->scsi_target_id == -1 ||
1498 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
2e0fef85 1499 spin_lock_irq(shost->host_lock);
de0c5b32 1500 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
2e0fef85
JS
1501 spin_unlock_irq(shost->host_lock);
1502 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 1503 }
de0c5b32
JS
1504}
1505
685f0bf7
JS
1506static char *
1507lpfc_nlp_state_name(char *buffer, size_t size, int state)
1508{
1509 static char *states[] = {
1510 [NLP_STE_UNUSED_NODE] = "UNUSED",
1511 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1512 [NLP_STE_ADISC_ISSUE] = "ADISC",
1513 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1514 [NLP_STE_PRLI_ISSUE] = "PRLI",
1515 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1516 [NLP_STE_MAPPED_NODE] = "MAPPED",
1517 [NLP_STE_NPR_NODE] = "NPR",
1518 };
1519
1520 if (state < ARRAY_SIZE(states) && states[state])
1521 strlcpy(buffer, states[state], size);
1522 else
1523 snprintf(buffer, size, "unknown (%d)", state);
1524 return buffer;
1525}
1526
de0c5b32 1527void
2e0fef85
JS
1528lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1529 int state)
de0c5b32 1530{
2e0fef85 1531 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
de0c5b32 1532 int old_state = ndlp->nlp_state;
685f0bf7 1533 char name1[16], name2[16];
de0c5b32 1534
2e0fef85 1535 lpfc_printf_log(vport->phba, KERN_INFO, LOG_NODE,
685f0bf7 1536 "%d:0904 NPort state transition x%06x, %s -> %s\n",
2e0fef85 1537 vport->phba->brd_no,
685f0bf7
JS
1538 ndlp->nlp_DID,
1539 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1540 lpfc_nlp_state_name(name2, sizeof(name2), state));
de0c5b32
JS
1541 if (old_state == NLP_STE_NPR_NODE &&
1542 (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1543 state != NLP_STE_NPR_NODE)
2e0fef85 1544 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32
JS
1545 if (old_state == NLP_STE_UNMAPPED_NODE) {
1546 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1547 ndlp->nlp_type &= ~NLP_FC_NODE;
1548 }
1549
685f0bf7 1550 if (list_empty(&ndlp->nlp_listp)) {
2e0fef85
JS
1551 spin_lock_irq(shost->host_lock);
1552 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1553 spin_unlock_irq(shost->host_lock);
685f0bf7 1554 } else if (old_state)
2e0fef85 1555 lpfc_nlp_counters(vport, old_state, -1);
de0c5b32
JS
1556
1557 ndlp->nlp_state = state;
2e0fef85
JS
1558 lpfc_nlp_counters(vport, state, 1);
1559 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
de0c5b32
JS
1560}
1561
1562void
2e0fef85 1563lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 1564{
2e0fef85
JS
1565 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1566
de0c5b32 1567 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
2e0fef85 1568 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32 1569 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2e0fef85
JS
1570 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1571 spin_lock_irq(shost->host_lock);
685f0bf7 1572 list_del_init(&ndlp->nlp_listp);
2e0fef85
JS
1573 spin_unlock_irq(shost->host_lock);
1574 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state, 0);
de0c5b32
JS
1575}
1576
1577void
2e0fef85 1578lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
de0c5b32 1579{
2e0fef85
JS
1580 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1581
de0c5b32 1582 if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
2e0fef85 1583 lpfc_cancel_retry_delay_tmo(vport, ndlp);
de0c5b32 1584 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
2e0fef85
JS
1585 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1586 spin_lock_irq(shost->host_lock);
685f0bf7 1587 list_del_init(&ndlp->nlp_listp);
2e0fef85 1588 spin_unlock_irq(shost->host_lock);
329f9bc7 1589 lpfc_nlp_put(ndlp);
dea3101e 1590}
1591
1592/*
1593 * Start / ReStart rescue timer for Discovery / RSCN handling
1594 */
1595void
2e0fef85 1596lpfc_set_disctmo(struct lpfc_vport *vport)
dea3101e 1597{
2e0fef85
JS
1598 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1599 struct lpfc_hba *phba = vport->phba;
dea3101e 1600 uint32_t tmo;
1601
2e0fef85 1602 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
c9f8735b
JW
1603 /* For FAN, timeout should be greater then edtov */
1604 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1605 } else {
1606 /* Normal discovery timeout should be > then ELS/CT timeout
1607 * FC spec states we need 3 * ratov for CT requests
1608 */
1609 tmo = ((phba->fc_ratov * 3) + 3);
1610 }
dea3101e 1611
2e0fef85
JS
1612 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
1613 spin_lock_irq(shost->host_lock);
1614 vport->fc_flag |= FC_DISC_TMO;
1615 spin_unlock_irq(shost->host_lock);
dea3101e 1616
1617 /* Start Discovery Timer state <hba_state> */
1618 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
92d7f7b0 1619 "%d (%d):0247 Start Discovery Timer state x%x "
dea3101e 1620 "Data: x%x x%lx x%x x%x\n",
92d7f7b0 1621 phba->brd_no, vport->vpi, vport->port_state, tmo,
2e0fef85
JS
1622 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
1623 vport->fc_adisc_cnt);
dea3101e 1624
1625 return;
1626}
1627
1628/*
1629 * Cancel rescue timer for Discovery / RSCN handling
1630 */
1631int
2e0fef85 1632lpfc_can_disctmo(struct lpfc_vport *vport)
dea3101e 1633{
2e0fef85
JS
1634 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1635 struct lpfc_hba *phba = vport->phba;
1636 unsigned long iflags;
1637
dea3101e 1638 /* Turn off discovery timer if its running */
2e0fef85
JS
1639 if (vport->fc_flag & FC_DISC_TMO) {
1640 spin_lock_irqsave(shost->host_lock, iflags);
1641 vport->fc_flag &= ~FC_DISC_TMO;
1642 spin_unlock_irqrestore(shost->host_lock, iflags);
1643 del_timer_sync(&vport->fc_disctmo);
1644 spin_lock_irqsave(&vport->work_port_lock, iflags);
1645 vport->work_port_events &= ~WORKER_DISC_TMO;
1646 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea3101e 1647 }
1648
1649 /* Cancel Discovery Timer state <hba_state> */
1650 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
92d7f7b0 1651 "%d (%d):0248 Cancel Discovery Timer state x%x "
dea3101e 1652 "Data: x%x x%x x%x\n",
92d7f7b0
JS
1653 phba->brd_no, vport->vpi, vport->port_state,
1654 vport->fc_flag, vport->fc_plogi_cnt,
1655 vport->fc_adisc_cnt);
dea3101e 1656
2fe165b6 1657 return 0;
dea3101e 1658}
1659
1660/*
1661 * Check specified ring for outstanding IOCB on the SLI queue
1662 * Return true if iocb matches the specified nport
1663 */
1664int
2e0fef85
JS
1665lpfc_check_sli_ndlp(struct lpfc_hba *phba,
1666 struct lpfc_sli_ring *pring,
1667 struct lpfc_iocbq *iocb,
1668 struct lpfc_nodelist *ndlp)
dea3101e 1669{
2e0fef85
JS
1670 struct lpfc_sli *psli = &phba->sli;
1671 IOCB_t *icmd = &iocb->iocb;
92d7f7b0
JS
1672 struct lpfc_vport *vport = ndlp->vport;
1673
1674 if (iocb->vport != vport)
1675 return 0;
1676
dea3101e 1677 if (pring->ringno == LPFC_ELS_RING) {
1678 switch (icmd->ulpCommand) {
1679 case CMD_GEN_REQUEST64_CR:
1680 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
2fe165b6 1681 return 1;
dea3101e 1682 case CMD_ELS_REQUEST64_CR:
10d4e957
JS
1683 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1684 return 1;
dea3101e 1685 case CMD_XMIT_ELS_RSP64_CX:
1686 if (iocb->context1 == (uint8_t *) ndlp)
2fe165b6 1687 return 1;
dea3101e 1688 }
a4bc3379 1689 } else if (pring->ringno == psli->extra_ring) {
dea3101e 1690
1691 } else if (pring->ringno == psli->fcp_ring) {
1692 /* Skip match check if waiting to relogin to FCP target */
1693 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
92d7f7b0 1694 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
2fe165b6 1695 return 0;
dea3101e 1696 }
1697 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
2fe165b6 1698 return 1;
dea3101e 1699 }
1700 } else if (pring->ringno == psli->next_ring) {
1701
1702 }
2fe165b6 1703 return 0;
dea3101e 1704}
1705
1706/*
1707 * Free resources / clean up outstanding I/Os
1708 * associated with nlp_rpi in the LPFC_NODELIST entry.
1709 */
1710static int
2e0fef85 1711lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 1712{
2534ba75 1713 LIST_HEAD(completions);
dea3101e 1714 struct lpfc_sli *psli;
1715 struct lpfc_sli_ring *pring;
1716 struct lpfc_iocbq *iocb, *next_iocb;
1717 IOCB_t *icmd;
1718 uint32_t rpi, i;
1719
92d7f7b0
JS
1720 lpfc_fabric_abort_nport(ndlp);
1721
dea3101e 1722 /*
1723 * Everything that matches on txcmplq will be returned
1724 * by firmware with a no rpi error.
1725 */
1726 psli = &phba->sli;
1727 rpi = ndlp->nlp_rpi;
1728 if (rpi) {
1729 /* Now process each ring */
1730 for (i = 0; i < psli->num_rings; i++) {
1731 pring = &psli->ring[i];
1732
2e0fef85 1733 spin_lock_irq(&phba->hbalock);
dea3101e 1734 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
2e0fef85 1735 list) {
dea3101e 1736 /*
1737 * Check to see if iocb matches the nport we are
1738 * looking for
1739 */
92d7f7b0
JS
1740 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
1741 ndlp))) {
dea3101e 1742 /* It matches, so deque and call compl
1743 with an error */
2534ba75
JS
1744 list_move_tail(&iocb->list,
1745 &completions);
dea3101e 1746 pring->txq_cnt--;
dea3101e 1747 }
1748 }
2e0fef85 1749 spin_unlock_irq(&phba->hbalock);
dea3101e 1750 }
1751 }
2534ba75
JS
1752
1753 while (!list_empty(&completions)) {
1754 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
92d7f7b0 1755 list_del_init(&iocb->list);
2534ba75 1756
2e0fef85
JS
1757 if (!iocb->iocb_cmpl)
1758 lpfc_sli_release_iocbq(phba, iocb);
1759 else {
2534ba75
JS
1760 icmd = &iocb->iocb;
1761 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1762 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2e0fef85
JS
1763 (iocb->iocb_cmpl)(phba, iocb, iocb);
1764 }
2534ba75
JS
1765 }
1766
2fe165b6 1767 return 0;
dea3101e 1768}
1769
1770/*
1771 * Free rpi associated with LPFC_NODELIST entry.
1772 * This routine is called from lpfc_freenode(), when we are removing
1773 * a LPFC_NODELIST entry. It is also called if the driver initiates a
1774 * LOGO that completes successfully, and we are waiting to PLOGI back
1775 * to the remote NPort. In addition, it is called after we receive
1776 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1777 * we are waiting to PLOGI back to the remote NPort.
1778 */
1779int
2e0fef85 1780lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1781{
2e0fef85
JS
1782 struct lpfc_hba *phba = vport->phba;
1783 LPFC_MBOXQ_t *mbox;
dea3101e 1784 int rc;
1785
1786 if (ndlp->nlp_rpi) {
2e0fef85
JS
1787 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1788 if (mbox) {
92d7f7b0 1789 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
ed957684 1790 mbox->vport = vport;
92d7f7b0
JS
1791 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1792 rc = lpfc_sli_issue_mbox(phba, mbox,
1793 (MBX_NOWAIT | MBX_STOP_IOCB));
dea3101e 1794 if (rc == MBX_NOT_FINISHED)
2e0fef85 1795 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 1796 }
dea3101e 1797 lpfc_no_rpi(phba, ndlp);
1798 ndlp->nlp_rpi = 0;
1799 return 1;
1800 }
1801 return 0;
1802}
1803
92d7f7b0
JS
1804void
1805lpfc_unreg_all_rpis(struct lpfc_vport *vport)
1806{
1807 struct lpfc_hba *phba = vport->phba;
1808 LPFC_MBOXQ_t *mbox;
1809 int rc;
1810
1811 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1812 if (mbox) {
1813 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
1814 mbox->vport = vport;
1815 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1816 rc = lpfc_sli_issue_mbox(phba, mbox,
1817 (MBX_NOWAIT | MBX_STOP_IOCB));
1818 if (rc == MBX_NOT_FINISHED) {
1819 mempool_free(mbox, phba->mbox_mem_pool);
1820 }
1821 }
1822}
1823
1824void
1825lpfc_unreg_default_rpis(struct lpfc_vport *vport)
1826{
1827 struct lpfc_hba *phba = vport->phba;
1828 LPFC_MBOXQ_t *mbox;
1829 int rc;
1830
1831 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1832 if (mbox) {
1833 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
1834 mbox->vport = vport;
1835 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1836 rc = lpfc_sli_issue_mbox(phba, mbox,
1837 (MBX_NOWAIT | MBX_STOP_IOCB));
1838 if (rc == MBX_NOT_FINISHED) {
1839 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_VPORT,
1840 "%d (%d):1815 Could not issue "
1841 "unreg_did (default rpis)\n",
1842 phba->brd_no, vport->vpi);
1843 mempool_free(mbox, phba->mbox_mem_pool);
1844 }
1845 }
1846}
1847
dea3101e 1848/*
1849 * Free resources associated with LPFC_NODELIST entry
1850 * so it can be freed.
1851 */
1852static int
2e0fef85 1853lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1854{
2e0fef85
JS
1855 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1856 struct lpfc_hba *phba = vport->phba;
1857 LPFC_MBOXQ_t *mb, *nextmb;
dea3101e 1858 struct lpfc_dmabuf *mp;
dea3101e 1859
1860 /* Cleanup node for NPort <nlp_DID> */
1861 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
92d7f7b0 1862 "%d (%d):0900 Cleanup node for NPort x%x "
dea3101e 1863 "Data: x%x x%x x%x\n",
92d7f7b0 1864 phba->brd_no, vport->vpi, ndlp->nlp_DID, ndlp->nlp_flag,
dea3101e 1865 ndlp->nlp_state, ndlp->nlp_rpi);
1866
2e0fef85 1867 lpfc_dequeue_node(vport, ndlp);
dea3101e 1868
dea3101e 1869 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1870 if ((mb = phba->sli.mbox_active)) {
1871 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1872 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1873 mb->context2 = NULL;
1874 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1875 }
1876 }
33ccf8d1 1877
2e0fef85 1878 spin_lock_irq(&phba->hbalock);
dea3101e 1879 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1880 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
92d7f7b0 1881 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea3101e 1882 mp = (struct lpfc_dmabuf *) (mb->context1);
1883 if (mp) {
2e0fef85 1884 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea3101e 1885 kfree(mp);
1886 }
1887 list_del(&mb->list);
1888 mempool_free(mb, phba->mbox_mem_pool);
329f9bc7 1889 lpfc_nlp_put(ndlp);
dea3101e 1890 }
1891 }
2e0fef85 1892 spin_unlock_irq(&phba->hbalock);
dea3101e 1893
07951076 1894 lpfc_els_abort(phba,ndlp);
2e0fef85 1895 spin_lock_irq(shost->host_lock);
c01f3208 1896 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 1897 spin_unlock_irq(shost->host_lock);
dea3101e 1898
5024ab17 1899 ndlp->nlp_last_elscmd = 0;
dea3101e 1900 del_timer_sync(&ndlp->nlp_delayfunc);
92d7f7b0 1901 del_timer_sync(&ndlp->nlp_initiator_tmr);
dea3101e 1902
dea3101e 1903 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1904 list_del_init(&ndlp->els_retry_evt.evt_listp);
92d7f7b0
JS
1905 if (!list_empty(&ndlp->dev_loss_evt.evt_listp))
1906 list_del_init(&ndlp->dev_loss_evt.evt_listp);
dea3101e 1907
2e0fef85 1908 lpfc_unreg_rpi(vport, ndlp);
dea3101e 1909
2fe165b6 1910 return 0;
dea3101e 1911}
1912
1913/*
1914 * Check to see if we can free the nlp back to the freelist.
1915 * If we are in the middle of using the nlp in the discovery state
1916 * machine, defer the free till we reach the end of the state machine.
1917 */
329f9bc7 1918static void
2e0fef85 1919lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea3101e 1920{
1dcb58e5 1921 struct lpfc_rport_data *rdata;
dea3101e 1922
1923 if (ndlp->nlp_flag & NLP_DELAY_TMO) {
2e0fef85 1924 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea3101e 1925 }
1926
2e0fef85 1927 lpfc_cleanup_node(vport, ndlp);
1dcb58e5 1928
2e0fef85 1929 /*
92d7f7b0
JS
1930 * We can get here with a non-NULL ndlp->rport because when we
1931 * unregister a rport we don't break the rport/node linkage. So if we
1932 * do, make sure we don't leaving any dangling pointers behind.
2e0fef85 1933 */
92d7f7b0 1934 if (ndlp->rport) {
329f9bc7
JS
1935 rdata = ndlp->rport->dd_data;
1936 rdata->pnode = NULL;
1937 ndlp->rport = NULL;
dea3101e 1938 }
dea3101e 1939}
1940
1941static int
2e0fef85
JS
1942lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1943 uint32_t did)
dea3101e 1944{
2e0fef85 1945 D_ID mydid, ndlpdid, matchdid;
dea3101e 1946
1947 if (did == Bcast_DID)
2fe165b6 1948 return 0;
dea3101e 1949
1950 if (ndlp->nlp_DID == 0) {
2fe165b6 1951 return 0;
dea3101e 1952 }
1953
1954 /* First check for Direct match */
1955 if (ndlp->nlp_DID == did)
2fe165b6 1956 return 1;
dea3101e 1957
1958 /* Next check for area/domain identically equals 0 match */
2e0fef85 1959 mydid.un.word = vport->fc_myDID;
dea3101e 1960 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
2fe165b6 1961 return 0;
dea3101e 1962 }
1963
1964 matchdid.un.word = did;
1965 ndlpdid.un.word = ndlp->nlp_DID;
1966 if (matchdid.un.b.id == ndlpdid.un.b.id) {
1967 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1968 (mydid.un.b.area == matchdid.un.b.area)) {
1969 if ((ndlpdid.un.b.domain == 0) &&
1970 (ndlpdid.un.b.area == 0)) {
1971 if (ndlpdid.un.b.id)
2fe165b6 1972 return 1;
dea3101e 1973 }
2fe165b6 1974 return 0;
dea3101e 1975 }
1976
1977 matchdid.un.word = ndlp->nlp_DID;
1978 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1979 (mydid.un.b.area == ndlpdid.un.b.area)) {
1980 if ((matchdid.un.b.domain == 0) &&
1981 (matchdid.un.b.area == 0)) {
1982 if (matchdid.un.b.id)
2fe165b6 1983 return 1;
dea3101e 1984 }
1985 }
1986 }
2fe165b6 1987 return 0;
dea3101e 1988}
1989
685f0bf7 1990/* Search for a nodelist entry */
2e0fef85
JS
1991static struct lpfc_nodelist *
1992__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea3101e 1993{
2e0fef85 1994 struct lpfc_hba *phba = vport->phba;
2fb9bd8b 1995 struct lpfc_nodelist *ndlp;
dea3101e 1996 uint32_t data1;
1997
2e0fef85
JS
1998 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1999 if (lpfc_matchdid(vport, ndlp, did)) {
685f0bf7
JS
2000 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2001 ((uint32_t) ndlp->nlp_xri << 16) |
2002 ((uint32_t) ndlp->nlp_type << 8) |
2003 ((uint32_t) ndlp->nlp_rpi & 0xff));
2004 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
92d7f7b0 2005 "%d (%d):0929 FIND node DID "
685f0bf7 2006 " Data: x%p x%x x%x x%x\n",
92d7f7b0 2007 phba->brd_no, vport->vpi,
685f0bf7
JS
2008 ndlp, ndlp->nlp_DID,
2009 ndlp->nlp_flag, data1);
685f0bf7 2010 return ndlp;
dea3101e 2011 }
2012 }
66a9ed66 2013
dea3101e 2014 /* FIND node did <did> NOT FOUND */
2fb9bd8b 2015 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
92d7f7b0
JS
2016 "%d (%d):0932 FIND node did x%x NOT FOUND.\n",
2017 phba->brd_no, vport->vpi, did);
dea3101e 2018 return NULL;
2019}
2020
2021struct lpfc_nodelist *
2e0fef85
JS
2022lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
2023{
2024 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2025 struct lpfc_nodelist *ndlp;
2026
2027 spin_lock_irq(shost->host_lock);
2028 ndlp = __lpfc_findnode_did(vport, did);
2029 spin_unlock_irq(shost->host_lock);
2030 return ndlp;
2031}
2032
2033struct lpfc_nodelist *
2034lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
dea3101e 2035{
2e0fef85 2036 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2037 struct lpfc_nodelist *ndlp;
dea3101e 2038
2e0fef85 2039 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 2040 if (!ndlp) {
2e0fef85
JS
2041 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2042 lpfc_rscn_payload_check(vport, did) == 0)
dea3101e 2043 return NULL;
2044 ndlp = (struct lpfc_nodelist *)
2e0fef85 2045 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea3101e 2046 if (!ndlp)
2047 return NULL;
2e0fef85
JS
2048 lpfc_nlp_init(vport, ndlp, did);
2049 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2050 spin_lock_irq(shost->host_lock);
dea3101e 2051 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 2052 spin_unlock_irq(shost->host_lock);
dea3101e 2053 return ndlp;
2054 }
2e0fef85
JS
2055 if (vport->fc_flag & FC_RSCN_MODE) {
2056 if (lpfc_rscn_payload_check(vport, did)) {
2057 spin_lock_irq(shost->host_lock);
dea3101e 2058 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 2059 spin_unlock_irq(shost->host_lock);
c9f8735b
JW
2060
2061 /* Since this node is marked for discovery,
2062 * delay timeout is not needed.
2063 */
fdcebe28 2064 if (ndlp->nlp_flag & NLP_DELAY_TMO)
2e0fef85 2065 lpfc_cancel_retry_delay_tmo(vport, ndlp);
071fbd3d 2066 } else
dea3101e 2067 ndlp = NULL;
2fe165b6 2068 } else {
685f0bf7
JS
2069 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
2070 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE)
dea3101e 2071 return NULL;
2e0fef85
JS
2072 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2073 spin_lock_irq(shost->host_lock);
dea3101e 2074 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 2075 spin_unlock_irq(shost->host_lock);
dea3101e 2076 }
2077 return ndlp;
2078}
2079
2080/* Build a list of nodes to discover based on the loopmap */
2081void
2e0fef85 2082lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea3101e 2083{
2e0fef85 2084 struct lpfc_hba *phba = vport->phba;
dea3101e 2085 int j;
2086 uint32_t alpa, index;
2087
2e0fef85 2088 if (!lpfc_is_link_up(phba))
dea3101e 2089 return;
2e0fef85
JS
2090
2091 if (phba->fc_topology != TOPOLOGY_LOOP)
dea3101e 2092 return;
dea3101e 2093
2094 /* Check for loop map present or not */
2095 if (phba->alpa_map[0]) {
2096 for (j = 1; j <= phba->alpa_map[0]; j++) {
2097 alpa = phba->alpa_map[j];
2e0fef85 2098 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea3101e 2099 continue;
2e0fef85 2100 lpfc_setup_disc_node(vport, alpa);
dea3101e 2101 }
2102 } else {
2103 /* No alpamap, so try all alpa's */
2104 for (j = 0; j < FC_MAXLOOP; j++) {
2105 /* If cfg_scan_down is set, start from highest
2106 * ALPA (0xef) to lowest (0x1).
2107 */
2108 if (phba->cfg_scan_down)
2109 index = j;
2110 else
2111 index = FC_MAXLOOP - j - 1;
2112 alpa = lpfcAlpaArray[index];
2e0fef85 2113 if ((vport->fc_myDID & 0xff) == alpa)
dea3101e 2114 continue;
2e0fef85 2115 lpfc_setup_disc_node(vport, alpa);
dea3101e 2116 }
2117 }
2118 return;
2119}
2120
dea3101e 2121void
2e0fef85 2122lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea3101e 2123{
dea3101e 2124 LPFC_MBOXQ_t *mbox;
2e0fef85
JS
2125 struct lpfc_sli *psli = &phba->sli;
2126 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2127 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2128 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2129 int rc;
2130
92d7f7b0
JS
2131 /*
2132 * if it's not a physical port or if we already send
2133 * clear_la then don't send it.
2134 */
2135 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2136 (vport->port_type != LPFC_PHYSICAL_PORT))
2137 return;
2138
2e0fef85
JS
2139 /* Link up discovery */
2140 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2141 phba->link_state = LPFC_CLEAR_LA;
2142 lpfc_clear_la(phba, mbox);
2143 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2144 mbox->vport = vport;
2145 rc = lpfc_sli_issue_mbox(phba, mbox, (MBX_NOWAIT |
2146 MBX_STOP_IOCB));
2147 if (rc == MBX_NOT_FINISHED) {
2148 mempool_free(mbox, phba->mbox_mem_pool);
2149 lpfc_disc_flush_list(vport);
2150 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2151 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2152 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
92d7f7b0
JS
2153 phba->link_state = LPFC_HBA_ERROR;
2154 }
2155 }
2156}
2157
2158/* Reg_vpi to tell firmware to resume normal operations */
2159void
2160lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2161{
2162 LPFC_MBOXQ_t *regvpimbox;
2163
2164 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2165 if (regvpimbox) {
2166 lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, regvpimbox);
2167 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2168 regvpimbox->vport = vport;
2169 if (lpfc_sli_issue_mbox(phba, regvpimbox,
2170 (MBX_NOWAIT | MBX_STOP_IOCB))
2171 == MBX_NOT_FINISHED) {
2172 mempool_free(regvpimbox, phba->mbox_mem_pool);
2e0fef85
JS
2173 }
2174 }
2175}
2176
2177/* Start Link up / RSCN discovery on NPR nodes */
2178void
2179lpfc_disc_start(struct lpfc_vport *vport)
2180{
2181 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2182 struct lpfc_hba *phba = vport->phba;
685f0bf7 2183 uint32_t num_sent;
dea3101e 2184 uint32_t clear_la_pending;
685f0bf7 2185 int did_changed;
dea3101e 2186
2e0fef85 2187 if (!lpfc_is_link_up(phba))
dea3101e 2188 return;
2e0fef85
JS
2189
2190 if (phba->link_state == LPFC_CLEAR_LA)
dea3101e 2191 clear_la_pending = 1;
2192 else
2193 clear_la_pending = 0;
2194
2e0fef85
JS
2195 if (vport->port_state < LPFC_VPORT_READY)
2196 vport->port_state = LPFC_DISC_AUTH;
dea3101e 2197
2e0fef85
JS
2198 lpfc_set_disctmo(vport);
2199
2200 if (vport->fc_prevDID == vport->fc_myDID)
dea3101e 2201 did_changed = 0;
2e0fef85 2202 else
dea3101e 2203 did_changed = 1;
2e0fef85
JS
2204
2205 vport->fc_prevDID = vport->fc_myDID;
2206 vport->num_disc_nodes = 0;
dea3101e 2207
2208 /* Start Discovery state <hba_state> */
2209 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
92d7f7b0 2210 "%d (%d):0202 Start Discovery hba state x%x "
dea3101e 2211 "Data: x%x x%x x%x\n",
92d7f7b0
JS
2212 phba->brd_no, vport->vpi, vport->port_state,
2213 vport->fc_flag, vport->fc_plogi_cnt,
2214 vport->fc_adisc_cnt);
dea3101e 2215
2216 /* First do ADISCs - if any */
2e0fef85 2217 num_sent = lpfc_els_disc_adisc(vport);
dea3101e 2218
2219 if (num_sent)
2220 return;
2221
92d7f7b0
JS
2222 /*
2223 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2224 * continue discovery.
2225 */
2226 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2227 !(vport->fc_flag & FC_RSCN_MODE)) {
2228 lpfc_issue_reg_vpi(phba, vport);
2229 return;
2230 }
2231
2232 /*
2233 * For SLI2, we need to set port_state to READY and continue
2234 * discovery.
2235 */
2e0fef85 2236 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
dea3101e 2237 /* If we get here, there is nothing to ADISC */
92d7f7b0 2238 if (vport->port_type == LPFC_PHYSICAL_PORT)
2e0fef85 2239 lpfc_issue_clear_la(phba, vport);
2e0fef85 2240
92d7f7b0 2241 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2e0fef85
JS
2242 vport->num_disc_nodes = 0;
2243 /* go thru NPR nodes and issue ELS PLOGIs */
2244 if (vport->fc_npr_cnt)
2245 lpfc_els_disc_plogi(vport);
2246
2247 if (!vport->num_disc_nodes) {
2248 spin_lock_irq(shost->host_lock);
2249 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2250 spin_unlock_irq(shost->host_lock);
92d7f7b0 2251 lpfc_can_disctmo(vport);
dea3101e 2252 }
2253 }
92d7f7b0 2254 vport->port_state = LPFC_VPORT_READY;
dea3101e 2255 } else {
2256 /* Next do PLOGIs - if any */
2e0fef85 2257 num_sent = lpfc_els_disc_plogi(vport);
dea3101e 2258
2259 if (num_sent)
2260 return;
2261
2e0fef85 2262 if (vport->fc_flag & FC_RSCN_MODE) {
dea3101e 2263 /* Check to see if more RSCNs came in while we
2264 * were processing this one.
2265 */
2e0fef85
JS
2266 if ((vport->fc_rscn_id_cnt == 0) &&
2267 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2268 spin_lock_irq(shost->host_lock);
2269 vport->fc_flag &= ~FC_RSCN_MODE;
2270 spin_unlock_irq(shost->host_lock);
92d7f7b0 2271 lpfc_can_disctmo(vport);
2fe165b6 2272 } else
2e0fef85 2273 lpfc_els_handle_rscn(vport);
dea3101e 2274 }
2275 }
2276 return;
2277}
2278
2279/*
2280 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2281 * ring the match the sppecified nodelist.
2282 */
2283static void
2e0fef85 2284lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 2285{
2534ba75 2286 LIST_HEAD(completions);
dea3101e 2287 struct lpfc_sli *psli;
2288 IOCB_t *icmd;
2289 struct lpfc_iocbq *iocb, *next_iocb;
2290 struct lpfc_sli_ring *pring;
dea3101e 2291
2292 psli = &phba->sli;
2293 pring = &psli->ring[LPFC_ELS_RING];
2294
2295 /* Error matching iocb on txq or txcmplq
2296 * First check the txq.
2297 */
2e0fef85 2298 spin_lock_irq(&phba->hbalock);
dea3101e 2299 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2300 if (iocb->context1 != ndlp) {
2301 continue;
2302 }
2303 icmd = &iocb->iocb;
2304 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2305 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2306
2534ba75 2307 list_move_tail(&iocb->list, &completions);
dea3101e 2308 pring->txq_cnt--;
dea3101e 2309 }
2310 }
2311
2312 /* Next check the txcmplq */
2313 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2314 if (iocb->context1 != ndlp) {
2315 continue;
2316 }
2317 icmd = &iocb->iocb;
2e0fef85
JS
2318 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2319 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
2534ba75
JS
2320 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2321 }
2322 }
2e0fef85 2323 spin_unlock_irq(&phba->hbalock);
dea3101e 2324
2534ba75
JS
2325 while (!list_empty(&completions)) {
2326 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
92d7f7b0 2327 list_del_init(&iocb->list);
dea3101e 2328
2e0fef85
JS
2329 if (!iocb->iocb_cmpl)
2330 lpfc_sli_release_iocbq(phba, iocb);
2331 else {
2534ba75
JS
2332 icmd = &iocb->iocb;
2333 icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2334 icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2335 (iocb->iocb_cmpl) (phba, iocb, iocb);
2e0fef85 2336 }
dea3101e 2337 }
dea3101e 2338}
2339
2340void
2e0fef85 2341lpfc_disc_flush_list(struct lpfc_vport *vport)
dea3101e 2342{
2343 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 2344 struct lpfc_hba *phba = vport->phba;
dea3101e 2345
2e0fef85
JS
2346 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2347 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7
JS
2348 nlp_listp) {
2349 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2350 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2351 lpfc_free_tx(phba, ndlp);
2352 lpfc_nlp_put(ndlp);
2353 }
dea3101e 2354 }
2355 }
dea3101e 2356}
2357
92d7f7b0
JS
2358void
2359lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2360{
2361 lpfc_els_flush_rscn(vport);
2362 lpfc_els_flush_cmd(vport);
2363 lpfc_disc_flush_list(vport);
2364}
2365
dea3101e 2366/*****************************************************************************/
2367/*
2368 * NAME: lpfc_disc_timeout
2369 *
2370 * FUNCTION: Fibre Channel driver discovery timeout routine.
2371 *
2372 * EXECUTION ENVIRONMENT: interrupt only
2373 *
2374 * CALLED FROM:
2375 * Timer function
2376 *
2377 * RETURNS:
2378 * none
2379 */
2380/*****************************************************************************/
2381void
2382lpfc_disc_timeout(unsigned long ptr)
2383{
2e0fef85
JS
2384 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2385 struct lpfc_hba *phba = vport->phba;
dea3101e 2386 unsigned long flags = 0;
2387
2388 if (unlikely(!phba))
2389 return;
2390
2e0fef85
JS
2391 if ((vport->work_port_events & WORKER_DISC_TMO) == 0) {
2392 spin_lock_irqsave(&vport->work_port_lock, flags);
2393 vport->work_port_events |= WORKER_DISC_TMO;
2394 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2395
92d7f7b0 2396 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 2397 if (phba->work_wait)
92d7f7b0
JS
2398 lpfc_worker_wake_up(phba);
2399 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 2400 }
dea3101e 2401 return;
2402}
2403
2404static void
2e0fef85 2405lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea3101e 2406{
2e0fef85
JS
2407 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2408 struct lpfc_hba *phba = vport->phba;
2409 struct lpfc_sli *psli = &phba->sli;
c9f8735b 2410 struct lpfc_nodelist *ndlp, *next_ndlp;
92d7f7b0 2411 LPFC_MBOXQ_t *initlinkmbox;
dea3101e 2412 int rc, clrlaerr = 0;
2413
2e0fef85 2414 if (!(vport->fc_flag & FC_DISC_TMO))
dea3101e 2415 return;
2416
2e0fef85
JS
2417 spin_lock_irq(shost->host_lock);
2418 vport->fc_flag &= ~FC_DISC_TMO;
2419 spin_unlock_irq(shost->host_lock);
dea3101e 2420
2e0fef85 2421 switch (vport->port_state) {
dea3101e 2422
2423 case LPFC_LOCAL_CFG_LINK:
2e0fef85
JS
2424 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2425 * FAN
2426 */
2427 /* FAN timeout */
ed957684 2428 lpfc_printf_log(phba, KERN_WARNING, LOG_DISCOVERY,
92d7f7b0
JS
2429 "%d (%d):0221 FAN timeout\n",
2430 phba->brd_no, vport->vpi);
dea3101e 2431
c9f8735b 2432 /* Start discovery by sending FLOGI, clean up old rpis */
2e0fef85 2433 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
685f0bf7
JS
2434 nlp_listp) {
2435 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2436 continue;
c9f8735b
JW
2437 if (ndlp->nlp_type & NLP_FABRIC) {
2438 /* Clean up the ndlp on Fabric connections */
2e0fef85 2439 lpfc_drop_node(vport, ndlp);
2fe165b6 2440 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
c9f8735b
JW
2441 /* Fail outstanding IO now since device
2442 * is marked for PLOGI.
2443 */
2e0fef85 2444 lpfc_unreg_rpi(vport, ndlp);
c9f8735b
JW
2445 }
2446 }
92d7f7b0
JS
2447 if (vport->port_state != LPFC_FLOGI) {
2448 vport->port_state = LPFC_FLOGI;
2449 lpfc_set_disctmo(vport);
2450 lpfc_initial_flogi(vport);
2451 }
dea3101e 2452 break;
2453
92d7f7b0 2454 case LPFC_FDISC:
dea3101e 2455 case LPFC_FLOGI:
2e0fef85 2456 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea3101e 2457 /* Initial FLOGI timeout */
ed957684 2458 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
2459 "%d (%d):0222 Initial %s timeout\n",
2460 phba->brd_no, vport->vpi,
2461 vport->vpi ? "FLOGI" : "FDISC");
dea3101e 2462
2463 /* Assume no Fabric and go on with discovery.
2464 * Check for outstanding ELS FLOGI to abort.
2465 */
2466
2467 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 2468 lpfc_disc_list_loopmap(vport);
dea3101e 2469
2470 /* Start discovery */
2e0fef85 2471 lpfc_disc_start(vport);
dea3101e 2472 break;
2473
2474 case LPFC_FABRIC_CFG_LINK:
2475 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2476 NameServer login */
2477 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
2478 "%d (%d):0223 Timeout while waiting for "
2479 "NameServer login\n",
2480 phba->brd_no, vport->vpi);
dea3101e 2481
2482 /* Next look for NameServer ndlp */
2e0fef85 2483 ndlp = lpfc_findnode_did(vport, NameServer_DID);
dea3101e 2484 if (ndlp)
329f9bc7 2485 lpfc_nlp_put(ndlp);
dea3101e 2486 /* Start discovery */
2e0fef85 2487 lpfc_disc_start(vport);
dea3101e 2488 break;
2489
2490 case LPFC_NS_QRY:
2491 /* Check for wait for NameServer Rsp timeout */
2492 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0 2493 "%d (%d):0224 NameServer Query timeout "
dea3101e 2494 "Data: x%x x%x\n",
92d7f7b0 2495 phba->brd_no, vport->vpi,
2e0fef85 2496 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 2497
92d7f7b0
JS
2498 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2499 /* Try it one more time */
2500 vport->fc_ns_retry++;
2501 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2502 vport->fc_ns_retry, 0);
2503 if (rc == 0)
2504 break;
dea3101e 2505 }
92d7f7b0 2506 vport->fc_ns_retry = 0;
dea3101e 2507
92d7f7b0
JS
2508 /*
2509 * Discovery is over.
2510 * set port_state to PORT_READY if SLI2.
2511 * cmpl_reg_vpi will set port_state to READY for SLI3.
2512 */
2513 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2514 lpfc_issue_reg_vpi(phba, vport);
2515 else { /* NPIV Not enabled */
2516 lpfc_issue_clear_la(phba, vport);
2517 vport->port_state = LPFC_VPORT_READY;
dea3101e 2518 }
2519
2520 /* Setup and issue mailbox INITIALIZE LINK command */
2521 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2522 if (!initlinkmbox) {
2523 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0 2524 "%d (%d):0206 Device Discovery "
dea3101e 2525 "completion error\n",
92d7f7b0 2526 phba->brd_no, vport->vpi);
2e0fef85 2527 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2528 break;
2529 }
2530
2531 lpfc_linkdown(phba);
2532 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2533 phba->cfg_link_speed);
2534 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
ed957684 2535 initlinkmbox->vport = vport;
92d7f7b0 2536 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
dea3101e 2537 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2538 (MBX_NOWAIT | MBX_STOP_IOCB));
5b8bd0c9 2539 lpfc_set_loopback_flag(phba);
dea3101e 2540 if (rc == MBX_NOT_FINISHED)
2541 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2542
2543 break;
2544
2545 case LPFC_DISC_AUTH:
2546 /* Node Authentication timeout */
ed957684 2547 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
2548 "%d (%d):0227 Node Authentication timeout\n",
2549 phba->brd_no, vport->vpi);
2e0fef85
JS
2550 lpfc_disc_flush_list(vport);
2551
92d7f7b0
JS
2552 /*
2553 * set port_state to PORT_READY if SLI2.
2554 * cmpl_reg_vpi will set port_state to READY for SLI3.
2555 */
2556 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2557 lpfc_issue_reg_vpi(phba, vport);
2558 else { /* NPIV Not enabled */
2559 lpfc_issue_clear_la(phba, vport);
2560 vport->port_state = LPFC_VPORT_READY;
dea3101e 2561 }
2562 break;
2563
2e0fef85
JS
2564 case LPFC_VPORT_READY:
2565 if (vport->fc_flag & FC_RSCN_MODE) {
ed957684 2566 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
2567 "%d (%d):0231 RSCN timeout Data: x%x "
2568 "x%x\n",
2569 phba->brd_no, vport->vpi,
2e0fef85 2570 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea3101e 2571
2572 /* Cleanup any outstanding ELS commands */
2e0fef85 2573 lpfc_els_flush_cmd(vport);
dea3101e 2574
2e0fef85
JS
2575 lpfc_els_flush_rscn(vport);
2576 lpfc_disc_flush_list(vport);
dea3101e 2577 }
2578 break;
2e0fef85 2579
92d7f7b0 2580 default:
ed957684 2581 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
2582 "%d (%d):0229 Unexpected discovery timeout, "
2583 "vport State x%x\n",
2584 phba->brd_no, vport->vpi, vport->port_state);
2e0fef85
JS
2585
2586 break;
2587 }
2588
2589 switch (phba->link_state) {
2590 case LPFC_CLEAR_LA:
92d7f7b0 2591 /* CLEAR LA timeout */
ed957684 2592 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0
JS
2593 "%d (%d):0228 CLEAR LA timeout\n",
2594 phba->brd_no, vport->vpi);
2e0fef85
JS
2595 clrlaerr = 1;
2596 break;
2597
2598 case LPFC_LINK_UNKNOWN:
2599 case LPFC_WARM_START:
2600 case LPFC_INIT_START:
2601 case LPFC_INIT_MBX_CMDS:
2602 case LPFC_LINK_DOWN:
2603 case LPFC_LINK_UP:
2604 case LPFC_HBA_ERROR:
ed957684 2605 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
92d7f7b0 2606 "%d (%d):0230 Unexpected timeout, hba link "
2e0fef85 2607 "state x%x\n",
92d7f7b0 2608 phba->brd_no, vport->vpi, phba->link_state);
2e0fef85
JS
2609 clrlaerr = 1;
2610 break;
92d7f7b0
JS
2611
2612 case LPFC_HBA_READY:
2613 break;
dea3101e 2614 }
2615
2616 if (clrlaerr) {
2e0fef85 2617 lpfc_disc_flush_list(vport);
a4bc3379 2618 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
dea3101e 2619 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2620 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2e0fef85 2621 vport->port_state = LPFC_VPORT_READY;
dea3101e 2622 }
2623
2624 return;
2625}
2626
dea3101e 2627/*
2628 * This routine handles processing a NameServer REG_LOGIN mailbox
2629 * command upon completion. It is setup in the LPFC_MBOXQ
2630 * as the completion routine when the command is
2631 * handed off to the SLI layer.
2632 */
2633void
2e0fef85 2634lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2635{
2e0fef85
JS
2636 MAILBOX_t *mb = &pmb->mb;
2637 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2638 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2639 struct lpfc_vport *vport = pmb->vport;
dea3101e 2640
2641 pmb->context1 = NULL;
2642
dea3101e 2643 ndlp->nlp_rpi = mb->un.varWords[0];
dea3101e 2644 ndlp->nlp_type |= NLP_FABRIC;
2e0fef85 2645 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea3101e 2646
2e0fef85
JS
2647 /*
2648 * Start issuing Fabric-Device Management Interface (FDMI) command to
2649 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
2650 * fdmi-on=2 (supporting RPA/hostnmae)
dea3101e 2651 */
2e0fef85
JS
2652
2653 if (phba->cfg_fdmi_on == 1)
2654 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
2655 else
2656 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
dea3101e 2657
329f9bc7
JS
2658 /* Mailbox took a reference to the node */
2659 lpfc_nlp_put(ndlp);
dea3101e 2660 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2661 kfree(mp);
329f9bc7 2662 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2663
2664 return;
2665}
2666
685f0bf7
JS
2667static int
2668lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
2669{
2670 uint16_t *rpi = param;
2671
2672 return ndlp->nlp_rpi == *rpi;
2673}
2674
2675static int
2676lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
2677{
2678 return memcmp(&ndlp->nlp_portname, param,
2679 sizeof(ndlp->nlp_portname)) == 0;
2680}
2681
dea3101e 2682struct lpfc_nodelist *
2e0fef85 2683__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea3101e 2684{
21568f53 2685 struct lpfc_nodelist *ndlp;
dea3101e 2686
2e0fef85 2687 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
685f0bf7
JS
2688 if (ndlp->nlp_state != NLP_STE_UNUSED_NODE &&
2689 filter(ndlp, param))
2690 return ndlp;
2691 }
21568f53 2692 return NULL;
dea3101e 2693}
2694
685f0bf7
JS
2695/*
2696 * Search node lists for a remote port matching filter criteria
92d7f7b0 2697 * Caller needs to hold host_lock before calling this routine.
685f0bf7
JS
2698 */
2699struct lpfc_nodelist *
2e0fef85 2700lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
685f0bf7 2701{
2e0fef85 2702 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
685f0bf7
JS
2703 struct lpfc_nodelist *ndlp;
2704
2e0fef85
JS
2705 spin_lock_irq(shost->host_lock);
2706 ndlp = __lpfc_find_node(vport, filter, param);
2707 spin_unlock_irq(shost->host_lock);
685f0bf7
JS
2708 return ndlp;
2709}
2710
2711/*
2712 * This routine looks up the ndlp lists for the given RPI. If rpi found it
2e0fef85 2713 * returns the node list element pointer else return NULL.
685f0bf7
JS
2714 */
2715struct lpfc_nodelist *
2e0fef85 2716__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
685f0bf7 2717{
2e0fef85 2718 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
685f0bf7
JS
2719}
2720
2534ba75 2721struct lpfc_nodelist *
2e0fef85 2722lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
2534ba75 2723{
2e0fef85 2724 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2534ba75
JS
2725 struct lpfc_nodelist *ndlp;
2726
2e0fef85
JS
2727 spin_lock_irq(shost->host_lock);
2728 ndlp = __lpfc_findnode_rpi(vport, rpi);
2729 spin_unlock_irq(shost->host_lock);
2534ba75
JS
2730 return ndlp;
2731}
2732
488d1469 2733/*
685f0bf7 2734 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
2e0fef85 2735 * returns the node element list pointer else return NULL.
488d1469
JS
2736 */
2737struct lpfc_nodelist *
2e0fef85 2738lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
488d1469 2739{
2e0fef85 2740 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
488d1469 2741 struct lpfc_nodelist *ndlp;
488d1469 2742
2e0fef85
JS
2743 spin_lock_irq(shost->host_lock);
2744 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
2745 spin_unlock_irq(shost->host_lock);
488d1469
JS
2746 return NULL;
2747}
2748
92d7f7b0
JS
2749void
2750lpfc_dev_loss_delay(unsigned long ptr)
2751{
2752 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2753 struct lpfc_vport *vport = ndlp->vport;
2754 struct lpfc_hba *phba = vport->phba;
2755 struct lpfc_work_evt *evtp = &ndlp->dev_loss_evt;
2756 unsigned long flags;
2757
2758 evtp = &ndlp->dev_loss_evt;
2759
2760 spin_lock_irqsave(&phba->hbalock, flags);
2761 if (!list_empty(&evtp->evt_listp)) {
2762 spin_unlock_irqrestore(&phba->hbalock, flags);
2763 return;
2764 }
2765
2766 evtp->evt_arg1 = ndlp;
2767 evtp->evt = LPFC_EVT_DEV_LOSS;
2768 list_add_tail(&evtp->evt_listp, &phba->work_list);
2769 if (phba->work_wait)
2770 lpfc_worker_wake_up(phba);
2771 spin_unlock_irqrestore(&phba->hbalock, flags);
2772 return;
2773}
2774
dea3101e 2775void
2e0fef85
JS
2776lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2777 uint32_t did)
dea3101e 2778{
2779 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
dea3101e 2780 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
92d7f7b0
JS
2781 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
2782 init_timer(&ndlp->nlp_initiator_tmr);
2783 ndlp->nlp_initiator_tmr.function = lpfc_dev_loss_delay;
2784 ndlp->nlp_initiator_tmr.data = (unsigned long)ndlp;
dea3101e 2785 init_timer(&ndlp->nlp_delayfunc);
2786 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2787 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2788 ndlp->nlp_DID = did;
2e0fef85 2789 ndlp->vport = vport;
dea3101e 2790 ndlp->nlp_sid = NLP_NO_SID;
685f0bf7 2791 INIT_LIST_HEAD(&ndlp->nlp_listp);
329f9bc7 2792 kref_init(&ndlp->kref);
dea3101e 2793 return;
2794}
329f9bc7
JS
2795
2796void
2797lpfc_nlp_release(struct kref *kref)
2798{
2799 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
2800 kref);
2e0fef85
JS
2801 lpfc_nlp_remove(ndlp->vport, ndlp);
2802 mempool_free(ndlp, ndlp->vport->phba->nlp_mem_pool);
329f9bc7
JS
2803}
2804
2805struct lpfc_nodelist *
2806lpfc_nlp_get(struct lpfc_nodelist *ndlp)
2807{
2808 if (ndlp)
2809 kref_get(&ndlp->kref);
2810 return ndlp;
2811}
2812
2813int
2814lpfc_nlp_put(struct lpfc_nodelist *ndlp)
2815{
2816 return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
2817}
This page took 0.48151 seconds and 5 git commands to generate.