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