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