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