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