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