164af8a7e95dc14f3e8913712a12185f7866cc2e
[powerpc.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-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                 if (phba->fc_flag & FC_LOOPBACK_MODE) {
904                         lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
905                                 "%d:1306 Link Up Event in loop back mode "
906                                 "x%x received Data: x%x x%x x%x x%x\n",
907                                 phba->brd_no, la->eventTag, phba->fc_eventTag,
908                                 la->granted_AL_PA, la->UlnkSpeed,
909                                 phba->alpa_map[0]);
910                 } else {
911                         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
912                                 "%d:1303 Link Up Event x%x received "
913                                 "Data: x%x x%x x%x x%x\n",
914                                 phba->brd_no, la->eventTag, phba->fc_eventTag,
915                                 la->granted_AL_PA, la->UlnkSpeed,
916                                 phba->alpa_map[0]);
917                 }
918                 lpfc_mbx_process_link_up(phba, la);
919         } else {
920                 phba->fc_stat.LinkDown++;
921                 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
922                                 "%d:1305 Link Down Event x%x received "
923                                 "Data: x%x x%x x%x\n",
924                                 phba->brd_no, la->eventTag, phba->fc_eventTag,
925                                 phba->hba_state, phba->fc_flag);
926                 lpfc_mbx_issue_link_down(phba);
927         }
928
929 lpfc_mbx_cmpl_read_la_free_mbuf:
930         lpfc_mbuf_free(phba, mp->virt, mp->phys);
931         kfree(mp);
932         mempool_free(pmb, phba->mbox_mem_pool);
933         return;
934 }
935
936 /*
937  * This routine handles processing a REG_LOGIN mailbox
938  * command upon completion. It is setup in the LPFC_MBOXQ
939  * as the completion routine when the command is
940  * handed off to the SLI layer.
941  */
942 void
943 lpfc_mbx_cmpl_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
944 {
945         struct lpfc_sli *psli;
946         MAILBOX_t *mb;
947         struct lpfc_dmabuf *mp;
948         struct lpfc_nodelist *ndlp;
949
950         psli = &phba->sli;
951         mb = &pmb->mb;
952
953         ndlp = (struct lpfc_nodelist *) pmb->context2;
954         mp = (struct lpfc_dmabuf *) (pmb->context1);
955
956         pmb->context1 = NULL;
957
958         /* Good status, call state machine */
959         lpfc_disc_state_machine(phba, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
960         lpfc_mbuf_free(phba, mp->virt, mp->phys);
961         kfree(mp);
962         mempool_free( pmb, phba->mbox_mem_pool);
963
964         return;
965 }
966
967 /*
968  * This routine handles processing a Fabric REG_LOGIN mailbox
969  * command upon completion. It is setup in the LPFC_MBOXQ
970  * as the completion routine when the command is
971  * handed off to the SLI layer.
972  */
973 void
974 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
975 {
976         struct lpfc_sli *psli;
977         MAILBOX_t *mb;
978         struct lpfc_dmabuf *mp;
979         struct lpfc_nodelist *ndlp;
980         struct lpfc_nodelist *ndlp_fdmi;
981
982
983         psli = &phba->sli;
984         mb = &pmb->mb;
985
986         ndlp = (struct lpfc_nodelist *) pmb->context2;
987         mp = (struct lpfc_dmabuf *) (pmb->context1);
988
989         if (mb->mbxStatus) {
990                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
991                 kfree(mp);
992                 mempool_free( pmb, phba->mbox_mem_pool);
993                 mempool_free( ndlp, phba->nlp_mem_pool);
994
995                 /* FLOGI failed, so just use loop map to make discovery list */
996                 lpfc_disc_list_loopmap(phba);
997
998                 /* Start discovery */
999                 lpfc_disc_start(phba);
1000                 return;
1001         }
1002
1003         pmb->context1 = NULL;
1004
1005         ndlp->nlp_rpi = mb->un.varWords[0];
1006         ndlp->nlp_type |= NLP_FABRIC;
1007         lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
1008
1009         if (phba->hba_state == LPFC_FABRIC_CFG_LINK) {
1010                 /* This NPort has been assigned an NPort_ID by the fabric as a
1011                  * result of the completed fabric login.  Issue a State Change
1012                  * Registration (SCR) ELS request to the fabric controller
1013                  * (SCR_DID) so that this NPort gets RSCN events from the
1014                  * fabric.
1015                  */
1016                 lpfc_issue_els_scr(phba, SCR_DID, 0);
1017
1018                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, NameServer_DID);
1019                 if (!ndlp) {
1020                         /* Allocate a new node instance. If the pool is empty,
1021                          * start the discovery process and skip the Nameserver
1022                          * login process.  This is attempted again later on.
1023                          * Otherwise, issue a Port Login (PLOGI) to NameServer.
1024                          */
1025                         ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1026                         if (!ndlp) {
1027                                 lpfc_disc_start(phba);
1028                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1029                                 kfree(mp);
1030                                 mempool_free( pmb, phba->mbox_mem_pool);
1031                                 return;
1032                         } else {
1033                                 lpfc_nlp_init(phba, ndlp, NameServer_DID);
1034                                 ndlp->nlp_type |= NLP_FABRIC;
1035                         }
1036                 }
1037                 lpfc_nlp_set_state(phba, ndlp, NLP_STE_PLOGI_ISSUE);
1038                 lpfc_issue_els_plogi(phba, NameServer_DID, 0);
1039                 if (phba->cfg_fdmi_on) {
1040                         ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
1041                                                                 GFP_KERNEL);
1042                         if (ndlp_fdmi) {
1043                                 lpfc_nlp_init(phba, ndlp_fdmi, FDMI_DID);
1044                                 ndlp_fdmi->nlp_type |= NLP_FABRIC;
1045                                 ndlp_fdmi->nlp_state = NLP_STE_PLOGI_ISSUE;
1046                                 lpfc_issue_els_plogi(phba, FDMI_DID, 0);
1047                         }
1048                 }
1049         }
1050
1051         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1052         kfree(mp);
1053         mempool_free( pmb, phba->mbox_mem_pool);
1054         return;
1055 }
1056
1057 /*
1058  * This routine handles processing a NameServer REG_LOGIN mailbox
1059  * command upon completion. It is setup in the LPFC_MBOXQ
1060  * as the completion routine when the command is
1061  * handed off to the SLI layer.
1062  */
1063 void
1064 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
1065 {
1066         struct lpfc_sli *psli;
1067         MAILBOX_t *mb;
1068         struct lpfc_dmabuf *mp;
1069         struct lpfc_nodelist *ndlp;
1070
1071         psli = &phba->sli;
1072         mb = &pmb->mb;
1073
1074         ndlp = (struct lpfc_nodelist *) pmb->context2;
1075         mp = (struct lpfc_dmabuf *) (pmb->context1);
1076
1077         if (mb->mbxStatus) {
1078                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1079                 kfree(mp);
1080                 mempool_free(pmb, phba->mbox_mem_pool);
1081                 lpfc_drop_node(phba, ndlp);
1082
1083                 /* RegLogin failed, so just use loop map to make discovery
1084                    list */
1085                 lpfc_disc_list_loopmap(phba);
1086
1087                 /* Start discovery */
1088                 lpfc_disc_start(phba);
1089                 return;
1090         }
1091
1092         pmb->context1 = NULL;
1093
1094         ndlp->nlp_rpi = mb->un.varWords[0];
1095         ndlp->nlp_type |= NLP_FABRIC;
1096         lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
1097
1098         if (phba->hba_state < LPFC_HBA_READY) {
1099                 /* Link up discovery requires Fabrib registration. */
1100                 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RNN_ID);
1101                 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RSNN_NN);
1102                 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RFT_ID);
1103                 lpfc_ns_cmd(phba, ndlp, SLI_CTNS_RFF_ID);
1104         }
1105
1106         phba->fc_ns_retry = 0;
1107         /* Good status, issue CT Request to NameServer */
1108         if (lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT)) {
1109                 /* Cannot issue NameServer Query, so finish up discovery */
1110                 lpfc_disc_start(phba);
1111         }
1112
1113         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1114         kfree(mp);
1115         mempool_free( pmb, phba->mbox_mem_pool);
1116
1117         return;
1118 }
1119
1120 static void
1121 lpfc_register_remote_port(struct lpfc_hba * phba,
1122                             struct lpfc_nodelist * ndlp)
1123 {
1124         struct fc_rport *rport;
1125         struct lpfc_rport_data *rdata;
1126         struct fc_rport_identifiers rport_ids;
1127
1128         /* Remote port has reappeared. Re-register w/ FC transport */
1129         rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1130         rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
1131         rport_ids.port_id = ndlp->nlp_DID;
1132         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1133
1134         ndlp->rport = rport = fc_remote_port_add(phba->host, 0, &rport_ids);
1135         if (!rport) {
1136                 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1137                            "Warning: fc_remote_port_add failed\n");
1138                 return;
1139         }
1140
1141         /* initialize static port data */
1142         rport->maxframe_size = ndlp->nlp_maxframe;
1143         rport->supported_classes = ndlp->nlp_class_sup;
1144         rdata = rport->dd_data;
1145         rdata->pnode = ndlp;
1146
1147         if (ndlp->nlp_type & NLP_FCP_TARGET)
1148                 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1149         if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1150                 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1151
1152
1153         if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
1154                 fc_remote_port_rolechg(rport, rport_ids.roles);
1155
1156         if ((rport->scsi_target_id != -1) &&
1157                 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
1158                 ndlp->nlp_sid = rport->scsi_target_id;
1159         }
1160
1161         return;
1162 }
1163
1164 static void
1165 lpfc_unregister_remote_port(struct lpfc_hba * phba,
1166                             struct lpfc_nodelist * ndlp)
1167 {
1168         struct fc_rport *rport = ndlp->rport;
1169         struct lpfc_rport_data *rdata = rport->dd_data;
1170
1171         if (rport->scsi_target_id == -1) {
1172                 ndlp->rport = NULL;
1173                 rdata->pnode = NULL;
1174         }
1175
1176         fc_remote_port_delete(rport);
1177
1178         return;
1179 }
1180
1181 static void
1182 lpfc_nlp_counters(struct lpfc_hba *phba, int state, int count)
1183 {
1184         switch (state) {
1185         case NLP_STE_UNUSED_NODE:
1186                 phba->fc_unused_cnt += count;
1187                 break;
1188         case NLP_STE_PLOGI_ISSUE:
1189                 phba->fc_plogi_cnt += count;
1190                 break;
1191         case NLP_STE_ADISC_ISSUE:
1192                 phba->fc_adisc_cnt += count;
1193                 break;
1194         case NLP_STE_REG_LOGIN_ISSUE:
1195                 phba->fc_reglogin_cnt += count;
1196                 break;
1197         case NLP_STE_PRLI_ISSUE:
1198                 phba->fc_prli_cnt += count;
1199                 break;
1200         case NLP_STE_UNMAPPED_NODE:
1201                 phba->fc_unmap_cnt += count;
1202                 break;
1203         case NLP_STE_MAPPED_NODE:
1204                 phba->fc_map_cnt += count;
1205                 break;
1206         case NLP_STE_NPR_NODE:
1207                 phba->fc_npr_cnt += count;
1208                 break;
1209         }
1210 }
1211
1212 void
1213 lpfc_delink_node(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1214 {
1215         switch (ndlp->nlp_flag & NLP_LIST_MASK) {
1216         case NLP_UNUSED_LIST:
1217                 list_del_init(&ndlp->nlp_listp);
1218                 break;
1219         case NLP_PLOGI_LIST:
1220                 list_del_init(&ndlp->nlp_listp);
1221                 break;
1222         case NLP_ADISC_LIST:
1223                 list_del_init(&ndlp->nlp_listp);
1224                 break;
1225         case NLP_REGLOGIN_LIST:
1226                 list_del_init(&ndlp->nlp_listp);
1227                 break;
1228         case NLP_PRLI_LIST:
1229                 list_del_init(&ndlp->nlp_listp);
1230                 break;
1231         case NLP_UNMAPPED_LIST:
1232                 list_del_init(&ndlp->nlp_listp);
1233                 break;
1234         case NLP_MAPPED_LIST:
1235                 list_del_init(&ndlp->nlp_listp);
1236                 break;
1237         case NLP_NPR_LIST:
1238                 list_del_init(&ndlp->nlp_listp);
1239                 break;
1240         }
1241
1242         ndlp->nlp_flag &= ~NLP_LIST_MASK;
1243 }
1244
1245 static int
1246 lpfc_nlp_list(struct lpfc_hba * phba, struct lpfc_nodelist * nlp, int list)
1247 {
1248         struct lpfc_sli      *psli;
1249
1250         psli = &phba->sli;
1251         /* Sanity check to ensure we are not moving to / from the same list */
1252         if ((nlp->nlp_flag & NLP_LIST_MASK) == list)
1253                 return 0;
1254
1255         spin_lock_irq(phba->host->host_lock);
1256         lpfc_delink_node(phba, nlp);
1257
1258         /* Add NPort <did> to <num> list */
1259         lpfc_printf_log(phba,
1260                         KERN_INFO,
1261                         LOG_NODE,
1262                         "%d:0904 Add NPort x%x to %d list Data: x%x\n",
1263                         phba->brd_no,
1264                         nlp->nlp_DID, list, nlp->nlp_flag);
1265
1266         switch (list) {
1267         case NLP_UNUSED_LIST:
1268                 nlp->nlp_flag |= list;
1269                 /* Put it at the end of the unused list */
1270                 list_add_tail(&nlp->nlp_listp, &phba->fc_unused_list);
1271                 break;
1272         case NLP_PLOGI_LIST:
1273                 nlp->nlp_flag |= list;
1274                 /* Put it at the end of the plogi list */
1275                 list_add_tail(&nlp->nlp_listp, &phba->fc_plogi_list);
1276                 break;
1277         case NLP_ADISC_LIST:
1278                 nlp->nlp_flag |= list;
1279                 /* Put it at the end of the adisc list */
1280                 list_add_tail(&nlp->nlp_listp, &phba->fc_adisc_list);
1281                 break;
1282         case NLP_REGLOGIN_LIST:
1283                 nlp->nlp_flag |= list;
1284                 /* Put it at the end of the reglogin list */
1285                 list_add_tail(&nlp->nlp_listp, &phba->fc_reglogin_list);
1286                 break;
1287         case NLP_PRLI_LIST:
1288                 nlp->nlp_flag |= list;
1289                 /* Put it at the end of the prli list */
1290                 list_add_tail(&nlp->nlp_listp, &phba->fc_prli_list);
1291                 break;
1292         case NLP_UNMAPPED_LIST:
1293                 nlp->nlp_flag |= list;
1294                 /* Put it at the end of the unmap list */
1295                 list_add_tail(&nlp->nlp_listp, &phba->fc_nlpunmap_list);
1296                 break;
1297         case NLP_MAPPED_LIST:
1298                 nlp->nlp_flag |= list;
1299                 /* Put it at the end of the map list */
1300                 list_add_tail(&nlp->nlp_listp, &phba->fc_nlpmap_list);
1301                 break;
1302         case NLP_NPR_LIST:
1303                 nlp->nlp_flag |= list;
1304                 /* Put it at the end of the npr list */
1305                 list_add_tail(&nlp->nlp_listp, &phba->fc_npr_list);
1306                 break;
1307         }
1308
1309         spin_unlock_irq(phba->host->host_lock);
1310         return 0;
1311 }
1312
1313 static void
1314 lpfc_nlp_state_cleanup(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1315                        int old_state, int new_state)
1316 {
1317         if (new_state == NLP_STE_UNMAPPED_NODE) {
1318                 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1319                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1320                 ndlp->nlp_type |= NLP_FC_NODE;
1321         }
1322         if (new_state == NLP_STE_MAPPED_NODE)
1323                 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1324         if (new_state == NLP_STE_NPR_NODE)
1325                 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
1326
1327         /* Transport interface */
1328         if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1329                             old_state == NLP_STE_UNMAPPED_NODE)) {
1330                 phba->nport_event_cnt++;
1331                 lpfc_unregister_remote_port(phba, ndlp);
1332         }
1333
1334         if (new_state ==  NLP_STE_MAPPED_NODE ||
1335             new_state == NLP_STE_UNMAPPED_NODE) {
1336                 phba->nport_event_cnt++;
1337                         /*
1338                          * Tell the fc transport about the port, if we haven't
1339                          * already. If we have, and it's a scsi entity, be
1340                          * sure to unblock any attached scsi devices
1341                          */
1342                         lpfc_register_remote_port(phba, ndlp);
1343         }
1344
1345                         /*
1346                          * if we added to Mapped list, but the remote port
1347                          * registration failed or assigned a target id outside
1348                          * our presentable range - move the node to the
1349                          * Unmapped List
1350                          */
1351         if (new_state == NLP_STE_MAPPED_NODE &&
1352             (!ndlp->rport ||
1353              ndlp->rport->scsi_target_id == -1 ||
1354              ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
1355                 spin_lock_irq(phba->host->host_lock);
1356                 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
1357                 spin_unlock_irq(phba->host->host_lock);
1358                 lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
1359         }
1360 }
1361
1362 void
1363 lpfc_nlp_set_state(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int state)
1364 {
1365         int  old_state = ndlp->nlp_state;
1366         static int  list_id[] = {
1367                 [NLP_STE_UNUSED_NODE] = NLP_UNUSED_LIST,
1368                 [NLP_STE_PLOGI_ISSUE] = NLP_PLOGI_LIST,
1369                 [NLP_STE_ADISC_ISSUE] = NLP_ADISC_LIST,
1370                 [NLP_STE_REG_LOGIN_ISSUE] = NLP_REGLOGIN_LIST,
1371                 [NLP_STE_PRLI_ISSUE] = NLP_PRLI_LIST,
1372                 [NLP_STE_UNMAPPED_NODE] = NLP_UNMAPPED_LIST,
1373                 [NLP_STE_MAPPED_NODE] = NLP_MAPPED_LIST,
1374                 [NLP_STE_NPR_NODE] = NLP_NPR_LIST,
1375         };
1376
1377         if (old_state == NLP_STE_NPR_NODE &&
1378             (ndlp->nlp_flag & NLP_DELAY_TMO) != 0 &&
1379             state != NLP_STE_NPR_NODE)
1380                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1381         if (old_state == NLP_STE_UNMAPPED_NODE) {
1382                 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1383                 ndlp->nlp_type &= ~NLP_FC_NODE;
1384         }
1385
1386         if (old_state && !list_empty(&ndlp->nlp_listp))
1387                 lpfc_nlp_counters(phba, old_state, -1);
1388
1389         ndlp->nlp_state = state;
1390         lpfc_nlp_list(phba, ndlp, list_id[state]);
1391         lpfc_nlp_counters(phba, state, 1);
1392
1393         lpfc_nlp_state_cleanup(phba, ndlp, old_state, state);
1394 }
1395
1396 void
1397 lpfc_dequeue_node(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1398 {
1399         if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1400                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1401         spin_lock_irq(phba->host->host_lock);
1402         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1403                 lpfc_nlp_counters(phba, ndlp->nlp_state, -1);
1404         lpfc_delink_node(phba, ndlp);
1405         spin_unlock_irq(phba->host->host_lock);
1406         lpfc_nlp_state_cleanup(phba, ndlp, ndlp->nlp_state, 0);
1407 }
1408
1409 void
1410 lpfc_drop_node(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1411 {
1412         if ((ndlp->nlp_flag & NLP_DELAY_TMO) != 0)
1413                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1414         spin_lock_irq(phba->host->host_lock);
1415         if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1416                 lpfc_nlp_counters(phba, ndlp->nlp_state, -1);
1417         lpfc_delink_node(phba, ndlp);
1418         spin_unlock_irq(phba->host->host_lock);
1419         lpfc_nlp_remove(phba, ndlp);
1420 }
1421
1422 /*
1423  * Start / ReStart rescue timer for Discovery / RSCN handling
1424  */
1425 void
1426 lpfc_set_disctmo(struct lpfc_hba * phba)
1427 {
1428         uint32_t tmo;
1429
1430         if (phba->hba_state == LPFC_LOCAL_CFG_LINK) {
1431                 /* For FAN, timeout should be greater then edtov */
1432                 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
1433         } else {
1434                 /* Normal discovery timeout should be > then ELS/CT timeout
1435                  * FC spec states we need 3 * ratov for CT requests
1436                  */
1437                 tmo = ((phba->fc_ratov * 3) + 3);
1438         }
1439
1440         mod_timer(&phba->fc_disctmo, jiffies + HZ * tmo);
1441         spin_lock_irq(phba->host->host_lock);
1442         phba->fc_flag |= FC_DISC_TMO;
1443         spin_unlock_irq(phba->host->host_lock);
1444
1445         /* Start Discovery Timer state <hba_state> */
1446         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1447                         "%d:0247 Start Discovery Timer state x%x "
1448                         "Data: x%x x%lx x%x x%x\n",
1449                         phba->brd_no,
1450                         phba->hba_state, tmo, (unsigned long)&phba->fc_disctmo,
1451                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1452
1453         return;
1454 }
1455
1456 /*
1457  * Cancel rescue timer for Discovery / RSCN handling
1458  */
1459 int
1460 lpfc_can_disctmo(struct lpfc_hba * phba)
1461 {
1462         /* Turn off discovery timer if its running */
1463         if (phba->fc_flag & FC_DISC_TMO) {
1464                 spin_lock_irq(phba->host->host_lock);
1465                 phba->fc_flag &= ~FC_DISC_TMO;
1466                 spin_unlock_irq(phba->host->host_lock);
1467                 del_timer_sync(&phba->fc_disctmo);
1468                 phba->work_hba_events &= ~WORKER_DISC_TMO;
1469         }
1470
1471         /* Cancel Discovery Timer state <hba_state> */
1472         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1473                         "%d:0248 Cancel Discovery Timer state x%x "
1474                         "Data: x%x x%x x%x\n",
1475                         phba->brd_no, phba->hba_state, phba->fc_flag,
1476                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1477
1478         return 0;
1479 }
1480
1481 /*
1482  * Check specified ring for outstanding IOCB on the SLI queue
1483  * Return true if iocb matches the specified nport
1484  */
1485 int
1486 lpfc_check_sli_ndlp(struct lpfc_hba * phba,
1487                     struct lpfc_sli_ring * pring,
1488                     struct lpfc_iocbq * iocb, struct lpfc_nodelist * ndlp)
1489 {
1490         struct lpfc_sli *psli;
1491         IOCB_t *icmd;
1492
1493         psli = &phba->sli;
1494         icmd = &iocb->iocb;
1495         if (pring->ringno == LPFC_ELS_RING) {
1496                 switch (icmd->ulpCommand) {
1497                 case CMD_GEN_REQUEST64_CR:
1498                         if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
1499                                 return 1;
1500                 case CMD_ELS_REQUEST64_CR:
1501                         if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
1502                                 return 1;
1503                 case CMD_XMIT_ELS_RSP64_CX:
1504                         if (iocb->context1 == (uint8_t *) ndlp)
1505                                 return 1;
1506                 }
1507         } else if (pring->ringno == psli->extra_ring) {
1508
1509         } else if (pring->ringno == psli->fcp_ring) {
1510                 /* Skip match check if waiting to relogin to FCP target */
1511                 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
1512                   (ndlp->nlp_flag & NLP_DELAY_TMO)) {
1513                         return 0;
1514                 }
1515                 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
1516                         return 1;
1517                 }
1518         } else if (pring->ringno == psli->next_ring) {
1519
1520         }
1521         return 0;
1522 }
1523
1524 /*
1525  * Free resources / clean up outstanding I/Os
1526  * associated with nlp_rpi in the LPFC_NODELIST entry.
1527  */
1528 static int
1529 lpfc_no_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1530 {
1531         LIST_HEAD(completions);
1532         struct lpfc_sli *psli;
1533         struct lpfc_sli_ring *pring;
1534         struct lpfc_iocbq *iocb, *next_iocb;
1535         IOCB_t *icmd;
1536         uint32_t rpi, i;
1537
1538         /*
1539          * Everything that matches on txcmplq will be returned
1540          * by firmware with a no rpi error.
1541          */
1542         psli = &phba->sli;
1543         rpi = ndlp->nlp_rpi;
1544         if (rpi) {
1545                 /* Now process each ring */
1546                 for (i = 0; i < psli->num_rings; i++) {
1547                         pring = &psli->ring[i];
1548
1549                         spin_lock_irq(phba->host->host_lock);
1550                         list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
1551                                                 list) {
1552                                 /*
1553                                  * Check to see if iocb matches the nport we are
1554                                  * looking for
1555                                  */
1556                                 if ((lpfc_check_sli_ndlp
1557                                      (phba, pring, iocb, ndlp))) {
1558                                         /* It matches, so deque and call compl
1559                                            with an error */
1560                                         list_move_tail(&iocb->list,
1561                                                        &completions);
1562                                         pring->txq_cnt--;
1563                                 }
1564                         }
1565                         spin_unlock_irq(phba->host->host_lock);
1566
1567                 }
1568         }
1569
1570         while (!list_empty(&completions)) {
1571                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1572                 list_del(&iocb->list);
1573
1574                 if (iocb->iocb_cmpl) {
1575                         icmd = &iocb->iocb;
1576                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1577                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1578                         (iocb->iocb_cmpl) (phba, iocb, iocb);
1579                 } else
1580                         lpfc_sli_release_iocbq(phba, iocb);
1581         }
1582
1583         return 0;
1584 }
1585
1586 /*
1587  * Free rpi associated with LPFC_NODELIST entry.
1588  * This routine is called from lpfc_freenode(), when we are removing
1589  * a LPFC_NODELIST entry. It is also called if the driver initiates a
1590  * LOGO that completes successfully, and we are waiting to PLOGI back
1591  * to the remote NPort. In addition, it is called after we receive
1592  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
1593  * we are waiting to PLOGI back to the remote NPort.
1594  */
1595 int
1596 lpfc_unreg_rpi(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1597 {
1598         LPFC_MBOXQ_t *mbox;
1599         int rc;
1600
1601         if (ndlp->nlp_rpi) {
1602                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1603                         lpfc_unreg_login(phba, ndlp->nlp_rpi, mbox);
1604                         mbox->mbox_cmpl=lpfc_sli_def_mbox_cmpl;
1605                         rc = lpfc_sli_issue_mbox
1606                                     (phba, mbox, (MBX_NOWAIT | MBX_STOP_IOCB));
1607                         if (rc == MBX_NOT_FINISHED)
1608                                 mempool_free( mbox, phba->mbox_mem_pool);
1609                 }
1610                 lpfc_no_rpi(phba, ndlp);
1611                 ndlp->nlp_rpi = 0;
1612                 return 1;
1613         }
1614         return 0;
1615 }
1616
1617 /*
1618  * Free resources associated with LPFC_NODELIST entry
1619  * so it can be freed.
1620  */
1621 static int
1622 lpfc_freenode(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1623 {
1624         LPFC_MBOXQ_t       *mb;
1625         LPFC_MBOXQ_t       *nextmb;
1626         struct lpfc_dmabuf *mp;
1627
1628         /* Cleanup node for NPort <nlp_DID> */
1629         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1630                         "%d:0900 Cleanup node for NPort x%x "
1631                         "Data: x%x x%x x%x\n",
1632                         phba->brd_no, ndlp->nlp_DID, ndlp->nlp_flag,
1633                         ndlp->nlp_state, ndlp->nlp_rpi);
1634
1635         lpfc_dequeue_node(phba, ndlp);
1636
1637         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1638         if ((mb = phba->sli.mbox_active)) {
1639                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1640                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1641                         mb->context2 = NULL;
1642                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1643                 }
1644         }
1645
1646         spin_lock_irq(phba->host->host_lock);
1647         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1648                 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
1649                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1650                         mp = (struct lpfc_dmabuf *) (mb->context1);
1651                         if (mp) {
1652                                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1653                                 kfree(mp);
1654                         }
1655                         list_del(&mb->list);
1656                         mempool_free(mb, phba->mbox_mem_pool);
1657                 }
1658         }
1659         spin_unlock_irq(phba->host->host_lock);
1660
1661         lpfc_els_abort(phba,ndlp);
1662         spin_lock_irq(phba->host->host_lock);
1663         ndlp->nlp_flag &= ~NLP_DELAY_TMO;
1664         spin_unlock_irq(phba->host->host_lock);
1665
1666         ndlp->nlp_last_elscmd = 0;
1667         del_timer_sync(&ndlp->nlp_delayfunc);
1668
1669         if (!list_empty(&ndlp->els_retry_evt.evt_listp))
1670                 list_del_init(&ndlp->els_retry_evt.evt_listp);
1671
1672         lpfc_unreg_rpi(phba, ndlp);
1673
1674         return 0;
1675 }
1676
1677 /*
1678  * Check to see if we can free the nlp back to the freelist.
1679  * If we are in the middle of using the nlp in the discovery state
1680  * machine, defer the free till we reach the end of the state machine.
1681  */
1682 int
1683 lpfc_nlp_remove(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
1684 {
1685         struct lpfc_rport_data *rdata;
1686
1687         if (ndlp->nlp_flag & NLP_DELAY_TMO) {
1688                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1689         }
1690
1691         if (ndlp->nlp_disc_refcnt) {
1692                 spin_lock_irq(phba->host->host_lock);
1693                 ndlp->nlp_flag |= NLP_DELAY_REMOVE;
1694                 spin_unlock_irq(phba->host->host_lock);
1695         } else {
1696                 lpfc_freenode(phba, ndlp);
1697
1698                 if ((ndlp->rport) && !(phba->fc_flag & FC_UNLOADING)) {
1699                         rdata = ndlp->rport->dd_data;
1700                         rdata->pnode = NULL;
1701                         ndlp->rport = NULL;
1702                 }
1703
1704                 mempool_free( ndlp, phba->nlp_mem_pool);
1705         }
1706         return 0;
1707 }
1708
1709 static int
1710 lpfc_matchdid(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp, uint32_t did)
1711 {
1712         D_ID mydid;
1713         D_ID ndlpdid;
1714         D_ID matchdid;
1715
1716         if (did == Bcast_DID)
1717                 return 0;
1718
1719         if (ndlp->nlp_DID == 0) {
1720                 return 0;
1721         }
1722
1723         /* First check for Direct match */
1724         if (ndlp->nlp_DID == did)
1725                 return 1;
1726
1727         /* Next check for area/domain identically equals 0 match */
1728         mydid.un.word = phba->fc_myDID;
1729         if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
1730                 return 0;
1731         }
1732
1733         matchdid.un.word = did;
1734         ndlpdid.un.word = ndlp->nlp_DID;
1735         if (matchdid.un.b.id == ndlpdid.un.b.id) {
1736                 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
1737                     (mydid.un.b.area == matchdid.un.b.area)) {
1738                         if ((ndlpdid.un.b.domain == 0) &&
1739                             (ndlpdid.un.b.area == 0)) {
1740                                 if (ndlpdid.un.b.id)
1741                                         return 1;
1742                         }
1743                         return 0;
1744                 }
1745
1746                 matchdid.un.word = ndlp->nlp_DID;
1747                 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
1748                     (mydid.un.b.area == ndlpdid.un.b.area)) {
1749                         if ((matchdid.un.b.domain == 0) &&
1750                             (matchdid.un.b.area == 0)) {
1751                                 if (matchdid.un.b.id)
1752                                         return 1;
1753                         }
1754                 }
1755         }
1756         return 0;
1757 }
1758
1759 /* Search for a nodelist entry on a specific list */
1760 struct lpfc_nodelist *
1761 lpfc_findnode_did(struct lpfc_hba * phba, uint32_t order, uint32_t did)
1762 {
1763         struct lpfc_nodelist *ndlp;
1764         struct list_head *lists[]={&phba->fc_nlpunmap_list,
1765                                    &phba->fc_nlpmap_list,
1766                                    &phba->fc_plogi_list,
1767                                    &phba->fc_adisc_list,
1768                                    &phba->fc_reglogin_list,
1769                                    &phba->fc_prli_list,
1770                                    &phba->fc_npr_list,
1771                                    &phba->fc_unused_list};
1772         uint32_t search[]={NLP_SEARCH_UNMAPPED,
1773                            NLP_SEARCH_MAPPED,
1774                            NLP_SEARCH_PLOGI,
1775                            NLP_SEARCH_ADISC,
1776                            NLP_SEARCH_REGLOGIN,
1777                            NLP_SEARCH_PRLI,
1778                            NLP_SEARCH_NPR,
1779                            NLP_SEARCH_UNUSED};
1780         int i;
1781         uint32_t data1;
1782
1783         spin_lock_irq(phba->host->host_lock);
1784         for (i = 0; i < ARRAY_SIZE(lists); i++ ) {
1785                 if (!(order & search[i]))
1786                         continue;
1787                 list_for_each_entry(ndlp, lists[i], nlp_listp) {
1788                         if (lpfc_matchdid(phba, ndlp, did)) {
1789                                 data1 = (((uint32_t) ndlp->nlp_state << 24) |
1790                                          ((uint32_t) ndlp->nlp_xri << 16) |
1791                                          ((uint32_t) ndlp->nlp_type << 8) |
1792                                          ((uint32_t) ndlp->nlp_rpi & 0xff));
1793                                 lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1794                                                 "%d:0929 FIND node DID "
1795                                                 " Data: x%p x%x x%x x%x\n",
1796                                                 phba->brd_no,
1797                                                 ndlp, ndlp->nlp_DID,
1798                                                 ndlp->nlp_flag, data1);
1799                                 spin_unlock_irq(phba->host->host_lock);
1800                                 return ndlp;
1801                         }
1802                 }
1803         }
1804         spin_unlock_irq(phba->host->host_lock);
1805
1806         /* FIND node did <did> NOT FOUND */
1807         lpfc_printf_log(phba, KERN_INFO, LOG_NODE,
1808                         "%d:0932 FIND node did x%x NOT FOUND Data: x%x\n",
1809                         phba->brd_no, did, order);
1810         return NULL;
1811 }
1812
1813 struct lpfc_nodelist *
1814 lpfc_setup_disc_node(struct lpfc_hba * phba, uint32_t did)
1815 {
1816         struct lpfc_nodelist *ndlp;
1817         uint32_t flg;
1818
1819         ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, did);
1820         if (!ndlp) {
1821                 if ((phba->fc_flag & FC_RSCN_MODE) &&
1822                    ((lpfc_rscn_payload_check(phba, did) == 0)))
1823                         return NULL;
1824                 ndlp = (struct lpfc_nodelist *)
1825                      mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1826                 if (!ndlp)
1827                         return NULL;
1828                 lpfc_nlp_init(phba, ndlp, did);
1829                 lpfc_nlp_set_state(phba, ndlp, NLP_STE_NPR_NODE);
1830                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1831                 return ndlp;
1832         }
1833         if (phba->fc_flag & FC_RSCN_MODE) {
1834                 if (lpfc_rscn_payload_check(phba, did)) {
1835                         ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1836
1837                         /* Since this node is marked for discovery,
1838                          * delay timeout is not needed.
1839                          */
1840                         if (ndlp->nlp_flag & NLP_DELAY_TMO)
1841                                 lpfc_cancel_retry_delay_tmo(phba, ndlp);
1842                 } else
1843                         ndlp = NULL;
1844         } else {
1845                 flg = ndlp->nlp_flag & NLP_LIST_MASK;
1846                 if ((flg == NLP_ADISC_LIST) || (flg == NLP_PLOGI_LIST))
1847                         return NULL;
1848                 lpfc_nlp_set_state(phba, ndlp, NLP_STE_NPR_NODE);
1849                 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1850         }
1851         return ndlp;
1852 }
1853
1854 /* Build a list of nodes to discover based on the loopmap */
1855 void
1856 lpfc_disc_list_loopmap(struct lpfc_hba * phba)
1857 {
1858         int j;
1859         uint32_t alpa, index;
1860
1861         if (phba->hba_state <= LPFC_LINK_DOWN) {
1862                 return;
1863         }
1864         if (phba->fc_topology != TOPOLOGY_LOOP) {
1865                 return;
1866         }
1867
1868         /* Check for loop map present or not */
1869         if (phba->alpa_map[0]) {
1870                 for (j = 1; j <= phba->alpa_map[0]; j++) {
1871                         alpa = phba->alpa_map[j];
1872
1873                         if (((phba->fc_myDID & 0xff) == alpa) || (alpa == 0)) {
1874                                 continue;
1875                         }
1876                         lpfc_setup_disc_node(phba, alpa);
1877                 }
1878         } else {
1879                 /* No alpamap, so try all alpa's */
1880                 for (j = 0; j < FC_MAXLOOP; j++) {
1881                         /* If cfg_scan_down is set, start from highest
1882                          * ALPA (0xef) to lowest (0x1).
1883                          */
1884                         if (phba->cfg_scan_down)
1885                                 index = j;
1886                         else
1887                                 index = FC_MAXLOOP - j - 1;
1888                         alpa = lpfcAlpaArray[index];
1889                         if ((phba->fc_myDID & 0xff) == alpa) {
1890                                 continue;
1891                         }
1892
1893                         lpfc_setup_disc_node(phba, alpa);
1894                 }
1895         }
1896         return;
1897 }
1898
1899 /* Start Link up / RSCN discovery on NPR list */
1900 void
1901 lpfc_disc_start(struct lpfc_hba * phba)
1902 {
1903         struct lpfc_sli *psli;
1904         LPFC_MBOXQ_t *mbox;
1905         struct lpfc_nodelist *ndlp, *next_ndlp;
1906         uint32_t did_changed, num_sent;
1907         uint32_t clear_la_pending;
1908         int rc;
1909
1910         psli = &phba->sli;
1911
1912         if (phba->hba_state <= LPFC_LINK_DOWN) {
1913                 return;
1914         }
1915         if (phba->hba_state == LPFC_CLEAR_LA)
1916                 clear_la_pending = 1;
1917         else
1918                 clear_la_pending = 0;
1919
1920         if (phba->hba_state < LPFC_HBA_READY) {
1921                 phba->hba_state = LPFC_DISC_AUTH;
1922         }
1923         lpfc_set_disctmo(phba);
1924
1925         if (phba->fc_prevDID == phba->fc_myDID) {
1926                 did_changed = 0;
1927         } else {
1928                 did_changed = 1;
1929         }
1930         phba->fc_prevDID = phba->fc_myDID;
1931         phba->num_disc_nodes = 0;
1932
1933         /* Start Discovery state <hba_state> */
1934         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1935                         "%d:0202 Start Discovery hba state x%x "
1936                         "Data: x%x x%x x%x\n",
1937                         phba->brd_no, phba->hba_state, phba->fc_flag,
1938                         phba->fc_plogi_cnt, phba->fc_adisc_cnt);
1939
1940         /* If our did changed, we MUST do PLOGI */
1941         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1942                                 nlp_listp) {
1943                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1944                         if (did_changed) {
1945                                 spin_lock_irq(phba->host->host_lock);
1946                                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
1947                                 spin_unlock_irq(phba->host->host_lock);
1948                         }
1949                 }
1950         }
1951
1952         /* First do ADISCs - if any */
1953         num_sent = lpfc_els_disc_adisc(phba);
1954
1955         if (num_sent)
1956                 return;
1957
1958         if ((phba->hba_state < LPFC_HBA_READY) && (!clear_la_pending)) {
1959                 /* If we get here, there is nothing to ADISC */
1960                 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))) {
1961                         phba->hba_state = LPFC_CLEAR_LA;
1962                         lpfc_clear_la(phba, mbox);
1963                         mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
1964                         rc = lpfc_sli_issue_mbox(phba, mbox,
1965                                                  (MBX_NOWAIT | MBX_STOP_IOCB));
1966                         if (rc == MBX_NOT_FINISHED) {
1967                                 mempool_free( mbox, phba->mbox_mem_pool);
1968                                 lpfc_disc_flush_list(phba);
1969                                 psli->ring[(psli->extra_ring)].flag &=
1970                                         ~LPFC_STOP_IOCB_EVENT;
1971                                 psli->ring[(psli->fcp_ring)].flag &=
1972                                         ~LPFC_STOP_IOCB_EVENT;
1973                                 psli->ring[(psli->next_ring)].flag &=
1974                                         ~LPFC_STOP_IOCB_EVENT;
1975                                 phba->hba_state = LPFC_HBA_READY;
1976                         }
1977                 }
1978         } else {
1979                 /* Next do PLOGIs - if any */
1980                 num_sent = lpfc_els_disc_plogi(phba);
1981
1982                 if (num_sent)
1983                         return;
1984
1985                 if (phba->fc_flag & FC_RSCN_MODE) {
1986                         /* Check to see if more RSCNs came in while we
1987                          * were processing this one.
1988                          */
1989                         if ((phba->fc_rscn_id_cnt == 0) &&
1990                             (!(phba->fc_flag & FC_RSCN_DISCOVERY))) {
1991                                 spin_lock_irq(phba->host->host_lock);
1992                                 phba->fc_flag &= ~FC_RSCN_MODE;
1993                                 spin_unlock_irq(phba->host->host_lock);
1994                         } else
1995                                 lpfc_els_handle_rscn(phba);
1996                 }
1997         }
1998         return;
1999 }
2000
2001 /*
2002  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2003  *  ring the match the sppecified nodelist.
2004  */
2005 static void
2006 lpfc_free_tx(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp)
2007 {
2008         LIST_HEAD(completions);
2009         struct lpfc_sli *psli;
2010         IOCB_t     *icmd;
2011         struct lpfc_iocbq    *iocb, *next_iocb;
2012         struct lpfc_sli_ring *pring;
2013
2014         psli = &phba->sli;
2015         pring = &psli->ring[LPFC_ELS_RING];
2016
2017         /* Error matching iocb on txq or txcmplq
2018          * First check the txq.
2019          */
2020         spin_lock_irq(phba->host->host_lock);
2021         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2022                 if (iocb->context1 != ndlp) {
2023                         continue;
2024                 }
2025                 icmd = &iocb->iocb;
2026                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2027                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2028
2029                         list_move_tail(&iocb->list, &completions);
2030                         pring->txq_cnt--;
2031                 }
2032         }
2033
2034         /* Next check the txcmplq */
2035         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2036                 if (iocb->context1 != ndlp) {
2037                         continue;
2038                 }
2039                 icmd = &iocb->iocb;
2040                 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2041                     (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2042                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2043                 }
2044         }
2045         spin_unlock_irq(phba->host->host_lock);
2046
2047         while (!list_empty(&completions)) {
2048                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
2049                 list_del(&iocb->list);
2050
2051                 if (iocb->iocb_cmpl) {
2052                         icmd = &iocb->iocb;
2053                         icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2054                         icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2055                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2056                 } else
2057                         lpfc_sli_release_iocbq(phba, iocb);
2058         }
2059
2060         return;
2061 }
2062
2063 void
2064 lpfc_disc_flush_list(struct lpfc_hba * phba)
2065 {
2066         struct lpfc_nodelist *ndlp, *next_ndlp;
2067
2068         if (phba->fc_plogi_cnt) {
2069                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
2070                                         nlp_listp) {
2071                         lpfc_free_tx(phba, ndlp);
2072                         lpfc_nlp_remove(phba, ndlp);
2073                 }
2074         }
2075         if (phba->fc_adisc_cnt) {
2076                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
2077                                         nlp_listp) {
2078                         lpfc_free_tx(phba, ndlp);
2079                         lpfc_nlp_remove(phba, ndlp);
2080                 }
2081         }
2082         return;
2083 }
2084
2085 /*****************************************************************************/
2086 /*
2087  * NAME:     lpfc_disc_timeout
2088  *
2089  * FUNCTION: Fibre Channel driver discovery timeout routine.
2090  *
2091  * EXECUTION ENVIRONMENT: interrupt only
2092  *
2093  * CALLED FROM:
2094  *      Timer function
2095  *
2096  * RETURNS:
2097  *      none
2098  */
2099 /*****************************************************************************/
2100 void
2101 lpfc_disc_timeout(unsigned long ptr)
2102 {
2103         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
2104         unsigned long flags = 0;
2105
2106         if (unlikely(!phba))
2107                 return;
2108
2109         spin_lock_irqsave(phba->host->host_lock, flags);
2110         if (!(phba->work_hba_events & WORKER_DISC_TMO)) {
2111                 phba->work_hba_events |= WORKER_DISC_TMO;
2112                 if (phba->work_wait)
2113                         wake_up(phba->work_wait);
2114         }
2115         spin_unlock_irqrestore(phba->host->host_lock, flags);
2116         return;
2117 }
2118
2119 static void
2120 lpfc_disc_timeout_handler(struct lpfc_hba *phba)
2121 {
2122         struct lpfc_sli *psli;
2123         struct lpfc_nodelist *ndlp, *next_ndlp;
2124         LPFC_MBOXQ_t *clearlambox, *initlinkmbox;
2125         int rc, clrlaerr = 0;
2126
2127         if (unlikely(!phba))
2128                 return;
2129
2130         if (!(phba->fc_flag & FC_DISC_TMO))
2131                 return;
2132
2133         psli = &phba->sli;
2134
2135         spin_lock_irq(phba->host->host_lock);
2136         phba->fc_flag &= ~FC_DISC_TMO;
2137         spin_unlock_irq(phba->host->host_lock);
2138
2139         switch (phba->hba_state) {
2140
2141         case LPFC_LOCAL_CFG_LINK:
2142         /* hba_state is identically LPFC_LOCAL_CFG_LINK while waiting for FAN */
2143                 /* FAN timeout */
2144                 lpfc_printf_log(phba,
2145                                  KERN_WARNING,
2146                                  LOG_DISCOVERY,
2147                                  "%d:0221 FAN timeout\n",
2148                                  phba->brd_no);
2149
2150                 /* Start discovery by sending FLOGI, clean up old rpis */
2151                 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
2152                                         nlp_listp) {
2153                         if (ndlp->nlp_type & NLP_FABRIC) {
2154                                 /* Clean up the ndlp on Fabric connections */
2155                                 lpfc_drop_node(phba, ndlp);
2156                         } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2157                                 /* Fail outstanding IO now since device
2158                                  * is marked for PLOGI.
2159                                  */
2160                                 lpfc_unreg_rpi(phba, ndlp);
2161                         }
2162                 }
2163                 phba->hba_state = LPFC_FLOGI;
2164                 lpfc_set_disctmo(phba);
2165                 lpfc_initial_flogi(phba);
2166                 break;
2167
2168         case LPFC_FLOGI:
2169         /* hba_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
2170                 /* Initial FLOGI timeout */
2171                 lpfc_printf_log(phba,
2172                                  KERN_ERR,
2173                                  LOG_DISCOVERY,
2174                                  "%d:0222 Initial FLOGI timeout\n",
2175                                  phba->brd_no);
2176
2177                 /* Assume no Fabric and go on with discovery.
2178                  * Check for outstanding ELS FLOGI to abort.
2179                  */
2180
2181                 /* FLOGI failed, so just use loop map to make discovery list */
2182                 lpfc_disc_list_loopmap(phba);
2183
2184                 /* Start discovery */
2185                 lpfc_disc_start(phba);
2186                 break;
2187
2188         case LPFC_FABRIC_CFG_LINK:
2189         /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2190            NameServer login */
2191                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2192                                 "%d:0223 Timeout while waiting for NameServer "
2193                                 "login\n", phba->brd_no);
2194
2195                 /* Next look for NameServer ndlp */
2196                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_ALL, NameServer_DID);
2197                 if (ndlp)
2198                         lpfc_nlp_remove(phba, ndlp);
2199                 /* Start discovery */
2200                 lpfc_disc_start(phba);
2201                 break;
2202
2203         case LPFC_NS_QRY:
2204         /* Check for wait for NameServer Rsp timeout */
2205                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2206                                 "%d:0224 NameServer Query timeout "
2207                                 "Data: x%x x%x\n",
2208                                 phba->brd_no,
2209                                 phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2210
2211                 ndlp = lpfc_findnode_did(phba, NLP_SEARCH_UNMAPPED,
2212                                                                 NameServer_DID);
2213                 if (ndlp) {
2214                         if (phba->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2215                                 /* Try it one more time */
2216                                 rc = lpfc_ns_cmd(phba, ndlp, SLI_CTNS_GID_FT);
2217                                 if (rc == 0)
2218                                         break;
2219                         }
2220                         phba->fc_ns_retry = 0;
2221                 }
2222
2223                 /* Nothing to authenticate, so CLEAR_LA right now */
2224                 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2225                 if (!clearlambox) {
2226                         clrlaerr = 1;
2227                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2228                                         "%d:0226 Device Discovery "
2229                                         "completion error\n",
2230                                         phba->brd_no);
2231                         phba->hba_state = LPFC_HBA_ERROR;
2232                         break;
2233                 }
2234
2235                 phba->hba_state = LPFC_CLEAR_LA;
2236                 lpfc_clear_la(phba, clearlambox);
2237                 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2238                 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2239                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2240                 if (rc == MBX_NOT_FINISHED) {
2241                         mempool_free(clearlambox, phba->mbox_mem_pool);
2242                         clrlaerr = 1;
2243                         break;
2244                 }
2245
2246                 /* Setup and issue mailbox INITIALIZE LINK command */
2247                 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2248                 if (!initlinkmbox) {
2249                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2250                                         "%d:0206 Device Discovery "
2251                                         "completion error\n",
2252                                         phba->brd_no);
2253                         phba->hba_state = LPFC_HBA_ERROR;
2254                         break;
2255                 }
2256
2257                 lpfc_linkdown(phba);
2258                 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2259                                phba->cfg_link_speed);
2260                 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2261                 rc = lpfc_sli_issue_mbox(phba, initlinkmbox,
2262                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2263                 lpfc_set_loopback_flag(phba);
2264                 if (rc == MBX_NOT_FINISHED)
2265                         mempool_free(initlinkmbox, phba->mbox_mem_pool);
2266
2267                 break;
2268
2269         case LPFC_DISC_AUTH:
2270         /* Node Authentication timeout */
2271                 lpfc_printf_log(phba,
2272                                  KERN_ERR,
2273                                  LOG_DISCOVERY,
2274                                  "%d:0227 Node Authentication timeout\n",
2275                                  phba->brd_no);
2276                 lpfc_disc_flush_list(phba);
2277                 clearlambox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2278                 if (!clearlambox) {
2279                         clrlaerr = 1;
2280                         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
2281                                         "%d:0207 Device Discovery "
2282                                         "completion error\n",
2283                                         phba->brd_no);
2284                         phba->hba_state = LPFC_HBA_ERROR;
2285                         break;
2286                 }
2287                 phba->hba_state = LPFC_CLEAR_LA;
2288                 lpfc_clear_la(phba, clearlambox);
2289                 clearlambox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2290                 rc = lpfc_sli_issue_mbox(phba, clearlambox,
2291                                          (MBX_NOWAIT | MBX_STOP_IOCB));
2292                 if (rc == MBX_NOT_FINISHED) {
2293                         mempool_free(clearlambox, phba->mbox_mem_pool);
2294                         clrlaerr = 1;
2295                 }
2296                 break;
2297
2298         case LPFC_CLEAR_LA:
2299         /* CLEAR LA timeout */
2300                 lpfc_printf_log(phba,
2301                                  KERN_ERR,
2302                                  LOG_DISCOVERY,
2303                                  "%d:0228 CLEAR LA timeout\n",
2304                                  phba->brd_no);
2305                 clrlaerr = 1;
2306                 break;
2307
2308         case LPFC_HBA_READY:
2309                 if (phba->fc_flag & FC_RSCN_MODE) {
2310                         lpfc_printf_log(phba,
2311                                         KERN_ERR,
2312                                         LOG_DISCOVERY,
2313                                         "%d:0231 RSCN timeout Data: x%x x%x\n",
2314                                         phba->brd_no,
2315                                         phba->fc_ns_retry, LPFC_MAX_NS_RETRY);
2316
2317                         /* Cleanup any outstanding ELS commands */
2318                         lpfc_els_flush_cmd(phba);
2319
2320                         lpfc_els_flush_rscn(phba);
2321                         lpfc_disc_flush_list(phba);
2322                 }
2323                 break;
2324         }
2325
2326         if (clrlaerr) {
2327                 lpfc_disc_flush_list(phba);
2328                 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2329                 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2330                 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
2331                 phba->hba_state = LPFC_HBA_READY;
2332         }
2333
2334         return;
2335 }
2336
2337 /*
2338  * This routine handles processing a NameServer REG_LOGIN mailbox
2339  * command upon completion. It is setup in the LPFC_MBOXQ
2340  * as the completion routine when the command is
2341  * handed off to the SLI layer.
2342  */
2343 void
2344 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
2345 {
2346         struct lpfc_sli *psli;
2347         MAILBOX_t *mb;
2348         struct lpfc_dmabuf *mp;
2349         struct lpfc_nodelist *ndlp;
2350
2351         psli = &phba->sli;
2352         mb = &pmb->mb;
2353
2354         ndlp = (struct lpfc_nodelist *) pmb->context2;
2355         mp = (struct lpfc_dmabuf *) (pmb->context1);
2356
2357         pmb->context1 = NULL;
2358
2359         ndlp->nlp_rpi = mb->un.varWords[0];
2360         ndlp->nlp_type |= NLP_FABRIC;
2361         lpfc_nlp_set_state(phba, ndlp, NLP_STE_UNMAPPED_NODE);
2362
2363         /* Start issuing Fabric-Device Management Interface (FDMI)
2364          * command to 0xfffffa (FDMI well known port)
2365          */
2366         if (phba->cfg_fdmi_on == 1) {
2367                 lpfc_fdmi_cmd(phba, ndlp, SLI_MGMT_DHBA);
2368         } else {
2369                 /*
2370                  * Delay issuing FDMI command if fdmi-on=2
2371                  * (supporting RPA/hostnmae)
2372                  */
2373                 mod_timer(&phba->fc_fdmitmo, jiffies + HZ * 60);
2374         }
2375
2376         lpfc_mbuf_free(phba, mp->virt, mp->phys);
2377         kfree(mp);
2378         mempool_free( pmb, phba->mbox_mem_pool);
2379
2380         return;
2381 }
2382
2383 /*
2384  * This routine looks up the ndlp  lists
2385  * for the given RPI. If rpi found
2386  * it return the node list pointer
2387  * else return NULL.
2388  */
2389 struct lpfc_nodelist *
2390 __lpfc_findnode_rpi(struct lpfc_hba * phba, uint16_t rpi)
2391 {
2392         struct lpfc_nodelist *ndlp;
2393         struct list_head * lists[]={&phba->fc_nlpunmap_list,
2394                                     &phba->fc_nlpmap_list,
2395                                     &phba->fc_plogi_list,
2396                                     &phba->fc_adisc_list,
2397                                     &phba->fc_reglogin_list};
2398         int i;
2399
2400         for (i = 0; i < ARRAY_SIZE(lists); i++ )
2401                 list_for_each_entry(ndlp, lists[i], nlp_listp)
2402                         if (ndlp->nlp_rpi == rpi) {
2403                                 return ndlp;
2404                         }
2405         return NULL;
2406 }
2407
2408 struct lpfc_nodelist *
2409 lpfc_findnode_rpi(struct lpfc_hba * phba, uint16_t rpi)
2410 {
2411         struct lpfc_nodelist *ndlp;
2412
2413         spin_lock_irq(phba->host->host_lock);
2414         ndlp = __lpfc_findnode_rpi(phba, rpi);
2415         spin_unlock_irq(phba->host->host_lock);
2416         return ndlp;
2417 }
2418
2419 /*
2420  * This routine looks up the ndlp  lists
2421  * for the given WWPN. If WWPN found
2422  * it return the node list pointer
2423  * else return NULL.
2424  */
2425 struct lpfc_nodelist *
2426 lpfc_findnode_wwpn(struct lpfc_hba * phba, uint32_t order,
2427                    struct lpfc_name * wwpn)
2428 {
2429         struct lpfc_nodelist *ndlp;
2430         struct list_head * lists[]={&phba->fc_nlpunmap_list,
2431                                     &phba->fc_nlpmap_list,
2432                                     &phba->fc_npr_list,
2433                                     &phba->fc_plogi_list,
2434                                     &phba->fc_adisc_list,
2435                                     &phba->fc_reglogin_list,
2436                                     &phba->fc_prli_list};
2437         uint32_t search[]={NLP_SEARCH_UNMAPPED,
2438                            NLP_SEARCH_MAPPED,
2439                            NLP_SEARCH_NPR,
2440                            NLP_SEARCH_PLOGI,
2441                            NLP_SEARCH_ADISC,
2442                            NLP_SEARCH_REGLOGIN,
2443                            NLP_SEARCH_PRLI};
2444         int i;
2445
2446         spin_lock_irq(phba->host->host_lock);
2447         for (i = 0; i < ARRAY_SIZE(lists); i++ ) {
2448                 if (!(order & search[i]))
2449                         continue;
2450                 list_for_each_entry(ndlp, lists[i], nlp_listp) {
2451                         if (memcmp(&ndlp->nlp_portname, wwpn,
2452                                    sizeof(struct lpfc_name)) == 0) {
2453                                 spin_unlock_irq(phba->host->host_lock);
2454                                 return ndlp;
2455                         }
2456                 }
2457         }
2458         spin_unlock_irq(phba->host->host_lock);
2459         return NULL;
2460 }
2461
2462 void
2463 lpfc_nlp_init(struct lpfc_hba * phba, struct lpfc_nodelist * ndlp,
2464                  uint32_t did)
2465 {
2466         memset(ndlp, 0, sizeof (struct lpfc_nodelist));
2467         INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
2468         init_timer(&ndlp->nlp_delayfunc);
2469         ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
2470         ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
2471         ndlp->nlp_DID = did;
2472         ndlp->nlp_phba = phba;
2473         ndlp->nlp_sid = NLP_NO_SID;
2474         return;
2475 }