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