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