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