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