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