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