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