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