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