]> Pileus Git - ~andy/linux/blob - drivers/scsi/lpfc/lpfc_hbadisc.c
[SCSI] lpfc 8.1.12 : Fix unlock inside list traversal
[~andy/linux] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-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         LIST_HEAD(completions);
1477         struct lpfc_sli *psli;
1478         struct lpfc_sli_ring *pring;
1479         struct lpfc_iocbq *iocb, *next_iocb;
1480         IOCB_t *icmd;
1481         uint32_t rpi, i;
1482
1483         /*
1484          * Everything that matches on txcmplq will be returned
1485          * by firmware with a no rpi error.
1486          */
1487         psli = &phba->sli;
1488         rpi = ndlp->nlp_rpi;
1489         if (rpi) {
1490                 /* Now process each ring */
1491                 for (i = 0; i < psli->num_rings; i++) {
1492                         pring = &psli->ring[i];
1493
1494                         spin_lock_irq(phba->host->host_lock);
1495                         list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
1496                                                 list) {
1497                                 /*
1498                                  * Check to see if iocb matches the nport we are
1499                                  * looking for
1500                                  */
1501                                 if ((lpfc_check_sli_ndlp
1502                                      (phba, pring, iocb, ndlp))) {
1503                                         /* It matches, so deque and call compl
1504                                            with an error */
1505                                         list_move_tail(&iocb->list,
1506                                                        &completions);
1507                                         pring->txq_cnt--;
1508                                 }
1509                         }
1510                         spin_unlock_irq(phba->host->host_lock);
1511
1512                 }
1513         }
1514
1515         while (!list_empty(&completions)) {
1516                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1517                 list_del(&iocb->list);
1518
1519                 if (iocb->iocb_cmpl) {
1520                         icmd = &iocb->iocb;
1521                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1522                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1523                         (iocb->iocb_cmpl) (phba, iocb, iocb);
1524                 } else
1525                         lpfc_sli_release_iocbq(phba, iocb);
1526         }
1527
1528         return 0;
1529 }
1530
1531 /*
1532  * Free rpi associated with LPFC_NODELIST entry.
1533  * This routine is called from lpfc_freenode(), when we are removing
1534  * a LPFC_NODELIST entry. It is also called if the driver initiates a
1535  * LOGO that completes successfully, and we are waiting to PLOGI back
1536  * to the remote NPort. In addition, it is called after we receive
1537  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1538  * we are waiting to PLOGI back to the remote NPort.
1539  */
1540 int
1541 lpfc_unreg_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1542 {
1543         LPFC_MBOXQ_t *mbox;
1544         int rc;
1545
1546         if (ndlp->nlp_rpi) {
1547                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1548                         lpfc_unreg_login(phba, ndlp->nlp_rpi, mbox);
1549                         mbox->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
1550                         rc = lpfc_sli_issue_mbox
1551                                     (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1552                         if (rc == MBX_NOT_FINISHED)
1553                                 mempool_free( mbox, phba->mbox_mem_pool);
1554                 }
1555                 lpfc_no_rpi(phba, ndlp);
1556                 ndlp->nlp_rpi = 0;
1557                 return 1;
1558         }
1559         return 0;
1560 }
1561
1562 /*
1563  * Free resources associated with LPFC_NODELIST entry
1564  * so it can be freed.
1565  */
1566 static int
1567 lpfc_freenode(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1568 {
1569         LPFC_MBOXQ_t       *mb;
1570         LPFC_MBOXQ_t       *nextmb;
1571         struct lpfc_dmabuf *mp;
1572
1573         /* Cleanup node for NPort <nlp_DID> */
1574         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1575                         "%d:0900 Cleanup node for NPort x%x "
1576                         "Data: x%x x%x x%x\n",
1577                         phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
1578                         ndlp->nlp_state, ndlp->nlp_rpi);
1579
1580         lpfc_nlp_list(phba, ndlp, NLP_JUST_DQ);
1581
1582         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1583         if ((mb = phba->sli.mbox_active)) {
1584                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1585                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1586                         mb->context2 = NULL;
1587                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1588                 }
1589         }
1590
1591         spin_lock_irq(phba->host->host_lock);
1592         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1593                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1594                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1595                         mp = (struct lpfc_dmabuf *) (mb->context1);
1596                         if (mp) {
1597                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1598                                 kfree(mp);
1599                         }
1600                         list_del(&mb->list);
1601                         mempool_free(mb, phba->mbox_mem_pool);
1602                 }
1603         }
1604         spin_unlock_irq(phba->host->host_lock);
1605
1606         lpfc_els_abort(phba,ndlp);
1607         spin_lock_irq(phba->host->host_lock);
1608         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1609         spin_unlock_irq(phba->host->host_lock);
1610
1611         ndlp->nlp_last_elscmd = 0;
1612         del_timer_sync(&ndlp->nlp_delayfunc);
1613
1614         if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1615                 list_del_init(&ndlp->els_retry_evt.evt_listp);
1616
1617         lpfc_unreg_rpi(phba, ndlp);
1618
1619         return 0;
1620 }
1621
1622 /*
1623  * Check to see if we can free the nlp back to the freelist.
1624  * If we are in the middle of using the nlp in the discovery state
1625  * machine, defer the free till we reach the end of the state machine.
1626  */
1627 int
1628 lpfc_nlp_remove(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1629 {
1630         struct lpfc_rport_data *rdata;
1631
1632         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1633                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1634         }
1635
1636         if (ndlp->nlp_disc_refcnt) {
1637                 spin_lock_irq(phba->host->host_lock);
1638                 ndlp->nlp_flag |= NLP_DELAY_REMOVE;
1639                 spin_unlock_irq(phba->host->host_lock);
1640         } else {
1641                 lpfc_freenode(phba, ndlp);
1642
1643                 if ((ndlp->rport) && !(phba->fc_flag & FC_UNLOADING)) {
1644                         rdata = ndlp->rport->dd_data;
1645                         rdata->pnode = NULL;
1646                         ndlp->rport = NULL;
1647                 }
1648
1649                 mempool_free( ndlp, phba->nlp_mem_pool);
1650         }
1651         return 0;
1652 }
1653
1654 static int
1655 lpfc_matchdid(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp, uint32_t did)
1656 {
1657         D_ID mydid;
1658         D_ID ndlpdid;
1659         D_ID matchdid;
1660
1661         if (did == Bcast_DID)
1662                 return 0;
1663
1664         if (ndlp->nlp_DID == 0) {
1665                 return 0;
1666         }
1667
1668         /* First check for Direct match */
1669         if (ndlp->nlp_DID == did)
1670                 return 1;
1671
1672         /* Next check for area/domain identically equals 0 match */
1673         mydid.un.word = phba->fc_myDID;
1674         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
1675                 return 0;
1676         }
1677
1678         matchdid.un.word = did;
1679         ndlpdid.un.word = ndlp->nlp_DID;
1680         if (matchdid.un.b.id == ndlpdid.un.b.id) {
1681                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1682                     (mydid.un.b.area == matchdid.un.b.area)) {
1683                         if ((ndlpdid.un.b.domain == 0) &&
1684                             (ndlpdid.un.b.area == 0)) {
1685                                 if (ndlpdid.un.b.id)
1686                                         return 1;
1687                         }
1688                         return 0;
1689                 }
1690
1691                 matchdid.un.word = ndlp->nlp_DID;
1692                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1693                     (mydid.un.b.area == ndlpdid.un.b.area)) {
1694                         if ((matchdid.un.b.domain == 0) &&
1695                             (matchdid.un.b.area == 0)) {
1696                                 if (matchdid.un.b.id)
1697                                         return 1;
1698                         }
1699                 }
1700         }
1701         return 0;
1702 }
1703
1704 /* Search for a nodelist entry on a specific list */
1705 struct lpfc_nodelist *
1706 lpfc_findnode_did(struct lpfc_hba * phba, uint32_t order, uint32_t did)
1707 {
1708         struct lpfc_nodelist *ndlp;
1709         struct list_head *lists[]={&phba->fc_nlpunmap_list,
1710                                    &phba->fc_nlpmap_list,
1711                                    &phba->fc_plogi_list,
1712                                    &phba->fc_adisc_list,
1713                                    &phba->fc_reglogin_list,
1714                                    &phba->fc_prli_list,
1715                                    &phba->fc_npr_list,
1716                                    &phba->fc_unused_list};
1717         uint32_t search[]={NLP_SEARCH_UNMAPPED,
1718                            NLP_SEARCH_MAPPED,
1719                            NLP_SEARCH_PLOGI,
1720                            NLP_SEARCH_ADISC,
1721                            NLP_SEARCH_REGLOGIN,
1722                            NLP_SEARCH_PRLI,
1723                            NLP_SEARCH_NPR,
1724                            NLP_SEARCH_UNUSED};
1725         int i;
1726         uint32_t data1;
1727
1728         spin_lock_irq(phba->host->host_lock);
1729         for (i = 0; i < ARRAY_SIZE(lists); i++ ) {
1730                 if (!(order & search[i]))
1731                         continue;
1732                 list_for_each_entry(ndlp, lists[i], nlp_listp) {
1733                         if (lpfc_matchdid(phba, ndlp, did)) {
1734                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1735                                          ((uint32_t) ndlp->nlp_xri << 16) |
1736                                          ((uint32_t) ndlp->nlp_type << 8) |
1737                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1738                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1739                                                 "%d:0929 FIND node DID "
1740                                                 " Data: x%p x%x x%x x%x\n",
1741                                                 phba->brd_no,
1742                                                 ndlp, ndlp->nlp_DID,
1743                                                 ndlp->nlp_flag, data1);
1744                                 spin_unlock_irq(phba->host->host_lock);
1745                                 return ndlp;
1746                         }
1747                 }
1748         }
1749         spin_unlock_irq(phba->host->host_lock);
1750
1751         /* FIND node did <did> NOT FOUND */
1752         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1753                         "%d:0932 FIND node did x%x NOT FOUND Data: x%x\n",
1754                         phba->brd_no, did, order);
1755         return NULL;
1756 }
1757
1758 struct lpfc_nodelist *
1759 lpfc_setup_disc_node(struct lpfc_hba * phba, uint32_t did)
1760 {
1761         struct lpfc_nodelist *ndlp;
1762         uint32_t flg;
1763
1764         ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, did);
1765         if (!ndlp) {
1766                 if ((phba->fc_flag & FC_RSCN_MODE) &&
1767                    ((lpfc_rscn_payload_check(phba, did) == 0)))
1768                         return NULL;
1769                 ndlp = (struct lpfc_nodelist *)
1770                      mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1771                 if (!ndlp)
1772                         return NULL;
1773                 lpfc_nlp_init(phba, ndlp, did);
1774                 ndlp->nlp_state = NLP_STE_NPR_NODE;
1775                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1776                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1777                 return ndlp;
1778         }
1779         if (phba->fc_flag & FC_RSCN_MODE) {
1780                 if (lpfc_rscn_payload_check(phba, did)) {
1781                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1782
1783                         /* Since this node is marked for discovery,
1784                          * delay timeout is not needed.
1785                          */
1786                         if (ndlp->nlp_flag & NLP_DELAY_TMO)
1787                                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1788                 } else
1789                         ndlp = NULL;
1790         } else {
1791                 flg = ndlp->nlp_flag & NLP_LIST_MASK;
1792                 if ((flg == NLP_ADISC_LIST) || (flg == NLP_PLOGI_LIST))
1793                         return NULL;
1794                 ndlp->nlp_state = NLP_STE_NPR_NODE;
1795                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1796                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1797         }
1798         return ndlp;
1799 }
1800
1801 /* Build a list of nodes to discover based on the loopmap */
1802 void
1803 lpfc_disc_list_loopmap(struct lpfc_hba * phba)
1804 {
1805         int j;
1806         uint32_t alpa, index;
1807
1808         if (phba->hba_state <= LPFC_LINK_DOWN) {
1809                 return;
1810         }
1811         if (phba->fc_topology != TOPOLOGY_LOOP) {
1812                 return;
1813         }
1814
1815         /* Check for loop map present or not */
1816         if (phba->alpa_map[0]) {
1817                 for (j = 1; j <= phba->alpa_map[0]; j++) {
1818                         alpa = phba->alpa_map[j];
1819
1820                         if (((phba->fc_myDID & 0xff) == alpa) || (alpa == 0)) {
1821                                 continue;
1822                         }
1823                         lpfc_setup_disc_node(phba, alpa);
1824                 }
1825         } else {
1826                 /* No alpamap, so try all alpa's */
1827                 for (j = 0; j < FC_MAXLOOP; j++) {
1828                         /* If cfg_scan_down is set, start from highest
1829                          * ALPA (0xef) to lowest (0x1).
1830                          */
1831                         if (phba->cfg_scan_down)
1832                                 index = j;
1833                         else
1834                                 index = FC_MAXLOOP - j - 1;
1835                         alpa = lpfcAlpaArray[index];
1836                         if ((phba->fc_myDID & 0xff) == alpa) {
1837                                 continue;
1838                         }
1839
1840                         lpfc_setup_disc_node(phba, alpa);
1841                 }
1842         }
1843         return;
1844 }
1845
1846 /* Start Link up / RSCN discovery on NPR list */
1847 void
1848 lpfc_disc_start(struct lpfc_hba * phba)
1849 {
1850         struct lpfc_sli *psli;
1851         LPFC_MBOXQ_t *mbox;
1852         struct lpfc_nodelist *ndlp, *next_ndlp;
1853         uint32_t did_changed, num_sent;
1854         uint32_t clear_la_pending;
1855         int rc;
1856
1857         psli = &phba->sli;
1858
1859         if (phba->hba_state <= LPFC_LINK_DOWN) {
1860                 return;
1861         }
1862         if (phba->hba_state == LPFC_CLEAR_LA)
1863                 clear_la_pending = 1;
1864         else
1865                 clear_la_pending = 0;
1866
1867         if (phba->hba_state < LPFC_HBA_READY) {
1868                 phba->hba_state = LPFC_DISC_AUTH;
1869         }
1870         lpfc_set_disctmo(phba);
1871
1872         if (phba->fc_prevDID == phba->fc_myDID) {
1873                 did_changed = 0;
1874         } else {
1875                 did_changed = 1;
1876         }
1877         phba->fc_prevDID = phba->fc_myDID;
1878         phba->num_disc_nodes = 0;
1879
1880         /* Start Discovery state <hba_state> */
1881         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1882                         "%d:0202 Start Discovery hba state x%x "
1883                         "Data: x%x x%x x%x\n",
1884                         phba->brd_no, phba->hba_state, phba->fc_flag,
1885                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1886
1887         /* If our did changed, we MUST do PLOGI */
1888         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1889                                 nlp_listp) {
1890                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1891                         if (did_changed) {
1892                                 spin_lock_irq(phba->host->host_lock);
1893                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1894                                 spin_unlock_irq(phba->host->host_lock);
1895                         }
1896                 }
1897         }
1898
1899         /* First do ADISCs - if any */
1900         num_sent = lpfc_els_disc_adisc(phba);
1901
1902         if (num_sent)
1903                 return;
1904
1905         if ((phba->hba_state < LPFC_HBA_READY) && (!clear_la_pending)) {
1906                 /* If we get here, there is nothing to ADISC */
1907                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1908                         phba->hba_state = LPFC_CLEAR_LA;
1909                         lpfc_clear_la(phba, mbox);
1910                         mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
1911                         rc = lpfc_sli_issue_mbox(phba, mbox,
1912                                                  (MBX_NOWAIT | MBX_STOP_IOCB));
1913                         if (rc == MBX_NOT_FINISHED) {
1914                                 mempool_free( mbox, phba->mbox_mem_pool);
1915                                 lpfc_disc_flush_list(phba);
1916                                 psli->ring[(psli->extra_ring)].flag &=
1917                                         ~LPFC_STOP_IOCB_EVENT;
1918                                 psli->ring[(psli->fcp_ring)].flag &=
1919                                         ~LPFC_STOP_IOCB_EVENT;
1920                                 psli->ring[(psli->next_ring)].flag &=
1921                                         ~LPFC_STOP_IOCB_EVENT;
1922                                 phba->hba_state = LPFC_HBA_READY;
1923                         }
1924                 }
1925         } else {
1926                 /* Next do PLOGIs - if any */
1927                 num_sent = lpfc_els_disc_plogi(phba);
1928
1929                 if (num_sent)
1930                         return;
1931
1932                 if (phba->fc_flag & FC_RSCN_MODE) {
1933                         /* Check to see if more RSCNs came in while we
1934                          * were processing this one.
1935                          */
1936                         if ((phba->fc_rscn_id_cnt == 0) &&
1937                             (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
1938                                 spin_lock_irq(phba->host->host_lock);
1939                                 phba->fc_flag &= ~FC_RSCN_MODE;
1940                                 spin_unlock_irq(phba->host->host_lock);
1941                         } else
1942                                 lpfc_els_handle_rscn(phba);
1943                 }
1944         }
1945         return;
1946 }
1947
1948 /*
1949  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
1950  *  ring the match the sppecified nodelist.
1951  */
1952 static void
1953 lpfc_free_tx(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1954 {
1955         LIST_HEAD(completions);
1956         struct lpfc_sli *psli;
1957         IOCB_t     *icmd;
1958         struct lpfc_iocbq    *iocb, *next_iocb;
1959         struct lpfc_sli_ring *pring;
1960
1961         psli = &phba->sli;
1962         pring = &psli->ring[LPFC_ELS_RING];
1963
1964         /* Error matching iocb on txq or txcmplq
1965          * First check the txq.
1966          */
1967         spin_lock_irq(phba->host->host_lock);
1968         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1969                 if (iocb->context1 != ndlp) {
1970                         continue;
1971                 }
1972                 icmd = &iocb->iocb;
1973                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
1974                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
1975
1976                         list_move_tail(&iocb->list, &completions);
1977                         pring->txq_cnt--;
1978                 }
1979         }
1980
1981         /* Next check the txcmplq */
1982         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1983                 if (iocb->context1 != ndlp) {
1984                         continue;
1985                 }
1986                 icmd = &iocb->iocb;
1987                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
1988                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
1989                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1990                 }
1991         }
1992         spin_unlock_irq(phba->host->host_lock);
1993
1994         while (!list_empty(&completions)) {
1995                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1996                 list_del(&iocb->list);
1997
1998                 if (iocb->iocb_cmpl) {
1999                         icmd = &iocb->iocb;
2000                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2001                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2002                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2003                 } else
2004                         lpfc_sli_release_iocbq(phba, iocb);
2005         }
2006
2007         return;
2008 }
2009
2010 void
2011 lpfc_disc_flush_list(struct lpfc_hba * phba)
2012 {
2013         struct lpfc_nodelist *ndlp, *next_ndlp;
2014
2015         if (phba->fc_plogi_cnt) {
2016                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
2017                                         nlp_listp) {
2018                         lpfc_free_tx(phba, ndlp);
2019                         lpfc_nlp_remove(phba, ndlp);
2020                 }
2021         }
2022         if (phba->fc_adisc_cnt) {
2023                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
2024                                         nlp_listp) {
2025                         lpfc_free_tx(phba, ndlp);
2026                         lpfc_nlp_remove(phba, ndlp);
2027                 }
2028         }
2029         return;
2030 }
2031
2032 /*****************************************************************************/
2033 /*
2034  * NAME:     lpfc_disc_timeout
2035  *
2036  * FUNCTION: Fibre Channel driver discovery timeout routine.
2037  *
2038  * EXECUTION ENVIRONMENT: interrupt only
2039  *
2040  * CALLED FROM:
2041  *      Timer function
2042  *
2043  * RETURNS:
2044  *      none
2045  */
2046 /*****************************************************************************/
2047 void
2048 lpfc_disc_timeout(unsigned long ptr)
2049 {
2050         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2051         unsigned long flags = 0;
2052
2053         if (unlikely(!phba))
2054                 return;
2055
2056         spin_lock_irqsave(phba->host->host_lock, flags);
2057         if (!(phba->work_hba_events & WORKER_DISC_TMO)) {
2058                 phba->work_hba_events |= WORKER_DISC_TMO;
2059                 if (phba->work_wait)
2060                         wake_up(phba->work_wait);
2061         }
2062         spin_unlock_irqrestore(phba->host->host_lock, flags);
2063         return;
2064 }
2065
2066 static void
2067 lpfc_disc_timeout_handler(struct lpfc_hba *phba)
2068 {
2069         struct lpfc_sli *psli;
2070         struct lpfc_nodelist *ndlp, *next_ndlp;
2071         LPFC_MBOXQ_t *clearlambox, *initlinkmbox;
2072         int rc, clrlaerr = 0;
2073
2074         if (unlikely(!phba))
2075                 return;
2076
2077         if (!(phba->fc_flag & FC_DISC_TMO))
2078                 return;
2079
2080         psli = &phba->sli;
2081
2082         spin_lock_irq(phba->host->host_lock);
2083         phba->fc_flag &= ~FC_DISC_TMO;
2084         spin_unlock_irq(phba->host->host_lock);
2085
2086         switch (phba->hba_state) {
2087
2088         case LPFC_LOCAL_CFG_LINK:
2089         /* hba_state is identically LPFC_LOCAL_CFG_LINK while waiting for FAN */
2090                 /* FAN timeout */
2091                 lpfc_printf_log(phba,
2092                                  KERN_WARNING,
2093                                  LOG_DISCOVERY,
2094                                  "%d:0221 FAN timeout\n",
2095                                  phba->brd_no);
2096
2097                 /* Start discovery by sending FLOGI, clean up old rpis */
2098                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
2099                                         nlp_listp) {
2100                         if (ndlp->nlp_type & NLP_FABRIC) {
2101                                 /* Clean up the ndlp on Fabric connections */
2102                                 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
2103                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2104                                 /* Fail outstanding IO now since device
2105                                  * is marked for PLOGI.
2106                                  */
2107                                 lpfc_unreg_rpi(phba, ndlp);
2108                         }
2109                 }
2110                 phba->hba_state = LPFC_FLOGI;
2111                 lpfc_set_disctmo(phba);
2112                 lpfc_initial_flogi(phba);
2113                 break;
2114
2115         case LPFC_FLOGI:
2116         /* hba_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
2117                 /* Initial FLOGI timeout */
2118                 lpfc_printf_log(phba,
2119                                  KERN_ERR,
2120                                  LOG_DISCOVERY,
2121                                  "%d:0222 Initial FLOGI timeout\n",
2122                                  phba->brd_no);
2123
2124                 /* Assume no Fabric and go on with discovery.
2125                  * Check for outstanding ELS FLOGI to abort.
2126                  */
2127
2128                 /* FLOGI failed, so just use loop map to make discovery list */
2129                 lpfc_disc_list_loopmap(phba);
2130
2131                 /* Start discovery */
2132                 lpfc_disc_start(phba);
2133                 break;
2134
2135         case LPFC_FABRIC_CFG_LINK:
2136         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2137            NameServer login */
2138                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2139                                 "%d:0223 Timeout while waiting for NameServer "
2140                                 "login\n", phba->brd_no);
2141
2142                 /* Next look for NameServer ndlp */
2143                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, NameServer_DID);
2144                 if (ndlp)
2145                         lpfc_nlp_remove(phba, ndlp);
2146                 /* Start discovery */
2147                 lpfc_disc_start(phba);
2148                 break;
2149
2150         case LPFC_NS_QRY:
2151         /* Check for wait for NameServer Rsp timeout */
2152                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2153                                 "%d:0224 NameServer Query timeout "
2154                                 "Data: x%x x%x\n",
2155                                 phba->brd_no,
2156                                 phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2157
2158                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED,
2159                                                                 NameServer_DID);
2160                 if (ndlp) {
2161                         if (phba->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2162                                 /* Try it one more time */
2163                                 rc = lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT);
2164                                 if (rc == 0)
2165                                         break;
2166                         }
2167                         phba->fc_ns_retry = 0;
2168                 }
2169
2170                 /* Nothing to authenticate, so CLEAR_LA right now */
2171                 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2172                 if (!clearlambox) {
2173                         clrlaerr = 1;
2174                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2175                                         "%d:0226 Device Discovery "
2176                                         "completion error\n",
2177                                         phba->brd_no);
2178                         phba->hba_state = LPFC_HBA_ERROR;
2179                         break;
2180                 }
2181
2182                 phba->hba_state = LPFC_CLEAR_LA;
2183                 lpfc_clear_la(phba, clearlambox);
2184                 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2185                 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2186                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2187                 if (rc == MBX_NOT_FINISHED) {
2188                         mempool_free(clearlambox, phba->mbox_mem_pool);
2189                         clrlaerr = 1;
2190                         break;
2191                 }
2192
2193                 /* Setup and issue mailbox INITIALIZE LINK command */
2194                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2195                 if (!initlinkmbox) {
2196                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2197                                         "%d:0206 Device Discovery "
2198                                         "completion error\n",
2199                                         phba->brd_no);
2200                         phba->hba_state = LPFC_HBA_ERROR;
2201                         break;
2202                 }
2203
2204                 lpfc_linkdown(phba);
2205                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2206                                phba->cfg_link_speed);
2207                 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2208                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2209                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2210                 if (rc == MBX_NOT_FINISHED)
2211                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
2212
2213                 break;
2214
2215         case LPFC_DISC_AUTH:
2216         /* Node Authentication timeout */
2217                 lpfc_printf_log(phba,
2218                                  KERN_ERR,
2219                                  LOG_DISCOVERY,
2220                                  "%d:0227 Node Authentication timeout\n",
2221                                  phba->brd_no);
2222                 lpfc_disc_flush_list(phba);
2223                 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2224                 if (!clearlambox) {
2225                         clrlaerr = 1;
2226                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2227                                         "%d:0207 Device Discovery "
2228                                         "completion error\n",
2229                                         phba->brd_no);
2230                         phba->hba_state = LPFC_HBA_ERROR;
2231                         break;
2232                 }
2233                 phba->hba_state = LPFC_CLEAR_LA;
2234                 lpfc_clear_la(phba, clearlambox);
2235                 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2236                 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2237                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2238                 if (rc == MBX_NOT_FINISHED) {
2239                         mempool_free(clearlambox, phba->mbox_mem_pool);
2240                         clrlaerr = 1;
2241                 }
2242                 break;
2243
2244         case LPFC_CLEAR_LA:
2245         /* CLEAR LA timeout */
2246                 lpfc_printf_log(phba,
2247                                  KERN_ERR,
2248                                  LOG_DISCOVERY,
2249                                  "%d:0228 CLEAR LA timeout\n",
2250                                  phba->brd_no);
2251                 clrlaerr = 1;
2252                 break;
2253
2254         case LPFC_HBA_READY:
2255                 if (phba->fc_flag & FC_RSCN_MODE) {
2256                         lpfc_printf_log(phba,
2257                                         KERN_ERR,
2258                                         LOG_DISCOVERY,
2259                                         "%d:0231 RSCN timeout Data: x%x x%x\n",
2260                                         phba->brd_no,
2261                                         phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2262
2263                         /* Cleanup any outstanding ELS commands */
2264                         lpfc_els_flush_cmd(phba);
2265
2266                         lpfc_els_flush_rscn(phba);
2267                         lpfc_disc_flush_list(phba);
2268                 }
2269                 break;
2270         }
2271
2272         if (clrlaerr) {
2273                 lpfc_disc_flush_list(phba);
2274                 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2275                 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2276                 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2277                 phba->hba_state = LPFC_HBA_READY;
2278         }
2279
2280         return;
2281 }
2282
2283 /*
2284  * This routine handles processing a NameServer REG_LOGIN mailbox
2285  * command upon completion. It is setup in the LPFC_MBOXQ
2286  * as the completion routine when the command is
2287  * handed off to the SLI layer.
2288  */
2289 void
2290 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
2291 {
2292         struct lpfc_sli *psli;
2293         MAILBOX_t *mb;
2294         struct lpfc_dmabuf *mp;
2295         struct lpfc_nodelist *ndlp;
2296
2297         psli = &phba->sli;
2298         mb = &pmb->mb;
2299
2300         ndlp = (struct lpfc_nodelist *) pmb->context2;
2301         mp = (struct lpfc_dmabuf *) (pmb->context1);
2302
2303         pmb->context1 = NULL;
2304
2305         ndlp->nlp_rpi = mb->un.varWords[0];
2306         ndlp->nlp_type |= NLP_FABRIC;
2307         ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
2308         lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
2309
2310         /* Start issuing Fabric-Device Management Interface (FDMI)
2311          * command to 0xfffffa (FDMI well known port)
2312          */
2313         if (phba->cfg_fdmi_on == 1) {
2314                 lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_DHBA);
2315         } else {
2316                 /*
2317                  * Delay issuing FDMI command if fdmi-on=2
2318                  * (supporting RPA/hostnmae)
2319                  */
2320                 mod_timer(&phba->fc_fdmitmo, jiffies + HZ * 60);
2321         }
2322
2323         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2324         kfree(mp);
2325         mempool_free( pmb, phba->mbox_mem_pool);
2326
2327         return;
2328 }
2329
2330 /*
2331  * This routine looks up the ndlp  lists
2332  * for the given RPI. If rpi found
2333  * it return the node list pointer
2334  * else return NULL.
2335  */
2336 struct lpfc_nodelist *
2337 __lpfc_findnode_rpi(struct lpfc_hba * phba, uint16_t rpi)
2338 {
2339         struct lpfc_nodelist *ndlp;
2340         struct list_head * lists[]={&phba->fc_nlpunmap_list,
2341                                     &phba->fc_nlpmap_list,
2342                                     &phba->fc_plogi_list,
2343                                     &phba->fc_adisc_list,
2344                                     &phba->fc_reglogin_list};
2345         int i;
2346
2347         for (i = 0; i < ARRAY_SIZE(lists); i++ )
2348                 list_for_each_entry(ndlp, lists[i], nlp_listp)
2349                         if (ndlp->nlp_rpi == rpi) {
2350                                 return ndlp;
2351                         }
2352         return NULL;
2353 }
2354
2355 struct lpfc_nodelist *
2356 lpfc_findnode_rpi(struct lpfc_hba * phba, uint16_t rpi)
2357 {
2358         struct lpfc_nodelist *ndlp;
2359
2360         spin_lock_irq(phba->host->host_lock);
2361         ndlp = __lpfc_findnode_rpi(phba, rpi);
2362         spin_unlock_irq(phba->host->host_lock);
2363         return ndlp;
2364 }
2365
2366 /*
2367  * This routine looks up the ndlp  lists
2368  * for the given WWPN. If WWPN found
2369  * it return the node list pointer
2370  * else return NULL.
2371  */
2372 struct lpfc_nodelist *
2373 lpfc_findnode_wwpn(struct lpfc_hba * phba, uint32_t order,
2374                    struct lpfc_name * wwpn)
2375 {
2376         struct lpfc_nodelist *ndlp;
2377         struct list_head * lists[]={&phba->fc_nlpunmap_list,
2378                                     &phba->fc_nlpmap_list,
2379                                     &phba->fc_npr_list,
2380                                     &phba->fc_plogi_list,
2381                                     &phba->fc_adisc_list,
2382                                     &phba->fc_reglogin_list,
2383                                     &phba->fc_prli_list};
2384         uint32_t search[]={NLP_SEARCH_UNMAPPED,
2385                            NLP_SEARCH_MAPPED,
2386                            NLP_SEARCH_NPR,
2387                            NLP_SEARCH_PLOGI,
2388                            NLP_SEARCH_ADISC,
2389                            NLP_SEARCH_REGLOGIN,
2390                            NLP_SEARCH_PRLI};
2391         int i;
2392
2393         spin_lock_irq(phba->host->host_lock);
2394         for (i = 0; i < ARRAY_SIZE(lists); i++ ) {
2395                 if (!(order & search[i]))
2396                         continue;
2397                 list_for_each_entry(ndlp, lists[i], nlp_listp) {
2398                         if (memcmp(&ndlp->nlp_portname, wwpn,
2399                                    sizeof(struct lpfc_name)) == 0) {
2400                                 spin_unlock_irq(phba->host->host_lock);
2401                                 return ndlp;
2402                         }
2403                 }
2404         }
2405         spin_unlock_irq(phba->host->host_lock);
2406         return NULL;
2407 }
2408
2409 void
2410 lpfc_nlp_init(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
2411                  uint32_t did)
2412 {
2413         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
2414         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2415         init_timer(&ndlp->nlp_delayfunc);
2416         ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2417         ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2418         ndlp->nlp_DID = did;
2419         ndlp->nlp_phba = phba;
2420         ndlp->nlp_sid = NLP_NO_SID;
2421         return;
2422 }