Merge tag 'soc-for-linus-3' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / lpfc / lpfc_nportdisc.c
1  /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2012 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/slab.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_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_sli4.h"
36 #include "lpfc_nl.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45
46 /* Called to verify a rcv'ed ADISC was intended for us. */
47 static int
48 lpfc_check_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
49                  struct lpfc_name *nn, struct lpfc_name *pn)
50 {
51         /* First, we MUST have a RPI registered */
52         if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED))
53                 return 0;
54
55         /* Compare the ADISC rsp WWNN / WWPN matches our internal node
56          * table entry for that node.
57          */
58         if (memcmp(nn, &ndlp->nlp_nodename, sizeof (struct lpfc_name)))
59                 return 0;
60
61         if (memcmp(pn, &ndlp->nlp_portname, sizeof (struct lpfc_name)))
62                 return 0;
63
64         /* we match, return success */
65         return 1;
66 }
67
68 int
69 lpfc_check_sparm(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
70                  struct serv_parm *sp, uint32_t class, int flogi)
71 {
72         volatile struct serv_parm *hsp = &vport->fc_sparam;
73         uint16_t hsp_value, ssp_value = 0;
74
75         /*
76          * The receive data field size and buffer-to-buffer receive data field
77          * size entries are 16 bits but are represented as two 8-bit fields in
78          * the driver data structure to account for rsvd bits and other control
79          * bits.  Reconstruct and compare the fields as a 16-bit values before
80          * correcting the byte values.
81          */
82         if (sp->cls1.classValid) {
83                 if (!flogi) {
84                         hsp_value = ((hsp->cls1.rcvDataSizeMsb << 8) |
85                                      hsp->cls1.rcvDataSizeLsb);
86                         ssp_value = ((sp->cls1.rcvDataSizeMsb << 8) |
87                                      sp->cls1.rcvDataSizeLsb);
88                         if (!ssp_value)
89                                 goto bad_service_param;
90                         if (ssp_value > hsp_value) {
91                                 sp->cls1.rcvDataSizeLsb =
92                                         hsp->cls1.rcvDataSizeLsb;
93                                 sp->cls1.rcvDataSizeMsb =
94                                         hsp->cls1.rcvDataSizeMsb;
95                         }
96                 }
97         } else if (class == CLASS1)
98                 goto bad_service_param;
99         if (sp->cls2.classValid) {
100                 if (!flogi) {
101                         hsp_value = ((hsp->cls2.rcvDataSizeMsb << 8) |
102                                      hsp->cls2.rcvDataSizeLsb);
103                         ssp_value = ((sp->cls2.rcvDataSizeMsb << 8) |
104                                      sp->cls2.rcvDataSizeLsb);
105                         if (!ssp_value)
106                                 goto bad_service_param;
107                         if (ssp_value > hsp_value) {
108                                 sp->cls2.rcvDataSizeLsb =
109                                         hsp->cls2.rcvDataSizeLsb;
110                                 sp->cls2.rcvDataSizeMsb =
111                                         hsp->cls2.rcvDataSizeMsb;
112                         }
113                 }
114         } else if (class == CLASS2)
115                 goto bad_service_param;
116         if (sp->cls3.classValid) {
117                 if (!flogi) {
118                         hsp_value = ((hsp->cls3.rcvDataSizeMsb << 8) |
119                                      hsp->cls3.rcvDataSizeLsb);
120                         ssp_value = ((sp->cls3.rcvDataSizeMsb << 8) |
121                                      sp->cls3.rcvDataSizeLsb);
122                         if (!ssp_value)
123                                 goto bad_service_param;
124                         if (ssp_value > hsp_value) {
125                                 sp->cls3.rcvDataSizeLsb =
126                                         hsp->cls3.rcvDataSizeLsb;
127                                 sp->cls3.rcvDataSizeMsb =
128                                         hsp->cls3.rcvDataSizeMsb;
129                         }
130                 }
131         } else if (class == CLASS3)
132                 goto bad_service_param;
133
134         /*
135          * Preserve the upper four bits of the MSB from the PLOGI response.
136          * These bits contain the Buffer-to-Buffer State Change Number
137          * from the target and need to be passed to the FW.
138          */
139         hsp_value = (hsp->cmn.bbRcvSizeMsb << 8) | hsp->cmn.bbRcvSizeLsb;
140         ssp_value = (sp->cmn.bbRcvSizeMsb << 8) | sp->cmn.bbRcvSizeLsb;
141         if (ssp_value > hsp_value) {
142                 sp->cmn.bbRcvSizeLsb = hsp->cmn.bbRcvSizeLsb;
143                 sp->cmn.bbRcvSizeMsb = (sp->cmn.bbRcvSizeMsb & 0xF0) |
144                                        (hsp->cmn.bbRcvSizeMsb & 0x0F);
145         }
146
147         memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof (struct lpfc_name));
148         memcpy(&ndlp->nlp_portname, &sp->portName, sizeof (struct lpfc_name));
149         return 1;
150 bad_service_param:
151         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
152                          "0207 Device %x "
153                          "(%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x) sent "
154                          "invalid service parameters.  Ignoring device.\n",
155                          ndlp->nlp_DID,
156                          sp->nodeName.u.wwn[0], sp->nodeName.u.wwn[1],
157                          sp->nodeName.u.wwn[2], sp->nodeName.u.wwn[3],
158                          sp->nodeName.u.wwn[4], sp->nodeName.u.wwn[5],
159                          sp->nodeName.u.wwn[6], sp->nodeName.u.wwn[7]);
160         return 0;
161 }
162
163 static void *
164 lpfc_check_elscmpl_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
165                         struct lpfc_iocbq *rspiocb)
166 {
167         struct lpfc_dmabuf *pcmd, *prsp;
168         uint32_t *lp;
169         void     *ptr = NULL;
170         IOCB_t   *irsp;
171
172         irsp = &rspiocb->iocb;
173         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
174
175         /* For lpfc_els_abort, context2 could be zero'ed to delay
176          * freeing associated memory till after ABTS completes.
177          */
178         if (pcmd) {
179                 prsp =  list_get_first(&pcmd->list, struct lpfc_dmabuf,
180                                        list);
181                 if (prsp) {
182                         lp = (uint32_t *) prsp->virt;
183                         ptr = (void *)((uint8_t *)lp + sizeof(uint32_t));
184                 }
185         } else {
186                 /* Force ulpStatus error since we are returning NULL ptr */
187                 if (!(irsp->ulpStatus)) {
188                         irsp->ulpStatus = IOSTAT_LOCAL_REJECT;
189                         irsp->un.ulpWord[4] = IOERR_SLI_ABORTED;
190                 }
191                 ptr = NULL;
192         }
193         return ptr;
194 }
195
196
197
198 /*
199  * Free resources / clean up outstanding I/Os
200  * associated with a LPFC_NODELIST entry. This
201  * routine effectively results in a "software abort".
202  */
203 int
204 lpfc_els_abort(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
205 {
206         LIST_HEAD(completions);
207         LIST_HEAD(txcmplq_completions);
208         LIST_HEAD(abort_list);
209         struct lpfc_sli  *psli = &phba->sli;
210         struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
211         struct lpfc_iocbq *iocb, *next_iocb;
212
213         /* Abort outstanding I/O on NPort <nlp_DID> */
214         lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_DISCOVERY,
215                          "2819 Abort outstanding I/O on NPort x%x "
216                          "Data: x%x x%x x%x\n",
217                          ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
218                          ndlp->nlp_rpi);
219
220         lpfc_fabric_abort_nport(ndlp);
221
222         /* First check the txq */
223         spin_lock_irq(&phba->hbalock);
224         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
225                 /* Check to see if iocb matches the nport we are looking for */
226                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp)) {
227                         /* It matches, so deque and call compl with anp error */
228                         list_move_tail(&iocb->list, &completions);
229                 }
230         }
231
232         /* Next check the txcmplq */
233         list_splice_init(&pring->txcmplq, &txcmplq_completions);
234         spin_unlock_irq(&phba->hbalock);
235
236         list_for_each_entry_safe(iocb, next_iocb, &txcmplq_completions, list) {
237                 /* Check to see if iocb matches the nport we are looking for */
238                 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
239                         list_add_tail(&iocb->dlist, &abort_list);
240         }
241         spin_lock_irq(&phba->hbalock);
242         list_splice(&txcmplq_completions, &pring->txcmplq);
243         spin_unlock_irq(&phba->hbalock);
244
245         list_for_each_entry_safe(iocb, next_iocb, &abort_list, dlist) {
246                         spin_lock_irq(&phba->hbalock);
247                         list_del_init(&iocb->dlist);
248                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
249                         spin_unlock_irq(&phba->hbalock);
250         }
251
252         /* Cancel all the IOCBs from the completions list */
253         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
254                               IOERR_SLI_ABORTED);
255
256         lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
257         return 0;
258 }
259
260 static int
261 lpfc_rcv_plogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
262                struct lpfc_iocbq *cmdiocb)
263 {
264         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
265         struct lpfc_hba    *phba = vport->phba;
266         struct lpfc_dmabuf *pcmd;
267         uint32_t *lp;
268         IOCB_t *icmd;
269         struct serv_parm *sp;
270         LPFC_MBOXQ_t *mbox;
271         struct ls_rjt stat;
272         int rc;
273
274         memset(&stat, 0, sizeof (struct ls_rjt));
275         if (vport->port_state <= LPFC_FDISC) {
276                 /* Before responding to PLOGI, check for pt2pt mode.
277                  * If we are pt2pt, with an outstanding FLOGI, abort
278                  * the FLOGI and resend it first.
279                  */
280                 if (vport->fc_flag & FC_PT2PT) {
281                          lpfc_els_abort_flogi(phba);
282                         if (!(vport->fc_flag & FC_PT2PT_PLOGI)) {
283                                 /* If the other side is supposed to initiate
284                                  * the PLOGI anyway, just ACC it now and
285                                  * move on with discovery.
286                                  */
287                                 phba->fc_edtov = FF_DEF_EDTOV;
288                                 phba->fc_ratov = FF_DEF_RATOV;
289                                 /* Start discovery - this should just do
290                                    CLEAR_LA */
291                                 lpfc_disc_start(vport);
292                         } else
293                                 lpfc_initial_flogi(vport);
294                 } else {
295                         stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
296                         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
297                         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
298                                             ndlp, NULL);
299                         return 0;
300                 }
301         }
302         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
303         lp = (uint32_t *) pcmd->virt;
304         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
305         if (wwn_to_u64(sp->portName.u.wwn) == 0) {
306                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
307                                  "0140 PLOGI Reject: invalid nname\n");
308                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
309                 stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_PNAME;
310                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
311                         NULL);
312                 return 0;
313         }
314         if (wwn_to_u64(sp->nodeName.u.wwn) == 0) {
315                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
316                                  "0141 PLOGI Reject: invalid pname\n");
317                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
318                 stat.un.b.lsRjtRsnCodeExp = LSEXP_INVALID_NNAME;
319                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
320                         NULL);
321                 return 0;
322         }
323         if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0) == 0)) {
324                 /* Reject this request because invalid parameters */
325                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
326                 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
327                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
328                         NULL);
329                 return 0;
330         }
331         icmd = &cmdiocb->iocb;
332
333         /* PLOGI chkparm OK */
334         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
335                          "0114 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
336                          ndlp->nlp_DID, ndlp->nlp_state, ndlp->nlp_flag,
337                          ndlp->nlp_rpi);
338
339         if (vport->cfg_fcp_class == 2 && sp->cls2.classValid)
340                 ndlp->nlp_fcp_info |= CLASS2;
341         else
342                 ndlp->nlp_fcp_info |= CLASS3;
343
344         ndlp->nlp_class_sup = 0;
345         if (sp->cls1.classValid)
346                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
347         if (sp->cls2.classValid)
348                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
349         if (sp->cls3.classValid)
350                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
351         if (sp->cls4.classValid)
352                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
353         ndlp->nlp_maxframe =
354                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
355
356         /* no need to reg_login if we are already in one of these states */
357         switch (ndlp->nlp_state) {
358         case  NLP_STE_NPR_NODE:
359                 if (!(ndlp->nlp_flag & NLP_NPR_ADISC))
360                         break;
361         case  NLP_STE_REG_LOGIN_ISSUE:
362         case  NLP_STE_PRLI_ISSUE:
363         case  NLP_STE_UNMAPPED_NODE:
364         case  NLP_STE_MAPPED_NODE:
365                 lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
366                 return 1;
367         }
368
369         /* Check for Nport to NPort pt2pt protocol */
370         if ((vport->fc_flag & FC_PT2PT) &&
371             !(vport->fc_flag & FC_PT2PT_PLOGI)) {
372
373                 /* rcv'ed PLOGI decides what our NPortId will be */
374                 vport->fc_myDID = icmd->un.rcvels.parmRo;
375                 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
376                 if (mbox == NULL)
377                         goto out;
378                 lpfc_config_link(phba, mbox);
379                 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
380                 mbox->vport = vport;
381                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
382                 if (rc == MBX_NOT_FINISHED) {
383                         mempool_free(mbox, phba->mbox_mem_pool);
384                         goto out;
385                 }
386                 /*
387                  * For SLI4, the VFI/VPI are registered AFTER the
388                  * Nport with the higher WWPN sends us a PLOGI with
389                  * our assigned NPortId.
390                  */
391                 if (phba->sli_rev == LPFC_SLI_REV4)
392                         lpfc_issue_reg_vfi(vport);
393
394                 lpfc_can_disctmo(vport);
395         }
396         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
397         if (!mbox)
398                 goto out;
399
400         /* Registering an existing RPI behaves differently for SLI3 vs SLI4 */
401         if (phba->sli_rev == LPFC_SLI_REV4)
402                 lpfc_unreg_rpi(vport, ndlp);
403
404         rc = lpfc_reg_rpi(phba, vport->vpi, icmd->un.rcvels.remoteID,
405                             (uint8_t *) sp, mbox, ndlp->nlp_rpi);
406         if (rc) {
407                 mempool_free(mbox, phba->mbox_mem_pool);
408                 goto out;
409         }
410
411         /* ACC PLOGI rsp command needs to execute first,
412          * queue this mbox command to be processed later.
413          */
414         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
415         /*
416          * mbox->context2 = lpfc_nlp_get(ndlp) deferred until mailbox
417          * command issued in lpfc_cmpl_els_acc().
418          */
419         mbox->vport = vport;
420         spin_lock_irq(shost->host_lock);
421         ndlp->nlp_flag |= (NLP_ACC_REGLOGIN | NLP_RCV_PLOGI);
422         spin_unlock_irq(shost->host_lock);
423
424         /*
425          * If there is an outstanding PLOGI issued, abort it before
426          * sending ACC rsp for received PLOGI. If pending plogi
427          * is not canceled here, the plogi will be rejected by
428          * remote port and will be retried. On a configuration with
429          * single discovery thread, this will cause a huge delay in
430          * discovery. Also this will cause multiple state machines
431          * running in parallel for this node.
432          */
433         if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE) {
434                 /* software abort outstanding PLOGI */
435                 lpfc_els_abort(phba, ndlp);
436         }
437
438         if ((vport->port_type == LPFC_NPIV_PORT &&
439              vport->cfg_restrict_login)) {
440
441                 /* In order to preserve RPIs, we want to cleanup
442                  * the default RPI the firmware created to rcv
443                  * this ELS request. The only way to do this is
444                  * to register, then unregister the RPI.
445                  */
446                 spin_lock_irq(shost->host_lock);
447                 ndlp->nlp_flag |= NLP_RM_DFLT_RPI;
448                 spin_unlock_irq(shost->host_lock);
449                 stat.un.b.lsRjtRsnCode = LSRJT_INVALID_CMD;
450                 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
451                 rc = lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb,
452                         ndlp, mbox);
453                 if (rc)
454                         mempool_free(mbox, phba->mbox_mem_pool);
455                 return 1;
456         }
457         rc = lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, mbox);
458         if (rc)
459                 mempool_free(mbox, phba->mbox_mem_pool);
460         return 1;
461 out:
462         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
463         stat.un.b.lsRjtRsnCodeExp = LSEXP_OUT_OF_RESOURCE;
464         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
465         return 0;
466 }
467
468 /**
469  * lpfc_mbx_cmpl_resume_rpi - Resume RPI completion routine
470  * @phba: pointer to lpfc hba data structure.
471  * @mboxq: pointer to mailbox object
472  *
473  * This routine is invoked to issue a completion to a rcv'ed
474  * ADISC or PDISC after the paused RPI has been resumed.
475  **/
476 static void
477 lpfc_mbx_cmpl_resume_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
478 {
479         struct lpfc_vport *vport;
480         struct lpfc_iocbq *elsiocb;
481         struct lpfc_nodelist *ndlp;
482         uint32_t cmd;
483
484         elsiocb = (struct lpfc_iocbq *)mboxq->context1;
485         ndlp = (struct lpfc_nodelist *) mboxq->context2;
486         vport = mboxq->vport;
487         cmd = elsiocb->drvrTimeout;
488
489         if (cmd == ELS_CMD_ADISC) {
490                 lpfc_els_rsp_adisc_acc(vport, elsiocb, ndlp);
491         } else {
492                 lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, elsiocb,
493                         ndlp, NULL);
494         }
495         kfree(elsiocb);
496         mempool_free(mboxq, phba->mbox_mem_pool);
497 }
498
499 static int
500 lpfc_rcv_padisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
501                 struct lpfc_iocbq *cmdiocb)
502 {
503         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
504         struct lpfc_iocbq  *elsiocb;
505         struct lpfc_dmabuf *pcmd;
506         struct serv_parm   *sp;
507         struct lpfc_name   *pnn, *ppn;
508         struct ls_rjt stat;
509         ADISC *ap;
510         IOCB_t *icmd;
511         uint32_t *lp;
512         uint32_t cmd;
513
514         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
515         lp = (uint32_t *) pcmd->virt;
516
517         cmd = *lp++;
518         if (cmd == ELS_CMD_ADISC) {
519                 ap = (ADISC *) lp;
520                 pnn = (struct lpfc_name *) & ap->nodeName;
521                 ppn = (struct lpfc_name *) & ap->portName;
522         } else {
523                 sp = (struct serv_parm *) lp;
524                 pnn = (struct lpfc_name *) & sp->nodeName;
525                 ppn = (struct lpfc_name *) & sp->portName;
526         }
527
528         icmd = &cmdiocb->iocb;
529         if (icmd->ulpStatus == 0 && lpfc_check_adisc(vport, ndlp, pnn, ppn)) {
530
531                 /*
532                  * As soon as  we send ACC, the remote NPort can
533                  * start sending us data. Thus, for SLI4 we must
534                  * resume the RPI before the ACC goes out.
535                  */
536                 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
537                         elsiocb = kmalloc(sizeof(struct lpfc_iocbq),
538                                 GFP_KERNEL);
539                         if (elsiocb) {
540
541                                 /* Save info from cmd IOCB used in rsp */
542                                 memcpy((uint8_t *)elsiocb, (uint8_t *)cmdiocb,
543                                         sizeof(struct lpfc_iocbq));
544
545                                 /* Save the ELS cmd */
546                                 elsiocb->drvrTimeout = cmd;
547
548                                 lpfc_sli4_resume_rpi(ndlp,
549                                         lpfc_mbx_cmpl_resume_rpi, elsiocb);
550                                 goto out;
551                         }
552                 }
553
554                 if (cmd == ELS_CMD_ADISC) {
555                         lpfc_els_rsp_adisc_acc(vport, cmdiocb, ndlp);
556                 } else {
557                         lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb,
558                                 ndlp, NULL);
559                 }
560 out:
561                 /* If we are authenticated, move to the proper state */
562                 if (ndlp->nlp_type & NLP_FCP_TARGET)
563                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
564                 else
565                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
566
567                 return 1;
568         }
569         /* Reject this request because invalid parameters */
570         stat.un.b.lsRjtRsvd0 = 0;
571         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
572         stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
573         stat.un.b.vendorUnique = 0;
574         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
575
576         /* 1 sec timeout */
577         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
578
579         spin_lock_irq(shost->host_lock);
580         ndlp->nlp_flag |= NLP_DELAY_TMO;
581         spin_unlock_irq(shost->host_lock);
582         ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
583         ndlp->nlp_prev_state = ndlp->nlp_state;
584         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
585         return 0;
586 }
587
588 static int
589 lpfc_rcv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
590               struct lpfc_iocbq *cmdiocb, uint32_t els_cmd)
591 {
592         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
593         struct lpfc_hba    *phba = vport->phba;
594         struct lpfc_vport **vports;
595         int i, active_vlink_present = 0 ;
596
597         /* Put ndlp in NPR state with 1 sec timeout for plogi, ACC logo */
598         /* Only call LOGO ACC for first LOGO, this avoids sending unnecessary
599          * PLOGIs during LOGO storms from a device.
600          */
601         spin_lock_irq(shost->host_lock);
602         ndlp->nlp_flag |= NLP_LOGO_ACC;
603         spin_unlock_irq(shost->host_lock);
604         if (els_cmd == ELS_CMD_PRLO)
605                 lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
606         else
607                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
608         if (ndlp->nlp_DID == Fabric_DID) {
609                 if (vport->port_state <= LPFC_FDISC)
610                         goto out;
611                 lpfc_linkdown_port(vport);
612                 spin_lock_irq(shost->host_lock);
613                 vport->fc_flag |= FC_VPORT_LOGO_RCVD;
614                 spin_unlock_irq(shost->host_lock);
615                 vports = lpfc_create_vport_work_array(phba);
616                 if (vports) {
617                         for (i = 0; i <= phba->max_vports && vports[i] != NULL;
618                                         i++) {
619                                 if ((!(vports[i]->fc_flag &
620                                         FC_VPORT_LOGO_RCVD)) &&
621                                         (vports[i]->port_state > LPFC_FDISC)) {
622                                         active_vlink_present = 1;
623                                         break;
624                                 }
625                         }
626                         lpfc_destroy_vport_work_array(phba, vports);
627                 }
628
629                 if (active_vlink_present) {
630                         /*
631                          * If there are other active VLinks present,
632                          * re-instantiate the Vlink using FDISC.
633                          */
634                         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
635                         spin_lock_irq(shost->host_lock);
636                         ndlp->nlp_flag |= NLP_DELAY_TMO;
637                         spin_unlock_irq(shost->host_lock);
638                         ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
639                         vport->port_state = LPFC_FDISC;
640                 } else {
641                         spin_lock_irq(shost->host_lock);
642                         phba->pport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG;
643                         spin_unlock_irq(shost->host_lock);
644                         lpfc_retry_pport_discovery(phba);
645                 }
646         } else if ((!(ndlp->nlp_type & NLP_FABRIC) &&
647                 ((ndlp->nlp_type & NLP_FCP_TARGET) ||
648                 !(ndlp->nlp_type & NLP_FCP_INITIATOR))) ||
649                 (ndlp->nlp_state == NLP_STE_ADISC_ISSUE)) {
650                 /* Only try to re-login if this is NOT a Fabric Node */
651                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
652                 spin_lock_irq(shost->host_lock);
653                 ndlp->nlp_flag |= NLP_DELAY_TMO;
654                 spin_unlock_irq(shost->host_lock);
655
656                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
657         }
658 out:
659         ndlp->nlp_prev_state = ndlp->nlp_state;
660         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
661
662         spin_lock_irq(shost->host_lock);
663         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
664         spin_unlock_irq(shost->host_lock);
665         /* The driver has to wait until the ACC completes before it continues
666          * processing the LOGO.  The action will resume in
667          * lpfc_cmpl_els_logo_acc routine. Since part of processing includes an
668          * unreg_login, the driver waits so the ACC does not get aborted.
669          */
670         return 0;
671 }
672
673 static void
674 lpfc_rcv_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
675               struct lpfc_iocbq *cmdiocb)
676 {
677         struct lpfc_dmabuf *pcmd;
678         uint32_t *lp;
679         PRLI *npr;
680         struct fc_rport *rport = ndlp->rport;
681         u32 roles;
682
683         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
684         lp = (uint32_t *) pcmd->virt;
685         npr = (PRLI *) ((uint8_t *) lp + sizeof (uint32_t));
686
687         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
688         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
689         if (npr->prliType == PRLI_FCP_TYPE) {
690                 if (npr->initiatorFunc)
691                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
692                 if (npr->targetFunc)
693                         ndlp->nlp_type |= NLP_FCP_TARGET;
694                 if (npr->Retry)
695                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
696         }
697         if (rport) {
698                 /* We need to update the rport role values */
699                 roles = FC_RPORT_ROLE_UNKNOWN;
700                 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
701                         roles |= FC_RPORT_ROLE_FCP_INITIATOR;
702                 if (ndlp->nlp_type & NLP_FCP_TARGET)
703                         roles |= FC_RPORT_ROLE_FCP_TARGET;
704
705                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
706                         "rport rolechg:   role:x%x did:x%x flg:x%x",
707                         roles, ndlp->nlp_DID, ndlp->nlp_flag);
708
709                 fc_remote_port_rolechg(rport, roles);
710         }
711 }
712
713 static uint32_t
714 lpfc_disc_set_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
715 {
716         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
717
718         if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
719                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
720                 return 0;
721         }
722
723         if (!(vport->fc_flag & FC_PT2PT)) {
724                 /* Check config parameter use-adisc or FCP-2 */
725                 if ((vport->cfg_use_adisc && (vport->fc_flag & FC_RSCN_MODE)) ||
726                     ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) &&
727                      (ndlp->nlp_type & NLP_FCP_TARGET))) {
728                         spin_lock_irq(shost->host_lock);
729                         ndlp->nlp_flag |= NLP_NPR_ADISC;
730                         spin_unlock_irq(shost->host_lock);
731                         return 1;
732                 }
733         }
734         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
735         lpfc_unreg_rpi(vport, ndlp);
736         return 0;
737 }
738
739 /**
740  * lpfc_release_rpi - Release a RPI by issuing unreg_login mailbox cmd.
741  * @phba : Pointer to lpfc_hba structure.
742  * @vport: Pointer to lpfc_vport structure.
743  * @rpi  : rpi to be release.
744  *
745  * This function will send a unreg_login mailbox command to the firmware
746  * to release a rpi.
747  **/
748 void
749 lpfc_release_rpi(struct lpfc_hba *phba,
750                 struct lpfc_vport *vport,
751                 uint16_t rpi)
752 {
753         LPFC_MBOXQ_t *pmb;
754         int rc;
755
756         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
757                         GFP_KERNEL);
758         if (!pmb)
759                 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
760                         "2796 mailbox memory allocation failed \n");
761         else {
762                 lpfc_unreg_login(phba, vport->vpi, rpi, pmb);
763                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
764                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
765                 if (rc == MBX_NOT_FINISHED)
766                         mempool_free(pmb, phba->mbox_mem_pool);
767         }
768 }
769
770 static uint32_t
771 lpfc_disc_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
772                   void *arg, uint32_t evt)
773 {
774         struct lpfc_hba *phba;
775         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
776         MAILBOX_t *mb;
777         uint16_t rpi;
778
779         phba = vport->phba;
780         /* Release the RPI if reglogin completing */
781         if (!(phba->pport->load_flag & FC_UNLOADING) &&
782                 (evt == NLP_EVT_CMPL_REG_LOGIN) &&
783                 (!pmb->u.mb.mbxStatus)) {
784                 mb = &pmb->u.mb;
785                 rpi = pmb->u.mb.un.varWords[0];
786                 lpfc_release_rpi(phba, vport, rpi);
787         }
788         lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
789                          "0271 Illegal State Transition: node x%x "
790                          "event x%x, state x%x Data: x%x x%x\n",
791                          ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
792                          ndlp->nlp_flag);
793         return ndlp->nlp_state;
794 }
795
796 static uint32_t
797 lpfc_cmpl_plogi_illegal(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
798                   void *arg, uint32_t evt)
799 {
800         /* This transition is only legal if we previously
801          * rcv'ed a PLOGI. Since we don't want 2 discovery threads
802          * working on the same NPortID, do nothing for this thread
803          * to stop it.
804          */
805         if (!(ndlp->nlp_flag & NLP_RCV_PLOGI)) {
806                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
807                          "0272 Illegal State Transition: node x%x "
808                          "event x%x, state x%x Data: x%x x%x\n",
809                          ndlp->nlp_DID, evt, ndlp->nlp_state, ndlp->nlp_rpi,
810                          ndlp->nlp_flag);
811         }
812         return ndlp->nlp_state;
813 }
814
815 /* Start of Discovery State Machine routines */
816
817 static uint32_t
818 lpfc_rcv_plogi_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
819                            void *arg, uint32_t evt)
820 {
821         struct lpfc_iocbq *cmdiocb;
822
823         cmdiocb = (struct lpfc_iocbq *) arg;
824
825         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
826                 return ndlp->nlp_state;
827         }
828         return NLP_STE_FREED_NODE;
829 }
830
831 static uint32_t
832 lpfc_rcv_els_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
833                          void *arg, uint32_t evt)
834 {
835         lpfc_issue_els_logo(vport, ndlp, 0);
836         return ndlp->nlp_state;
837 }
838
839 static uint32_t
840 lpfc_rcv_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
841                           void *arg, uint32_t evt)
842 {
843         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
844         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
845
846         spin_lock_irq(shost->host_lock);
847         ndlp->nlp_flag |= NLP_LOGO_ACC;
848         spin_unlock_irq(shost->host_lock);
849         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
850
851         return ndlp->nlp_state;
852 }
853
854 static uint32_t
855 lpfc_cmpl_logo_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
856                            void *arg, uint32_t evt)
857 {
858         return NLP_STE_FREED_NODE;
859 }
860
861 static uint32_t
862 lpfc_device_rm_unused_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
863                            void *arg, uint32_t evt)
864 {
865         return NLP_STE_FREED_NODE;
866 }
867
868 static uint32_t
869 lpfc_device_recov_unused_node(struct lpfc_vport *vport,
870                         struct lpfc_nodelist *ndlp,
871                            void *arg, uint32_t evt)
872 {
873         return ndlp->nlp_state;
874 }
875
876 static uint32_t
877 lpfc_rcv_plogi_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
878                            void *arg, uint32_t evt)
879 {
880         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
881         struct lpfc_hba   *phba = vport->phba;
882         struct lpfc_iocbq *cmdiocb = arg;
883         struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
884         uint32_t *lp = (uint32_t *) pcmd->virt;
885         struct serv_parm *sp = (struct serv_parm *) (lp + 1);
886         struct ls_rjt stat;
887         int port_cmp;
888
889         memset(&stat, 0, sizeof (struct ls_rjt));
890
891         /* For a PLOGI, we only accept if our portname is less
892          * than the remote portname.
893          */
894         phba->fc_stat.elsLogiCol++;
895         port_cmp = memcmp(&vport->fc_portname, &sp->portName,
896                           sizeof(struct lpfc_name));
897
898         if (port_cmp >= 0) {
899                 /* Reject this request because the remote node will accept
900                    ours */
901                 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
902                 stat.un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
903                 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
904                         NULL);
905         } else {
906                 if (lpfc_rcv_plogi(vport, ndlp, cmdiocb) &&
907                     (ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
908                     (vport->num_disc_nodes)) {
909                         spin_lock_irq(shost->host_lock);
910                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
911                         spin_unlock_irq(shost->host_lock);
912                         /* Check if there are more PLOGIs to be sent */
913                         lpfc_more_plogi(vport);
914                         if (vport->num_disc_nodes == 0) {
915                                 spin_lock_irq(shost->host_lock);
916                                 vport->fc_flag &= ~FC_NDISC_ACTIVE;
917                                 spin_unlock_irq(shost->host_lock);
918                                 lpfc_can_disctmo(vport);
919                                 lpfc_end_rscn(vport);
920                         }
921                 }
922         } /* If our portname was less */
923
924         return ndlp->nlp_state;
925 }
926
927 static uint32_t
928 lpfc_rcv_prli_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
929                           void *arg, uint32_t evt)
930 {
931         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
932         struct ls_rjt     stat;
933
934         memset(&stat, 0, sizeof (struct ls_rjt));
935         stat.un.b.lsRjtRsnCode = LSRJT_LOGICAL_BSY;
936         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
937         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
938         return ndlp->nlp_state;
939 }
940
941 static uint32_t
942 lpfc_rcv_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
943                           void *arg, uint32_t evt)
944 {
945         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
946
947                                 /* software abort outstanding PLOGI */
948         lpfc_els_abort(vport->phba, ndlp);
949
950         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
951         return ndlp->nlp_state;
952 }
953
954 static uint32_t
955 lpfc_rcv_els_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
956                          void *arg, uint32_t evt)
957 {
958         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
959         struct lpfc_hba   *phba = vport->phba;
960         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
961
962         /* software abort outstanding PLOGI */
963         lpfc_els_abort(phba, ndlp);
964
965         if (evt == NLP_EVT_RCV_LOGO) {
966                 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
967         } else {
968                 lpfc_issue_els_logo(vport, ndlp, 0);
969         }
970
971         /* Put ndlp in npr state set plogi timer for 1 sec */
972         mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
973         spin_lock_irq(shost->host_lock);
974         ndlp->nlp_flag |= NLP_DELAY_TMO;
975         spin_unlock_irq(shost->host_lock);
976         ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
977         ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
978         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
979
980         return ndlp->nlp_state;
981 }
982
983 static uint32_t
984 lpfc_cmpl_plogi_plogi_issue(struct lpfc_vport *vport,
985                             struct lpfc_nodelist *ndlp,
986                             void *arg,
987                             uint32_t evt)
988 {
989         struct lpfc_hba    *phba = vport->phba;
990         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
991         struct lpfc_iocbq  *cmdiocb, *rspiocb;
992         struct lpfc_dmabuf *pcmd, *prsp, *mp;
993         uint32_t *lp;
994         IOCB_t *irsp;
995         struct serv_parm *sp;
996         LPFC_MBOXQ_t *mbox;
997
998         cmdiocb = (struct lpfc_iocbq *) arg;
999         rspiocb = cmdiocb->context_un.rsp_iocb;
1000
1001         if (ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
1002                 /* Recovery from PLOGI collision logic */
1003                 return ndlp->nlp_state;
1004         }
1005
1006         irsp = &rspiocb->iocb;
1007
1008         if (irsp->ulpStatus)
1009                 goto out;
1010
1011         pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
1012
1013         prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1014
1015         lp = (uint32_t *) prsp->virt;
1016         sp = (struct serv_parm *) ((uint8_t *) lp + sizeof (uint32_t));
1017
1018         /* Some switches have FDMI servers returning 0 for WWN */
1019         if ((ndlp->nlp_DID != FDMI_DID) &&
1020                 (wwn_to_u64(sp->portName.u.wwn) == 0 ||
1021                 wwn_to_u64(sp->nodeName.u.wwn) == 0)) {
1022                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1023                                  "0142 PLOGI RSP: Invalid WWN.\n");
1024                 goto out;
1025         }
1026         if (!lpfc_check_sparm(vport, ndlp, sp, CLASS3, 0))
1027                 goto out;
1028         /* PLOGI chkparm OK */
1029         lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1030                          "0121 PLOGI chkparm OK Data: x%x x%x x%x x%x\n",
1031                          ndlp->nlp_DID, ndlp->nlp_state,
1032                          ndlp->nlp_flag, ndlp->nlp_rpi);
1033         if (vport->cfg_fcp_class == 2 && (sp->cls2.classValid))
1034                 ndlp->nlp_fcp_info |= CLASS2;
1035         else
1036                 ndlp->nlp_fcp_info |= CLASS3;
1037
1038         ndlp->nlp_class_sup = 0;
1039         if (sp->cls1.classValid)
1040                 ndlp->nlp_class_sup |= FC_COS_CLASS1;
1041         if (sp->cls2.classValid)
1042                 ndlp->nlp_class_sup |= FC_COS_CLASS2;
1043         if (sp->cls3.classValid)
1044                 ndlp->nlp_class_sup |= FC_COS_CLASS3;
1045         if (sp->cls4.classValid)
1046                 ndlp->nlp_class_sup |= FC_COS_CLASS4;
1047         ndlp->nlp_maxframe =
1048                 ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) | sp->cmn.bbRcvSizeLsb;
1049
1050         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1051         if (!mbox) {
1052                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1053                         "0133 PLOGI: no memory for reg_login "
1054                         "Data: x%x x%x x%x x%x\n",
1055                         ndlp->nlp_DID, ndlp->nlp_state,
1056                         ndlp->nlp_flag, ndlp->nlp_rpi);
1057                 goto out;
1058         }
1059
1060         lpfc_unreg_rpi(vport, ndlp);
1061
1062         if (lpfc_reg_rpi(phba, vport->vpi, irsp->un.elsreq64.remoteID,
1063                          (uint8_t *) sp, mbox, ndlp->nlp_rpi) == 0) {
1064                 switch (ndlp->nlp_DID) {
1065                 case NameServer_DID:
1066                         mbox->mbox_cmpl = lpfc_mbx_cmpl_ns_reg_login;
1067                         break;
1068                 case FDMI_DID:
1069                         mbox->mbox_cmpl = lpfc_mbx_cmpl_fdmi_reg_login;
1070                         break;
1071                 default:
1072                         ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
1073                         mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
1074                 }
1075                 mbox->context2 = lpfc_nlp_get(ndlp);
1076                 mbox->vport = vport;
1077                 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
1078                     != MBX_NOT_FINISHED) {
1079                         lpfc_nlp_set_state(vport, ndlp,
1080                                            NLP_STE_REG_LOGIN_ISSUE);
1081                         return ndlp->nlp_state;
1082                 }
1083                 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
1084                         ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
1085                 /* decrement node reference count to the failed mbox
1086                  * command
1087                  */
1088                 lpfc_nlp_put(ndlp);
1089                 mp = (struct lpfc_dmabuf *) mbox->context1;
1090                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1091                 kfree(mp);
1092                 mempool_free(mbox, phba->mbox_mem_pool);
1093
1094                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1095                                  "0134 PLOGI: cannot issue reg_login "
1096                                  "Data: x%x x%x x%x x%x\n",
1097                                  ndlp->nlp_DID, ndlp->nlp_state,
1098                                  ndlp->nlp_flag, ndlp->nlp_rpi);
1099         } else {
1100                 mempool_free(mbox, phba->mbox_mem_pool);
1101
1102                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1103                                  "0135 PLOGI: cannot format reg_login "
1104                                  "Data: x%x x%x x%x x%x\n",
1105                                  ndlp->nlp_DID, ndlp->nlp_state,
1106                                  ndlp->nlp_flag, ndlp->nlp_rpi);
1107         }
1108
1109
1110 out:
1111         if (ndlp->nlp_DID == NameServer_DID) {
1112                 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1113                 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1114                                  "0261 Cannot Register NameServer login\n");
1115         }
1116
1117         /*
1118         ** In case the node reference counter does not go to zero, ensure that
1119         ** the stale state for the node is not processed.
1120         */
1121
1122         ndlp->nlp_prev_state = ndlp->nlp_state;
1123         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1124         spin_lock_irq(shost->host_lock);
1125         ndlp->nlp_flag |= NLP_DEFER_RM;
1126         spin_unlock_irq(shost->host_lock);
1127         return NLP_STE_FREED_NODE;
1128 }
1129
1130 static uint32_t
1131 lpfc_cmpl_logo_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1132                            void *arg, uint32_t evt)
1133 {
1134         return ndlp->nlp_state;
1135 }
1136
1137 static uint32_t
1138 lpfc_cmpl_reglogin_plogi_issue(struct lpfc_vport *vport,
1139         struct lpfc_nodelist *ndlp, void *arg, uint32_t evt)
1140 {
1141         struct lpfc_hba *phba;
1142         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1143         MAILBOX_t *mb = &pmb->u.mb;
1144         uint16_t rpi;
1145
1146         phba = vport->phba;
1147         /* Release the RPI */
1148         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1149                 !mb->mbxStatus) {
1150                 rpi = pmb->u.mb.un.varWords[0];
1151                 lpfc_release_rpi(phba, vport, rpi);
1152         }
1153         return ndlp->nlp_state;
1154 }
1155
1156 static uint32_t
1157 lpfc_device_rm_plogi_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1158                            void *arg, uint32_t evt)
1159 {
1160         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1161
1162         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1163                 spin_lock_irq(shost->host_lock);
1164                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1165                 spin_unlock_irq(shost->host_lock);
1166                 return ndlp->nlp_state;
1167         } else {
1168                 /* software abort outstanding PLOGI */
1169                 lpfc_els_abort(vport->phba, ndlp);
1170
1171                 lpfc_drop_node(vport, ndlp);
1172                 return NLP_STE_FREED_NODE;
1173         }
1174 }
1175
1176 static uint32_t
1177 lpfc_device_recov_plogi_issue(struct lpfc_vport *vport,
1178                               struct lpfc_nodelist *ndlp,
1179                               void *arg,
1180                               uint32_t evt)
1181 {
1182         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1183         struct lpfc_hba  *phba = vport->phba;
1184
1185         /* Don't do anything that will mess up processing of the
1186          * previous RSCN.
1187          */
1188         if (vport->fc_flag & FC_RSCN_DEFERRED)
1189                 return ndlp->nlp_state;
1190
1191         /* software abort outstanding PLOGI */
1192         lpfc_els_abort(phba, ndlp);
1193
1194         ndlp->nlp_prev_state = NLP_STE_PLOGI_ISSUE;
1195         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1196         spin_lock_irq(shost->host_lock);
1197         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1198         spin_unlock_irq(shost->host_lock);
1199
1200         return ndlp->nlp_state;
1201 }
1202
1203 static uint32_t
1204 lpfc_rcv_plogi_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1205                            void *arg, uint32_t evt)
1206 {
1207         struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1208         struct lpfc_hba   *phba = vport->phba;
1209         struct lpfc_iocbq *cmdiocb;
1210
1211         /* software abort outstanding ADISC */
1212         lpfc_els_abort(phba, ndlp);
1213
1214         cmdiocb = (struct lpfc_iocbq *) arg;
1215
1216         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
1217                 if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1218                         spin_lock_irq(shost->host_lock);
1219                         ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1220                         spin_unlock_irq(shost->host_lock);
1221                         if (vport->num_disc_nodes)
1222                                 lpfc_more_adisc(vport);
1223                 }
1224                 return ndlp->nlp_state;
1225         }
1226         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1227         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
1228         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
1229
1230         return ndlp->nlp_state;
1231 }
1232
1233 static uint32_t
1234 lpfc_rcv_prli_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1235                           void *arg, uint32_t evt)
1236 {
1237         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1238
1239         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1240         return ndlp->nlp_state;
1241 }
1242
1243 static uint32_t
1244 lpfc_rcv_logo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1245                           void *arg, uint32_t evt)
1246 {
1247         struct lpfc_hba *phba = vport->phba;
1248         struct lpfc_iocbq *cmdiocb;
1249
1250         cmdiocb = (struct lpfc_iocbq *) arg;
1251
1252         /* software abort outstanding ADISC */
1253         lpfc_els_abort(phba, ndlp);
1254
1255         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1256         return ndlp->nlp_state;
1257 }
1258
1259 static uint32_t
1260 lpfc_rcv_padisc_adisc_issue(struct lpfc_vport *vport,
1261                             struct lpfc_nodelist *ndlp,
1262                             void *arg, uint32_t evt)
1263 {
1264         struct lpfc_iocbq *cmdiocb;
1265
1266         cmdiocb = (struct lpfc_iocbq *) arg;
1267
1268         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1269         return ndlp->nlp_state;
1270 }
1271
1272 static uint32_t
1273 lpfc_rcv_prlo_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1274                           void *arg, uint32_t evt)
1275 {
1276         struct lpfc_iocbq *cmdiocb;
1277
1278         cmdiocb = (struct lpfc_iocbq *) arg;
1279
1280         /* Treat like rcv logo */
1281         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
1282         return ndlp->nlp_state;
1283 }
1284
1285 static uint32_t
1286 lpfc_cmpl_adisc_adisc_issue(struct lpfc_vport *vport,
1287                             struct lpfc_nodelist *ndlp,
1288                             void *arg, uint32_t evt)
1289 {
1290         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1291         struct lpfc_hba   *phba = vport->phba;
1292         struct lpfc_iocbq *cmdiocb, *rspiocb;
1293         IOCB_t *irsp;
1294         ADISC *ap;
1295         int rc;
1296
1297         cmdiocb = (struct lpfc_iocbq *) arg;
1298         rspiocb = cmdiocb->context_un.rsp_iocb;
1299
1300         ap = (ADISC *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1301         irsp = &rspiocb->iocb;
1302
1303         if ((irsp->ulpStatus) ||
1304             (!lpfc_check_adisc(vport, ndlp, &ap->nodeName, &ap->portName))) {
1305                 /* 1 sec timeout */
1306                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
1307                 spin_lock_irq(shost->host_lock);
1308                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1309                 spin_unlock_irq(shost->host_lock);
1310                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1311
1312                 memset(&ndlp->nlp_nodename, 0, sizeof(struct lpfc_name));
1313                 memset(&ndlp->nlp_portname, 0, sizeof(struct lpfc_name));
1314
1315                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1316                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1317                 lpfc_unreg_rpi(vport, ndlp);
1318                 return ndlp->nlp_state;
1319         }
1320
1321         if (phba->sli_rev == LPFC_SLI_REV4) {
1322                 rc = lpfc_sli4_resume_rpi(ndlp, NULL, NULL);
1323                 if (rc) {
1324                         /* Stay in state and retry. */
1325                         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1326                         return ndlp->nlp_state;
1327                 }
1328         }
1329
1330         if (ndlp->nlp_type & NLP_FCP_TARGET) {
1331                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1332                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1333         } else {
1334                 ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1335                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1336         }
1337
1338         return ndlp->nlp_state;
1339 }
1340
1341 static uint32_t
1342 lpfc_device_rm_adisc_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1343                            void *arg, uint32_t evt)
1344 {
1345         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1346
1347         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1348                 spin_lock_irq(shost->host_lock);
1349                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1350                 spin_unlock_irq(shost->host_lock);
1351                 return ndlp->nlp_state;
1352         } else {
1353                 /* software abort outstanding ADISC */
1354                 lpfc_els_abort(vport->phba, ndlp);
1355
1356                 lpfc_drop_node(vport, ndlp);
1357                 return NLP_STE_FREED_NODE;
1358         }
1359 }
1360
1361 static uint32_t
1362 lpfc_device_recov_adisc_issue(struct lpfc_vport *vport,
1363                               struct lpfc_nodelist *ndlp,
1364                               void *arg,
1365                               uint32_t evt)
1366 {
1367         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1368         struct lpfc_hba  *phba = vport->phba;
1369
1370         /* Don't do anything that will mess up processing of the
1371          * previous RSCN.
1372          */
1373         if (vport->fc_flag & FC_RSCN_DEFERRED)
1374                 return ndlp->nlp_state;
1375
1376         /* software abort outstanding ADISC */
1377         lpfc_els_abort(phba, ndlp);
1378
1379         ndlp->nlp_prev_state = NLP_STE_ADISC_ISSUE;
1380         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1381         spin_lock_irq(shost->host_lock);
1382         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1383         spin_unlock_irq(shost->host_lock);
1384         lpfc_disc_set_adisc(vport, ndlp);
1385         return ndlp->nlp_state;
1386 }
1387
1388 static uint32_t
1389 lpfc_rcv_plogi_reglogin_issue(struct lpfc_vport *vport,
1390                               struct lpfc_nodelist *ndlp,
1391                               void *arg,
1392                               uint32_t evt)
1393 {
1394         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1395
1396         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1397         return ndlp->nlp_state;
1398 }
1399
1400 static uint32_t
1401 lpfc_rcv_prli_reglogin_issue(struct lpfc_vport *vport,
1402                              struct lpfc_nodelist *ndlp,
1403                              void *arg,
1404                              uint32_t evt)
1405 {
1406         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1407
1408         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1409         return ndlp->nlp_state;
1410 }
1411
1412 static uint32_t
1413 lpfc_rcv_logo_reglogin_issue(struct lpfc_vport *vport,
1414                              struct lpfc_nodelist *ndlp,
1415                              void *arg,
1416                              uint32_t evt)
1417 {
1418         struct lpfc_hba   *phba = vport->phba;
1419         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1420         LPFC_MBOXQ_t      *mb;
1421         LPFC_MBOXQ_t      *nextmb;
1422         struct lpfc_dmabuf *mp;
1423
1424         cmdiocb = (struct lpfc_iocbq *) arg;
1425
1426         /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
1427         if ((mb = phba->sli.mbox_active)) {
1428                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1429                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1430                         lpfc_nlp_put(ndlp);
1431                         mb->context2 = NULL;
1432                         mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1433                 }
1434         }
1435
1436         spin_lock_irq(&phba->hbalock);
1437         list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
1438                 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
1439                    (ndlp == (struct lpfc_nodelist *) mb->context2)) {
1440                         mp = (struct lpfc_dmabuf *) (mb->context1);
1441                         if (mp) {
1442                                 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
1443                                 kfree(mp);
1444                         }
1445                         lpfc_nlp_put(ndlp);
1446                         list_del(&mb->list);
1447                         phba->sli.mboxq_cnt--;
1448                         mempool_free(mb, phba->mbox_mem_pool);
1449                 }
1450         }
1451         spin_unlock_irq(&phba->hbalock);
1452
1453         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1454         return ndlp->nlp_state;
1455 }
1456
1457 static uint32_t
1458 lpfc_rcv_padisc_reglogin_issue(struct lpfc_vport *vport,
1459                                struct lpfc_nodelist *ndlp,
1460                                void *arg,
1461                                uint32_t evt)
1462 {
1463         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1464
1465         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1466         return ndlp->nlp_state;
1467 }
1468
1469 static uint32_t
1470 lpfc_rcv_prlo_reglogin_issue(struct lpfc_vport *vport,
1471                              struct lpfc_nodelist *ndlp,
1472                              void *arg,
1473                              uint32_t evt)
1474 {
1475         struct lpfc_iocbq *cmdiocb;
1476
1477         cmdiocb = (struct lpfc_iocbq *) arg;
1478         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1479         return ndlp->nlp_state;
1480 }
1481
1482 static uint32_t
1483 lpfc_cmpl_reglogin_reglogin_issue(struct lpfc_vport *vport,
1484                                   struct lpfc_nodelist *ndlp,
1485                                   void *arg,
1486                                   uint32_t evt)
1487 {
1488         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1489         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
1490         MAILBOX_t *mb = &pmb->u.mb;
1491         uint32_t did  = mb->un.varWords[1];
1492
1493         if (mb->mbxStatus) {
1494                 /* RegLogin failed */
1495                 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1496                                 "0246 RegLogin failed Data: x%x x%x x%x x%x "
1497                                  "x%x\n",
1498                                  did, mb->mbxStatus, vport->port_state,
1499                                  mb->un.varRegLogin.vpi,
1500                                  mb->un.varRegLogin.rpi);
1501                 /*
1502                  * If RegLogin failed due to lack of HBA resources do not
1503                  * retry discovery.
1504                  */
1505                 if (mb->mbxStatus == MBXERR_RPI_FULL) {
1506                         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1507                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1508                         return ndlp->nlp_state;
1509                 }
1510
1511                 /* Put ndlp in npr state set plogi timer for 1 sec */
1512                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
1513                 spin_lock_irq(shost->host_lock);
1514                 ndlp->nlp_flag |= NLP_DELAY_TMO;
1515                 spin_unlock_irq(shost->host_lock);
1516                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
1517
1518                 lpfc_issue_els_logo(vport, ndlp, 0);
1519                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1520                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1521                 return ndlp->nlp_state;
1522         }
1523
1524         /* SLI4 ports have preallocated logical rpis. */
1525         if (vport->phba->sli_rev < LPFC_SLI_REV4)
1526                 ndlp->nlp_rpi = mb->un.varWords[0];
1527
1528         ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1529
1530         /* Only if we are not a fabric nport do we issue PRLI */
1531         if (!(ndlp->nlp_type & NLP_FABRIC)) {
1532                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1533                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
1534                 lpfc_issue_els_prli(vport, ndlp, 0);
1535         } else {
1536                 ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1537                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1538         }
1539         return ndlp->nlp_state;
1540 }
1541
1542 static uint32_t
1543 lpfc_device_rm_reglogin_issue(struct lpfc_vport *vport,
1544                               struct lpfc_nodelist *ndlp,
1545                               void *arg,
1546                               uint32_t evt)
1547 {
1548         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1549
1550         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1551                 spin_lock_irq(shost->host_lock);
1552                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1553                 spin_unlock_irq(shost->host_lock);
1554                 return ndlp->nlp_state;
1555         } else {
1556                 lpfc_drop_node(vport, ndlp);
1557                 return NLP_STE_FREED_NODE;
1558         }
1559 }
1560
1561 static uint32_t
1562 lpfc_device_recov_reglogin_issue(struct lpfc_vport *vport,
1563                                  struct lpfc_nodelist *ndlp,
1564                                  void *arg,
1565                                  uint32_t evt)
1566 {
1567         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1568
1569         /* Don't do anything that will mess up processing of the
1570          * previous RSCN.
1571          */
1572         if (vport->fc_flag & FC_RSCN_DEFERRED)
1573                 return ndlp->nlp_state;
1574
1575         ndlp->nlp_prev_state = NLP_STE_REG_LOGIN_ISSUE;
1576         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1577         spin_lock_irq(shost->host_lock);
1578         ndlp->nlp_flag |= NLP_IGNR_REG_CMPL;
1579         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1580         spin_unlock_irq(shost->host_lock);
1581         lpfc_disc_set_adisc(vport, ndlp);
1582         return ndlp->nlp_state;
1583 }
1584
1585 static uint32_t
1586 lpfc_rcv_plogi_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1587                           void *arg, uint32_t evt)
1588 {
1589         struct lpfc_iocbq *cmdiocb;
1590
1591         cmdiocb = (struct lpfc_iocbq *) arg;
1592
1593         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1594         return ndlp->nlp_state;
1595 }
1596
1597 static uint32_t
1598 lpfc_rcv_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1599                          void *arg, uint32_t evt)
1600 {
1601         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1602
1603         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1604         return ndlp->nlp_state;
1605 }
1606
1607 static uint32_t
1608 lpfc_rcv_logo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1609                          void *arg, uint32_t evt)
1610 {
1611         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1612
1613         /* Software abort outstanding PRLI before sending acc */
1614         lpfc_els_abort(vport->phba, ndlp);
1615
1616         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1617         return ndlp->nlp_state;
1618 }
1619
1620 static uint32_t
1621 lpfc_rcv_padisc_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1622                            void *arg, uint32_t evt)
1623 {
1624         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1625
1626         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1627         return ndlp->nlp_state;
1628 }
1629
1630 /* This routine is envoked when we rcv a PRLO request from a nport
1631  * we are logged into.  We should send back a PRLO rsp setting the
1632  * appropriate bits.
1633  * NEXT STATE = PRLI_ISSUE
1634  */
1635 static uint32_t
1636 lpfc_rcv_prlo_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1637                          void *arg, uint32_t evt)
1638 {
1639         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1640
1641         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1642         return ndlp->nlp_state;
1643 }
1644
1645 static uint32_t
1646 lpfc_cmpl_prli_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1647                           void *arg, uint32_t evt)
1648 {
1649         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1650         struct lpfc_iocbq *cmdiocb, *rspiocb;
1651         struct lpfc_hba   *phba = vport->phba;
1652         IOCB_t *irsp;
1653         PRLI *npr;
1654
1655         cmdiocb = (struct lpfc_iocbq *) arg;
1656         rspiocb = cmdiocb->context_un.rsp_iocb;
1657         npr = (PRLI *)lpfc_check_elscmpl_iocb(phba, cmdiocb, rspiocb);
1658
1659         irsp = &rspiocb->iocb;
1660         if (irsp->ulpStatus) {
1661                 if ((vport->port_type == LPFC_NPIV_PORT) &&
1662                     vport->cfg_restrict_login) {
1663                         goto out;
1664                 }
1665                 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1666                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1667                 return ndlp->nlp_state;
1668         }
1669
1670         /* Check out PRLI rsp */
1671         ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1672         ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
1673         if ((npr->acceptRspCode == PRLI_REQ_EXECUTED) &&
1674             (npr->prliType == PRLI_FCP_TYPE)) {
1675                 if (npr->initiatorFunc)
1676                         ndlp->nlp_type |= NLP_FCP_INITIATOR;
1677                 if (npr->targetFunc)
1678                         ndlp->nlp_type |= NLP_FCP_TARGET;
1679                 if (npr->Retry)
1680                         ndlp->nlp_fcp_info |= NLP_FCP_2_DEVICE;
1681         }
1682         if (!(ndlp->nlp_type & NLP_FCP_TARGET) &&
1683             (vport->port_type == LPFC_NPIV_PORT) &&
1684              vport->cfg_restrict_login) {
1685 out:
1686                 spin_lock_irq(shost->host_lock);
1687                 ndlp->nlp_flag |= NLP_TARGET_REMOVE;
1688                 spin_unlock_irq(shost->host_lock);
1689                 lpfc_issue_els_logo(vport, ndlp, 0);
1690
1691                 ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1692                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1693                 return ndlp->nlp_state;
1694         }
1695
1696         ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1697         if (ndlp->nlp_type & NLP_FCP_TARGET)
1698                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_MAPPED_NODE);
1699         else
1700                 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1701         return ndlp->nlp_state;
1702 }
1703
1704 /*! lpfc_device_rm_prli_issue
1705  *
1706  * \pre
1707  * \post
1708  * \param   phba
1709  * \param   ndlp
1710  * \param   arg
1711  * \param   evt
1712  * \return  uint32_t
1713  *
1714  * \b Description:
1715  *    This routine is envoked when we a request to remove a nport we are in the
1716  *    process of PRLIing. We should software abort outstanding prli, unreg
1717  *    login, send a logout. We will change node state to UNUSED_NODE, put it
1718  *    on plogi list so it can be freed when LOGO completes.
1719  *
1720  */
1721
1722 static uint32_t
1723 lpfc_device_rm_prli_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1724                           void *arg, uint32_t evt)
1725 {
1726         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1727
1728         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
1729                 spin_lock_irq(shost->host_lock);
1730                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
1731                 spin_unlock_irq(shost->host_lock);
1732                 return ndlp->nlp_state;
1733         } else {
1734                 /* software abort outstanding PLOGI */
1735                 lpfc_els_abort(vport->phba, ndlp);
1736
1737                 lpfc_drop_node(vport, ndlp);
1738                 return NLP_STE_FREED_NODE;
1739         }
1740 }
1741
1742
1743 /*! lpfc_device_recov_prli_issue
1744  *
1745  * \pre
1746  * \post
1747  * \param   phba
1748  * \param   ndlp
1749  * \param   arg
1750  * \param   evt
1751  * \return  uint32_t
1752  *
1753  * \b Description:
1754  *    The routine is envoked when the state of a device is unknown, like
1755  *    during a link down. We should remove the nodelist entry from the
1756  *    unmapped list, issue a UNREG_LOGIN, do a software abort of the
1757  *    outstanding PRLI command, then free the node entry.
1758  */
1759 static uint32_t
1760 lpfc_device_recov_prli_issue(struct lpfc_vport *vport,
1761                              struct lpfc_nodelist *ndlp,
1762                              void *arg,
1763                              uint32_t evt)
1764 {
1765         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1766         struct lpfc_hba  *phba = vport->phba;
1767
1768         /* Don't do anything that will mess up processing of the
1769          * previous RSCN.
1770          */
1771         if (vport->fc_flag & FC_RSCN_DEFERRED)
1772                 return ndlp->nlp_state;
1773
1774         /* software abort outstanding PRLI */
1775         lpfc_els_abort(phba, ndlp);
1776
1777         ndlp->nlp_prev_state = NLP_STE_PRLI_ISSUE;
1778         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1779         spin_lock_irq(shost->host_lock);
1780         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1781         spin_unlock_irq(shost->host_lock);
1782         lpfc_disc_set_adisc(vport, ndlp);
1783         return ndlp->nlp_state;
1784 }
1785
1786 static uint32_t
1787 lpfc_rcv_plogi_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1788                           void *arg, uint32_t evt)
1789 {
1790         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1791         struct ls_rjt     stat;
1792
1793         memset(&stat, 0, sizeof(struct ls_rjt));
1794         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1795         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1796         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1797         return ndlp->nlp_state;
1798 }
1799
1800 static uint32_t
1801 lpfc_rcv_prli_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1802                          void *arg, uint32_t evt)
1803 {
1804         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1805         struct ls_rjt     stat;
1806
1807         memset(&stat, 0, sizeof(struct ls_rjt));
1808         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1809         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1810         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1811         return ndlp->nlp_state;
1812 }
1813
1814 static uint32_t
1815 lpfc_rcv_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1816                          void *arg, uint32_t evt)
1817 {
1818         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1819         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1820
1821         spin_lock_irq(shost->host_lock);
1822         ndlp->nlp_flag &= NLP_LOGO_ACC;
1823         spin_unlock_irq(shost->host_lock);
1824         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
1825         return ndlp->nlp_state;
1826 }
1827
1828 static uint32_t
1829 lpfc_rcv_padisc_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1830                            void *arg, uint32_t evt)
1831 {
1832         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1833         struct ls_rjt     stat;
1834
1835         memset(&stat, 0, sizeof(struct ls_rjt));
1836         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1837         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1838         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1839         return ndlp->nlp_state;
1840 }
1841
1842 static uint32_t
1843 lpfc_rcv_prlo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1844                          void *arg, uint32_t evt)
1845 {
1846         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *)arg;
1847         struct ls_rjt     stat;
1848
1849         memset(&stat, 0, sizeof(struct ls_rjt));
1850         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
1851         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
1852         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
1853         return ndlp->nlp_state;
1854 }
1855
1856 static uint32_t
1857 lpfc_cmpl_logo_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1858                           void *arg, uint32_t evt)
1859 {
1860         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1861
1862         ndlp->nlp_prev_state = NLP_STE_LOGO_ISSUE;
1863         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1864         spin_lock_irq(shost->host_lock);
1865         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1866         spin_unlock_irq(shost->host_lock);
1867         lpfc_disc_set_adisc(vport, ndlp);
1868         return ndlp->nlp_state;
1869 }
1870
1871 static uint32_t
1872 lpfc_device_rm_logo_issue(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1873                           void *arg, uint32_t evt)
1874 {
1875         /*
1876          * Take no action.  If a LOGO is outstanding, then possibly DevLoss has
1877          * timed out and is calling for Device Remove.  In this case, the LOGO
1878          * must be allowed to complete in state LOGO_ISSUE so that the rpi
1879          * and other NLP flags are correctly cleaned up.
1880          */
1881         return ndlp->nlp_state;
1882 }
1883
1884 static uint32_t
1885 lpfc_device_recov_logo_issue(struct lpfc_vport *vport,
1886                              struct lpfc_nodelist *ndlp,
1887                              void *arg, uint32_t evt)
1888 {
1889         /*
1890          * Device Recovery events have no meaning for a node with a LOGO
1891          * outstanding.  The LOGO has to complete first and handle the
1892          * node from that point.
1893          */
1894         return ndlp->nlp_state;
1895 }
1896
1897 static uint32_t
1898 lpfc_rcv_plogi_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1899                           void *arg, uint32_t evt)
1900 {
1901         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1902
1903         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1904         return ndlp->nlp_state;
1905 }
1906
1907 static uint32_t
1908 lpfc_rcv_prli_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1909                          void *arg, uint32_t evt)
1910 {
1911         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1912
1913         lpfc_rcv_prli(vport, ndlp, cmdiocb);
1914         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1915         return ndlp->nlp_state;
1916 }
1917
1918 static uint32_t
1919 lpfc_rcv_logo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1920                          void *arg, uint32_t evt)
1921 {
1922         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1923
1924         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1925         return ndlp->nlp_state;
1926 }
1927
1928 static uint32_t
1929 lpfc_rcv_padisc_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1930                            void *arg, uint32_t evt)
1931 {
1932         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1933
1934         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
1935         return ndlp->nlp_state;
1936 }
1937
1938 static uint32_t
1939 lpfc_rcv_prlo_unmap_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1940                          void *arg, uint32_t evt)
1941 {
1942         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1943
1944         lpfc_els_rsp_acc(vport, ELS_CMD_PRLO, cmdiocb, ndlp, NULL);
1945         return ndlp->nlp_state;
1946 }
1947
1948 static uint32_t
1949 lpfc_device_recov_unmap_node(struct lpfc_vport *vport,
1950                              struct lpfc_nodelist *ndlp,
1951                              void *arg,
1952                              uint32_t evt)
1953 {
1954         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1955
1956         ndlp->nlp_prev_state = NLP_STE_UNMAPPED_NODE;
1957         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1958         spin_lock_irq(shost->host_lock);
1959         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
1960         spin_unlock_irq(shost->host_lock);
1961         lpfc_disc_set_adisc(vport, ndlp);
1962
1963         return ndlp->nlp_state;
1964 }
1965
1966 static uint32_t
1967 lpfc_rcv_plogi_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1968                            void *arg, uint32_t evt)
1969 {
1970         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1971
1972         lpfc_rcv_plogi(vport, ndlp, cmdiocb);
1973         return ndlp->nlp_state;
1974 }
1975
1976 static uint32_t
1977 lpfc_rcv_prli_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1978                           void *arg, uint32_t evt)
1979 {
1980         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1981
1982         lpfc_els_rsp_prli_acc(vport, cmdiocb, ndlp);
1983         return ndlp->nlp_state;
1984 }
1985
1986 static uint32_t
1987 lpfc_rcv_logo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1988                           void *arg, uint32_t evt)
1989 {
1990         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
1991
1992         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
1993         return ndlp->nlp_state;
1994 }
1995
1996 static uint32_t
1997 lpfc_rcv_padisc_mapped_node(struct lpfc_vport *vport,
1998                             struct lpfc_nodelist *ndlp,
1999                             void *arg, uint32_t evt)
2000 {
2001         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2002
2003         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2004         return ndlp->nlp_state;
2005 }
2006
2007 static uint32_t
2008 lpfc_rcv_prlo_mapped_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2009                           void *arg, uint32_t evt)
2010 {
2011         struct lpfc_hba  *phba = vport->phba;
2012         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2013
2014         /* flush the target */
2015         lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
2016                             ndlp->nlp_sid, 0, LPFC_CTX_TGT);
2017
2018         /* Treat like rcv logo */
2019         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_PRLO);
2020         return ndlp->nlp_state;
2021 }
2022
2023 static uint32_t
2024 lpfc_device_recov_mapped_node(struct lpfc_vport *vport,
2025                               struct lpfc_nodelist *ndlp,
2026                               void *arg,
2027                               uint32_t evt)
2028 {
2029         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2030
2031         ndlp->nlp_prev_state = NLP_STE_MAPPED_NODE;
2032         lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2033         spin_lock_irq(shost->host_lock);
2034         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2035         spin_unlock_irq(shost->host_lock);
2036         lpfc_disc_set_adisc(vport, ndlp);
2037         return ndlp->nlp_state;
2038 }
2039
2040 static uint32_t
2041 lpfc_rcv_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2042                         void *arg, uint32_t evt)
2043 {
2044         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2045         struct lpfc_iocbq *cmdiocb  = (struct lpfc_iocbq *) arg;
2046
2047         /* Ignore PLOGI if we have an outstanding LOGO */
2048         if (ndlp->nlp_flag & (NLP_LOGO_SND | NLP_LOGO_ACC))
2049                 return ndlp->nlp_state;
2050         if (lpfc_rcv_plogi(vport, ndlp, cmdiocb)) {
2051                 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2052                 spin_lock_irq(shost->host_lock);
2053                 ndlp->nlp_flag &= ~(NLP_NPR_ADISC | NLP_NPR_2B_DISC);
2054                 spin_unlock_irq(shost->host_lock);
2055         } else if (!(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2056                 /* send PLOGI immediately, move to PLOGI issue state */
2057                 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2058                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2059                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2060                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2061                 }
2062         }
2063         return ndlp->nlp_state;
2064 }
2065
2066 static uint32_t
2067 lpfc_rcv_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2068                        void *arg, uint32_t evt)
2069 {
2070         struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2071         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2072         struct ls_rjt     stat;
2073
2074         memset(&stat, 0, sizeof (struct ls_rjt));
2075         stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
2076         stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
2077         lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
2078
2079         if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2080                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2081                         spin_lock_irq(shost->host_lock);
2082                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2083                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2084                         spin_unlock_irq(shost->host_lock);
2085                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2086                         lpfc_issue_els_adisc(vport, ndlp, 0);
2087                 } else {
2088                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2089                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2090                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2091                 }
2092         }
2093         return ndlp->nlp_state;
2094 }
2095
2096 static uint32_t
2097 lpfc_rcv_logo_npr_node(struct lpfc_vport *vport,  struct lpfc_nodelist *ndlp,
2098                        void *arg, uint32_t evt)
2099 {
2100         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2101
2102         lpfc_rcv_logo(vport, ndlp, cmdiocb, ELS_CMD_LOGO);
2103         return ndlp->nlp_state;
2104 }
2105
2106 static uint32_t
2107 lpfc_rcv_padisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2108                          void *arg, uint32_t evt)
2109 {
2110         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2111
2112         lpfc_rcv_padisc(vport, ndlp, cmdiocb);
2113         /*
2114          * Do not start discovery if discovery is about to start
2115          * or discovery in progress for this node. Starting discovery
2116          * here will affect the counting of discovery threads.
2117          */
2118         if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
2119             !(ndlp->nlp_flag & NLP_NPR_2B_DISC)) {
2120                 if (ndlp->nlp_flag & NLP_NPR_ADISC) {
2121                         ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2122                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2123                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2124                         lpfc_issue_els_adisc(vport, ndlp, 0);
2125                 } else {
2126                         ndlp->nlp_prev_state = NLP_STE_NPR_NODE;
2127                         lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2128                         lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
2129                 }
2130         }
2131         return ndlp->nlp_state;
2132 }
2133
2134 static uint32_t
2135 lpfc_rcv_prlo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2136                        void *arg, uint32_t evt)
2137 {
2138         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2139         struct lpfc_iocbq *cmdiocb = (struct lpfc_iocbq *) arg;
2140
2141         spin_lock_irq(shost->host_lock);
2142         ndlp->nlp_flag |= NLP_LOGO_ACC;
2143         spin_unlock_irq(shost->host_lock);
2144
2145         lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
2146
2147         if ((ndlp->nlp_flag & NLP_DELAY_TMO) == 0) {
2148                 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ * 1);
2149                 spin_lock_irq(shost->host_lock);
2150                 ndlp->nlp_flag |= NLP_DELAY_TMO;
2151                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2152                 spin_unlock_irq(shost->host_lock);
2153                 ndlp->nlp_last_elscmd = ELS_CMD_PLOGI;
2154         } else {
2155                 spin_lock_irq(shost->host_lock);
2156                 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2157                 spin_unlock_irq(shost->host_lock);
2158         }
2159         return ndlp->nlp_state;
2160 }
2161
2162 static uint32_t
2163 lpfc_cmpl_plogi_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2164                          void *arg, uint32_t evt)
2165 {
2166         struct lpfc_iocbq *cmdiocb, *rspiocb;
2167         IOCB_t *irsp;
2168         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2169
2170         cmdiocb = (struct lpfc_iocbq *) arg;
2171         rspiocb = cmdiocb->context_un.rsp_iocb;
2172
2173         irsp = &rspiocb->iocb;
2174         if (irsp->ulpStatus) {
2175                 spin_lock_irq(shost->host_lock);
2176                 ndlp->nlp_flag |= NLP_DEFER_RM;
2177                 spin_unlock_irq(shost->host_lock);
2178                 return NLP_STE_FREED_NODE;
2179         }
2180         return ndlp->nlp_state;
2181 }
2182
2183 static uint32_t
2184 lpfc_cmpl_prli_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2185                         void *arg, uint32_t evt)
2186 {
2187         struct lpfc_iocbq *cmdiocb, *rspiocb;
2188         IOCB_t *irsp;
2189
2190         cmdiocb = (struct lpfc_iocbq *) arg;
2191         rspiocb = cmdiocb->context_un.rsp_iocb;
2192
2193         irsp = &rspiocb->iocb;
2194         if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2195                 lpfc_drop_node(vport, ndlp);
2196                 return NLP_STE_FREED_NODE;
2197         }
2198         return ndlp->nlp_state;
2199 }
2200
2201 static uint32_t
2202 lpfc_cmpl_logo_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2203                         void *arg, uint32_t evt)
2204 {
2205         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2206
2207         /* For the fabric port just clear the fc flags. */
2208         if (ndlp->nlp_DID == Fabric_DID) {
2209                 spin_lock_irq(shost->host_lock);
2210                 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
2211                 spin_unlock_irq(shost->host_lock);
2212         }
2213         lpfc_unreg_rpi(vport, ndlp);
2214         return ndlp->nlp_state;
2215 }
2216
2217 static uint32_t
2218 lpfc_cmpl_adisc_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2219                          void *arg, uint32_t evt)
2220 {
2221         struct lpfc_iocbq *cmdiocb, *rspiocb;
2222         IOCB_t *irsp;
2223
2224         cmdiocb = (struct lpfc_iocbq *) arg;
2225         rspiocb = cmdiocb->context_un.rsp_iocb;
2226
2227         irsp = &rspiocb->iocb;
2228         if (irsp->ulpStatus && (ndlp->nlp_flag & NLP_NODEV_REMOVE)) {
2229                 lpfc_drop_node(vport, ndlp);
2230                 return NLP_STE_FREED_NODE;
2231         }
2232         return ndlp->nlp_state;
2233 }
2234
2235 static uint32_t
2236 lpfc_cmpl_reglogin_npr_node(struct lpfc_vport *vport,
2237                             struct lpfc_nodelist *ndlp,
2238                             void *arg, uint32_t evt)
2239 {
2240         LPFC_MBOXQ_t *pmb = (LPFC_MBOXQ_t *) arg;
2241         MAILBOX_t    *mb = &pmb->u.mb;
2242
2243         if (!mb->mbxStatus) {
2244                 /* SLI4 ports have preallocated logical rpis. */
2245                 if (vport->phba->sli_rev < LPFC_SLI_REV4)
2246                         ndlp->nlp_rpi = mb->un.varWords[0];
2247                 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
2248         } else {
2249                 if (ndlp->nlp_flag & NLP_NODEV_REMOVE) {
2250                         lpfc_drop_node(vport, ndlp);
2251                         return NLP_STE_FREED_NODE;
2252                 }
2253         }
2254         return ndlp->nlp_state;
2255 }
2256
2257 static uint32_t
2258 lpfc_device_rm_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2259                         void *arg, uint32_t evt)
2260 {
2261         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2262
2263         if (ndlp->nlp_flag & NLP_NPR_2B_DISC) {
2264                 spin_lock_irq(shost->host_lock);
2265                 ndlp->nlp_flag |= NLP_NODEV_REMOVE;
2266                 spin_unlock_irq(shost->host_lock);
2267                 return ndlp->nlp_state;
2268         }
2269         lpfc_drop_node(vport, ndlp);
2270         return NLP_STE_FREED_NODE;
2271 }
2272
2273 static uint32_t
2274 lpfc_device_recov_npr_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2275                            void *arg, uint32_t evt)
2276 {
2277         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2278
2279         /* Don't do anything that will mess up processing of the
2280          * previous RSCN.
2281          */
2282         if (vport->fc_flag & FC_RSCN_DEFERRED)
2283                 return ndlp->nlp_state;
2284
2285         lpfc_cancel_retry_delay_tmo(vport, ndlp);
2286         spin_lock_irq(shost->host_lock);
2287         ndlp->nlp_flag &= ~(NLP_NODEV_REMOVE | NLP_NPR_2B_DISC);
2288         spin_unlock_irq(shost->host_lock);
2289         return ndlp->nlp_state;
2290 }
2291
2292
2293 /* This next section defines the NPort Discovery State Machine */
2294
2295 /* There are 4 different double linked lists nodelist entries can reside on.
2296  * The plogi list and adisc list are used when Link Up discovery or RSCN
2297  * processing is needed. Each list holds the nodes that we will send PLOGI
2298  * or ADISC on. These lists will keep track of what nodes will be effected
2299  * by an RSCN, or a Link Up (Typically, all nodes are effected on Link Up).
2300  * The unmapped_list will contain all nodes that we have successfully logged
2301  * into at the Fibre Channel level. The mapped_list will contain all nodes
2302  * that are mapped FCP targets.
2303  */
2304 /*
2305  * The bind list is a list of undiscovered (potentially non-existent) nodes
2306  * that we have saved binding information on. This information is used when
2307  * nodes transition from the unmapped to the mapped list.
2308  */
2309 /* For UNUSED_NODE state, the node has just been allocated .
2310  * For PLOGI_ISSUE and REG_LOGIN_ISSUE, the node is on
2311  * the PLOGI list. For REG_LOGIN_COMPL, the node is taken off the PLOGI list
2312  * and put on the unmapped list. For ADISC processing, the node is taken off
2313  * the ADISC list and placed on either the mapped or unmapped list (depending
2314  * on its previous state). Once on the unmapped list, a PRLI is issued and the
2315  * state changed to PRLI_ISSUE. When the PRLI completion occurs, the state is
2316  * changed to UNMAPPED_NODE. If the completion indicates a mapped
2317  * node, the node is taken off the unmapped list. The binding list is checked
2318  * for a valid binding, or a binding is automatically assigned. If binding
2319  * assignment is unsuccessful, the node is left on the unmapped list. If
2320  * binding assignment is successful, the associated binding list entry (if
2321  * any) is removed, and the node is placed on the mapped list.
2322  */
2323 /*
2324  * For a Link Down, all nodes on the ADISC, PLOGI, unmapped or mapped
2325  * lists will receive a DEVICE_RECOVERY event. If the linkdown or devloss timers
2326  * expire, all effected nodes will receive a DEVICE_RM event.
2327  */
2328 /*
2329  * For a Link Up or RSCN, all nodes will move from the mapped / unmapped lists
2330  * to either the ADISC or PLOGI list.  After a Nameserver query or ALPA loopmap
2331  * check, additional nodes may be added or removed (via DEVICE_RM) to / from
2332  * the PLOGI or ADISC lists. Once the PLOGI and ADISC lists are populated,
2333  * we will first process the ADISC list.  32 entries are processed initially and
2334  * ADISC is initited for each one.  Completions / Events for each node are
2335  * funnelled thru the state machine.  As each node finishes ADISC processing, it
2336  * starts ADISC for any nodes waiting for ADISC processing. If no nodes are
2337  * waiting, and the ADISC list count is identically 0, then we are done. For
2338  * Link Up discovery, since all nodes on the PLOGI list are UNREG_LOGIN'ed, we
2339  * can issue a CLEAR_LA and reenable Link Events. Next we will process the PLOGI
2340  * list.  32 entries are processed initially and PLOGI is initited for each one.
2341  * Completions / Events for each node are funnelled thru the state machine.  As
2342  * each node finishes PLOGI processing, it starts PLOGI for any nodes waiting
2343  * for PLOGI processing. If no nodes are waiting, and the PLOGI list count is
2344  * indentically 0, then we are done. We have now completed discovery / RSCN
2345  * handling. Upon completion, ALL nodes should be on either the mapped or
2346  * unmapped lists.
2347  */
2348
2349 static uint32_t (*lpfc_disc_action[NLP_STE_MAX_STATE * NLP_EVT_MAX_EVENT])
2350      (struct lpfc_vport *, struct lpfc_nodelist *, void *, uint32_t) = {
2351         /* Action routine                  Event       Current State  */
2352         lpfc_rcv_plogi_unused_node,     /* RCV_PLOGI   UNUSED_NODE    */
2353         lpfc_rcv_els_unused_node,       /* RCV_PRLI        */
2354         lpfc_rcv_logo_unused_node,      /* RCV_LOGO        */
2355         lpfc_rcv_els_unused_node,       /* RCV_ADISC       */
2356         lpfc_rcv_els_unused_node,       /* RCV_PDISC       */
2357         lpfc_rcv_els_unused_node,       /* RCV_PRLO        */
2358         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2359         lpfc_disc_illegal,              /* CMPL_PRLI       */
2360         lpfc_cmpl_logo_unused_node,     /* CMPL_LOGO       */
2361         lpfc_disc_illegal,              /* CMPL_ADISC      */
2362         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2363         lpfc_device_rm_unused_node,     /* DEVICE_RM       */
2364         lpfc_device_recov_unused_node,  /* DEVICE_RECOVERY */
2365
2366         lpfc_rcv_plogi_plogi_issue,     /* RCV_PLOGI   PLOGI_ISSUE    */
2367         lpfc_rcv_prli_plogi_issue,      /* RCV_PRLI        */
2368         lpfc_rcv_logo_plogi_issue,      /* RCV_LOGO        */
2369         lpfc_rcv_els_plogi_issue,       /* RCV_ADISC       */
2370         lpfc_rcv_els_plogi_issue,       /* RCV_PDISC       */
2371         lpfc_rcv_els_plogi_issue,       /* RCV_PRLO        */
2372         lpfc_cmpl_plogi_plogi_issue,    /* CMPL_PLOGI      */
2373         lpfc_disc_illegal,              /* CMPL_PRLI       */
2374         lpfc_cmpl_logo_plogi_issue,     /* CMPL_LOGO       */
2375         lpfc_disc_illegal,              /* CMPL_ADISC      */
2376         lpfc_cmpl_reglogin_plogi_issue,/* CMPL_REG_LOGIN  */
2377         lpfc_device_rm_plogi_issue,     /* DEVICE_RM       */
2378         lpfc_device_recov_plogi_issue,  /* DEVICE_RECOVERY */
2379
2380         lpfc_rcv_plogi_adisc_issue,     /* RCV_PLOGI   ADISC_ISSUE    */
2381         lpfc_rcv_prli_adisc_issue,      /* RCV_PRLI        */
2382         lpfc_rcv_logo_adisc_issue,      /* RCV_LOGO        */
2383         lpfc_rcv_padisc_adisc_issue,    /* RCV_ADISC       */
2384         lpfc_rcv_padisc_adisc_issue,    /* RCV_PDISC       */
2385         lpfc_rcv_prlo_adisc_issue,      /* RCV_PRLO        */
2386         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2387         lpfc_disc_illegal,              /* CMPL_PRLI       */
2388         lpfc_disc_illegal,              /* CMPL_LOGO       */
2389         lpfc_cmpl_adisc_adisc_issue,    /* CMPL_ADISC      */
2390         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2391         lpfc_device_rm_adisc_issue,     /* DEVICE_RM       */
2392         lpfc_device_recov_adisc_issue,  /* DEVICE_RECOVERY */
2393
2394         lpfc_rcv_plogi_reglogin_issue,  /* RCV_PLOGI  REG_LOGIN_ISSUE */
2395         lpfc_rcv_prli_reglogin_issue,   /* RCV_PLOGI       */
2396         lpfc_rcv_logo_reglogin_issue,   /* RCV_LOGO        */
2397         lpfc_rcv_padisc_reglogin_issue, /* RCV_ADISC       */
2398         lpfc_rcv_padisc_reglogin_issue, /* RCV_PDISC       */
2399         lpfc_rcv_prlo_reglogin_issue,   /* RCV_PRLO        */
2400         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2401         lpfc_disc_illegal,              /* CMPL_PRLI       */
2402         lpfc_disc_illegal,              /* CMPL_LOGO       */
2403         lpfc_disc_illegal,              /* CMPL_ADISC      */
2404         lpfc_cmpl_reglogin_reglogin_issue,/* CMPL_REG_LOGIN  */
2405         lpfc_device_rm_reglogin_issue,  /* DEVICE_RM       */
2406         lpfc_device_recov_reglogin_issue,/* DEVICE_RECOVERY */
2407
2408         lpfc_rcv_plogi_prli_issue,      /* RCV_PLOGI   PRLI_ISSUE     */
2409         lpfc_rcv_prli_prli_issue,       /* RCV_PRLI        */
2410         lpfc_rcv_logo_prli_issue,       /* RCV_LOGO        */
2411         lpfc_rcv_padisc_prli_issue,     /* RCV_ADISC       */
2412         lpfc_rcv_padisc_prli_issue,     /* RCV_PDISC       */
2413         lpfc_rcv_prlo_prli_issue,       /* RCV_PRLO        */
2414         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2415         lpfc_cmpl_prli_prli_issue,      /* CMPL_PRLI       */
2416         lpfc_disc_illegal,              /* CMPL_LOGO       */
2417         lpfc_disc_illegal,              /* CMPL_ADISC      */
2418         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2419         lpfc_device_rm_prli_issue,      /* DEVICE_RM       */
2420         lpfc_device_recov_prli_issue,   /* DEVICE_RECOVERY */
2421
2422         lpfc_rcv_plogi_logo_issue,      /* RCV_PLOGI   LOGO_ISSUE     */
2423         lpfc_rcv_prli_logo_issue,       /* RCV_PRLI        */
2424         lpfc_rcv_logo_logo_issue,       /* RCV_LOGO        */
2425         lpfc_rcv_padisc_logo_issue,     /* RCV_ADISC       */
2426         lpfc_rcv_padisc_logo_issue,     /* RCV_PDISC       */
2427         lpfc_rcv_prlo_logo_issue,       /* RCV_PRLO        */
2428         lpfc_cmpl_plogi_illegal,        /* CMPL_PLOGI      */
2429         lpfc_disc_illegal,              /* CMPL_PRLI       */
2430         lpfc_cmpl_logo_logo_issue,      /* CMPL_LOGO       */
2431         lpfc_disc_illegal,              /* CMPL_ADISC      */
2432         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2433         lpfc_device_rm_logo_issue,      /* DEVICE_RM       */
2434         lpfc_device_recov_logo_issue,   /* DEVICE_RECOVERY */
2435
2436         lpfc_rcv_plogi_unmap_node,      /* RCV_PLOGI   UNMAPPED_NODE  */
2437         lpfc_rcv_prli_unmap_node,       /* RCV_PRLI        */
2438         lpfc_rcv_logo_unmap_node,       /* RCV_LOGO        */
2439         lpfc_rcv_padisc_unmap_node,     /* RCV_ADISC       */
2440         lpfc_rcv_padisc_unmap_node,     /* RCV_PDISC       */
2441         lpfc_rcv_prlo_unmap_node,       /* RCV_PRLO        */
2442         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2443         lpfc_disc_illegal,              /* CMPL_PRLI       */
2444         lpfc_disc_illegal,              /* CMPL_LOGO       */
2445         lpfc_disc_illegal,              /* CMPL_ADISC      */
2446         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2447         lpfc_disc_illegal,              /* DEVICE_RM       */
2448         lpfc_device_recov_unmap_node,   /* DEVICE_RECOVERY */
2449
2450         lpfc_rcv_plogi_mapped_node,     /* RCV_PLOGI   MAPPED_NODE    */
2451         lpfc_rcv_prli_mapped_node,      /* RCV_PRLI        */
2452         lpfc_rcv_logo_mapped_node,      /* RCV_LOGO        */
2453         lpfc_rcv_padisc_mapped_node,    /* RCV_ADISC       */
2454         lpfc_rcv_padisc_mapped_node,    /* RCV_PDISC       */
2455         lpfc_rcv_prlo_mapped_node,      /* RCV_PRLO        */
2456         lpfc_disc_illegal,              /* CMPL_PLOGI      */
2457         lpfc_disc_illegal,              /* CMPL_PRLI       */
2458         lpfc_disc_illegal,              /* CMPL_LOGO       */
2459         lpfc_disc_illegal,              /* CMPL_ADISC      */
2460         lpfc_disc_illegal,              /* CMPL_REG_LOGIN  */
2461         lpfc_disc_illegal,              /* DEVICE_RM       */
2462         lpfc_device_recov_mapped_node,  /* DEVICE_RECOVERY */
2463
2464         lpfc_rcv_plogi_npr_node,        /* RCV_PLOGI   NPR_NODE    */
2465         lpfc_rcv_prli_npr_node,         /* RCV_PRLI        */
2466         lpfc_rcv_logo_npr_node,         /* RCV_LOGO        */
2467         lpfc_rcv_padisc_npr_node,       /* RCV_ADISC       */
2468         lpfc_rcv_padisc_npr_node,       /* RCV_PDISC       */
2469         lpfc_rcv_prlo_npr_node,         /* RCV_PRLO        */
2470         lpfc_cmpl_plogi_npr_node,       /* CMPL_PLOGI      */
2471         lpfc_cmpl_prli_npr_node,        /* CMPL_PRLI       */
2472         lpfc_cmpl_logo_npr_node,        /* CMPL_LOGO       */
2473         lpfc_cmpl_adisc_npr_node,       /* CMPL_ADISC      */
2474         lpfc_cmpl_reglogin_npr_node,    /* CMPL_REG_LOGIN  */
2475         lpfc_device_rm_npr_node,        /* DEVICE_RM       */
2476         lpfc_device_recov_npr_node,     /* DEVICE_RECOVERY */
2477 };
2478
2479 int
2480 lpfc_disc_state_machine(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2481                         void *arg, uint32_t evt)
2482 {
2483         uint32_t cur_state, rc;
2484         uint32_t(*func) (struct lpfc_vport *, struct lpfc_nodelist *, void *,
2485                          uint32_t);
2486         uint32_t got_ndlp = 0;
2487
2488         if (lpfc_nlp_get(ndlp))
2489                 got_ndlp = 1;
2490
2491         cur_state = ndlp->nlp_state;
2492
2493         /* DSM in event <evt> on NPort <nlp_DID> in state <cur_state> */
2494         lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2495                          "0211 DSM in event x%x on NPort x%x in "
2496                          "state %d Data: x%x\n",
2497                          evt, ndlp->nlp_DID, cur_state, ndlp->nlp_flag);
2498
2499         lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2500                  "DSM in:          evt:%d ste:%d did:x%x",
2501                 evt, cur_state, ndlp->nlp_DID);
2502
2503         func = lpfc_disc_action[(cur_state * NLP_EVT_MAX_EVENT) + evt];
2504         rc = (func) (vport, ndlp, arg, evt);
2505
2506         /* DSM out state <rc> on NPort <nlp_DID> */
2507         if (got_ndlp) {
2508                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2509                          "0212 DSM out state %d on NPort x%x Data: x%x\n",
2510                          rc, ndlp->nlp_DID, ndlp->nlp_flag);
2511
2512                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2513                         "DSM out:         ste:%d did:x%x flg:x%x",
2514                         rc, ndlp->nlp_DID, ndlp->nlp_flag);
2515                 /* Decrement the ndlp reference count held for this function */
2516                 lpfc_nlp_put(ndlp);
2517         } else {
2518                 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2519                         "0213 DSM out state %d on NPort free\n", rc);
2520
2521                 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_DSM,
2522                         "DSM out:         ste:%d did:x%x flg:x%x",
2523                         rc, 0, 0);
2524         }
2525
2526         return rc;
2527 }