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