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