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