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