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