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