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