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