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