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