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