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