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