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