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