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