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