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