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