]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/lpfc/lpfc_hbadisc.c
[PATCH] lpfc 8.1.3: Protect NPL lists with host lock
[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         spin_lock_irq(phba->host->host_lock);
1101         switch (nlp->nlp_flag & NLP_LIST_MASK) {
1102         case NLP_NO_LIST: /* Not on any list */
1103                 break;
1104         case NLP_UNUSED_LIST:
1105                 phba->fc_unused_cnt--;
1106                 list_del(&nlp->nlp_listp);
1107                 break;
1108         case NLP_PLOGI_LIST:
1109                 phba->fc_plogi_cnt--;
1110                 list_del(&nlp->nlp_listp);
1111                 break;
1112         case NLP_ADISC_LIST:
1113                 phba->fc_adisc_cnt--;
1114                 list_del(&nlp->nlp_listp);
1115                 break;
1116         case NLP_REGLOGIN_LIST:
1117                 phba->fc_reglogin_cnt--;
1118                 list_del(&nlp->nlp_listp);
1119                 break;
1120         case NLP_PRLI_LIST:
1121                 phba->fc_prli_cnt--;
1122                 list_del(&nlp->nlp_listp);
1123                 break;
1124         case NLP_UNMAPPED_LIST:
1125                 phba->fc_unmap_cnt--;
1126                 list_del(&nlp->nlp_listp);
1127                 nlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1128                 nlp->nlp_type &= ~NLP_FC_NODE;
1129                 phba->nport_event_cnt++;
1130                 if (nlp->rport)
1131                         rport_del = unmapped;
1132                 break;
1133         case NLP_MAPPED_LIST:
1134                 phba->fc_map_cnt--;
1135                 list_del(&nlp->nlp_listp);
1136                 phba->nport_event_cnt++;
1137                 if (nlp->rport)
1138                         rport_del = mapped;
1139                 break;
1140         case NLP_NPR_LIST:
1141                 phba->fc_npr_cnt--;
1142                 list_del(&nlp->nlp_listp);
1143                 /* Stop delay tmo if taking node off NPR list */
1144                 if ((nlp->nlp_flag & NLP_DELAY_TMO) &&
1145                    (list != NLP_NPR_LIST)) {
1146                         nlp->nlp_flag &= ~NLP_DELAY_TMO;
1147                         nlp->nlp_last_elscmd = 0;
1148                         spin_unlock_irq(phba->host->host_lock);
1149                         del_timer_sync(&nlp->nlp_delayfunc);
1150                         spin_lock_irq(phba->host->host_lock);
1151                         if (!list_empty(&nlp->els_retry_evt.evt_listp))
1152                                 list_del_init(&nlp->els_retry_evt.evt_listp);
1153                 }
1154                 break;
1155         }
1156
1157         nlp->nlp_flag &= ~NLP_LIST_MASK;
1158
1159         /* Add NPort <did> to <num> list */
1160         lpfc_printf_log(phba,
1161                         KERN_INFO,
1162                         LOG_NODE,
1163                         "%d:0904 Add NPort x%x to %d list Data: x%x\n",
1164                         phba->brd_no,
1165                         nlp->nlp_DID, list, nlp->nlp_flag);
1166
1167         switch (list) {
1168         case NLP_NO_LIST: /* No list, just remove it */
1169                 spin_unlock_irq(phba->host->host_lock);
1170                 lpfc_nlp_remove(phba, nlp);
1171                 spin_lock_irq(phba->host->host_lock);
1172                 /* as node removed - stop further transport calls */
1173                 rport_del = none;
1174                 break;
1175         case NLP_UNUSED_LIST:
1176                 nlp->nlp_flag |= list;
1177                 /* Put it at the end of the unused list */
1178                 list_add_tail(&nlp->nlp_listp, &phba->fc_unused_list);
1179                 phba->fc_unused_cnt++;
1180                 break;
1181         case NLP_PLOGI_LIST:
1182                 nlp->nlp_flag |= list;
1183                 /* Put it at the end of the plogi list */
1184                 list_add_tail(&nlp->nlp_listp, &phba->fc_plogi_list);
1185                 phba->fc_plogi_cnt++;
1186                 break;
1187         case NLP_ADISC_LIST:
1188                 nlp->nlp_flag |= list;
1189                 /* Put it at the end of the adisc list */
1190                 list_add_tail(&nlp->nlp_listp, &phba->fc_adisc_list);
1191                 phba->fc_adisc_cnt++;
1192                 break;
1193         case NLP_REGLOGIN_LIST:
1194                 nlp->nlp_flag |= list;
1195                 /* Put it at the end of the reglogin list */
1196                 list_add_tail(&nlp->nlp_listp, &phba->fc_reglogin_list);
1197                 phba->fc_reglogin_cnt++;
1198                 break;
1199         case NLP_PRLI_LIST:
1200                 nlp->nlp_flag |= list;
1201                 /* Put it at the end of the prli list */
1202                 list_add_tail(&nlp->nlp_listp, &phba->fc_prli_list);
1203                 phba->fc_prli_cnt++;
1204                 break;
1205         case NLP_UNMAPPED_LIST:
1206                 rport_add = unmapped;
1207                 /* ensure all vestiges of "mapped" significance are gone */
1208                 nlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1209                 nlp->nlp_flag |= list;
1210                 /* Put it at the end of the unmap list */
1211                 list_add_tail(&nlp->nlp_listp, &phba->fc_nlpunmap_list);
1212                 phba->fc_unmap_cnt++;
1213                 phba->nport_event_cnt++;
1214                 /* stop nodev tmo if running */
1215                 if (nlp->nlp_flag & NLP_NODEV_TMO) {
1216                         nlp->nlp_flag &= ~NLP_NODEV_TMO;
1217                         spin_unlock_irq(phba->host->host_lock);
1218                         del_timer_sync(&nlp->nlp_tmofunc);
1219                         spin_lock_irq(phba->host->host_lock);
1220                         if (!list_empty(&nlp->nodev_timeout_evt.evt_listp))
1221                                 list_del_init(&nlp->nodev_timeout_evt.
1222                                                 evt_listp);
1223
1224                 }
1225                 nlp->nlp_type |= NLP_FC_NODE;
1226                 break;
1227         case NLP_MAPPED_LIST:
1228                 rport_add = mapped;
1229                 nlp->nlp_flag |= list;
1230                 /* Put it at the end of the map list */
1231                 list_add_tail(&nlp->nlp_listp, &phba->fc_nlpmap_list);
1232                 phba->fc_map_cnt++;
1233                 phba->nport_event_cnt++;
1234                 /* stop nodev tmo if running */
1235                 if (nlp->nlp_flag & NLP_NODEV_TMO) {
1236                         nlp->nlp_flag &= ~NLP_NODEV_TMO;
1237                         spin_unlock_irq(phba->host->host_lock);
1238                         del_timer_sync(&nlp->nlp_tmofunc);
1239                         spin_lock_irq(phba->host->host_lock);
1240                         if (!list_empty(&nlp->nodev_timeout_evt.evt_listp))
1241                                 list_del_init(&nlp->nodev_timeout_evt.
1242                                                 evt_listp);
1243
1244                 }
1245                 break;
1246         case NLP_NPR_LIST:
1247                 nlp->nlp_flag |= list;
1248                 /* Put it at the end of the npr list */
1249                 list_add_tail(&nlp->nlp_listp, &phba->fc_npr_list);
1250                 phba->fc_npr_cnt++;
1251
1252                 if (!(nlp->nlp_flag & NLP_NODEV_TMO))
1253                         mod_timer(&nlp->nlp_tmofunc,
1254                                         jiffies + HZ * phba->cfg_nodev_tmo);
1255
1256                 nlp->nlp_flag |= NLP_NODEV_TMO;
1257                 nlp->nlp_flag &= ~NLP_RCV_PLOGI;
1258                 break;
1259         case NLP_JUST_DQ:
1260                 break;
1261         }
1262
1263         spin_unlock_irq(phba->host->host_lock);
1264
1265         /*
1266          * We make all the calls into the transport after we have
1267          * moved the node between lists. This so that we don't
1268          * release the lock while in-between lists.
1269          */
1270
1271         /* Don't upcall midlayer if we're unloading */
1272         if (!(phba->fc_flag & FC_UNLOADING)) {
1273                 /*
1274                  * We revalidate the rport pointer as the "add" function
1275                  * may have removed the remote port.
1276                  */
1277                 if ((rport_del != none) && nlp->rport)
1278                         lpfc_unregister_remote_port(phba, nlp);
1279
1280                 if (rport_add != none) {
1281                         /*
1282                          * Tell the fc transport about the port, if we haven't
1283                          * already. If we have, and it's a scsi entity, be
1284                          * sure to unblock any attached scsi devices
1285                          */
1286                         if (!nlp->rport)
1287                                 lpfc_register_remote_port(phba, nlp);
1288
1289                         /*
1290                          * if we added to Mapped list, but the remote port
1291                          * registration failed or assigned a target id outside
1292                          * our presentable range - move the node to the
1293                          * Unmapped List
1294                          */
1295                         if ((rport_add == mapped) &&
1296                             ((!nlp->rport) ||
1297                              (nlp->rport->scsi_target_id == -1) ||
1298                              (nlp->rport->scsi_target_id >= MAX_FCP_TARGET))) {
1299                                 nlp->nlp_state = NLP_STE_UNMAPPED_NODE;
1300                                 spin_lock_irq(phba->host->host_lock);
1301                                 nlp->nlp_flag |= NLP_TGT_NO_SCSIID;
1302                                 spin_unlock_irq(phba->host->host_lock);
1303                                 lpfc_nlp_list(phba, nlp, NLP_UNMAPPED_LIST);
1304                         }
1305                 }
1306         }
1307         return 0;
1308 }
1309
1310 /*
1311  * Start / ReStart rescue timer for Discovery / RSCN handling
1312  */
1313 void
1314 lpfc_set_disctmo(struct lpfc_hba * phba)
1315 {
1316         uint32_t tmo;
1317
1318         if (phba->hba_state == LPFC_LOCAL_CFG_LINK) {
1319                 /* For FAN, timeout should be greater then edtov */
1320                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1321         } else {
1322                 /* Normal discovery timeout should be > then ELS/CT timeout
1323                  * FC spec states we need 3 * ratov for CT requests
1324                  */
1325                 tmo = ((phba->fc_ratov * 3) + 3);
1326         }
1327
1328         mod_timer(&phba->fc_disctmo, jiffies + HZ * tmo);
1329         spin_lock_irq(phba->host->host_lock);
1330         phba->fc_flag |= FC_DISC_TMO;
1331         spin_unlock_irq(phba->host->host_lock);
1332
1333         /* Start Discovery Timer state <hba_state> */
1334         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1335                         "%d:0247 Start Discovery Timer state x%x "
1336                         "Data: x%x x%lx x%x x%x\n",
1337                         phba->brd_no,
1338                         phba->hba_state, tmo, (unsigned long)&phba->fc_disctmo,
1339                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1340
1341         return;
1342 }
1343
1344 /*
1345  * Cancel rescue timer for Discovery / RSCN handling
1346  */
1347 int
1348 lpfc_can_disctmo(struct lpfc_hba * phba)
1349 {
1350         /* Turn off discovery timer if its running */
1351         if (phba->fc_flag & FC_DISC_TMO) {
1352                 spin_lock_irq(phba->host->host_lock);
1353                 phba->fc_flag &= ~FC_DISC_TMO;
1354                 spin_unlock_irq(phba->host->host_lock);
1355                 del_timer_sync(&phba->fc_disctmo);
1356                 phba->work_hba_events &= ~WORKER_DISC_TMO;
1357         }
1358
1359         /* Cancel Discovery Timer state <hba_state> */
1360         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1361                         "%d:0248 Cancel Discovery Timer state x%x "
1362                         "Data: x%x x%x x%x\n",
1363                         phba->brd_no, phba->hba_state, phba->fc_flag,
1364                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1365
1366         return 0;
1367 }
1368
1369 /*
1370  * Check specified ring for outstanding IOCB on the SLI queue
1371  * Return true if iocb matches the specified nport
1372  */
1373 int
1374 lpfc_check_sli_ndlp(struct lpfc_hba * phba,
1375                     struct lpfc_sli_ring * pring,
1376                     struct lpfc_iocbq * iocb, struct lpfc_nodelist * ndlp)
1377 {
1378         struct lpfc_sli *psli;
1379         IOCB_t *icmd;
1380
1381         psli = &phba->sli;
1382         icmd = &iocb->iocb;
1383         if (pring->ringno == LPFC_ELS_RING) {
1384                 switch (icmd->ulpCommand) {
1385                 case CMD_GEN_REQUEST64_CR:
1386                         if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
1387                                 return 1;
1388                 case CMD_ELS_REQUEST64_CR:
1389                 case CMD_XMIT_ELS_RSP64_CX:
1390                         if (iocb->context1 == (uint8_t *) ndlp)
1391                                 return 1;
1392                 }
1393         } else if (pring->ringno == psli->ip_ring) {
1394
1395         } else if (pring->ringno == psli->fcp_ring) {
1396                 /* Skip match check if waiting to relogin to FCP target */
1397                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
1398                   (ndlp->nlp_flag & NLP_DELAY_TMO)) {
1399                         return 0;
1400                 }
1401                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
1402                         return 1;
1403                 }
1404         } else if (pring->ringno == psli->next_ring) {
1405
1406         }
1407         return 0;
1408 }
1409
1410 /*
1411  * Free resources / clean up outstanding I/Os
1412  * associated with nlp_rpi in the LPFC_NODELIST entry.
1413  */
1414 static int
1415 lpfc_no_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1416 {
1417         struct lpfc_sli *psli;
1418         struct lpfc_sli_ring *pring;
1419         struct lpfc_iocbq *iocb, *next_iocb;
1420         IOCB_t *icmd;
1421         uint32_t rpi, i;
1422
1423         /*
1424          * Everything that matches on txcmplq will be returned
1425          * by firmware with a no rpi error.
1426          */
1427         psli = &phba->sli;
1428         rpi = ndlp->nlp_rpi;
1429         if (rpi) {
1430                 /* Now process each ring */
1431                 for (i = 0; i < psli->num_rings; i++) {
1432                         pring = &psli->ring[i];
1433
1434                         spin_lock_irq(phba->host->host_lock);
1435                         list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
1436                                                 list) {
1437                                 /*
1438                                  * Check to see if iocb matches the nport we are
1439                                  * looking for
1440                                  */
1441                                 if ((lpfc_check_sli_ndlp
1442                                      (phba, pring, iocb, ndlp))) {
1443                                         /* It matches, so deque and call compl
1444                                            with an error */
1445                                         list_del(&iocb->list);
1446                                         pring->txq_cnt--;
1447                                         if (iocb->iocb_cmpl) {
1448                                                 icmd = &iocb->iocb;
1449                                                 icmd->ulpStatus =
1450                                                     IOSTAT_LOCAL_REJECT;
1451                                                 icmd->un.ulpWord[4] =
1452                                                     IOERR_SLI_ABORTED;
1453                                                 spin_unlock_irq(phba->host->
1454                                                                 host_lock);
1455                                                 (iocb->iocb_cmpl) (phba,
1456                                                                    iocb, iocb);
1457                                                 spin_lock_irq(phba->host->
1458                                                               host_lock);
1459                                         } else
1460                                                 lpfc_sli_release_iocbq(phba,
1461                                                                        iocb);
1462                                 }
1463                         }
1464                         spin_unlock_irq(phba->host->host_lock);
1465
1466                 }
1467         }
1468         return 0;
1469 }
1470
1471 /*
1472  * Free rpi associated with LPFC_NODELIST entry.
1473  * This routine is called from lpfc_freenode(), when we are removing
1474  * a LPFC_NODELIST entry. It is also called if the driver initiates a
1475  * LOGO that completes successfully, and we are waiting to PLOGI back
1476  * to the remote NPort. In addition, it is called after we receive
1477  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1478  * we are waiting to PLOGI back to the remote NPort.
1479  */
1480 int
1481 lpfc_unreg_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1482 {
1483         LPFC_MBOXQ_t *mbox;
1484         int rc;
1485
1486         if (ndlp->nlp_rpi) {
1487                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1488                         lpfc_unreg_login(phba, ndlp->nlp_rpi, mbox);
1489                         mbox->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
1490                         rc = lpfc_sli_issue_mbox
1491                                     (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1492                         if (rc == MBX_NOT_FINISHED)
1493                                 mempool_free( mbox, phba->mbox_mem_pool);
1494                 }
1495                 lpfc_no_rpi(phba, ndlp);
1496                 ndlp->nlp_rpi = 0;
1497                 return 1;
1498         }
1499         return 0;
1500 }
1501
1502 /*
1503  * Free resources associated with LPFC_NODELIST entry
1504  * so it can be freed.
1505  */
1506 static int
1507 lpfc_freenode(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1508 {
1509         LPFC_MBOXQ_t       *mb;
1510         LPFC_MBOXQ_t       *nextmb;
1511         struct lpfc_dmabuf *mp;
1512
1513         /* Cleanup node for NPort <nlp_DID> */
1514         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1515                         "%d:0900 Cleanup node for NPort x%x "
1516                         "Data: x%x x%x x%x\n",
1517                         phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
1518                         ndlp->nlp_state, ndlp->nlp_rpi);
1519
1520         lpfc_nlp_list(phba, ndlp, NLP_JUST_DQ);
1521
1522         /*
1523          * if unloading the driver - just leave the remote port in place.
1524          * The driver unload will force the attached devices to detach
1525          * and flush cache's w/o generating flush errors.
1526          */
1527         if ((ndlp->rport) && !(phba->fc_flag & FC_UNLOADING)) {
1528                 lpfc_unregister_remote_port(phba, ndlp);
1529                 ndlp->nlp_sid = NLP_NO_SID;
1530         }
1531
1532         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1533         if ((mb = phba->sli.mbox_active)) {
1534                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1535                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1536                         mb->context2 = NULL;
1537                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1538                 }
1539         }
1540         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1541                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1542                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1543                         mp = (struct lpfc_dmabuf *) (mb->context1);
1544                         if (mp) {
1545                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1546                                 kfree(mp);
1547                         }
1548                         list_del(&mb->list);
1549                         mempool_free(mb, phba->mbox_mem_pool);
1550                 }
1551         }
1552
1553         lpfc_els_abort(phba,ndlp,0);
1554         spin_lock_irq(phba->host->host_lock);
1555         ndlp->nlp_flag &= ~(NLP_NODEV_TMO|NLP_DELAY_TMO);
1556         spin_unlock_irq(phba->host->host_lock);
1557         del_timer_sync(&ndlp->nlp_tmofunc);
1558
1559         ndlp->nlp_last_elscmd = 0;
1560         del_timer_sync(&ndlp->nlp_delayfunc);
1561
1562         if (!list_empty(&ndlp->nodev_timeout_evt.evt_listp))
1563                 list_del_init(&ndlp->nodev_timeout_evt.evt_listp);
1564         if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1565                 list_del_init(&ndlp->els_retry_evt.evt_listp);
1566
1567         lpfc_unreg_rpi(phba, ndlp);
1568
1569         return 0;
1570 }
1571
1572 /*
1573  * Check to see if we can free the nlp back to the freelist.
1574  * If we are in the middle of using the nlp in the discovery state
1575  * machine, defer the free till we reach the end of the state machine.
1576  */
1577 int
1578 lpfc_nlp_remove(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1579 {
1580         if (ndlp->nlp_flag & NLP_NODEV_TMO) {
1581                 spin_lock_irq(phba->host->host_lock);
1582                 ndlp->nlp_flag &= ~NLP_NODEV_TMO;
1583                 spin_unlock_irq(phba->host->host_lock);
1584                 del_timer_sync(&ndlp->nlp_tmofunc);
1585                 if (!list_empty(&ndlp->nodev_timeout_evt.evt_listp))
1586                         list_del_init(&ndlp->nodev_timeout_evt.evt_listp);
1587
1588         }
1589
1590
1591         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1592                 spin_lock_irq(phba->host->host_lock);
1593                 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1594                 spin_unlock_irq(phba->host->host_lock);
1595                 ndlp->nlp_last_elscmd = 0;
1596                 del_timer_sync(&ndlp->nlp_delayfunc);
1597                 if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1598                         list_del_init(&ndlp->els_retry_evt.evt_listp);
1599         }
1600
1601         if (ndlp->nlp_disc_refcnt) {
1602                 spin_lock_irq(phba->host->host_lock);
1603                 ndlp->nlp_flag |= NLP_DELAY_REMOVE;
1604                 spin_unlock_irq(phba->host->host_lock);
1605         } else {
1606                 lpfc_freenode(phba, ndlp);
1607                 mempool_free( ndlp, phba->nlp_mem_pool);
1608         }
1609         return 0;
1610 }
1611
1612 static int
1613 lpfc_matchdid(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp, uint32_t did)
1614 {
1615         D_ID mydid;
1616         D_ID ndlpdid;
1617         D_ID matchdid;
1618
1619         if (did == Bcast_DID)
1620                 return 0;
1621
1622         if (ndlp->nlp_DID == 0) {
1623                 return 0;
1624         }
1625
1626         /* First check for Direct match */
1627         if (ndlp->nlp_DID == did)
1628                 return 1;
1629
1630         /* Next check for area/domain identically equals 0 match */
1631         mydid.un.word = phba->fc_myDID;
1632         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
1633                 return 0;
1634         }
1635
1636         matchdid.un.word = did;
1637         ndlpdid.un.word = ndlp->nlp_DID;
1638         if (matchdid.un.b.id == ndlpdid.un.b.id) {
1639                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1640                     (mydid.un.b.area == matchdid.un.b.area)) {
1641                         if ((ndlpdid.un.b.domain == 0) &&
1642                             (ndlpdid.un.b.area == 0)) {
1643                                 if (ndlpdid.un.b.id)
1644                                         return 1;
1645                         }
1646                         return 0;
1647                 }
1648
1649                 matchdid.un.word = ndlp->nlp_DID;
1650                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1651                     (mydid.un.b.area == ndlpdid.un.b.area)) {
1652                         if ((matchdid.un.b.domain == 0) &&
1653                             (matchdid.un.b.area == 0)) {
1654                                 if (matchdid.un.b.id)
1655                                         return 1;
1656                         }
1657                 }
1658         }
1659         return 0;
1660 }
1661
1662 /* Search for a nodelist entry on a specific list */
1663 struct lpfc_nodelist *
1664 lpfc_findnode_did(struct lpfc_hba * phba, uint32_t order, uint32_t did)
1665 {
1666         struct lpfc_nodelist *ndlp, *next_ndlp;
1667         uint32_t data1;
1668
1669         spin_lock_irq(phba->host->host_lock);
1670         if (order & NLP_SEARCH_UNMAPPED) {
1671                 list_for_each_entry_safe(ndlp, next_ndlp,
1672                                          &phba->fc_nlpunmap_list, nlp_listp) {
1673                         if (lpfc_matchdid(phba, ndlp, did)) {
1674                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1675                                          ((uint32_t) ndlp->nlp_xri << 16) |
1676                                          ((uint32_t) ndlp->nlp_type << 8) |
1677                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1678                                 /* FIND node DID unmapped */
1679                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1680                                                 "%d:0929 FIND node DID unmapped"
1681                                                 " Data: x%p x%x x%x x%x\n",
1682                                                 phba->brd_no,
1683                                                 ndlp, ndlp->nlp_DID,
1684                                                 ndlp->nlp_flag, data1);
1685                                 spin_unlock_irq(phba->host->host_lock);
1686                                 return ndlp;
1687                         }
1688                 }
1689         }
1690
1691         if (order & NLP_SEARCH_MAPPED) {
1692                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpmap_list,
1693                                         nlp_listp) {
1694                         if (lpfc_matchdid(phba, ndlp, did)) {
1695
1696                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1697                                          ((uint32_t) ndlp->nlp_xri << 16) |
1698                                          ((uint32_t) ndlp->nlp_type << 8) |
1699                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1700                                 /* FIND node DID mapped */
1701                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1702                                                 "%d:0930 FIND node DID mapped "
1703                                                 "Data: x%p x%x x%x x%x\n",
1704                                                 phba->brd_no,
1705                                                 ndlp, ndlp->nlp_DID,
1706                                                 ndlp->nlp_flag, data1);
1707                                 spin_unlock_irq(phba->host->host_lock);
1708                                 return ndlp;
1709                         }
1710                 }
1711         }
1712
1713         if (order & NLP_SEARCH_PLOGI) {
1714                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
1715                                         nlp_listp) {
1716                         if (lpfc_matchdid(phba, ndlp, did)) {
1717
1718                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1719                                          ((uint32_t) ndlp->nlp_xri << 16) |
1720                                          ((uint32_t) ndlp->nlp_type << 8) |
1721                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1722                                 /* LOG change to PLOGI */
1723                                 /* FIND node DID plogi */
1724                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1725                                                 "%d:0908 FIND node DID plogi "
1726                                                 "Data: x%p x%x x%x x%x\n",
1727                                                 phba->brd_no,
1728                                                 ndlp, ndlp->nlp_DID,
1729                                                 ndlp->nlp_flag, data1);
1730                                 spin_unlock_irq(phba->host->host_lock);
1731                                 return ndlp;
1732                         }
1733                 }
1734         }
1735
1736         if (order & NLP_SEARCH_ADISC) {
1737                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1738                                         nlp_listp) {
1739                         if (lpfc_matchdid(phba, ndlp, did)) {
1740
1741                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1742                                          ((uint32_t) ndlp->nlp_xri << 16) |
1743                                          ((uint32_t) ndlp->nlp_type << 8) |
1744                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1745                                 /* LOG change to ADISC */
1746                                 /* FIND node DID adisc */
1747                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1748                                                 "%d:0931 FIND node DID adisc "
1749                                                 "Data: x%p x%x x%x x%x\n",
1750                                                 phba->brd_no,
1751                                                 ndlp, ndlp->nlp_DID,
1752                                                 ndlp->nlp_flag, data1);
1753                                 return ndlp;
1754                         }
1755                 }
1756         }
1757
1758         if (order & NLP_SEARCH_REGLOGIN) {
1759                 list_for_each_entry_safe(ndlp, next_ndlp,
1760                                          &phba->fc_reglogin_list, nlp_listp) {
1761                         if (lpfc_matchdid(phba, ndlp, did)) {
1762
1763                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1764                                          ((uint32_t) ndlp->nlp_xri << 16) |
1765                                          ((uint32_t) ndlp->nlp_type << 8) |
1766                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1767                                 /* LOG change to REGLOGIN */
1768                                 /* FIND node DID reglogin */
1769                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1770                                                 "%d:0931 FIND node DID reglogin"
1771                                                 " Data: x%p x%x x%x x%x\n",
1772                                                 phba->brd_no,
1773                                                 ndlp, ndlp->nlp_DID,
1774                                                 ndlp->nlp_flag, data1);
1775                                 spin_unlock_irq(phba->host->host_lock);
1776                                 return ndlp;
1777                         }
1778                 }
1779         }
1780
1781         if (order & NLP_SEARCH_PRLI) {
1782                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_prli_list,
1783                                         nlp_listp) {
1784                         if (lpfc_matchdid(phba, ndlp, did)) {
1785
1786                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1787                                          ((uint32_t) ndlp->nlp_xri << 16) |
1788                                          ((uint32_t) ndlp->nlp_type << 8) |
1789                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1790                                 /* LOG change to PRLI */
1791                                 /* FIND node DID prli */
1792                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1793                                                 "%d:0931 FIND node DID prli "
1794                                                 "Data: x%p x%x x%x x%x\n",
1795                                                 phba->brd_no,
1796                                                 ndlp, ndlp->nlp_DID,
1797                                                 ndlp->nlp_flag, data1);
1798                                 return ndlp;
1799                         }
1800                 }
1801         }
1802
1803         if (order & NLP_SEARCH_NPR) {
1804                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1805                                         nlp_listp) {
1806                         if (lpfc_matchdid(phba, ndlp, did)) {
1807
1808                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1809                                          ((uint32_t) ndlp->nlp_xri << 16) |
1810                                          ((uint32_t) ndlp->nlp_type << 8) |
1811                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1812                                 /* LOG change to NPR */
1813                                 /* FIND node DID npr */
1814                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1815                                                 "%d:0931 FIND node DID npr "
1816                                                 "Data: x%p x%x x%x x%x\n",
1817                                                 phba->brd_no,
1818                                                 ndlp, ndlp->nlp_DID,
1819                                                 ndlp->nlp_flag, data1);
1820                                 spin_unlock_irq(phba->host->host_lock);
1821                                 return ndlp;
1822                         }
1823                 }
1824         }
1825
1826         if (order & NLP_SEARCH_UNUSED) {
1827                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1828                                         nlp_listp) {
1829                         if (lpfc_matchdid(phba, ndlp, did)) {
1830
1831                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1832                                          ((uint32_t) ndlp->nlp_xri << 16) |
1833                                          ((uint32_t) ndlp->nlp_type << 8) |
1834                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1835                                 /* LOG change to UNUSED */
1836                                 /* FIND node DID unused */
1837                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1838                                                 "%d:0931 FIND node DID unused "
1839                                                 "Data: x%p x%x x%x x%x\n",
1840                                                 phba->brd_no,
1841                                                 ndlp, ndlp->nlp_DID,
1842                                                 ndlp->nlp_flag, data1);
1843                                 spin_unlock_irq(phba->host->host_lock);
1844                                 return ndlp;
1845                         }
1846                 }
1847         }
1848
1849         spin_unlock_irq(phba->host->host_lock);
1850
1851         /* FIND node did <did> NOT FOUND */
1852         lpfc_printf_log(phba,
1853                         KERN_INFO,
1854                         LOG_NODE,
1855                         "%d:0932 FIND node did x%x NOT FOUND Data: x%x\n",
1856                         phba->brd_no, did, order);
1857
1858         /* no match found */
1859         return NULL;
1860 }
1861
1862 struct lpfc_nodelist *
1863 lpfc_setup_disc_node(struct lpfc_hba * phba, uint32_t did)
1864 {
1865         struct lpfc_nodelist *ndlp;
1866         uint32_t flg;
1867
1868         ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, did);
1869         if (!ndlp) {
1870                 if ((phba->fc_flag & FC_RSCN_MODE) &&
1871                    ((lpfc_rscn_payload_check(phba, did) == 0)))
1872                         return NULL;
1873                 ndlp = (struct lpfc_nodelist *)
1874                      mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1875                 if (!ndlp)
1876                         return NULL;
1877                 lpfc_nlp_init(phba, ndlp, did);
1878                 ndlp->nlp_state = NLP_STE_NPR_NODE;
1879                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1880                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1881                 return ndlp;
1882         }
1883         if (phba->fc_flag & FC_RSCN_MODE) {
1884                 if (lpfc_rscn_payload_check(phba, did)) {
1885                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1886
1887                         /* Since this node is marked for discovery,
1888                          * delay timeout is not needed.
1889                          */
1890                         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1891                                 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1892                                 del_timer_sync(&ndlp->nlp_delayfunc);
1893                                 if (!list_empty(&ndlp->els_retry_evt.
1894                                                                 evt_listp))
1895                                         list_del_init(&ndlp->els_retry_evt.
1896                                                                 evt_listp);
1897                         }
1898                 } else {
1899                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1900                         ndlp = NULL;
1901                 }
1902         } else {
1903                 flg = ndlp->nlp_flag & NLP_LIST_MASK;
1904                 if ((flg == NLP_ADISC_LIST) || (flg == NLP_PLOGI_LIST))
1905                         return NULL;
1906                 ndlp->nlp_state = NLP_STE_NPR_NODE;
1907                 lpfc_nlp_list(phba, ndlp, NLP_NPR_LIST);
1908                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1909         }
1910         return ndlp;
1911 }
1912
1913 /* Build a list of nodes to discover based on the loopmap */
1914 void
1915 lpfc_disc_list_loopmap(struct lpfc_hba * phba)
1916 {
1917         int j;
1918         uint32_t alpa, index;
1919
1920         if (phba->hba_state <= LPFC_LINK_DOWN) {
1921                 return;
1922         }
1923         if (phba->fc_topology != TOPOLOGY_LOOP) {
1924                 return;
1925         }
1926
1927         /* Check for loop map present or not */
1928         if (phba->alpa_map[0]) {
1929                 for (j = 1; j <= phba->alpa_map[0]; j++) {
1930                         alpa = phba->alpa_map[j];
1931
1932                         if (((phba->fc_myDID & 0xff) == alpa) || (alpa == 0)) {
1933                                 continue;
1934                         }
1935                         lpfc_setup_disc_node(phba, alpa);
1936                 }
1937         } else {
1938                 /* No alpamap, so try all alpa's */
1939                 for (j = 0; j < FC_MAXLOOP; j++) {
1940                         /* If cfg_scan_down is set, start from highest
1941                          * ALPA (0xef) to lowest (0x1).
1942                          */
1943                         if (phba->cfg_scan_down)
1944                                 index = j;
1945                         else
1946                                 index = FC_MAXLOOP - j - 1;
1947                         alpa = lpfcAlpaArray[index];
1948                         if ((phba->fc_myDID & 0xff) == alpa) {
1949                                 continue;
1950                         }
1951
1952                         lpfc_setup_disc_node(phba, alpa);
1953                 }
1954         }
1955         return;
1956 }
1957
1958 /* Start Link up / RSCN discovery on NPR list */
1959 void
1960 lpfc_disc_start(struct lpfc_hba * phba)
1961 {
1962         struct lpfc_sli *psli;
1963         LPFC_MBOXQ_t *mbox;
1964         struct lpfc_nodelist *ndlp, *next_ndlp;
1965         uint32_t did_changed, num_sent;
1966         uint32_t clear_la_pending;
1967         int rc;
1968
1969         psli = &phba->sli;
1970
1971         if (phba->hba_state <= LPFC_LINK_DOWN) {
1972                 return;
1973         }
1974         if (phba->hba_state == LPFC_CLEAR_LA)
1975                 clear_la_pending = 1;
1976         else
1977                 clear_la_pending = 0;
1978
1979         if (phba->hba_state < LPFC_HBA_READY) {
1980                 phba->hba_state = LPFC_DISC_AUTH;
1981         }
1982         lpfc_set_disctmo(phba);
1983
1984         if (phba->fc_prevDID == phba->fc_myDID) {
1985                 did_changed = 0;
1986         } else {
1987                 did_changed = 1;
1988         }
1989         phba->fc_prevDID = phba->fc_myDID;
1990         phba->num_disc_nodes = 0;
1991
1992         /* Start Discovery state <hba_state> */
1993         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1994                         "%d:0202 Start Discovery hba state x%x "
1995                         "Data: x%x x%x x%x\n",
1996                         phba->brd_no, phba->hba_state, phba->fc_flag,
1997                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1998
1999         /* If our did changed, we MUST do PLOGI */
2000         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
2001                                 nlp_listp) {
2002                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2003                         if (did_changed) {
2004                                 spin_lock_irq(phba->host->host_lock);
2005                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2006                                 spin_unlock_irq(phba->host->host_lock);
2007                         }
2008                 }
2009         }
2010
2011         /* First do ADISCs - if any */
2012         num_sent = lpfc_els_disc_adisc(phba);
2013
2014         if (num_sent)
2015                 return;
2016
2017         if ((phba->hba_state < LPFC_HBA_READY) && (!clear_la_pending)) {
2018                 /* If we get here, there is nothing to ADISC */
2019                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
2020                         phba->hba_state = LPFC_CLEAR_LA;
2021                         lpfc_clear_la(phba, mbox);
2022                         mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2023                         rc = lpfc_sli_issue_mbox(phba, mbox,
2024                                                  (MBX_NOWAIT | MBX_STOP_IOCB));
2025                         if (rc == MBX_NOT_FINISHED) {
2026                                 mempool_free( mbox, phba->mbox_mem_pool);
2027                                 lpfc_disc_flush_list(phba);
2028                                 psli->ring[(psli->ip_ring)].flag &=
2029                                         ~LPFC_STOP_IOCB_EVENT;
2030                                 psli->ring[(psli->fcp_ring)].flag &=
2031                                         ~LPFC_STOP_IOCB_EVENT;
2032                                 psli->ring[(psli->next_ring)].flag &=
2033                                         ~LPFC_STOP_IOCB_EVENT;
2034                                 phba->hba_state = LPFC_HBA_READY;
2035                         }
2036                 }
2037         } else {
2038                 /* Next do PLOGIs - if any */
2039                 num_sent = lpfc_els_disc_plogi(phba);
2040
2041                 if (num_sent)
2042                         return;
2043
2044                 if (phba->fc_flag & FC_RSCN_MODE) {
2045                         /* Check to see if more RSCNs came in while we
2046                          * were processing this one.
2047                          */
2048                         if ((phba->fc_rscn_id_cnt == 0) &&
2049                             (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
2050                                 spin_lock_irq(phba->host->host_lock);
2051                                 phba->fc_flag &= ~FC_RSCN_MODE;
2052                                 spin_unlock_irq(phba->host->host_lock);
2053                         } else
2054                                 lpfc_els_handle_rscn(phba);
2055                 }
2056         }
2057         return;
2058 }
2059
2060 /*
2061  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2062  *  ring the match the sppecified nodelist.
2063  */
2064 static void
2065 lpfc_free_tx(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
2066 {
2067         struct lpfc_sli *psli;
2068         IOCB_t     *icmd;
2069         struct lpfc_iocbq    *iocb, *next_iocb;
2070         struct lpfc_sli_ring *pring;
2071         struct lpfc_dmabuf   *mp;
2072
2073         psli = &phba->sli;
2074         pring = &psli->ring[LPFC_ELS_RING];
2075
2076         /* Error matching iocb on txq or txcmplq
2077          * First check the txq.
2078          */
2079         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2080                 if (iocb->context1 != ndlp) {
2081                         continue;
2082                 }
2083                 icmd = &iocb->iocb;
2084                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2085                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2086
2087                         list_del(&iocb->list);
2088                         pring->txq_cnt--;
2089                         lpfc_els_free_iocb(phba, iocb);
2090                 }
2091         }
2092
2093         /* Next check the txcmplq */
2094         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2095                 if (iocb->context1 != ndlp) {
2096                         continue;
2097                 }
2098                 icmd = &iocb->iocb;
2099                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2100                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2101
2102                         iocb->iocb_cmpl = NULL;
2103                         /* context2 = cmd, context2->next = rsp, context3 =
2104                            bpl */
2105                         if (iocb->context2) {
2106                                 /* Free the response IOCB before handling the
2107                                    command. */
2108
2109                                 mp = (struct lpfc_dmabuf *) (iocb->context2);
2110                                 mp = list_get_first(&mp->list,
2111                                                     struct lpfc_dmabuf,
2112                                                     list);
2113                                 if (mp) {
2114                                         /* Delay before releasing rsp buffer to
2115                                          * give UNREG mbox a chance to take
2116                                          * effect.
2117                                          */
2118                                         list_add(&mp->list,
2119                                                 &phba->freebufList);
2120                                 }
2121                                 lpfc_mbuf_free(phba,
2122                                                ((struct lpfc_dmabuf *)
2123                                                 iocb->context2)->virt,
2124                                                ((struct lpfc_dmabuf *)
2125                                                 iocb->context2)->phys);
2126                                 kfree(iocb->context2);
2127                         }
2128
2129                         if (iocb->context3) {
2130                                 lpfc_mbuf_free(phba,
2131                                                ((struct lpfc_dmabuf *)
2132                                                 iocb->context3)->virt,
2133                                                ((struct lpfc_dmabuf *)
2134                                                 iocb->context3)->phys);
2135                                 kfree(iocb->context3);
2136                         }
2137                 }
2138         }
2139
2140         return;
2141 }
2142
2143 void
2144 lpfc_disc_flush_list(struct lpfc_hba * phba)
2145 {
2146         struct lpfc_nodelist *ndlp, *next_ndlp;
2147
2148         if (phba->fc_plogi_cnt) {
2149                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
2150                                         nlp_listp) {
2151                         lpfc_free_tx(phba, ndlp);
2152                         lpfc_nlp_remove(phba, ndlp);
2153                 }
2154         }
2155         if (phba->fc_adisc_cnt) {
2156                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
2157                                         nlp_listp) {
2158                         lpfc_free_tx(phba, ndlp);
2159                         lpfc_nlp_remove(phba, ndlp);
2160                 }
2161         }
2162         return;
2163 }
2164
2165 /*****************************************************************************/
2166 /*
2167  * NAME:     lpfc_disc_timeout
2168  *
2169  * FUNCTION: Fibre Channel driver discovery timeout routine.
2170  *
2171  * EXECUTION ENVIRONMENT: interrupt only
2172  *
2173  * CALLED FROM:
2174  *      Timer function
2175  *
2176  * RETURNS:
2177  *      none
2178  */
2179 /*****************************************************************************/
2180 void
2181 lpfc_disc_timeout(unsigned long ptr)
2182 {
2183         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2184         unsigned long flags = 0;
2185
2186         if (unlikely(!phba))
2187                 return;
2188
2189         spin_lock_irqsave(phba->host->host_lock, flags);
2190         if (!(phba->work_hba_events & WORKER_DISC_TMO)) {
2191                 phba->work_hba_events |= WORKER_DISC_TMO;
2192                 if (phba->work_wait)
2193                         wake_up(phba->work_wait);
2194         }
2195         spin_unlock_irqrestore(phba->host->host_lock, flags);
2196         return;
2197 }
2198
2199 static void
2200 lpfc_disc_timeout_handler(struct lpfc_hba *phba)
2201 {
2202         struct lpfc_sli *psli;
2203         struct lpfc_nodelist *ndlp, *next_ndlp;
2204         LPFC_MBOXQ_t *clearlambox, *initlinkmbox;
2205         int rc, clrlaerr = 0;
2206
2207         if (unlikely(!phba))
2208                 return;
2209
2210         if (!(phba->fc_flag & FC_DISC_TMO))
2211                 return;
2212
2213         psli = &phba->sli;
2214
2215         spin_lock_irq(phba->host->host_lock);
2216         phba->fc_flag &= ~FC_DISC_TMO;
2217         spin_unlock_irq(phba->host->host_lock);
2218
2219         switch (phba->hba_state) {
2220
2221         case LPFC_LOCAL_CFG_LINK:
2222         /* hba_state is identically LPFC_LOCAL_CFG_LINK while waiting for FAN */
2223                 /* FAN timeout */
2224                 lpfc_printf_log(phba,
2225                                  KERN_WARNING,
2226                                  LOG_DISCOVERY,
2227                                  "%d:0221 FAN timeout\n",
2228                                  phba->brd_no);
2229
2230                 /* Start discovery by sending FLOGI, clean up old rpis */
2231                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
2232                                         nlp_listp) {
2233                         if (ndlp->nlp_type & NLP_FABRIC) {
2234                                 /* Clean up the ndlp on Fabric connections */
2235                                 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
2236                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2237                                 /* Fail outstanding IO now since device
2238                                  * is marked for PLOGI.
2239                                  */
2240                                 lpfc_unreg_rpi(phba, ndlp);
2241                         }
2242                 }
2243                 phba->hba_state = LPFC_FLOGI;
2244                 lpfc_set_disctmo(phba);
2245                 lpfc_initial_flogi(phba);
2246                 break;
2247
2248         case LPFC_FLOGI:
2249         /* hba_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
2250                 /* Initial FLOGI timeout */
2251                 lpfc_printf_log(phba,
2252                                  KERN_ERR,
2253                                  LOG_DISCOVERY,
2254                                  "%d:0222 Initial FLOGI timeout\n",
2255                                  phba->brd_no);
2256
2257                 /* Assume no Fabric and go on with discovery.
2258                  * Check for outstanding ELS FLOGI to abort.
2259                  */
2260
2261                 /* FLOGI failed, so just use loop map to make discovery list */
2262                 lpfc_disc_list_loopmap(phba);
2263
2264                 /* Start discovery */
2265                 lpfc_disc_start(phba);
2266                 break;
2267
2268         case LPFC_FABRIC_CFG_LINK:
2269         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2270            NameServer login */
2271                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2272                                 "%d:0223 Timeout while waiting for NameServer "
2273                                 "login\n", phba->brd_no);
2274
2275                 /* Next look for NameServer ndlp */
2276                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, NameServer_DID);
2277                 if (ndlp)
2278                         lpfc_nlp_remove(phba, ndlp);
2279                 /* Start discovery */
2280                 lpfc_disc_start(phba);
2281                 break;
2282
2283         case LPFC_NS_QRY:
2284         /* Check for wait for NameServer Rsp timeout */
2285                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2286                                 "%d:0224 NameServer Query timeout "
2287                                 "Data: x%x x%x\n",
2288                                 phba->brd_no,
2289                                 phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2290
2291                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED,
2292                                                                 NameServer_DID);
2293                 if (ndlp) {
2294                         if (phba->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2295                                 /* Try it one more time */
2296                                 rc = lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT);
2297                                 if (rc == 0)
2298                                         break;
2299                         }
2300                         phba->fc_ns_retry = 0;
2301                 }
2302
2303                 /* Nothing to authenticate, so CLEAR_LA right now */
2304                 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2305                 if (!clearlambox) {
2306                         clrlaerr = 1;
2307                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2308                                         "%d:0226 Device Discovery "
2309                                         "completion error\n",
2310                                         phba->brd_no);
2311                         phba->hba_state = LPFC_HBA_ERROR;
2312                         break;
2313                 }
2314
2315                 phba->hba_state = LPFC_CLEAR_LA;
2316                 lpfc_clear_la(phba, clearlambox);
2317                 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2318                 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2319                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2320                 if (rc == MBX_NOT_FINISHED) {
2321                         mempool_free(clearlambox, phba->mbox_mem_pool);
2322                         clrlaerr = 1;
2323                         break;
2324                 }
2325
2326                 /* Setup and issue mailbox INITIALIZE LINK command */
2327                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2328                 if (!initlinkmbox) {
2329                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2330                                         "%d:0226 Device Discovery "
2331                                         "completion error\n",
2332                                         phba->brd_no);
2333                         phba->hba_state = LPFC_HBA_ERROR;
2334                         break;
2335                 }
2336
2337                 lpfc_linkdown(phba);
2338                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2339                                phba->cfg_link_speed);
2340                 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2341                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2342                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2343                 if (rc == MBX_NOT_FINISHED)
2344                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
2345
2346                 break;
2347
2348         case LPFC_DISC_AUTH:
2349         /* Node Authentication timeout */
2350                 lpfc_printf_log(phba,
2351                                  KERN_ERR,
2352                                  LOG_DISCOVERY,
2353                                  "%d:0227 Node Authentication timeout\n",
2354                                  phba->brd_no);
2355                 lpfc_disc_flush_list(phba);
2356                 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2357                 if (!clearlambox) {
2358                         clrlaerr = 1;
2359                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2360                                         "%d:0226 Device Discovery "
2361                                         "completion error\n",
2362                                         phba->brd_no);
2363                         phba->hba_state = LPFC_HBA_ERROR;
2364                         break;
2365                 }
2366                 phba->hba_state = LPFC_CLEAR_LA;
2367                 lpfc_clear_la(phba, clearlambox);
2368                 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2369                 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2370                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2371                 if (rc == MBX_NOT_FINISHED) {
2372                         mempool_free(clearlambox, phba->mbox_mem_pool);
2373                         clrlaerr = 1;
2374                 }
2375                 break;
2376
2377         case LPFC_CLEAR_LA:
2378         /* CLEAR LA timeout */
2379                 lpfc_printf_log(phba,
2380                                  KERN_ERR,
2381                                  LOG_DISCOVERY,
2382                                  "%d:0228 CLEAR LA timeout\n",
2383                                  phba->brd_no);
2384                 clrlaerr = 1;
2385                 break;
2386
2387         case LPFC_HBA_READY:
2388                 if (phba->fc_flag & FC_RSCN_MODE) {
2389                         lpfc_printf_log(phba,
2390                                         KERN_ERR,
2391                                         LOG_DISCOVERY,
2392                                         "%d:0231 RSCN timeout Data: x%x x%x\n",
2393                                         phba->brd_no,
2394                                         phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2395
2396                         /* Cleanup any outstanding ELS commands */
2397                         lpfc_els_flush_cmd(phba);
2398
2399                         lpfc_els_flush_rscn(phba);
2400                         lpfc_disc_flush_list(phba);
2401                 }
2402                 break;
2403         }
2404
2405         if (clrlaerr) {
2406                 lpfc_disc_flush_list(phba);
2407                 psli->ring[(psli->ip_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2408                 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2409                 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2410                 phba->hba_state = LPFC_HBA_READY;
2411         }
2412
2413         return;
2414 }
2415
2416 static void
2417 lpfc_nodev_timeout(unsigned long ptr)
2418 {
2419         struct lpfc_hba *phba;
2420         struct lpfc_nodelist *ndlp;
2421         unsigned long iflag;
2422         struct lpfc_work_evt  *evtp;
2423
2424         ndlp = (struct lpfc_nodelist *)ptr;
2425         phba = ndlp->nlp_phba;
2426         evtp = &ndlp->nodev_timeout_evt;
2427         spin_lock_irqsave(phba->host->host_lock, iflag);
2428
2429         if (!list_empty(&evtp->evt_listp)) {
2430                 spin_unlock_irqrestore(phba->host->host_lock, iflag);
2431                 return;
2432         }
2433         evtp->evt_arg1  = ndlp;
2434         evtp->evt       = LPFC_EVT_NODEV_TMO;
2435         list_add_tail(&evtp->evt_listp, &phba->work_list);
2436         if (phba->work_wait)
2437                 wake_up(phba->work_wait);
2438
2439         spin_unlock_irqrestore(phba->host->host_lock, iflag);
2440         return;
2441 }
2442
2443
2444 /*
2445  * This routine handles processing a NameServer REG_LOGIN mailbox
2446  * command upon completion. It is setup in the LPFC_MBOXQ
2447  * as the completion routine when the command is
2448  * handed off to the SLI layer.
2449  */
2450 void
2451 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
2452 {
2453         struct lpfc_sli *psli;
2454         MAILBOX_t *mb;
2455         struct lpfc_dmabuf *mp;
2456         struct lpfc_nodelist *ndlp;
2457
2458         psli = &phba->sli;
2459         mb = &pmb->mb;
2460
2461         ndlp = (struct lpfc_nodelist *) pmb->context2;
2462         mp = (struct lpfc_dmabuf *) (pmb->context1);
2463
2464         pmb->context1 = NULL;
2465
2466         ndlp->nlp_rpi = mb->un.varWords[0];
2467         ndlp->nlp_type |= NLP_FABRIC;
2468         ndlp->nlp_state = NLP_STE_UNMAPPED_NODE;
2469         lpfc_nlp_list(phba, ndlp, NLP_UNMAPPED_LIST);
2470
2471         /* Start issuing Fabric-Device Management Interface (FDMI)
2472          * command to 0xfffffa (FDMI well known port)
2473          */
2474         if (phba->cfg_fdmi_on == 1) {
2475                 lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_DHBA);
2476         } else {
2477                 /*
2478                  * Delay issuing FDMI command if fdmi-on=2
2479                  * (supporting RPA/hostnmae)
2480                  */
2481                 mod_timer(&phba->fc_fdmitmo, jiffies + HZ * 60);
2482         }
2483
2484         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2485         kfree(mp);
2486         mempool_free( pmb, phba->mbox_mem_pool);
2487
2488         return;
2489 }
2490
2491 /*
2492  * This routine looks up the ndlp  lists
2493  * for the given RPI. If rpi found
2494  * it return the node list pointer
2495  * else return NULL.
2496  */
2497 struct lpfc_nodelist *
2498 lpfc_findnode_rpi(struct lpfc_hba * phba, uint16_t rpi)
2499 {
2500         struct lpfc_nodelist *ndlp;
2501         struct list_head * lists[]={&phba->fc_nlpunmap_list,
2502                                     &phba->fc_nlpmap_list,
2503                                     &phba->fc_plogi_list,
2504                                     &phba->fc_adisc_list,
2505                                     &phba->fc_reglogin_list};
2506         int i;
2507
2508         spin_lock_irq(phba->host->host_lock);
2509         for (i = 0; i < ARRAY_SIZE(lists); i++ )
2510                 list_for_each_entry(ndlp, lists[i], nlp_listp)
2511                         if (ndlp->nlp_rpi == rpi) {
2512                                 spin_unlock_irq(phba->host->host_lock);
2513                                 return ndlp;
2514                         }
2515         spin_unlock_irq(phba->host->host_lock);
2516         return NULL;
2517 }
2518
2519 void
2520 lpfc_nlp_init(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
2521                  uint32_t did)
2522 {
2523         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
2524         INIT_LIST_HEAD(&ndlp->nodev_timeout_evt.evt_listp);
2525         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2526         init_timer(&ndlp->nlp_tmofunc);
2527         ndlp->nlp_tmofunc.function = lpfc_nodev_timeout;
2528         ndlp->nlp_tmofunc.data = (unsigned long)ndlp;
2529         init_timer(&ndlp->nlp_delayfunc);
2530         ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2531         ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2532         ndlp->nlp_DID = did;
2533         ndlp->nlp_phba = phba;
2534         ndlp->nlp_sid = NLP_NO_SID;
2535         return;
2536 }