]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/scsi/lpfc/lpfc_els.c
scsi: lpfc: rport port swap discovery issue.
[thirdparty/kernel/stable.git] / drivers / scsi / lpfc / lpfc_els.c
CommitLineData
dea3101e
JB
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
128bddac 4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
4ae2ebde 5 * “Broadcom” refers to Broadcom Inc. 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 22 *******************************************************************/
09372820 23/* See Fibre Channel protocol T11 FC-LS for details */
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>
33
da0436e9 34#include "lpfc_hw4.h"
dea3101e
JB
35#include "lpfc_hw.h"
36#include "lpfc_sli.h"
da0436e9 37#include "lpfc_sli4.h"
ea2151b4 38#include "lpfc_nl.h"
dea3101e
JB
39#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
92d7f7b0 44#include "lpfc_vport.h"
858c9f6c 45#include "lpfc_debugfs.h"
dea3101e
JB
46
47static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
48 struct lpfc_iocbq *);
92d7f7b0
JS
49static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
50 struct lpfc_iocbq *);
a6ababd2
AB
51static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
52static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
53 struct lpfc_nodelist *ndlp, uint8_t retry);
54static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
55 struct lpfc_iocbq *iocb);
92d7f7b0 56
dea3101e
JB
57static int lpfc_max_els_tries = 3;
58
e59058c4 59/**
3621a710 60 * lpfc_els_chk_latt - Check host link attention event for a vport
e59058c4
JS
61 * @vport: pointer to a host virtual N_Port data structure.
62 *
63 * This routine checks whether there is an outstanding host link
64 * attention event during the discovery process with the @vport. It is done
65 * by reading the HBA's Host Attention (HA) register. If there is any host
66 * link attention events during this @vport's discovery process, the @vport
67 * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
68 * be issued if the link state is not already in host link cleared state,
69 * and a return code shall indicate whether the host link attention event
70 * had happened.
71 *
72 * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
73 * state in LPFC_VPORT_READY, the request for checking host link attention
74 * event will be ignored and a return code shall indicate no host link
75 * attention event had happened.
76 *
77 * Return codes
78 * 0 - no host link attention event happened
79 * 1 - host link attention event happened
80 **/
858c9f6c 81int
2e0fef85 82lpfc_els_chk_latt(struct lpfc_vport *vport)
dea3101e 83{
2e0fef85
JS
84 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
85 struct lpfc_hba *phba = vport->phba;
dea3101e 86 uint32_t ha_copy;
dea3101e 87
2e0fef85 88 if (vport->port_state >= LPFC_VPORT_READY ||
3772a991
JS
89 phba->link_state == LPFC_LINK_DOWN ||
90 phba->sli_rev > LPFC_SLI_REV3)
dea3101e
JB
91 return 0;
92
93 /* Read the HBA Host Attention Register */
9940b97b
JS
94 if (lpfc_readl(phba->HAregaddr, &ha_copy))
95 return 1;
dea3101e
JB
96
97 if (!(ha_copy & HA_LATT))
98 return 0;
99
100 /* Pending Link Event during Discovery */
e8b62011
JS
101 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
102 "0237 Pending Link Event during "
103 "Discovery: State x%x\n",
104 phba->pport->port_state);
dea3101e
JB
105
106 /* CLEAR_LA should re-enable link attention events and
25985edc 107 * we should then immediately take a LATT event. The
dea3101e
JB
108 * LATT processing should call lpfc_linkdown() which
109 * will cleanup any left over in-progress discovery
110 * events.
111 */
2e0fef85
JS
112 spin_lock_irq(shost->host_lock);
113 vport->fc_flag |= FC_ABORT_DISCOVERY;
114 spin_unlock_irq(shost->host_lock);
dea3101e 115
92d7f7b0 116 if (phba->link_state != LPFC_CLEAR_LA)
ed957684 117 lpfc_issue_clear_la(phba, vport);
dea3101e 118
c9f8735b 119 return 1;
dea3101e
JB
120}
121
e59058c4 122/**
3621a710 123 * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
e59058c4
JS
124 * @vport: pointer to a host virtual N_Port data structure.
125 * @expectRsp: flag indicating whether response is expected.
126 * @cmdSize: size of the ELS command.
127 * @retry: number of retries to the command IOCB when it fails.
128 * @ndlp: pointer to a node-list data structure.
129 * @did: destination identifier.
130 * @elscmd: the ELS command code.
131 *
132 * This routine is used for allocating a lpfc-IOCB data structure from
133 * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
134 * passed into the routine for discovery state machine to issue an Extended
135 * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
136 * and preparation routine that is used by all the discovery state machine
137 * routines and the ELS command-specific fields will be later set up by
138 * the individual discovery machine routines after calling this routine
139 * allocating and preparing a generic IOCB data structure. It fills in the
140 * Buffer Descriptor Entries (BDEs), allocates buffers for both command
141 * payload and response payload (if expected). The reference count on the
142 * ndlp is incremented by 1 and the reference to the ndlp is put into
143 * context1 of the IOCB data structure for this IOCB to hold the ndlp
144 * reference for the command's callback function to access later.
145 *
146 * Return code
147 * Pointer to the newly allocated/prepared els iocb data structure
148 * NULL - when els iocb data structure allocation/preparation failed
149 **/
f1c3b0fc 150struct lpfc_iocbq *
2e0fef85
JS
151lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
152 uint16_t cmdSize, uint8_t retry,
153 struct lpfc_nodelist *ndlp, uint32_t did,
154 uint32_t elscmd)
dea3101e 155{
2e0fef85 156 struct lpfc_hba *phba = vport->phba;
0bd4ca25 157 struct lpfc_iocbq *elsiocb;
dea3101e
JB
158 struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
159 struct ulp_bde64 *bpl;
160 IOCB_t *icmd;
161
dea3101e 162
2e0fef85
JS
163 if (!lpfc_is_link_up(phba))
164 return NULL;
dea3101e 165
dea3101e 166 /* Allocate buffer for command iocb */
0bd4ca25 167 elsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
168
169 if (elsiocb == NULL)
170 return NULL;
e47c9093 171
0c287589
JS
172 /*
173 * If this command is for fabric controller and HBA running
174 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
175 */
176 if ((did == Fabric_DID) &&
45ed1190 177 (phba->hba_flag & HBA_FIP_SUPPORT) &&
0c287589
JS
178 ((elscmd == ELS_CMD_FLOGI) ||
179 (elscmd == ELS_CMD_FDISC) ||
180 (elscmd == ELS_CMD_LOGO)))
c868595d
JS
181 switch (elscmd) {
182 case ELS_CMD_FLOGI:
f0d9bccc
JS
183 elsiocb->iocb_flag |=
184 ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
c868595d
JS
185 & LPFC_FIP_ELS_ID_MASK);
186 break;
187 case ELS_CMD_FDISC:
f0d9bccc
JS
188 elsiocb->iocb_flag |=
189 ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
c868595d
JS
190 & LPFC_FIP_ELS_ID_MASK);
191 break;
192 case ELS_CMD_LOGO:
f0d9bccc
JS
193 elsiocb->iocb_flag |=
194 ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
c868595d
JS
195 & LPFC_FIP_ELS_ID_MASK);
196 break;
197 }
0c287589 198 else
c868595d 199 elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
0c287589 200
dea3101e
JB
201 icmd = &elsiocb->iocb;
202
203 /* fill in BDEs for command */
204 /* Allocate buffer for command payload */
98c9ea5c
JS
205 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
206 if (pcmd)
207 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
fa4066b6
JS
208 if (!pcmd || !pcmd->virt)
209 goto els_iocb_free_pcmb_exit;
dea3101e
JB
210
211 INIT_LIST_HEAD(&pcmd->list);
212
213 /* Allocate buffer for response payload */
214 if (expectRsp) {
92d7f7b0 215 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
dea3101e
JB
216 if (prsp)
217 prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
218 &prsp->phys);
fa4066b6
JS
219 if (!prsp || !prsp->virt)
220 goto els_iocb_free_prsp_exit;
dea3101e 221 INIT_LIST_HEAD(&prsp->list);
e47c9093 222 } else
dea3101e 223 prsp = NULL;
dea3101e
JB
224
225 /* Allocate buffer for Buffer ptr list */
92d7f7b0 226 pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
dea3101e 227 if (pbuflist)
ed957684
JS
228 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
229 &pbuflist->phys);
fa4066b6
JS
230 if (!pbuflist || !pbuflist->virt)
231 goto els_iocb_free_pbuf_exit;
dea3101e
JB
232
233 INIT_LIST_HEAD(&pbuflist->list);
234
dea3101e 235 if (expectRsp) {
939723a4
JS
236 icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
237 icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
238 icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
92d7f7b0 239 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
939723a4
JS
240
241 icmd->un.elsreq64.remoteID = did; /* DID */
dea3101e 242 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
88f43a08
JS
243 if (elscmd == ELS_CMD_FLOGI)
244 icmd->ulpTimeout = FF_DEF_RATOV * 2;
30e196ca
JS
245 else if (elscmd == ELS_CMD_LOGO)
246 icmd->ulpTimeout = phba->fc_ratov;
88f43a08
JS
247 else
248 icmd->ulpTimeout = phba->fc_ratov * 2;
dea3101e 249 } else {
939723a4
JS
250 icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
251 icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
252 icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
253 icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
254 icmd->un.xseq64.xmit_els_remoteID = did; /* DID */
dea3101e
JB
255 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
256 }
dea3101e
JB
257 icmd->ulpBdeCount = 1;
258 icmd->ulpLe = 1;
259 icmd->ulpClass = CLASS3;
260
939723a4
JS
261 /*
262 * If we have NPIV enabled, we want to send ELS traffic by VPI.
263 * For SLI4, since the driver controls VPIs we also want to include
264 * all ELS pt2pt protocol traffic as well.
265 */
266 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
267 ((phba->sli_rev == LPFC_SLI_REV4) &&
268 (vport->fc_flag & FC_PT2PT))) {
269
270 if (expectRsp) {
271 icmd->un.elsreq64.myID = vport->fc_myDID;
272
273 /* For ELS_REQUEST64_CR, use the VPI by default */
274 icmd->ulpContext = phba->vpi_ids[vport->vpi];
275 }
92d7f7b0 276
92d7f7b0 277 icmd->ulpCt_h = 0;
eada272d
JS
278 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
279 if (elscmd == ELS_CMD_ECHO)
280 icmd->ulpCt_l = 0; /* context = invalid RPI */
281 else
282 icmd->ulpCt_l = 1; /* context = VPI */
92d7f7b0
JS
283 }
284
dea3101e
JB
285 bpl = (struct ulp_bde64 *) pbuflist->virt;
286 bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
287 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
288 bpl->tus.f.bdeSize = cmdSize;
289 bpl->tus.f.bdeFlags = 0;
290 bpl->tus.w = le32_to_cpu(bpl->tus.w);
291
292 if (expectRsp) {
293 bpl++;
294 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
295 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
296 bpl->tus.f.bdeSize = FCELSSIZE;
34b02dcd 297 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
dea3101e
JB
298 bpl->tus.w = le32_to_cpu(bpl->tus.w);
299 }
300
fa4066b6 301 /* prevent preparing iocb with NULL ndlp reference */
51ef4c26 302 elsiocb->context1 = lpfc_nlp_get(ndlp);
fa4066b6
JS
303 if (!elsiocb->context1)
304 goto els_iocb_free_pbuf_exit;
329f9bc7
JS
305 elsiocb->context2 = pcmd;
306 elsiocb->context3 = pbuflist;
dea3101e 307 elsiocb->retry = retry;
2e0fef85 308 elsiocb->vport = vport;
dea3101e
JB
309 elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
310
311 if (prsp) {
312 list_add(&prsp->list, &pcmd->list);
313 }
dea3101e
JB
314 if (expectRsp) {
315 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
e8b62011
JS
316 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
317 "0116 Xmit ELS command x%x to remote "
dea16bda
JS
318 "NPORT x%x I/O tag: x%x, port state:x%x "
319 "rpi x%x fc_flag:x%x\n",
e8b62011 320 elscmd, did, elsiocb->iotag,
dea16bda 321 vport->port_state, ndlp->nlp_rpi,
e74c03c8 322 vport->fc_flag);
dea3101e
JB
323 } else {
324 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
e8b62011
JS
325 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
326 "0117 Xmit ELS response x%x to remote "
e74c03c8 327 "NPORT x%x I/O tag: x%x, size: x%x "
dea16bda 328 "port_state x%x rpi x%x fc_flag x%x\n",
e8b62011 329 elscmd, ndlp->nlp_DID, elsiocb->iotag,
e74c03c8 330 cmdSize, vport->port_state,
dea16bda 331 ndlp->nlp_rpi, vport->fc_flag);
dea3101e 332 }
c9f8735b 333 return elsiocb;
dea3101e 334
fa4066b6 335els_iocb_free_pbuf_exit:
eaf15d5b
JS
336 if (expectRsp)
337 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
fa4066b6
JS
338 kfree(pbuflist);
339
340els_iocb_free_prsp_exit:
341 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
342 kfree(prsp);
343
344els_iocb_free_pcmb_exit:
345 kfree(pcmd);
346 lpfc_sli_release_iocbq(phba, elsiocb);
347 return NULL;
348}
dea3101e 349
e59058c4 350/**
3621a710 351 * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
e59058c4
JS
352 * @vport: pointer to a host virtual N_Port data structure.
353 *
354 * This routine issues a fabric registration login for a @vport. An
355 * active ndlp node with Fabric_DID must already exist for this @vport.
356 * The routine invokes two mailbox commands to carry out fabric registration
357 * login through the HBA firmware: the first mailbox command requests the
358 * HBA to perform link configuration for the @vport; and the second mailbox
359 * command requests the HBA to perform the actual fabric registration login
360 * with the @vport.
361 *
362 * Return code
363 * 0 - successfully issued fabric registration login for @vport
364 * -ENXIO -- failed to issue fabric registration login for @vport
365 **/
3772a991 366int
92d7f7b0 367lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
dea3101e 368{
2e0fef85 369 struct lpfc_hba *phba = vport->phba;
dea3101e 370 LPFC_MBOXQ_t *mbox;
14691150 371 struct lpfc_dmabuf *mp;
92d7f7b0
JS
372 struct lpfc_nodelist *ndlp;
373 struct serv_parm *sp;
dea3101e 374 int rc;
98c9ea5c 375 int err = 0;
dea3101e 376
92d7f7b0
JS
377 sp = &phba->fc_fabparam;
378 ndlp = lpfc_findnode_did(vport, Fabric_DID);
e47c9093 379 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
98c9ea5c 380 err = 1;
92d7f7b0 381 goto fail;
98c9ea5c 382 }
92d7f7b0
JS
383
384 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
98c9ea5c
JS
385 if (!mbox) {
386 err = 2;
92d7f7b0 387 goto fail;
98c9ea5c 388 }
92d7f7b0
JS
389
390 vport->port_state = LPFC_FABRIC_CFG_LINK;
391 lpfc_config_link(phba, mbox);
392 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
393 mbox->vport = vport;
394
0b727fea 395 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
98c9ea5c
JS
396 if (rc == MBX_NOT_FINISHED) {
397 err = 3;
92d7f7b0 398 goto fail_free_mbox;
98c9ea5c 399 }
92d7f7b0
JS
400
401 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
98c9ea5c
JS
402 if (!mbox) {
403 err = 4;
92d7f7b0 404 goto fail;
98c9ea5c 405 }
4042629e
JS
406 rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
407 ndlp->nlp_rpi);
98c9ea5c
JS
408 if (rc) {
409 err = 5;
92d7f7b0 410 goto fail_free_mbox;
98c9ea5c 411 }
92d7f7b0
JS
412
413 mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
414 mbox->vport = vport;
e47c9093
JS
415 /* increment the reference count on ndlp to hold reference
416 * for the callback routine.
417 */
3e1f0718 418 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
92d7f7b0 419
0b727fea 420 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
98c9ea5c
JS
421 if (rc == MBX_NOT_FINISHED) {
422 err = 6;
92d7f7b0 423 goto fail_issue_reg_login;
98c9ea5c 424 }
92d7f7b0
JS
425
426 return 0;
427
428fail_issue_reg_login:
e47c9093
JS
429 /* decrement the reference count on ndlp just incremented
430 * for the failed mbox command.
431 */
92d7f7b0 432 lpfc_nlp_put(ndlp);
3e1f0718 433 mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
92d7f7b0
JS
434 lpfc_mbuf_free(phba, mp->virt, mp->phys);
435 kfree(mp);
436fail_free_mbox:
437 mempool_free(mbox, phba->mbox_mem_pool);
438
439fail:
440 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011 441 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
98c9ea5c 442 "0249 Cannot issue Register Fabric login: Err %d\n", err);
92d7f7b0
JS
443 return -ENXIO;
444}
445
6fb120a7
JS
446/**
447 * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
448 * @vport: pointer to a host virtual N_Port data structure.
449 *
450 * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
1b51197d 451 * the @vport. This mailbox command is necessary for SLI4 port only.
6fb120a7
JS
452 *
453 * Return code
454 * 0 - successfully issued REG_VFI for @vport
455 * A failure code otherwise.
456 **/
1b51197d 457int
6fb120a7
JS
458lpfc_issue_reg_vfi(struct lpfc_vport *vport)
459{
460 struct lpfc_hba *phba = vport->phba;
d6de08cc 461 LPFC_MBOXQ_t *mboxq = NULL;
6fb120a7 462 struct lpfc_nodelist *ndlp;
d6de08cc 463 struct lpfc_dmabuf *dmabuf = NULL;
6fb120a7
JS
464 int rc = 0;
465
939723a4 466 /* move forward in case of SLI4 FC port loopback test and pt2pt mode */
1b51197d 467 if ((phba->sli_rev == LPFC_SLI_REV4) &&
939723a4
JS
468 !(phba->link_flag & LS_LOOPBACK_MODE) &&
469 !(vport->fc_flag & FC_PT2PT)) {
1b51197d
JS
470 ndlp = lpfc_findnode_did(vport, Fabric_DID);
471 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
472 rc = -ENODEV;
473 goto fail;
474 }
6fb120a7
JS
475 }
476
d6de08cc
JS
477 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
478 if (!mboxq) {
6fb120a7
JS
479 rc = -ENOMEM;
480 goto fail;
481 }
6d368e53 482
d6de08cc
JS
483 /* Supply CSP's only if we are fabric connect or pt-to-pt connect */
484 if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
485 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
486 if (!dmabuf) {
487 rc = -ENOMEM;
488 goto fail;
489 }
490 dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
491 if (!dmabuf->virt) {
492 rc = -ENOMEM;
493 goto fail;
494 }
495 memcpy(dmabuf->virt, &phba->fc_fabparam,
496 sizeof(struct serv_parm));
6fb120a7 497 }
d6de08cc 498
6fb120a7 499 vport->port_state = LPFC_FABRIC_CFG_LINK;
d6de08cc
JS
500 if (dmabuf)
501 lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
502 else
503 lpfc_reg_vfi(mboxq, vport, 0);
ae05ebe3 504
6fb120a7
JS
505 mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
506 mboxq->vport = vport;
3e1f0718 507 mboxq->ctx_buf = dmabuf;
6fb120a7
JS
508 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
509 if (rc == MBX_NOT_FINISHED) {
510 rc = -ENXIO;
d6de08cc 511 goto fail;
6fb120a7
JS
512 }
513 return 0;
514
6fb120a7 515fail:
d6de08cc
JS
516 if (mboxq)
517 mempool_free(mboxq, phba->mbox_mem_pool);
518 if (dmabuf) {
519 if (dmabuf->virt)
520 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
521 kfree(dmabuf);
522 }
523
6fb120a7
JS
524 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
525 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
526 "0289 Issue Register VFI failed: Err %d\n", rc);
527 return rc;
528}
529
1b51197d
JS
530/**
531 * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
532 * @vport: pointer to a host virtual N_Port data structure.
533 *
534 * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
535 * the @vport. This mailbox command is necessary for SLI4 port only.
536 *
537 * Return code
538 * 0 - successfully issued REG_VFI for @vport
539 * A failure code otherwise.
540 **/
541int
542lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
543{
544 struct lpfc_hba *phba = vport->phba;
545 struct Scsi_Host *shost;
546 LPFC_MBOXQ_t *mboxq;
547 int rc;
548
549 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
550 if (!mboxq) {
551 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
552 "2556 UNREG_VFI mbox allocation failed"
553 "HBA state x%x\n", phba->pport->port_state);
554 return -ENOMEM;
555 }
556
557 lpfc_unreg_vfi(mboxq, vport);
558 mboxq->vport = vport;
559 mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
560
561 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
562 if (rc == MBX_NOT_FINISHED) {
563 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
564 "2557 UNREG_VFI issue mbox failed rc x%x "
565 "HBA state x%x\n",
566 rc, phba->pport->port_state);
567 mempool_free(mboxq, phba->mbox_mem_pool);
568 return -EIO;
569 }
570
571 shost = lpfc_shost_from_vport(vport);
572 spin_lock_irq(shost->host_lock);
573 vport->fc_flag &= ~FC_VFI_REGISTERED;
574 spin_unlock_irq(shost->host_lock);
575 return 0;
576}
577
92494144
JS
578/**
579 * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
580 * @vport: pointer to a host virtual N_Port data structure.
581 * @sp: pointer to service parameter data structure.
582 *
583 * This routine is called from FLOGI/FDISC completion handler functions.
584 * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
585 * node nodename is changed in the completion service parameter else return
586 * 0. This function also set flag in the vport data structure to delay
587 * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
588 * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
589 * node nodename is changed in the completion service parameter.
590 *
591 * Return code
592 * 0 - FCID and Fabric Nodename and Fabric portname is not changed.
593 * 1 - FCID or Fabric Nodename or Fabric portname is changed.
594 *
595 **/
596static uint8_t
597lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
598 struct serv_parm *sp)
599{
8eb8b960 600 struct lpfc_hba *phba = vport->phba;
92494144
JS
601 uint8_t fabric_param_changed = 0;
602 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
603
604 if ((vport->fc_prevDID != vport->fc_myDID) ||
605 memcmp(&vport->fabric_portname, &sp->portName,
606 sizeof(struct lpfc_name)) ||
607 memcmp(&vport->fabric_nodename, &sp->nodeName,
aeb3c817
JS
608 sizeof(struct lpfc_name)) ||
609 (vport->vport_flag & FAWWPN_PARAM_CHG)) {
92494144 610 fabric_param_changed = 1;
aeb3c817
JS
611 vport->vport_flag &= ~FAWWPN_PARAM_CHG;
612 }
92494144
JS
613 /*
614 * Word 1 Bit 31 in common service parameter is overloaded.
615 * Word 1 Bit 31 in FLOGI request is multiple NPort request
616 * Word 1 Bit 31 in FLOGI response is clean address bit
617 *
618 * If fabric parameter is changed and clean address bit is
619 * cleared delay nport discovery if
620 * - vport->fc_prevDID != 0 (not initial discovery) OR
621 * - lpfc_delay_discovery module parameter is set.
622 */
623 if (fabric_param_changed && !sp->cmn.clean_address_bit &&
8eb8b960 624 (vport->fc_prevDID || phba->cfg_delay_discovery)) {
92494144
JS
625 spin_lock_irq(shost->host_lock);
626 vport->fc_flag |= FC_DISC_DELAYED;
627 spin_unlock_irq(shost->host_lock);
628 }
629
630 return fabric_param_changed;
631}
632
633
e59058c4 634/**
3621a710 635 * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
e59058c4
JS
636 * @vport: pointer to a host virtual N_Port data structure.
637 * @ndlp: pointer to a node-list data structure.
638 * @sp: pointer to service parameter data structure.
639 * @irsp: pointer to the IOCB within the lpfc response IOCB.
640 *
641 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
642 * function to handle the completion of a Fabric Login (FLOGI) into a fabric
643 * port in a fabric topology. It properly sets up the parameters to the @ndlp
644 * from the IOCB response. It also check the newly assigned N_Port ID to the
645 * @vport against the previously assigned N_Port ID. If it is different from
646 * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
647 * is invoked on all the remaining nodes with the @vport to unregister the
648 * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
649 * is invoked to register login to the fabric.
650 *
651 * Return code
652 * 0 - Success (currently, always return 0)
653 **/
92d7f7b0
JS
654static int
655lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
656 struct serv_parm *sp, IOCB_t *irsp)
657{
658 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
659 struct lpfc_hba *phba = vport->phba;
660 struct lpfc_nodelist *np;
661 struct lpfc_nodelist *next_np;
92494144 662 uint8_t fabric_param_changed;
92d7f7b0 663
2e0fef85
JS
664 spin_lock_irq(shost->host_lock);
665 vport->fc_flag |= FC_FABRIC;
666 spin_unlock_irq(shost->host_lock);
dea3101e
JB
667
668 phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
669 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
670 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
671
12265f68 672 phba->fc_edtovResol = sp->cmn.edtovResolution;
dea3101e
JB
673 phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
674
76a95d75 675 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
2e0fef85
JS
676 spin_lock_irq(shost->host_lock);
677 vport->fc_flag |= FC_PUBLIC_LOOP;
678 spin_unlock_irq(shost->host_lock);
dea3101e
JB
679 }
680
2e0fef85 681 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
dea3101e 682 memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
92d7f7b0 683 memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
dea3101e
JB
684 ndlp->nlp_class_sup = 0;
685 if (sp->cls1.classValid)
686 ndlp->nlp_class_sup |= FC_COS_CLASS1;
687 if (sp->cls2.classValid)
688 ndlp->nlp_class_sup |= FC_COS_CLASS2;
689 if (sp->cls3.classValid)
690 ndlp->nlp_class_sup |= FC_COS_CLASS3;
691 if (sp->cls4.classValid)
692 ndlp->nlp_class_sup |= FC_COS_CLASS4;
693 ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
694 sp->cmn.bbRcvSizeLsb;
92494144
JS
695
696 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
4258e98e
JS
697 if (fabric_param_changed) {
698 /* Reset FDMI attribute masks based on config parameter */
8663cbbe
JS
699 if (phba->cfg_enable_SmartSAN ||
700 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
4258e98e
JS
701 /* Setup appropriate attribute masks */
702 vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
8663cbbe 703 if (phba->cfg_enable_SmartSAN)
4258e98e
JS
704 vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
705 else
706 vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
8663cbbe
JS
707 } else {
708 vport->fdmi_hba_mask = 0;
709 vport->fdmi_port_mask = 0;
4258e98e
JS
710 }
711
712 }
92494144
JS
713 memcpy(&vport->fabric_portname, &sp->portName,
714 sizeof(struct lpfc_name));
715 memcpy(&vport->fabric_nodename, &sp->nodeName,
716 sizeof(struct lpfc_name));
dea3101e
JB
717 memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
718
92d7f7b0
JS
719 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
720 if (sp->cmn.response_multiple_NPort) {
e8b62011
JS
721 lpfc_printf_vlog(vport, KERN_WARNING,
722 LOG_ELS | LOG_VPORT,
723 "1816 FLOGI NPIV supported, "
724 "response data 0x%x\n",
725 sp->cmn.response_multiple_NPort);
1b51197d 726 spin_lock_irq(&phba->hbalock);
92d7f7b0 727 phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
1b51197d 728 spin_unlock_irq(&phba->hbalock);
92d7f7b0
JS
729 } else {
730 /* Because we asked f/w for NPIV it still expects us
e8b62011
JS
731 to call reg_vnpid atleast for the physcial host */
732 lpfc_printf_vlog(vport, KERN_WARNING,
733 LOG_ELS | LOG_VPORT,
734 "1817 Fabric does not support NPIV "
735 "- configuring single port mode.\n");
1b51197d 736 spin_lock_irq(&phba->hbalock);
92d7f7b0 737 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
1b51197d 738 spin_unlock_irq(&phba->hbalock);
92d7f7b0
JS
739 }
740 }
dea3101e 741
ae05ebe3
JS
742 /*
743 * For FC we need to do some special processing because of the SLI
744 * Port's default settings of the Common Service Parameters.
745 */
d6de08cc
JS
746 if ((phba->sli_rev == LPFC_SLI_REV4) &&
747 (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
ae05ebe3 748 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
d6de08cc 749 if (fabric_param_changed)
ae05ebe3
JS
750 lpfc_unregister_fcf_prep(phba);
751
752 /* This should just update the VFI CSPs*/
753 if (vport->fc_flag & FC_VFI_REGISTERED)
754 lpfc_issue_reg_vfi(vport);
755 }
756
92494144 757 if (fabric_param_changed &&
92d7f7b0 758 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
dea3101e 759
92d7f7b0
JS
760 /* If our NportID changed, we need to ensure all
761 * remaining NPORTs get unreg_login'ed.
762 */
763 list_for_each_entry_safe(np, next_np,
764 &vport->fc_nodes, nlp_listp) {
d7c255b2 765 if (!NLP_CHK_NODE_ACT(np))
e47c9093 766 continue;
92d7f7b0
JS
767 if ((np->nlp_state != NLP_STE_NPR_NODE) ||
768 !(np->nlp_flag & NLP_NPR_ADISC))
769 continue;
770 spin_lock_irq(shost->host_lock);
771 np->nlp_flag &= ~NLP_NPR_ADISC;
772 spin_unlock_irq(shost->host_lock);
773 lpfc_unreg_rpi(vport, np);
774 }
78730cfe 775 lpfc_cleanup_pending_mbox(vport);
5af5eee7 776
5248a749 777 if (phba->sli_rev == LPFC_SLI_REV4) {
5af5eee7 778 lpfc_sli4_unreg_all_rpis(vport);
92d7f7b0 779 lpfc_mbx_unreg_vpi(vport);
09372820 780 spin_lock_irq(shost->host_lock);
ecfd03c6
JS
781 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
782 spin_unlock_irq(shost->host_lock);
783 }
27aa1b73
JS
784
785 /*
786 * For SLI3 and SLI4, the VPI needs to be reregistered in
787 * response to this fabric parameter change event.
788 */
789 spin_lock_irq(shost->host_lock);
790 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
791 spin_unlock_irq(shost->host_lock);
38b92ef8
JS
792 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
793 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
794 /*
795 * Driver needs to re-reg VPI in order for f/w
796 * to update the MAC address.
797 */
9589b062 798 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
38b92ef8
JS
799 lpfc_register_new_vport(phba, vport, ndlp);
800 return 0;
92d7f7b0 801 }
dea3101e 802
6fb120a7
JS
803 if (phba->sli_rev < LPFC_SLI_REV4) {
804 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
805 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
806 vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
807 lpfc_register_new_vport(phba, vport, ndlp);
808 else
809 lpfc_issue_fabric_reglogin(vport);
810 } else {
811 ndlp->nlp_type |= NLP_FABRIC;
812 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
695a814e
JS
813 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
814 (vport->vpi_state & LPFC_VPI_REGISTERED)) {
6fb120a7
JS
815 lpfc_start_fdiscs(phba);
816 lpfc_do_scr_ns_plogi(phba, vport);
695a814e 817 } else if (vport->fc_flag & FC_VFI_REGISTERED)
ecfd03c6 818 lpfc_issue_init_vpi(vport);
1b51197d
JS
819 else {
820 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
821 "3135 Need register VFI: (x%x/%x)\n",
822 vport->fc_prevDID, vport->fc_myDID);
6fb120a7 823 lpfc_issue_reg_vfi(vport);
1b51197d 824 }
92d7f7b0 825 }
dea3101e 826 return 0;
dea3101e 827}
1b51197d 828
e59058c4 829/**
3621a710 830 * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
e59058c4
JS
831 * @vport: pointer to a host virtual N_Port data structure.
832 * @ndlp: pointer to a node-list data structure.
833 * @sp: pointer to service parameter data structure.
834 *
835 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
836 * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
837 * in a point-to-point topology. First, the @vport's N_Port Name is compared
838 * with the received N_Port Name: if the @vport's N_Port Name is greater than
839 * the received N_Port Name lexicographically, this node shall assign local
840 * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
841 * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
842 * this node shall just wait for the remote node to issue PLOGI and assign
843 * N_Port IDs.
844 *
845 * Return code
846 * 0 - Success
847 * -ENXIO - Fail
848 **/
dea3101e 849static int
2e0fef85
JS
850lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
851 struct serv_parm *sp)
dea3101e 852{
2e0fef85
JS
853 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
854 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
855 LPFC_MBOXQ_t *mbox;
856 int rc;
857
2e0fef85
JS
858 spin_lock_irq(shost->host_lock);
859 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
d6de08cc 860 vport->fc_flag |= FC_PT2PT;
2e0fef85 861 spin_unlock_irq(shost->host_lock);
dea3101e 862
d33d0eb2
JS
863 /* If we are pt2pt with another NPort, force NPIV off! */
864 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
865
d6de08cc
JS
866 /* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
867 if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
868 lpfc_unregister_fcf_prep(phba);
869
870 spin_lock_irq(shost->host_lock);
871 vport->fc_flag &= ~FC_VFI_REGISTERED;
872 spin_unlock_irq(shost->host_lock);
873 phba->fc_topology_changed = 0;
874 }
875
2e0fef85 876 rc = memcmp(&vport->fc_portname, &sp->portName,
92d7f7b0 877 sizeof(vport->fc_portname));
2eb6862a 878
dea3101e
JB
879 if (rc >= 0) {
880 /* This side will initiate the PLOGI */
2e0fef85
JS
881 spin_lock_irq(shost->host_lock);
882 vport->fc_flag |= FC_PT2PT_PLOGI;
883 spin_unlock_irq(shost->host_lock);
dea3101e
JB
884
885 /*
d6de08cc
JS
886 * N_Port ID cannot be 0, set our Id to LocalID
887 * the other side will be RemoteID.
dea3101e
JB
888 */
889
890 /* not equal */
891 if (rc)
2e0fef85 892 vport->fc_myDID = PT2PT_LocalID;
dea3101e 893
e47c9093
JS
894 /* Decrement ndlp reference count indicating that ndlp can be
895 * safely released when other references to it are done.
896 */
329f9bc7 897 lpfc_nlp_put(ndlp);
dea3101e 898
2e0fef85 899 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
dea3101e
JB
900 if (!ndlp) {
901 /*
902 * Cannot find existing Fabric ndlp, so allocate a
903 * new one
904 */
9d3d340d 905 ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
dea3101e
JB
906 if (!ndlp)
907 goto fail;
e47c9093
JS
908 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
909 ndlp = lpfc_enable_node(vport, ndlp,
910 NLP_STE_UNUSED_NODE);
911 if(!ndlp)
912 goto fail;
dea3101e
JB
913 }
914
915 memcpy(&ndlp->nlp_portname, &sp->portName,
2e0fef85 916 sizeof(struct lpfc_name));
dea3101e 917 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
2e0fef85 918 sizeof(struct lpfc_name));
e47c9093 919 /* Set state will put ndlp onto node list if not already done */
2e0fef85
JS
920 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
921 spin_lock_irq(shost->host_lock);
dea3101e 922 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 923 spin_unlock_irq(shost->host_lock);
d33d0eb2
JS
924
925 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
926 if (!mbox)
927 goto fail;
928
929 lpfc_config_link(phba, mbox);
930
931 mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
932 mbox->vport = vport;
933 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
934 if (rc == MBX_NOT_FINISHED) {
935 mempool_free(mbox, phba->mbox_mem_pool);
936 goto fail;
937 }
938 } else {
e47c9093
JS
939 /* This side will wait for the PLOGI, decrement ndlp reference
940 * count indicating that ndlp can be released when other
941 * references to it are done.
942 */
329f9bc7 943 lpfc_nlp_put(ndlp);
dea3101e 944
d33d0eb2
JS
945 /* Start discovery - this should just do CLEAR_LA */
946 lpfc_disc_start(vport);
e74c03c8 947 }
dea3101e 948
dea3101e 949 return 0;
92d7f7b0 950fail:
dea3101e
JB
951 return -ENXIO;
952}
953
e59058c4 954/**
3621a710 955 * lpfc_cmpl_els_flogi - Completion callback function for flogi
e59058c4
JS
956 * @phba: pointer to lpfc hba data structure.
957 * @cmdiocb: pointer to lpfc command iocb data structure.
958 * @rspiocb: pointer to lpfc response iocb data structure.
959 *
960 * This routine is the top-level completion callback function for issuing
961 * a Fabric Login (FLOGI) command. If the response IOCB reported error,
962 * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
963 * retry has been made (either immediately or delayed with lpfc_els_retry()
964 * returning 1), the command IOCB will be released and function returned.
965 * If the retry attempt has been given up (possibly reach the maximum
966 * number of retries), one additional decrement of ndlp reference shall be
967 * invoked before going out after releasing the command IOCB. This will
968 * actually release the remote node (Note, lpfc_els_free_iocb() will also
969 * invoke one decrement of ndlp reference count). If no error reported in
970 * the IOCB status, the command Port ID field is used to determine whether
971 * this is a point-to-point topology or a fabric topology: if the Port ID
972 * field is assigned, it is a fabric topology; otherwise, it is a
973 * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
974 * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
975 * specific topology completion conditions.
976 **/
dea3101e 977static void
329f9bc7
JS
978lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
979 struct lpfc_iocbq *rspiocb)
dea3101e 980{
2e0fef85
JS
981 struct lpfc_vport *vport = cmdiocb->vport;
982 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
983 IOCB_t *irsp = &rspiocb->iocb;
984 struct lpfc_nodelist *ndlp = cmdiocb->context1;
985 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
986 struct serv_parm *sp;
0c9ab6f5 987 uint16_t fcf_index;
dea3101e
JB
988 int rc;
989
990 /* Check to see if link went down during discovery */
2e0fef85 991 if (lpfc_els_chk_latt(vport)) {
fa4066b6
JS
992 /* One additional decrement on node reference count to
993 * trigger the release of the node
994 */
329f9bc7 995 lpfc_nlp_put(ndlp);
dea3101e
JB
996 goto out;
997 }
998
858c9f6c
JS
999 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1000 "FLOGI cmpl: status:x%x/x%x state:x%x",
1001 irsp->ulpStatus, irsp->un.ulpWord[4],
1002 vport->port_state);
1003
dea3101e 1004 if (irsp->ulpStatus) {
0c9ab6f5 1005 /*
a93ff37a 1006 * In case of FIP mode, perform roundrobin FCF failover
0c9ab6f5
JS
1007 * due to new FCF discovery
1008 */
1009 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
80c17849
JS
1010 (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
1011 if (phba->link_state < LPFC_LINK_UP)
1012 goto stop_rr_fcf_flogi;
1013 if ((phba->fcoe_cvl_eventtag_attn ==
1014 phba->fcoe_cvl_eventtag) &&
1015 (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
e3d2b802
JS
1016 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1017 IOERR_SLI_ABORTED))
80c17849
JS
1018 goto stop_rr_fcf_flogi;
1019 else
1020 phba->fcoe_cvl_eventtag_attn =
1021 phba->fcoe_cvl_eventtag;
0c9ab6f5 1022 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
a93ff37a
JS
1023 "2611 FLOGI failed on FCF (x%x), "
1024 "status:x%x/x%x, tmo:x%x, perform "
1025 "roundrobin FCF failover\n",
38b92ef8
JS
1026 phba->fcf.current_rec.fcf_indx,
1027 irsp->ulpStatus, irsp->un.ulpWord[4],
1028 irsp->ulpTimeout);
7d791df7
JS
1029 lpfc_sli4_set_fcf_flogi_fail(phba,
1030 phba->fcf.current_rec.fcf_indx);
0c9ab6f5 1031 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
a93ff37a
JS
1032 rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1033 if (rc)
1034 goto out;
0c9ab6f5
JS
1035 }
1036
80c17849 1037stop_rr_fcf_flogi:
38b92ef8 1038 /* FLOGI failure */
c73455e1
JS
1039 if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1040 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1041 IOERR_LOOP_OPEN_FAILURE)))
1042 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1043 "2858 FLOGI failure Status:x%x/x%x "
1044 "TMO:x%x Data x%x x%x\n",
1045 irsp->ulpStatus, irsp->un.ulpWord[4],
1046 irsp->ulpTimeout, phba->hba_flag,
1047 phba->fcf.fcf_flag);
38b92ef8 1048
dea3101e 1049 /* Check for retry */
2e0fef85 1050 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
dea3101e 1051 goto out;
2e0fef85 1052
ae9e28f3
JS
1053 /* If this is not a loop open failure, bail out */
1054 if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1055 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1056 IOERR_LOOP_OPEN_FAILURE)))
1057 goto flogifail;
1058
c73455e1 1059 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
d496b9a7 1060 "0150 FLOGI failure Status:x%x/x%x xri x%x TMO:x%x\n",
c73455e1 1061 irsp->ulpStatus, irsp->un.ulpWord[4],
d496b9a7 1062 cmdiocb->sli4_xritag, irsp->ulpTimeout);
c73455e1 1063
dea3101e 1064 /* FLOGI failed, so there is no fabric */
2e0fef85
JS
1065 spin_lock_irq(shost->host_lock);
1066 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1067 spin_unlock_irq(shost->host_lock);
dea3101e 1068
329f9bc7 1069 /* If private loop, then allow max outstanding els to be
dea3101e
JB
1070 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1071 * alpa map would take too long otherwise.
1072 */
1b51197d 1073 if (phba->alpa_map[0] == 0)
3de2a653 1074 vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
ff78d8f9
JS
1075 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1076 (!(vport->fc_flag & FC_VFI_REGISTERED) ||
e74c03c8
JS
1077 (vport->fc_prevDID != vport->fc_myDID) ||
1078 phba->fc_topology_changed)) {
1079 if (vport->fc_flag & FC_VFI_REGISTERED) {
1080 if (phba->fc_topology_changed) {
1081 lpfc_unregister_fcf_prep(phba);
1082 spin_lock_irq(shost->host_lock);
1083 vport->fc_flag &= ~FC_VFI_REGISTERED;
1084 spin_unlock_irq(shost->host_lock);
1085 phba->fc_topology_changed = 0;
1086 } else {
1087 lpfc_sli4_unreg_all_rpis(vport);
1088 }
1089 }
342b59ca
JS
1090
1091 /* Do not register VFI if the driver aborted FLOGI */
1092 if (!lpfc_error_lost_link(irsp))
1093 lpfc_issue_reg_vfi(vport);
ff78d8f9
JS
1094 lpfc_nlp_put(ndlp);
1095 goto out;
dea3101e 1096 }
dea3101e
JB
1097 goto flogifail;
1098 }
695a814e
JS
1099 spin_lock_irq(shost->host_lock);
1100 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
4b40c59e 1101 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
695a814e 1102 spin_unlock_irq(shost->host_lock);
dea3101e
JB
1103
1104 /*
1105 * The FLogI succeeded. Sync the data for the CPU before
1106 * accessing it.
1107 */
1108 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
a2fc4aef
JS
1109 if (!prsp)
1110 goto out;
dea3101e
JB
1111 sp = prsp->virt + sizeof(uint32_t);
1112
1113 /* FLOGI completes successfully */
e8b62011 1114 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
88f43a08 1115 "0101 FLOGI completes successfully, I/O tag:x%x, "
d496b9a7
JS
1116 "xri x%x Data: x%x x%x x%x x%x x%x %x\n",
1117 cmdiocb->iotag, cmdiocb->sli4_xritag,
e8b62011 1118 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
e74c03c8
JS
1119 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1120 vport->port_state, vport->fc_flag);
dea3101e 1121
2e0fef85 1122 if (vport->port_state == LPFC_FLOGI) {
dea3101e
JB
1123 /*
1124 * If Common Service Parameters indicate Nport
1125 * we are point to point, if Fport we are Fabric.
1126 */
1127 if (sp->cmn.fPort)
2e0fef85 1128 rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
76a95d75 1129 else if (!(phba->hba_flag & HBA_FCOE_MODE))
2e0fef85 1130 rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
dbb6b3ab
JS
1131 else {
1132 lpfc_printf_vlog(vport, KERN_ERR,
1133 LOG_FIP | LOG_ELS,
1134 "2831 FLOGI response with cleared Fabric "
1135 "bit fcf_index 0x%x "
1136 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1137 "Fabric Name "
1138 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
1139 phba->fcf.current_rec.fcf_indx,
1140 phba->fcf.current_rec.switch_name[0],
1141 phba->fcf.current_rec.switch_name[1],
1142 phba->fcf.current_rec.switch_name[2],
1143 phba->fcf.current_rec.switch_name[3],
1144 phba->fcf.current_rec.switch_name[4],
1145 phba->fcf.current_rec.switch_name[5],
1146 phba->fcf.current_rec.switch_name[6],
1147 phba->fcf.current_rec.switch_name[7],
1148 phba->fcf.current_rec.fabric_name[0],
1149 phba->fcf.current_rec.fabric_name[1],
1150 phba->fcf.current_rec.fabric_name[2],
1151 phba->fcf.current_rec.fabric_name[3],
1152 phba->fcf.current_rec.fabric_name[4],
1153 phba->fcf.current_rec.fabric_name[5],
1154 phba->fcf.current_rec.fabric_name[6],
1155 phba->fcf.current_rec.fabric_name[7]);
1156 lpfc_nlp_put(ndlp);
1157 spin_lock_irq(&phba->hbalock);
1158 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
a93ff37a 1159 phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
dbb6b3ab 1160 spin_unlock_irq(&phba->hbalock);
036cad1f 1161 phba->fcf.fcf_redisc_attempted = 0; /* reset */
dbb6b3ab
JS
1162 goto out;
1163 }
0c9ab6f5
JS
1164 if (!rc) {
1165 /* Mark the FCF discovery process done */
999d813f
JS
1166 if (phba->hba_flag & HBA_FIP_SUPPORT)
1167 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1168 LOG_ELS,
a93ff37a
JS
1169 "2769 FLOGI to FCF (x%x) "
1170 "completed successfully\n",
999d813f 1171 phba->fcf.current_rec.fcf_indx);
0c9ab6f5
JS
1172 spin_lock_irq(&phba->hbalock);
1173 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
a93ff37a 1174 phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
0c9ab6f5 1175 spin_unlock_irq(&phba->hbalock);
036cad1f 1176 phba->fcf.fcf_redisc_attempted = 0; /* reset */
dea3101e 1177 goto out;
0c9ab6f5 1178 }
dea3101e
JB
1179 }
1180
1181flogifail:
8fe5c165
JS
1182 spin_lock_irq(&phba->hbalock);
1183 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1184 spin_unlock_irq(&phba->hbalock);
d6de08cc 1185
329f9bc7 1186 lpfc_nlp_put(ndlp);
dea3101e 1187
858c9f6c 1188 if (!lpfc_error_lost_link(irsp)) {
dea3101e 1189 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 1190 lpfc_disc_list_loopmap(vport);
dea3101e
JB
1191
1192 /* Start discovery */
2e0fef85 1193 lpfc_disc_start(vport);
87af33fe 1194 } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
e3d2b802
JS
1195 (((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1196 IOERR_SLI_ABORTED) &&
1197 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1198 IOERR_SLI_DOWN))) &&
87af33fe
JS
1199 (phba->link_state != LPFC_CLEAR_LA)) {
1200 /* If FLOGI failed enable link interrupt. */
1201 lpfc_issue_clear_la(phba, vport);
dea3101e 1202 }
dea3101e
JB
1203out:
1204 lpfc_els_free_iocb(phba, cmdiocb);
1205}
1206
e59058c4 1207/**
3621a710 1208 * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
e59058c4
JS
1209 * @vport: pointer to a host virtual N_Port data structure.
1210 * @ndlp: pointer to a node-list data structure.
1211 * @retry: number of retries to the command IOCB.
1212 *
1213 * This routine issues a Fabric Login (FLOGI) Request ELS command
1214 * for a @vport. The initiator service parameters are put into the payload
1215 * of the FLOGI Request IOCB and the top-level callback function pointer
1216 * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1217 * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1218 * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1219 *
1220 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1221 * will be incremented by 1 for holding the ndlp and the reference to ndlp
1222 * will be stored into the context1 field of the IOCB for the completion
1223 * callback function to the FLOGI ELS command.
1224 *
1225 * Return code
1226 * 0 - successfully issued flogi iocb for @vport
1227 * 1 - failed to issue flogi iocb for @vport
1228 **/
dea3101e 1229static int
2e0fef85 1230lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
1231 uint8_t retry)
1232{
2e0fef85 1233 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
1234 struct serv_parm *sp;
1235 IOCB_t *icmd;
1236 struct lpfc_iocbq *elsiocb;
dea3101e
JB
1237 uint8_t *pcmd;
1238 uint16_t cmdsize;
1239 uint32_t tmo;
1240 int rc;
1241
92d7f7b0 1242 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2e0fef85
JS
1243 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1244 ndlp->nlp_DID, ELS_CMD_FLOGI);
92d7f7b0 1245
488d1469 1246 if (!elsiocb)
c9f8735b 1247 return 1;
dea3101e
JB
1248
1249 icmd = &elsiocb->iocb;
1250 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1251
1252 /* For FLOGI request, remainder of payload is service parameters */
1253 *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
92d7f7b0
JS
1254 pcmd += sizeof(uint32_t);
1255 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
dea3101e
JB
1256 sp = (struct serv_parm *) pcmd;
1257
1258 /* Setup CSPs accordingly for Fabric */
1259 sp->cmn.e_d_tov = 0;
1260 sp->cmn.w2.r_a_tov = 0;
df9e1b59 1261 sp->cmn.virtual_fabric_support = 0;
dea3101e 1262 sp->cls1.classValid = 0;
dea3101e
JB
1263 if (sp->cmn.fcphLow < FC_PH3)
1264 sp->cmn.fcphLow = FC_PH3;
1265 if (sp->cmn.fcphHigh < FC_PH3)
1266 sp->cmn.fcphHigh = FC_PH3;
1267
c31098ce
JS
1268 if (phba->sli_rev == LPFC_SLI_REV4) {
1269 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1270 LPFC_SLI_INTF_IF_TYPE_0) {
1271 elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1272 elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1273 /* FLOGI needs to be 3 for WQE FCFI */
1274 /* Set the fcfi to the fcfi we registered with */
1275 elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1276 }
0f37887e
JS
1277 /* Can't do SLI4 class2 without support sequence coalescing */
1278 sp->cls2.classValid = 0;
1279 sp->cls2.seqDelivery = 0;
5248a749 1280 } else {
0f37887e
JS
1281 /* Historical, setting sequential-delivery bit for SLI3 */
1282 sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1283 sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
5248a749
JS
1284 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1285 sp->cmn.request_multiple_Nport = 1;
1286 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1287 icmd->ulpCt_h = 1;
1288 icmd->ulpCt_l = 0;
1289 } else
1290 sp->cmn.request_multiple_Nport = 0;
92d7f7b0
JS
1291 }
1292
76a95d75 1293 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
858c9f6c
JS
1294 icmd->un.elsreq64.myID = 0;
1295 icmd->un.elsreq64.fl = 1;
1296 }
1297
dea3101e
JB
1298 tmo = phba->fc_ratov;
1299 phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
2e0fef85 1300 lpfc_set_disctmo(vport);
dea3101e
JB
1301 phba->fc_ratov = tmo;
1302
1303 phba->fc_stat.elsXmitFLOGI++;
1304 elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
858c9f6c
JS
1305
1306 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1307 "Issue FLOGI: opt:x%x",
1308 phba->sli3_options, 0, 0);
1309
92d7f7b0 1310 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
dea3101e
JB
1311 if (rc == IOCB_ERROR) {
1312 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 1313 return 1;
dea3101e 1314 }
c9f8735b 1315 return 0;
dea3101e
JB
1316}
1317
e59058c4 1318/**
3621a710 1319 * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
e59058c4
JS
1320 * @phba: pointer to lpfc hba data structure.
1321 *
1322 * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1323 * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1324 * list and issues an abort IOCB commond on each outstanding IOCB that
1325 * contains a active Fabric_DID ndlp. Note that this function is to issue
1326 * the abort IOCB command on all the outstanding IOCBs, thus when this
1327 * function returns, it does not guarantee all the IOCBs are actually aborted.
1328 *
1329 * Return code
3ad2f3fb 1330 * 0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
e59058c4 1331 **/
dea3101e 1332int
2e0fef85 1333lpfc_els_abort_flogi(struct lpfc_hba *phba)
dea3101e
JB
1334{
1335 struct lpfc_sli_ring *pring;
1336 struct lpfc_iocbq *iocb, *next_iocb;
1337 struct lpfc_nodelist *ndlp;
1338 IOCB_t *icmd;
1339
1340 /* Abort outstanding I/O on NPort <nlp_DID> */
1341 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
e8b62011
JS
1342 "0201 Abort outstanding I/O on NPort x%x\n",
1343 Fabric_DID);
dea3101e 1344
895427bd 1345 pring = lpfc_phba_elsring(phba);
5a9eeff5
JS
1346 if (unlikely(!pring))
1347 return -EIO;
dea3101e
JB
1348
1349 /*
1350 * Check the txcmplq for an iocb that matches the nport the driver is
1351 * searching for.
1352 */
2e0fef85 1353 spin_lock_irq(&phba->hbalock);
dea3101e
JB
1354 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1355 icmd = &iocb->iocb;
1b51197d 1356 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
dea3101e 1357 ndlp = (struct lpfc_nodelist *)(iocb->context1);
58da1ffb
JS
1358 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1359 (ndlp->nlp_DID == Fabric_DID))
07951076 1360 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e
JB
1361 }
1362 }
2e0fef85 1363 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
1364
1365 return 0;
1366}
1367
e59058c4 1368/**
3621a710 1369 * lpfc_initial_flogi - Issue an initial fabric login for a vport
e59058c4
JS
1370 * @vport: pointer to a host virtual N_Port data structure.
1371 *
1372 * This routine issues an initial Fabric Login (FLOGI) for the @vport
1373 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1374 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1375 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1376 * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1377 * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1378 * @vport.
1379 *
1380 * Return code
1381 * 0 - failed to issue initial flogi for @vport
1382 * 1 - successfully issued initial flogi for @vport
1383 **/
dea3101e 1384int
2e0fef85 1385lpfc_initial_flogi(struct lpfc_vport *vport)
dea3101e
JB
1386{
1387 struct lpfc_nodelist *ndlp;
1388
98c9ea5c
JS
1389 vport->port_state = LPFC_FLOGI;
1390 lpfc_set_disctmo(vport);
1391
c9f8735b 1392 /* First look for the Fabric ndlp */
2e0fef85 1393 ndlp = lpfc_findnode_did(vport, Fabric_DID);
c9f8735b 1394 if (!ndlp) {
dea3101e 1395 /* Cannot find existing Fabric ndlp, so allocate a new one */
9d3d340d 1396 ndlp = lpfc_nlp_init(vport, Fabric_DID);
c9f8735b
JW
1397 if (!ndlp)
1398 return 0;
6fb120a7
JS
1399 /* Set the node type */
1400 ndlp->nlp_type |= NLP_FABRIC;
e47c9093
JS
1401 /* Put ndlp onto node list */
1402 lpfc_enqueue_node(vport, ndlp);
1403 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1404 /* re-setup ndlp without removing from node list */
1405 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1406 if (!ndlp)
1407 return 0;
dea3101e 1408 }
87af33fe 1409
5ac6b303 1410 if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
fa4066b6
JS
1411 /* This decrement of reference count to node shall kick off
1412 * the release of the node.
1413 */
329f9bc7 1414 lpfc_nlp_put(ndlp);
5ac6b303
JS
1415 return 0;
1416 }
c9f8735b 1417 return 1;
dea3101e
JB
1418}
1419
e59058c4 1420/**
3621a710 1421 * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
e59058c4
JS
1422 * @vport: pointer to a host virtual N_Port data structure.
1423 *
1424 * This routine issues an initial Fabric Discover (FDISC) for the @vport
1425 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1426 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1427 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1428 * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1429 * is then invoked with the @vport and the ndlp to perform the FDISC for the
1430 * @vport.
1431 *
1432 * Return code
1433 * 0 - failed to issue initial fdisc for @vport
1434 * 1 - successfully issued initial fdisc for @vport
1435 **/
92d7f7b0
JS
1436int
1437lpfc_initial_fdisc(struct lpfc_vport *vport)
1438{
92d7f7b0
JS
1439 struct lpfc_nodelist *ndlp;
1440
1441 /* First look for the Fabric ndlp */
1442 ndlp = lpfc_findnode_did(vport, Fabric_DID);
1443 if (!ndlp) {
1444 /* Cannot find existing Fabric ndlp, so allocate a new one */
9d3d340d 1445 ndlp = lpfc_nlp_init(vport, Fabric_DID);
92d7f7b0
JS
1446 if (!ndlp)
1447 return 0;
e47c9093
JS
1448 /* Put ndlp onto node list */
1449 lpfc_enqueue_node(vport, ndlp);
1450 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1451 /* re-setup ndlp without removing from node list */
1452 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1453 if (!ndlp)
1454 return 0;
92d7f7b0 1455 }
e47c9093 1456
92d7f7b0 1457 if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
fa4066b6
JS
1458 /* decrement node reference count to trigger the release of
1459 * the node.
1460 */
92d7f7b0 1461 lpfc_nlp_put(ndlp);
fa4066b6 1462 return 0;
92d7f7b0
JS
1463 }
1464 return 1;
1465}
87af33fe 1466
e59058c4 1467/**
3621a710 1468 * lpfc_more_plogi - Check and issue remaining plogis for a vport
e59058c4
JS
1469 * @vport: pointer to a host virtual N_Port data structure.
1470 *
1471 * This routine checks whether there are more remaining Port Logins
1472 * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1473 * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1474 * to issue ELS PLOGIs up to the configured discover threads with the
1475 * @vport (@vport->cfg_discovery_threads). The function also decrement
1476 * the @vport's num_disc_node by 1 if it is not already 0.
1477 **/
87af33fe 1478void
2e0fef85 1479lpfc_more_plogi(struct lpfc_vport *vport)
dea3101e 1480{
2e0fef85
JS
1481 if (vport->num_disc_nodes)
1482 vport->num_disc_nodes--;
dea3101e
JB
1483
1484 /* Continue discovery with <num_disc_nodes> PLOGIs to go */
e8b62011
JS
1485 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1486 "0232 Continue discovery with %d PLOGIs to go "
1487 "Data: x%x x%x x%x\n",
1488 vport->num_disc_nodes, vport->fc_plogi_cnt,
1489 vport->fc_flag, vport->port_state);
dea3101e 1490 /* Check to see if there are more PLOGIs to be sent */
2e0fef85
JS
1491 if (vport->fc_flag & FC_NLP_MORE)
1492 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
db6f1c2f 1493 lpfc_els_disc_plogi(vport);
2e0fef85 1494
dea3101e
JB
1495 return;
1496}
1497
e59058c4 1498/**
3621a710 1499 * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
e59058c4
JS
1500 * @phba: pointer to lpfc hba data structure.
1501 * @prsp: pointer to response IOCB payload.
1502 * @ndlp: pointer to a node-list data structure.
1503 *
1504 * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1505 * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1506 * The following cases are considered N_Port confirmed:
1507 * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1508 * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1509 * it does not have WWPN assigned either. If the WWPN is confirmed, the
1510 * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1511 * 1) if there is a node on vport list other than the @ndlp with the same
1512 * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1513 * on that node to release the RPI associated with the node; 2) if there is
1514 * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1515 * into, a new node shall be allocated (or activated). In either case, the
1516 * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1517 * be released and the new_ndlp shall be put on to the vport node list and
1518 * its pointer returned as the confirmed node.
1519 *
1520 * Note that before the @ndlp got "released", the keepDID from not-matching
1521 * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1522 * of the @ndlp. This is because the release of @ndlp is actually to put it
1523 * into an inactive state on the vport node list and the vport node list
1524 * management algorithm does not allow two node with a same DID.
1525 *
1526 * Return code
1527 * pointer to the PLOGI N_Port @ndlp
1528 **/
488d1469 1529static struct lpfc_nodelist *
92d7f7b0 1530lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
488d1469
JS
1531 struct lpfc_nodelist *ndlp)
1532{
a0f2d3ef 1533 struct lpfc_vport *vport = ndlp->vport;
953ceeda 1534 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
488d1469 1535 struct lpfc_nodelist *new_ndlp;
0ff10d46
JS
1536 struct lpfc_rport_data *rdata;
1537 struct fc_rport *rport;
488d1469 1538 struct serv_parm *sp;
92d7f7b0 1539 uint8_t name[sizeof(struct lpfc_name)];
e5abba4c 1540 uint32_t rc, keepDID = 0, keep_nlp_flag = 0;
92ea83a8 1541 uint32_t keep_new_nlp_flag = 0;
953ceeda 1542 uint16_t keep_nlp_state;
92ea83a8 1543 u32 keep_nlp_fc4_type = 0;
4adc041b 1544 struct lpfc_nvme_rport *keep_nrport = NULL;
38b92ef8
JS
1545 int put_node;
1546 int put_rport;
cff261f6 1547 unsigned long *active_rrqs_xri_bitmap = NULL;
488d1469 1548
2fb9bd8b
JS
1549 /* Fabric nodes can have the same WWPN so we don't bother searching
1550 * by WWPN. Just return the ndlp that was given to us.
1551 */
1552 if (ndlp->nlp_type & NLP_FABRIC)
1553 return ndlp;
1554
92d7f7b0 1555 sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
685f0bf7 1556 memset(name, 0, sizeof(struct lpfc_name));
488d1469 1557
685f0bf7 1558 /* Now we find out if the NPort we are logging into, matches the WWPN
488d1469
JS
1559 * we have for that ndlp. If not, we have some work to do.
1560 */
2e0fef85 1561 new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
488d1469 1562
d83ca3ea 1563 /* return immediately if the WWPN matches ndlp */
e47c9093 1564 if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
488d1469 1565 return ndlp;
d83ca3ea 1566
cff261f6
JS
1567 if (phba->sli_rev == LPFC_SLI_REV4) {
1568 active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1569 GFP_KERNEL);
1570 if (active_rrqs_xri_bitmap)
1571 memset(active_rrqs_xri_bitmap, 0,
1572 phba->cfg_rrq_xri_bitmap_sz);
1573 }
488d1469 1574
d83ca3ea
JS
1575 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1576 "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1577 "new_ndlp x%x x%x x%x\n",
1578 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_fc4_type,
1579 (new_ndlp ? new_ndlp->nlp_DID : 0),
1580 (new_ndlp ? new_ndlp->nlp_flag : 0),
1581 (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
34f5ad8b 1582
488d1469 1583 if (!new_ndlp) {
2e0fef85
JS
1584 rc = memcmp(&ndlp->nlp_portname, name,
1585 sizeof(struct lpfc_name));
cff261f6
JS
1586 if (!rc) {
1587 if (active_rrqs_xri_bitmap)
1588 mempool_free(active_rrqs_xri_bitmap,
1589 phba->active_rrq_pool);
92795650 1590 return ndlp;
cff261f6 1591 }
9d3d340d 1592 new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
cff261f6
JS
1593 if (!new_ndlp) {
1594 if (active_rrqs_xri_bitmap)
1595 mempool_free(active_rrqs_xri_bitmap,
1596 phba->active_rrq_pool);
488d1469 1597 return ndlp;
cff261f6 1598 }
e47c9093 1599 } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
58da1ffb
JS
1600 rc = memcmp(&ndlp->nlp_portname, name,
1601 sizeof(struct lpfc_name));
cff261f6
JS
1602 if (!rc) {
1603 if (active_rrqs_xri_bitmap)
1604 mempool_free(active_rrqs_xri_bitmap,
1605 phba->active_rrq_pool);
58da1ffb 1606 return ndlp;
cff261f6 1607 }
e47c9093
JS
1608 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1609 NLP_STE_UNUSED_NODE);
cff261f6
JS
1610 if (!new_ndlp) {
1611 if (active_rrqs_xri_bitmap)
1612 mempool_free(active_rrqs_xri_bitmap,
1613 phba->active_rrq_pool);
e47c9093 1614 return ndlp;
cff261f6 1615 }
58da1ffb 1616 keepDID = new_ndlp->nlp_DID;
cff261f6
JS
1617 if ((phba->sli_rev == LPFC_SLI_REV4) && active_rrqs_xri_bitmap)
1618 memcpy(active_rrqs_xri_bitmap,
1619 new_ndlp->active_rrqs_xri_bitmap,
1620 phba->cfg_rrq_xri_bitmap_sz);
19ca7609 1621 } else {
58da1ffb 1622 keepDID = new_ndlp->nlp_DID;
cff261f6
JS
1623 if (phba->sli_rev == LPFC_SLI_REV4 &&
1624 active_rrqs_xri_bitmap)
1625 memcpy(active_rrqs_xri_bitmap,
1626 new_ndlp->active_rrqs_xri_bitmap,
1627 phba->cfg_rrq_xri_bitmap_sz);
19ca7609 1628 }
488d1469 1629
d83ca3ea
JS
1630 /* At this point in this routine, we know new_ndlp will be
1631 * returned. however, any previous GID_FTs that were done
1632 * would have updated nlp_fc4_type in ndlp, so we must ensure
1633 * new_ndlp has the right value.
1634 */
92ea83a8
JS
1635 if (vport->fc_flag & FC_FABRIC) {
1636 keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
d83ca3ea 1637 new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
92ea83a8 1638 }
d83ca3ea 1639
2e0fef85 1640 lpfc_unreg_rpi(vport, new_ndlp);
488d1469 1641 new_ndlp->nlp_DID = ndlp->nlp_DID;
92795650 1642 new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
19ca7609 1643 if (phba->sli_rev == LPFC_SLI_REV4)
cff261f6
JS
1644 memcpy(new_ndlp->active_rrqs_xri_bitmap,
1645 ndlp->active_rrqs_xri_bitmap,
1646 phba->cfg_rrq_xri_bitmap_sz);
0ff10d46 1647
953ceeda 1648 spin_lock_irq(shost->host_lock);
92ea83a8
JS
1649 keep_new_nlp_flag = new_ndlp->nlp_flag;
1650 keep_nlp_flag = ndlp->nlp_flag;
e5abba4c 1651 new_ndlp->nlp_flag = ndlp->nlp_flag;
dea16bda
JS
1652
1653 /* if new_ndlp had NLP_UNREG_INP set, keep it */
92ea83a8 1654 if (keep_new_nlp_flag & NLP_UNREG_INP)
dea16bda
JS
1655 new_ndlp->nlp_flag |= NLP_UNREG_INP;
1656 else
1657 new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1658
92ea83a8
JS
1659 /* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1660 if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1661 new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1662 else
1663 new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1664
1665 ndlp->nlp_flag = keep_new_nlp_flag;
1666
dea16bda 1667 /* if ndlp had NLP_UNREG_INP set, keep it */
92ea83a8
JS
1668 if (keep_nlp_flag & NLP_UNREG_INP)
1669 ndlp->nlp_flag |= NLP_UNREG_INP;
dea16bda 1670 else
92ea83a8
JS
1671 ndlp->nlp_flag &= ~NLP_UNREG_INP;
1672
1673 /* if ndlp had NLP_RPI_REGISTERED set, keep it */
1674 if (keep_nlp_flag & NLP_RPI_REGISTERED)
1675 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1676 else
1677 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
dea16bda 1678
953ceeda 1679 spin_unlock_irq(shost->host_lock);
0ff10d46 1680
953ceeda
JS
1681 /* Set nlp_states accordingly */
1682 keep_nlp_state = new_ndlp->nlp_state;
2e0fef85 1683 lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
488d1469 1684
4adc041b
DK
1685 /* interchange the nvme remoteport structs */
1686 keep_nrport = new_ndlp->nrport;
1687 new_ndlp->nrport = ndlp->nrport;
1688
2e0fef85 1689 /* Move this back to NPR state */
87af33fe
JS
1690 if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1691 /* The new_ndlp is replacing ndlp totally, so we need
1692 * to put ndlp on UNUSED list and try to free it.
1693 */
34f5ad8b
JS
1694 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1695 "3179 PLOGI confirm NEW: %x %x\n",
1696 new_ndlp->nlp_DID, keepDID);
0ff10d46
JS
1697
1698 /* Fix up the rport accordingly */
1699 rport = ndlp->rport;
1700 if (rport) {
1701 rdata = rport->dd_data;
1702 if (rdata->pnode == ndlp) {
466e840b 1703 /* break the link before dropping the ref */
0ff10d46 1704 ndlp->rport = NULL;
466e840b 1705 lpfc_nlp_put(ndlp);
0ff10d46
JS
1706 rdata->pnode = lpfc_nlp_get(new_ndlp);
1707 new_ndlp->rport = rport;
1708 }
1709 new_ndlp->nlp_type = ndlp->nlp_type;
1710 }
4adc041b
DK
1711
1712 /* Fix up the nvme rport */
1713 if (ndlp->nrport) {
1714 ndlp->nrport = NULL;
1715 lpfc_nlp_put(ndlp);
1716 }
1717
58da1ffb
JS
1718 /* We shall actually free the ndlp with both nlp_DID and
1719 * nlp_portname fields equals 0 to avoid any ndlp on the
1720 * nodelist never to be used.
1721 */
1722 if (ndlp->nlp_DID == 0) {
1723 spin_lock_irq(&phba->ndlp_lock);
1724 NLP_SET_FREE_REQ(ndlp);
1725 spin_unlock_irq(&phba->ndlp_lock);
1726 }
0ff10d46 1727
92ea83a8
JS
1728 /* Two ndlps cannot have the same did on the nodelist.
1729 * Note: for this case, ndlp has a NULL WWPN so setting
1730 * the nlp_fc4_type isn't required.
1731 */
58da1ffb 1732 ndlp->nlp_DID = keepDID;
4938250e 1733 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
cff261f6
JS
1734 if (phba->sli_rev == LPFC_SLI_REV4 &&
1735 active_rrqs_xri_bitmap)
1736 memcpy(ndlp->active_rrqs_xri_bitmap,
1737 active_rrqs_xri_bitmap,
1738 phba->cfg_rrq_xri_bitmap_sz);
e5abba4c
JS
1739
1740 if (!NLP_CHK_NODE_ACT(ndlp))
1741 lpfc_drop_node(vport, ndlp);
87af33fe 1742 }
92795650 1743 else {
34f5ad8b
JS
1744 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1745 "3180 PLOGI confirm SWAP: %x %x\n",
1746 new_ndlp->nlp_DID, keepDID);
1747
2e0fef85 1748 lpfc_unreg_rpi(vport, ndlp);
34f5ad8b 1749
92ea83a8
JS
1750 /* Two ndlps cannot have the same did and the fc4
1751 * type must be transferred because the ndlp is in
1752 * flight.
1753 */
58da1ffb 1754 ndlp->nlp_DID = keepDID;
92ea83a8
JS
1755 ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1756
cff261f6
JS
1757 if (phba->sli_rev == LPFC_SLI_REV4 &&
1758 active_rrqs_xri_bitmap)
1759 memcpy(ndlp->active_rrqs_xri_bitmap,
1760 active_rrqs_xri_bitmap,
1761 phba->cfg_rrq_xri_bitmap_sz);
34f5ad8b 1762
953ceeda
JS
1763 /* Since we are switching over to the new_ndlp,
1764 * reset the old ndlp state
34f5ad8b
JS
1765 */
1766 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1767 (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
953ceeda
JS
1768 keep_nlp_state = NLP_STE_NPR_NODE;
1769 lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
34f5ad8b 1770
4adc041b
DK
1771 /* Previous ndlp no longer active with nvme host transport.
1772 * Remove reference from earlier registration unless the
1773 * nvme host took care of it.
1774 */
1775 if (ndlp->nrport)
1776 lpfc_nlp_put(ndlp);
1777 ndlp->nrport = keep_nrport;
1778
38b92ef8
JS
1779 /* Fix up the rport accordingly */
1780 rport = ndlp->rport;
1781 if (rport) {
1782 rdata = rport->dd_data;
1783 put_node = rdata->pnode != NULL;
1784 put_rport = ndlp->rport != NULL;
1785 rdata->pnode = NULL;
1786 ndlp->rport = NULL;
1787 if (put_node)
1788 lpfc_nlp_put(ndlp);
1789 if (put_rport)
1790 put_device(&rport->dev);
1791 }
92795650 1792 }
cff261f6
JS
1793 if (phba->sli_rev == LPFC_SLI_REV4 &&
1794 active_rrqs_xri_bitmap)
1795 mempool_free(active_rrqs_xri_bitmap,
1796 phba->active_rrq_pool);
d83ca3ea
JS
1797
1798 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1799 "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1800 new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1801 new_ndlp->nlp_fc4_type);
1802
488d1469
JS
1803 return new_ndlp;
1804}
1805
e59058c4 1806/**
3621a710 1807 * lpfc_end_rscn - Check and handle more rscn for a vport
e59058c4
JS
1808 * @vport: pointer to a host virtual N_Port data structure.
1809 *
1810 * This routine checks whether more Registration State Change
1811 * Notifications (RSCNs) came in while the discovery state machine was in
1812 * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1813 * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1814 * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1815 * handling the RSCNs.
1816 **/
87af33fe
JS
1817void
1818lpfc_end_rscn(struct lpfc_vport *vport)
1819{
1820 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1821
1822 if (vport->fc_flag & FC_RSCN_MODE) {
1823 /*
1824 * Check to see if more RSCNs came in while we were
1825 * processing this one.
1826 */
1827 if (vport->fc_rscn_id_cnt ||
1828 (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1829 lpfc_els_handle_rscn(vport);
1830 else {
1831 spin_lock_irq(shost->host_lock);
1832 vport->fc_flag &= ~FC_RSCN_MODE;
1833 spin_unlock_irq(shost->host_lock);
1834 }
1835 }
1836}
1837
19ca7609
JS
1838/**
1839 * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1840 * @phba: pointer to lpfc hba data structure.
1841 * @cmdiocb: pointer to lpfc command iocb data structure.
1842 * @rspiocb: pointer to lpfc response iocb data structure.
1843 *
1844 * This routine will call the clear rrq function to free the rrq and
1845 * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1846 * exist then the clear_rrq is still called because the rrq needs to
1847 * be freed.
1848 **/
1849
1850static void
1851lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1852 struct lpfc_iocbq *rspiocb)
1853{
1854 struct lpfc_vport *vport = cmdiocb->vport;
1855 IOCB_t *irsp;
1856 struct lpfc_nodelist *ndlp;
1857 struct lpfc_node_rrq *rrq;
1858
1859 /* we pass cmdiocb to state machine which needs rspiocb as well */
1860 rrq = cmdiocb->context_un.rrq;
1861 cmdiocb->context_un.rsp_iocb = rspiocb;
1862
1863 irsp = &rspiocb->iocb;
1864 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1865 "RRQ cmpl: status:x%x/x%x did:x%x",
1866 irsp->ulpStatus, irsp->un.ulpWord[4],
1867 irsp->un.elsreq64.remoteID);
1868
1869 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1870 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1871 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1872 "2882 RRQ completes to NPort x%x "
1873 "with no ndlp. Data: x%x x%x x%x\n",
1874 irsp->un.elsreq64.remoteID,
1875 irsp->ulpStatus, irsp->un.ulpWord[4],
1876 irsp->ulpIoTag);
1877 goto out;
1878 }
1879
1880 /* rrq completes to NPort <nlp_DID> */
1881 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1882 "2880 RRQ completes to NPort x%x "
1883 "Data: x%x x%x x%x x%x x%x\n",
1884 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1885 irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1886
1887 if (irsp->ulpStatus) {
1888 /* Check for retry */
1889 /* RRQ failed Don't print the vport to vport rjts */
1890 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1891 (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1892 ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1893 (phba)->pport->cfg_log_verbose & LOG_ELS)
1894 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1895 "2881 RRQ failure DID:%06X Status:x%x/x%x\n",
1896 ndlp->nlp_DID, irsp->ulpStatus,
1897 irsp->un.ulpWord[4]);
1898 }
1899out:
1900 if (rrq)
1901 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1902 lpfc_els_free_iocb(phba, cmdiocb);
1903 return;
1904}
e59058c4 1905/**
3621a710 1906 * lpfc_cmpl_els_plogi - Completion callback function for plogi
e59058c4
JS
1907 * @phba: pointer to lpfc hba data structure.
1908 * @cmdiocb: pointer to lpfc command iocb data structure.
1909 * @rspiocb: pointer to lpfc response iocb data structure.
1910 *
1911 * This routine is the completion callback function for issuing the Port
1912 * Login (PLOGI) command. For PLOGI completion, there must be an active
1913 * ndlp on the vport node list that matches the remote node ID from the
25985edc 1914 * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
e59058c4
JS
1915 * ignored and command IOCB released. The PLOGI response IOCB status is
1916 * checked for error conditons. If there is error status reported, PLOGI
1917 * retry shall be attempted by invoking the lpfc_els_retry() routine.
1918 * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1919 * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1920 * (DSM) is set for this PLOGI completion. Finally, it checks whether
1921 * there are additional N_Port nodes with the vport that need to perform
1922 * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1923 * PLOGIs.
1924 **/
dea3101e 1925static void
2e0fef85
JS
1926lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1927 struct lpfc_iocbq *rspiocb)
dea3101e 1928{
2e0fef85
JS
1929 struct lpfc_vport *vport = cmdiocb->vport;
1930 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 1931 IOCB_t *irsp;
dea3101e 1932 struct lpfc_nodelist *ndlp;
92795650 1933 struct lpfc_dmabuf *prsp;
eb016566 1934 int disc, rc;
dea3101e 1935
dea3101e
JB
1936 /* we pass cmdiocb to state machine which needs rspiocb as well */
1937 cmdiocb->context_un.rsp_iocb = rspiocb;
1938
1939 irsp = &rspiocb->iocb;
858c9f6c
JS
1940 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1941 "PLOGI cmpl: status:x%x/x%x did:x%x",
1942 irsp->ulpStatus, irsp->un.ulpWord[4],
1943 irsp->un.elsreq64.remoteID);
1944
2e0fef85 1945 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
e47c9093 1946 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
e8b62011
JS
1947 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1948 "0136 PLOGI completes to NPort x%x "
1949 "with no ndlp. Data: x%x x%x x%x\n",
1950 irsp->un.elsreq64.remoteID,
1951 irsp->ulpStatus, irsp->un.ulpWord[4],
1952 irsp->ulpIoTag);
488d1469 1953 goto out;
ed957684 1954 }
dea3101e
JB
1955
1956 /* Since ndlp can be freed in the disc state machine, note if this node
1957 * is being used during discovery.
1958 */
2e0fef85 1959 spin_lock_irq(shost->host_lock);
dea3101e 1960 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
488d1469 1961 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2e0fef85 1962 spin_unlock_irq(shost->host_lock);
dea16bda 1963 rc = 0;
dea3101e
JB
1964
1965 /* PLOGI completes to NPort <nlp_DID> */
e8b62011 1966 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
a0f2d3ef 1967 "0102 PLOGI completes to NPort x%06x "
e8b62011 1968 "Data: x%x x%x x%x x%x x%x\n",
a0f2d3ef
JS
1969 ndlp->nlp_DID, ndlp->nlp_fc4_type,
1970 irsp->ulpStatus, irsp->un.ulpWord[4],
1971 disc, vport->num_disc_nodes);
1972
dea3101e 1973 /* Check to see if link went down during discovery */
2e0fef85
JS
1974 if (lpfc_els_chk_latt(vport)) {
1975 spin_lock_irq(shost->host_lock);
dea3101e 1976 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1977 spin_unlock_irq(shost->host_lock);
dea3101e
JB
1978 goto out;
1979 }
1980
dea3101e
JB
1981 if (irsp->ulpStatus) {
1982 /* Check for retry */
1983 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1984 /* ELS command is being retried */
1985 if (disc) {
2e0fef85 1986 spin_lock_irq(shost->host_lock);
dea3101e 1987 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1988 spin_unlock_irq(shost->host_lock);
dea3101e
JB
1989 }
1990 goto out;
1991 }
2a9bf3d0
JS
1992 /* PLOGI failed Don't print the vport to vport rjts */
1993 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1994 (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1995 ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1996 (phba)->pport->cfg_log_verbose & LOG_ELS)
1997 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
e40a02c1
JS
1998 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1999 ndlp->nlp_DID, irsp->ulpStatus,
2000 irsp->un.ulpWord[4]);
dea3101e 2001 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 2002 if (lpfc_error_lost_link(irsp))
c9f8735b 2003 rc = NLP_STE_FREED_NODE;
e47c9093 2004 else
2e0fef85 2005 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 2006 NLP_EVT_CMPL_PLOGI);
dea3101e
JB
2007 } else {
2008 /* Good status, call state machine */
92795650 2009 prsp = list_entry(((struct lpfc_dmabuf *)
92d7f7b0
JS
2010 cmdiocb->context2)->list.next,
2011 struct lpfc_dmabuf, list);
2012 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2e0fef85 2013 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 2014 NLP_EVT_CMPL_PLOGI);
dea3101e
JB
2015 }
2016
2e0fef85 2017 if (disc && vport->num_disc_nodes) {
dea3101e 2018 /* Check to see if there are more PLOGIs to be sent */
2e0fef85 2019 lpfc_more_plogi(vport);
dea3101e 2020
2e0fef85
JS
2021 if (vport->num_disc_nodes == 0) {
2022 spin_lock_irq(shost->host_lock);
2023 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2024 spin_unlock_irq(shost->host_lock);
dea3101e 2025
2e0fef85 2026 lpfc_can_disctmo(vport);
87af33fe 2027 lpfc_end_rscn(vport);
dea3101e
JB
2028 }
2029 }
2030
2031out:
2032 lpfc_els_free_iocb(phba, cmdiocb);
2033 return;
2034}
2035
e59058c4 2036/**
3621a710 2037 * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
e59058c4
JS
2038 * @vport: pointer to a host virtual N_Port data structure.
2039 * @did: destination port identifier.
2040 * @retry: number of retries to the command IOCB.
2041 *
2042 * This routine issues a Port Login (PLOGI) command to a remote N_Port
2043 * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2044 * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2045 * This routine constructs the proper feilds of the PLOGI IOCB and invokes
2046 * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2047 *
2048 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2049 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2050 * will be stored into the context1 field of the IOCB for the completion
2051 * callback function to the PLOGI ELS command.
2052 *
2053 * Return code
2054 * 0 - Successfully issued a plogi for @vport
2055 * 1 - failed to issue a plogi for @vport
2056 **/
dea3101e 2057int
2e0fef85 2058lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
dea3101e 2059{
2e0fef85 2060 struct lpfc_hba *phba = vport->phba;
8db1c2b3 2061 struct Scsi_Host *shost;
dea3101e 2062 struct serv_parm *sp;
98c9ea5c 2063 struct lpfc_nodelist *ndlp;
dea3101e 2064 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2065 uint8_t *pcmd;
2066 uint16_t cmdsize;
92d7f7b0 2067 int ret;
dea3101e 2068
98c9ea5c 2069 ndlp = lpfc_findnode_did(vport, did);
dea16bda
JS
2070
2071 if (ndlp) {
2072 /* Defer the processing of the issue PLOGI until after the
2073 * outstanding UNREG_RPI mbox command completes, unless we
2074 * are going offline. This logic does not apply for Fabric DIDs
2075 */
2076 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2077 ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2078 !(vport->fc_flag & FC_OFFLINE_MODE)) {
2079 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2080 "4110 Issue PLOGI x%x deferred "
2081 "on NPort x%x rpi x%x Data: %p\n",
2082 ndlp->nlp_defer_did, ndlp->nlp_DID,
2083 ndlp->nlp_rpi, ndlp);
2084
2085 /* We can only defer 1st PLOGI */
2086 if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2087 ndlp->nlp_defer_did = did;
2088 return 0;
2089 }
2090 if (!NLP_CHK_NODE_ACT(ndlp))
2091 ndlp = NULL;
2092 }
98c9ea5c 2093
e47c9093 2094 /* If ndlp is not NULL, we will bump the reference count on it */
92d7f7b0 2095 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
98c9ea5c 2096 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2e0fef85 2097 ELS_CMD_PLOGI);
c9f8735b
JW
2098 if (!elsiocb)
2099 return 1;
dea3101e 2100
8db1c2b3
DK
2101 shost = lpfc_shost_from_vport(vport);
2102 spin_lock_irq(shost->host_lock);
2103 ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2104 spin_unlock_irq(shost->host_lock);
2105
dea3101e
JB
2106 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2107
2108 /* For PLOGI request, remainder of payload is service parameters */
2109 *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
92d7f7b0
JS
2110 pcmd += sizeof(uint32_t);
2111 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
dea3101e
JB
2112 sp = (struct serv_parm *) pcmd;
2113
5ac6b303
JS
2114 /*
2115 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2116 * to device on remote loops work.
2117 */
2118 if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2119 sp->cmn.altBbCredit = 1;
2120
dea3101e
JB
2121 if (sp->cmn.fcphLow < FC_PH_4_3)
2122 sp->cmn.fcphLow = FC_PH_4_3;
2123
2124 if (sp->cmn.fcphHigh < FC_PH3)
2125 sp->cmn.fcphHigh = FC_PH3;
2126
e0165f20 2127 sp->cmn.valid_vendor_ver_level = 0;
8c258641 2128 memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
44fd7fe3 2129 sp->cmn.bbRcvSizeMsb &= 0xF;
e0165f20 2130
858c9f6c
JS
2131 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2132 "Issue PLOGI: did:x%x",
2133 did, 0, 0);
2134
8c258641
JS
2135 /* If our firmware supports this feature, convey that
2136 * information to the target using the vendor specific field.
2137 */
2138 if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2139 sp->cmn.valid_vendor_ver_level = 1;
2140 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2141 sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2142 }
2143
dea3101e
JB
2144 phba->fc_stat.elsXmitPLOGI++;
2145 elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
3772a991 2146 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
92d7f7b0
JS
2147
2148 if (ret == IOCB_ERROR) {
dea3101e 2149 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2150 return 1;
dea3101e 2151 }
c9f8735b 2152 return 0;
dea3101e
JB
2153}
2154
e59058c4 2155/**
3621a710 2156 * lpfc_cmpl_els_prli - Completion callback function for prli
e59058c4
JS
2157 * @phba: pointer to lpfc hba data structure.
2158 * @cmdiocb: pointer to lpfc command iocb data structure.
2159 * @rspiocb: pointer to lpfc response iocb data structure.
2160 *
2161 * This routine is the completion callback function for a Process Login
2162 * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2163 * status. If there is error status reported, PRLI retry shall be attempted
2164 * by invoking the lpfc_els_retry() routine. Otherwise, the state
2165 * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2166 * ndlp to mark the PRLI completion.
2167 **/
dea3101e 2168static void
2e0fef85
JS
2169lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2170 struct lpfc_iocbq *rspiocb)
dea3101e 2171{
2e0fef85
JS
2172 struct lpfc_vport *vport = cmdiocb->vport;
2173 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2174 IOCB_t *irsp;
dea3101e
JB
2175 struct lpfc_nodelist *ndlp;
2176
dea3101e
JB
2177 /* we pass cmdiocb to state machine which needs rspiocb as well */
2178 cmdiocb->context_un.rsp_iocb = rspiocb;
2179
2180 irsp = &(rspiocb->iocb);
2181 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2e0fef85 2182 spin_lock_irq(shost->host_lock);
dea3101e 2183 ndlp->nlp_flag &= ~NLP_PRLI_SND;
9de416ac
JS
2184
2185 /* Driver supports multiple FC4 types. Counters matter. */
2186 vport->fc_prli_sent--;
2187 ndlp->fc4_prli_sent--;
2e0fef85 2188 spin_unlock_irq(shost->host_lock);
dea3101e 2189
858c9f6c
JS
2190 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2191 "PRLI cmpl: status:x%x/x%x did:x%x",
2192 irsp->ulpStatus, irsp->un.ulpWord[4],
2193 ndlp->nlp_DID);
a0f2d3ef 2194
dea3101e 2195 /* PRLI completes to NPort <nlp_DID> */
e8b62011 2196 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
a0f2d3ef 2197 "0103 PRLI completes to NPort x%06x "
e8b62011
JS
2198 "Data: x%x x%x x%x x%x\n",
2199 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
a0f2d3ef 2200 vport->num_disc_nodes, ndlp->fc4_prli_sent);
dea3101e 2201
dea3101e 2202 /* Check to see if link went down during discovery */
2e0fef85 2203 if (lpfc_els_chk_latt(vport))
dea3101e
JB
2204 goto out;
2205
2206 if (irsp->ulpStatus) {
2207 /* Check for retry */
2208 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2209 /* ELS command is being retried */
2210 goto out;
2211 }
3120046a 2212
dea3101e 2213 /* PRLI failed */
e40a02c1 2214 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3120046a
JS
2215 "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2216 "data: x%x\n",
e40a02c1 2217 ndlp->nlp_DID, irsp->ulpStatus,
3120046a
JS
2218 irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2219
dea3101e 2220 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 2221 if (lpfc_error_lost_link(irsp))
dea3101e 2222 goto out;
e47c9093 2223 else
2e0fef85 2224 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 2225 NLP_EVT_CMPL_PRLI);
dea16bda 2226 } else {
a0f2d3ef
JS
2227 /* Good status, call state machine. However, if another
2228 * PRLI is outstanding, don't call the state machine
2229 * because final disposition to Mapped or Unmapped is
2230 * completed there.
2231 */
2e0fef85 2232 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 2233 NLP_EVT_CMPL_PRLI);
dea16bda 2234 }
a0f2d3ef 2235
dea3101e
JB
2236out:
2237 lpfc_els_free_iocb(phba, cmdiocb);
2238 return;
2239}
2240
e59058c4 2241/**
3621a710 2242 * lpfc_issue_els_prli - Issue a prli iocb command for a vport
e59058c4
JS
2243 * @vport: pointer to a host virtual N_Port data structure.
2244 * @ndlp: pointer to a node-list data structure.
2245 * @retry: number of retries to the command IOCB.
2246 *
2247 * This routine issues a Process Login (PRLI) ELS command for the
2248 * @vport. The PRLI service parameters are set up in the payload of the
2249 * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2250 * is put to the IOCB completion callback func field before invoking the
2251 * routine lpfc_sli_issue_iocb() to send out PRLI command.
2252 *
2253 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2254 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2255 * will be stored into the context1 field of the IOCB for the completion
2256 * callback function to the PRLI ELS command.
2257 *
2258 * Return code
2259 * 0 - successfully issued prli iocb command for @vport
2260 * 1 - failed to issue prli iocb command for @vport
2261 **/
dea3101e 2262int
2e0fef85 2263lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
2264 uint8_t retry)
2265{
2e0fef85
JS
2266 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2267 struct lpfc_hba *phba = vport->phba;
dea3101e 2268 PRLI *npr;
a0f2d3ef 2269 struct lpfc_nvme_prli *npr_nvme;
dea3101e 2270 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2271 uint8_t *pcmd;
2272 uint16_t cmdsize;
a0f2d3ef
JS
2273 u32 local_nlp_type, elscmd;
2274
991f0c0e
DK
2275 /*
2276 * If we are in RSCN mode, the FC4 types supported from a
2277 * previous GFT_ID command may not be accurate. So, if we
2278 * are a NVME Initiator, always look for the possibility of
2279 * the remote NPort beng a NVME Target.
2280 */
2281 if (phba->sli_rev == LPFC_SLI_REV4 &&
2282 vport->fc_flag & FC_RSCN_MODE &&
2283 vport->nvmei_support)
2284 ndlp->nlp_fc4_type |= NLP_FC4_NVME;
a0f2d3ef
JS
2285 local_nlp_type = ndlp->nlp_fc4_type;
2286
9de416ac
JS
2287 /* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2288 * fields here before any of them can complete.
2289 */
2290 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2291 ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2292 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
dea16bda 2293 ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
9de416ac
JS
2294 ndlp->nvme_fb_size = 0;
2295
a0f2d3ef
JS
2296 send_next_prli:
2297 if (local_nlp_type & NLP_FC4_FCP) {
2298 /* Payload is 4 + 16 = 20 x14 bytes. */
2299 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2300 elscmd = ELS_CMD_PRLI;
2301 } else if (local_nlp_type & NLP_FC4_NVME) {
2302 /* Payload is 4 + 20 = 24 x18 bytes. */
2303 cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2304 elscmd = ELS_CMD_NVMEPRLI;
2305 } else {
2306 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2307 "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2308 ndlp->nlp_fc4_type, ndlp->nlp_DID);
2309 return 1;
2310 }
09559e81
JS
2311
2312 /* SLI3 ports don't support NVME. If this rport is a strict NVME
2313 * FC4 type, implicitly LOGO.
2314 */
2315 if (phba->sli_rev == LPFC_SLI_REV3 &&
2316 ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2317 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2318 "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2319 ndlp->nlp_type);
2320 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2321 return 1;
2322 }
2323
2e0fef85 2324 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
a0f2d3ef 2325 ndlp->nlp_DID, elscmd);
488d1469 2326 if (!elsiocb)
c9f8735b 2327 return 1;
dea3101e 2328
dea3101e
JB
2329 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2330
2331 /* For PRLI request, remainder of payload is service parameters */
a0f2d3ef 2332 memset(pcmd, 0, cmdsize);
dea3101e 2333
a0f2d3ef
JS
2334 if (local_nlp_type & NLP_FC4_FCP) {
2335 /* Remainder of payload is FCP PRLI parameter page.
2336 * Note: this data structure is defined as
2337 * BE/LE in the structure definition so no
2338 * byte swap call is made.
2339 */
2340 *((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2341 pcmd += sizeof(uint32_t);
2342 npr = (PRLI *)pcmd;
dea3101e 2343
a0f2d3ef
JS
2344 /*
2345 * If our firmware version is 3.20 or later,
2346 * set the following bits for FC-TAPE support.
2347 */
2348 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2349 npr->ConfmComplAllowed = 1;
2350 npr->Retry = 1;
2351 npr->TaskRetryIdReq = 1;
2352 }
2353 npr->estabImagePair = 1;
2354 npr->readXferRdyDis = 1;
2355 if (vport->cfg_first_burst_size)
2356 npr->writeXferRdyDis = 1;
2357
2358 /* For FCP support */
2359 npr->prliType = PRLI_FCP_TYPE;
2360 npr->initiatorFunc = 1;
2361 elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2362
2363 /* Remove FCP type - processed. */
2364 local_nlp_type &= ~NLP_FC4_FCP;
2365 } else if (local_nlp_type & NLP_FC4_NVME) {
2366 /* Remainder of payload is NVME PRLI parameter page.
2367 * This data structure is the newer definition that
2368 * uses bf macros so a byte swap is required.
2369 */
2370 *((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2371 pcmd += sizeof(uint32_t);
2372 npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2373 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2374 bf_set(prli_estabImagePair, npr_nvme, 0); /* Should be 0 */
2375
2376 /* Only initiators request first burst. */
2377 if ((phba->cfg_nvme_enable_fb) &&
2378 !phba->nvmet_support)
2379 bf_set(prli_fba, npr_nvme, 1);
2380
8c258641
JS
2381 if (phba->nvmet_support) {
2382 bf_set(prli_tgt, npr_nvme, 1);
2383 bf_set(prli_disc, npr_nvme, 1);
8c258641
JS
2384 } else {
2385 bf_set(prli_init, npr_nvme, 1);
a5ff0681 2386 bf_set(prli_conf, npr_nvme, 1);
8c258641 2387 }
a5ff0681 2388
a0f2d3ef
JS
2389 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2390 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2391 elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2392
2393 /* Remove NVME type - processed. */
2394 local_nlp_type &= ~NLP_FC4_NVME;
2395 }
dea3101e 2396
858c9f6c
JS
2397 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2398 "Issue PRLI: did:x%x",
2399 ndlp->nlp_DID, 0, 0);
2400
dea3101e
JB
2401 phba->fc_stat.elsXmitPRLI++;
2402 elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2e0fef85 2403 spin_lock_irq(shost->host_lock);
dea3101e 2404 ndlp->nlp_flag |= NLP_PRLI_SND;
9de416ac
JS
2405
2406 /* The vport counters are used for lpfc_scan_finished, but
2407 * the ndlp is used to track outstanding PRLIs for different
2408 * FC4 types.
2409 */
2410 vport->fc_prli_sent++;
2411 ndlp->fc4_prli_sent++;
2e0fef85 2412 spin_unlock_irq(shost->host_lock);
3772a991
JS
2413 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2414 IOCB_ERROR) {
2e0fef85 2415 spin_lock_irq(shost->host_lock);
dea3101e 2416 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2e0fef85 2417 spin_unlock_irq(shost->host_lock);
dea3101e 2418 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2419 return 1;
dea3101e 2420 }
a0f2d3ef 2421
a0f2d3ef
JS
2422
2423 /* The driver supports 2 FC4 types. Make sure
2424 * a PRLI is issued for all types before exiting.
2425 */
09559e81
JS
2426 if (phba->sli_rev == LPFC_SLI_REV4 &&
2427 local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
a0f2d3ef
JS
2428 goto send_next_prli;
2429
c9f8735b 2430 return 0;
dea3101e
JB
2431}
2432
90160e01 2433/**
3621a710 2434 * lpfc_rscn_disc - Perform rscn discovery for a vport
90160e01
JS
2435 * @vport: pointer to a host virtual N_Port data structure.
2436 *
2437 * This routine performs Registration State Change Notification (RSCN)
2438 * discovery for a @vport. If the @vport's node port recovery count is not
2439 * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2440 * the nodes that need recovery. If none of the PLOGI were needed through
2441 * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2442 * invoked to check and handle possible more RSCN came in during the period
2443 * of processing the current ones.
2444 **/
2445static void
2446lpfc_rscn_disc(struct lpfc_vport *vport)
2447{
2448 lpfc_can_disctmo(vport);
2449
2450 /* RSCN discovery */
2451 /* go thru NPR nodes and issue ELS PLOGIs */
2452 if (vport->fc_npr_cnt)
2453 if (lpfc_els_disc_plogi(vport))
2454 return;
2455
2456 lpfc_end_rscn(vport);
2457}
2458
2459/**
3621a710 2460 * lpfc_adisc_done - Complete the adisc phase of discovery
90160e01
JS
2461 * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2462 *
2463 * This function is called when the final ADISC is completed during discovery.
2464 * This function handles clearing link attention or issuing reg_vpi depending
2465 * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2466 * discovery.
2467 * This function is called with no locks held.
2468 **/
2469static void
2470lpfc_adisc_done(struct lpfc_vport *vport)
2471{
2472 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2473 struct lpfc_hba *phba = vport->phba;
2474
2475 /*
2476 * For NPIV, cmpl_reg_vpi will set port_state to READY,
2477 * and continue discovery.
2478 */
2479 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6fb120a7
JS
2480 !(vport->fc_flag & FC_RSCN_MODE) &&
2481 (phba->sli_rev < LPFC_SLI_REV4)) {
d454c91f
JS
2482 /* The ADISCs are complete. Doesn't matter if they
2483 * succeeded or failed because the ADISC completion
2484 * routine guarantees to call the state machine and
2485 * the RPI is either unregistered (failed ADISC response)
2486 * or the RPI is still valid and the node is marked
2487 * mapped for a target. The exchanges should be in the
2488 * correct state. This code is specific to SLI3.
2489 */
2490 lpfc_issue_clear_la(phba, vport);
90160e01
JS
2491 lpfc_issue_reg_vpi(phba, vport);
2492 return;
2493 }
2494 /*
2495 * For SLI2, we need to set port_state to READY
2496 * and continue discovery.
2497 */
2498 if (vport->port_state < LPFC_VPORT_READY) {
2499 /* If we get here, there is nothing to ADISC */
85c0f177 2500 lpfc_issue_clear_la(phba, vport);
90160e01
JS
2501 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2502 vport->num_disc_nodes = 0;
2503 /* go thru NPR list, issue ELS PLOGIs */
2504 if (vport->fc_npr_cnt)
2505 lpfc_els_disc_plogi(vport);
2506 if (!vport->num_disc_nodes) {
2507 spin_lock_irq(shost->host_lock);
2508 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2509 spin_unlock_irq(shost->host_lock);
2510 lpfc_can_disctmo(vport);
2511 lpfc_end_rscn(vport);
2512 }
2513 }
2514 vport->port_state = LPFC_VPORT_READY;
2515 } else
2516 lpfc_rscn_disc(vport);
2517}
2518
e59058c4 2519/**
3621a710 2520 * lpfc_more_adisc - Issue more adisc as needed
e59058c4
JS
2521 * @vport: pointer to a host virtual N_Port data structure.
2522 *
2523 * This routine determines whether there are more ndlps on a @vport
2524 * node list need to have Address Discover (ADISC) issued. If so, it will
2525 * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2526 * remaining nodes which need to have ADISC sent.
2527 **/
0ff10d46 2528void
2e0fef85 2529lpfc_more_adisc(struct lpfc_vport *vport)
dea3101e 2530{
2e0fef85
JS
2531 if (vport->num_disc_nodes)
2532 vport->num_disc_nodes--;
dea3101e 2533 /* Continue discovery with <num_disc_nodes> ADISCs to go */
e8b62011
JS
2534 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2535 "0210 Continue discovery with %d ADISCs to go "
2536 "Data: x%x x%x x%x\n",
2537 vport->num_disc_nodes, vport->fc_adisc_cnt,
2538 vport->fc_flag, vport->port_state);
dea3101e 2539 /* Check to see if there are more ADISCs to be sent */
2e0fef85
JS
2540 if (vport->fc_flag & FC_NLP_MORE) {
2541 lpfc_set_disctmo(vport);
2542 /* go thru NPR nodes and issue any remaining ELS ADISCs */
eb016566 2543 lpfc_els_disc_adisc(vport);
dea3101e 2544 }
90160e01
JS
2545 if (!vport->num_disc_nodes)
2546 lpfc_adisc_done(vport);
dea3101e
JB
2547 return;
2548}
2549
e59058c4 2550/**
3621a710 2551 * lpfc_cmpl_els_adisc - Completion callback function for adisc
e59058c4
JS
2552 * @phba: pointer to lpfc hba data structure.
2553 * @cmdiocb: pointer to lpfc command iocb data structure.
2554 * @rspiocb: pointer to lpfc response iocb data structure.
2555 *
2556 * This routine is the completion function for issuing the Address Discover
2557 * (ADISC) command. It first checks to see whether link went down during
2558 * the discovery process. If so, the node will be marked as node port
2559 * recovery for issuing discover IOCB by the link attention handler and
2560 * exit. Otherwise, the response status is checked. If error was reported
2561 * in the response status, the ADISC command shall be retried by invoking
2562 * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2563 * the response status, the state machine is invoked to set transition
2564 * with respect to NLP_EVT_CMPL_ADISC event.
2565 **/
dea3101e 2566static void
2e0fef85
JS
2567lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2568 struct lpfc_iocbq *rspiocb)
dea3101e 2569{
2e0fef85
JS
2570 struct lpfc_vport *vport = cmdiocb->vport;
2571 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2572 IOCB_t *irsp;
dea3101e 2573 struct lpfc_nodelist *ndlp;
2e0fef85 2574 int disc;
dea3101e
JB
2575
2576 /* we pass cmdiocb to state machine which needs rspiocb as well */
2577 cmdiocb->context_un.rsp_iocb = rspiocb;
2578
2579 irsp = &(rspiocb->iocb);
2580 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
dea3101e 2581
858c9f6c
JS
2582 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2583 "ADISC cmpl: status:x%x/x%x did:x%x",
2584 irsp->ulpStatus, irsp->un.ulpWord[4],
2585 ndlp->nlp_DID);
2586
dea3101e
JB
2587 /* Since ndlp can be freed in the disc state machine, note if this node
2588 * is being used during discovery.
2589 */
2e0fef85 2590 spin_lock_irq(shost->host_lock);
dea3101e 2591 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
c9f8735b 2592 ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2e0fef85 2593 spin_unlock_irq(shost->host_lock);
dea3101e 2594 /* ADISC completes to NPort <nlp_DID> */
e8b62011
JS
2595 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2596 "0104 ADISC completes to NPort x%x "
2597 "Data: x%x x%x x%x x%x x%x\n",
2598 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2599 irsp->ulpTimeout, disc, vport->num_disc_nodes);
dea3101e 2600 /* Check to see if link went down during discovery */
2e0fef85
JS
2601 if (lpfc_els_chk_latt(vport)) {
2602 spin_lock_irq(shost->host_lock);
dea3101e 2603 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 2604 spin_unlock_irq(shost->host_lock);
dea3101e
JB
2605 goto out;
2606 }
2607
2608 if (irsp->ulpStatus) {
2609 /* Check for retry */
2610 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2611 /* ELS command is being retried */
2612 if (disc) {
2e0fef85 2613 spin_lock_irq(shost->host_lock);
dea3101e 2614 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85
JS
2615 spin_unlock_irq(shost->host_lock);
2616 lpfc_set_disctmo(vport);
dea3101e
JB
2617 }
2618 goto out;
2619 }
2620 /* ADISC failed */
e40a02c1
JS
2621 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2622 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2623 ndlp->nlp_DID, irsp->ulpStatus,
2624 irsp->un.ulpWord[4]);
dea3101e 2625 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 2626 if (!lpfc_error_lost_link(irsp))
2e0fef85 2627 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
858c9f6c 2628 NLP_EVT_CMPL_ADISC);
e47c9093 2629 } else
dea3101e 2630 /* Good status, call state machine */
2e0fef85 2631 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
dea3101e 2632 NLP_EVT_CMPL_ADISC);
dea3101e 2633
90160e01
JS
2634 /* Check to see if there are more ADISCs to be sent */
2635 if (disc && vport->num_disc_nodes)
2e0fef85 2636 lpfc_more_adisc(vport);
dea3101e
JB
2637out:
2638 lpfc_els_free_iocb(phba, cmdiocb);
2639 return;
2640}
2641
e59058c4 2642/**
3621a710 2643 * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
e59058c4
JS
2644 * @vport: pointer to a virtual N_Port data structure.
2645 * @ndlp: pointer to a node-list data structure.
2646 * @retry: number of retries to the command IOCB.
2647 *
2648 * This routine issues an Address Discover (ADISC) for an @ndlp on a
2649 * @vport. It prepares the payload of the ADISC ELS command, updates the
2650 * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2651 * to issue the ADISC ELS command.
2652 *
2653 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2654 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2655 * will be stored into the context1 field of the IOCB for the completion
2656 * callback function to the ADISC ELS command.
2657 *
2658 * Return code
2659 * 0 - successfully issued adisc
2660 * 1 - failed to issue adisc
2661 **/
dea3101e 2662int
2e0fef85 2663lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
2664 uint8_t retry)
2665{
2e0fef85
JS
2666 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2667 struct lpfc_hba *phba = vport->phba;
dea3101e 2668 ADISC *ap;
dea3101e 2669 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2670 uint8_t *pcmd;
2671 uint16_t cmdsize;
2672
92d7f7b0 2673 cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2e0fef85
JS
2674 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2675 ndlp->nlp_DID, ELS_CMD_ADISC);
488d1469 2676 if (!elsiocb)
c9f8735b 2677 return 1;
dea3101e 2678
dea3101e
JB
2679 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2680
2681 /* For ADISC request, remainder of payload is service parameters */
2682 *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
92d7f7b0 2683 pcmd += sizeof(uint32_t);
dea3101e
JB
2684
2685 /* Fill in ADISC payload */
2686 ap = (ADISC *) pcmd;
2687 ap->hardAL_PA = phba->fc_pref_ALPA;
92d7f7b0
JS
2688 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2689 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 2690 ap->DID = be32_to_cpu(vport->fc_myDID);
dea3101e 2691
858c9f6c
JS
2692 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2693 "Issue ADISC: did:x%x",
2694 ndlp->nlp_DID, 0, 0);
2695
dea3101e
JB
2696 phba->fc_stat.elsXmitADISC++;
2697 elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2e0fef85 2698 spin_lock_irq(shost->host_lock);
dea3101e 2699 ndlp->nlp_flag |= NLP_ADISC_SND;
2e0fef85 2700 spin_unlock_irq(shost->host_lock);
3772a991
JS
2701 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2702 IOCB_ERROR) {
2e0fef85 2703 spin_lock_irq(shost->host_lock);
dea3101e 2704 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2e0fef85 2705 spin_unlock_irq(shost->host_lock);
dea3101e 2706 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2707 return 1;
dea3101e 2708 }
c9f8735b 2709 return 0;
dea3101e
JB
2710}
2711
e59058c4 2712/**
3621a710 2713 * lpfc_cmpl_els_logo - Completion callback function for logo
e59058c4
JS
2714 * @phba: pointer to lpfc hba data structure.
2715 * @cmdiocb: pointer to lpfc command iocb data structure.
2716 * @rspiocb: pointer to lpfc response iocb data structure.
2717 *
2718 * This routine is the completion function for issuing the ELS Logout (LOGO)
2719 * command. If no error status was reported from the LOGO response, the
2720 * state machine of the associated ndlp shall be invoked for transition with
2721 * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2722 * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2723 **/
dea3101e 2724static void
2e0fef85
JS
2725lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2726 struct lpfc_iocbq *rspiocb)
dea3101e 2727{
2e0fef85
JS
2728 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2729 struct lpfc_vport *vport = ndlp->vport;
2730 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2731 IOCB_t *irsp;
92494144 2732 struct lpfcMboxq *mbox;
086a345f
JS
2733 unsigned long flags;
2734 uint32_t skip_recovery = 0;
dea3101e 2735
dea3101e
JB
2736 /* we pass cmdiocb to state machine which needs rspiocb as well */
2737 cmdiocb->context_un.rsp_iocb = rspiocb;
2738
2739 irsp = &(rspiocb->iocb);
2e0fef85 2740 spin_lock_irq(shost->host_lock);
dea3101e 2741 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2e0fef85 2742 spin_unlock_irq(shost->host_lock);
dea3101e 2743
858c9f6c
JS
2744 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2745 "LOGO cmpl: status:x%x/x%x did:x%x",
2746 irsp->ulpStatus, irsp->un.ulpWord[4],
2747 ndlp->nlp_DID);
086a345f 2748
dea3101e 2749 /* LOGO completes to NPort <nlp_DID> */
e8b62011
JS
2750 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2751 "0105 LOGO completes to NPort x%x "
2752 "Data: x%x x%x x%x x%x\n",
2753 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2754 irsp->ulpTimeout, vport->num_disc_nodes);
086a345f
JS
2755
2756 if (lpfc_els_chk_latt(vport)) {
2757 skip_recovery = 1;
dea3101e 2758 goto out;
086a345f 2759 }
dea3101e 2760
086a345f 2761 /* Check to see if link went down during discovery */
92d7f7b0
JS
2762 if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2763 /* NLP_EVT_DEVICE_RM should unregister the RPI
2764 * which should abort all outstanding IOs.
2765 */
2766 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2767 NLP_EVT_DEVICE_RM);
086a345f 2768 skip_recovery = 1;
92d7f7b0
JS
2769 goto out;
2770 }
2771
30e196ca
JS
2772 /* The LOGO will not be retried on failure. A LOGO was
2773 * issued to the remote rport and a ACC or RJT or no Answer are
2774 * all acceptable. Note the failure and move forward with
2775 * discovery. The PLOGI will retry.
2776 */
dea3101e 2777 if (irsp->ulpStatus) {
dea3101e 2778 /* LOGO failed */
e40a02c1 2779 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
30e196ca 2780 "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
e40a02c1
JS
2781 ndlp->nlp_DID, irsp->ulpStatus,
2782 irsp->un.ulpWord[4]);
dea3101e 2783 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
086a345f
JS
2784 if (lpfc_error_lost_link(irsp)) {
2785 skip_recovery = 1;
dea3101e 2786 goto out;
086a345f
JS
2787 }
2788 }
2789
2790 /* Call state machine. This will unregister the rpi if needed. */
2791 lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2792
dea3101e
JB
2793out:
2794 lpfc_els_free_iocb(phba, cmdiocb);
92494144
JS
2795 /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2796 if ((vport->fc_flag & FC_PT2PT) &&
2797 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2798 phba->pport->fc_myDID = 0;
a0f2d3ef 2799
01649561 2800 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
8c258641 2801 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
d613b6a7
JS
2802 if (phba->nvmet_support)
2803 lpfc_nvmet_update_targetport(phba);
2804 else
8c258641 2805 lpfc_nvme_update_localport(phba->pport);
8c258641 2806 }
a0f2d3ef 2807
92494144
JS
2808 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2809 if (mbox) {
2810 lpfc_config_link(phba, mbox);
2811 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2812 mbox->vport = vport;
2813 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2814 MBX_NOT_FINISHED) {
2815 mempool_free(mbox, phba->mbox_mem_pool);
086a345f 2816 skip_recovery = 1;
92494144
JS
2817 }
2818 }
2819 }
086a345f
JS
2820
2821 /*
2822 * If the node is a target, the handling attempts to recover the port.
2823 * For any other port type, the rpi is unregistered as an implicit
2824 * LOGO.
2825 */
30e196ca
JS
2826 if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2827 skip_recovery == 0) {
086a345f
JS
2828 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2829 spin_lock_irqsave(shost->host_lock, flags);
2830 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2831 spin_unlock_irqrestore(shost->host_lock, flags);
2832
2833 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2834 "3187 LOGO completes to NPort x%x: Start "
2835 "Recovery Data: x%x x%x x%x x%x\n",
2836 ndlp->nlp_DID, irsp->ulpStatus,
2837 irsp->un.ulpWord[4], irsp->ulpTimeout,
2838 vport->num_disc_nodes);
2839 lpfc_disc_start(vport);
2840 }
dea3101e
JB
2841 return;
2842}
2843
e59058c4 2844/**
3621a710 2845 * lpfc_issue_els_logo - Issue a logo to an node on a vport
e59058c4
JS
2846 * @vport: pointer to a virtual N_Port data structure.
2847 * @ndlp: pointer to a node-list data structure.
2848 * @retry: number of retries to the command IOCB.
2849 *
2850 * This routine constructs and issues an ELS Logout (LOGO) iocb command
2851 * to a remote node, referred by an @ndlp on a @vport. It constructs the
2852 * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2853 * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2854 *
2855 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2856 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2857 * will be stored into the context1 field of the IOCB for the completion
2858 * callback function to the LOGO ELS command.
2859 *
30e196ca
JS
2860 * Callers of this routine are expected to unregister the RPI first
2861 *
e59058c4
JS
2862 * Return code
2863 * 0 - successfully issued logo
2864 * 1 - failed to issue logo
2865 **/
dea3101e 2866int
2e0fef85 2867lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
2868 uint8_t retry)
2869{
2e0fef85
JS
2870 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2871 struct lpfc_hba *phba = vport->phba;
dea3101e 2872 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2873 uint8_t *pcmd;
2874 uint16_t cmdsize;
92d7f7b0 2875 int rc;
dea3101e 2876
98c9ea5c
JS
2877 spin_lock_irq(shost->host_lock);
2878 if (ndlp->nlp_flag & NLP_LOGO_SND) {
2879 spin_unlock_irq(shost->host_lock);
2880 return 0;
2881 }
2882 spin_unlock_irq(shost->host_lock);
2883
92d7f7b0 2884 cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2e0fef85
JS
2885 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2886 ndlp->nlp_DID, ELS_CMD_LOGO);
488d1469 2887 if (!elsiocb)
c9f8735b 2888 return 1;
dea3101e 2889
dea3101e
JB
2890 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2891 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
92d7f7b0 2892 pcmd += sizeof(uint32_t);
dea3101e
JB
2893
2894 /* Fill in LOGO payload */
2e0fef85 2895 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
92d7f7b0
JS
2896 pcmd += sizeof(uint32_t);
2897 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
dea3101e 2898
858c9f6c
JS
2899 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2900 "Issue LOGO: did:x%x",
2901 ndlp->nlp_DID, 0, 0);
2902
dea3101e
JB
2903 phba->fc_stat.elsXmitLOGO++;
2904 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2e0fef85 2905 spin_lock_irq(shost->host_lock);
dea3101e 2906 ndlp->nlp_flag |= NLP_LOGO_SND;
086a345f 2907 ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2e0fef85 2908 spin_unlock_irq(shost->host_lock);
3772a991 2909 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
92d7f7b0 2910 if (rc == IOCB_ERROR) {
2e0fef85 2911 spin_lock_irq(shost->host_lock);
dea3101e 2912 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2e0fef85 2913 spin_unlock_irq(shost->host_lock);
dea3101e 2914 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2915 return 1;
dea3101e 2916 }
30e196ca
JS
2917
2918 spin_lock_irq(shost->host_lock);
2919 ndlp->nlp_prev_state = ndlp->nlp_state;
2920 spin_unlock_irq(shost->host_lock);
2921 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
c9f8735b 2922 return 0;
dea3101e
JB
2923}
2924
e59058c4 2925/**
3621a710 2926 * lpfc_cmpl_els_cmd - Completion callback function for generic els command
e59058c4
JS
2927 * @phba: pointer to lpfc hba data structure.
2928 * @cmdiocb: pointer to lpfc command iocb data structure.
2929 * @rspiocb: pointer to lpfc response iocb data structure.
2930 *
2931 * This routine is a generic completion callback function for ELS commands.
2932 * Specifically, it is the callback function which does not need to perform
2933 * any command specific operations. It is currently used by the ELS command
2934 * issuing routines for the ELS State Change Request (SCR),
2935 * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2936 * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2937 * certain debug loggings, this callback function simply invokes the
2938 * lpfc_els_chk_latt() routine to check whether link went down during the
2939 * discovery process.
2940 **/
dea3101e 2941static void
2e0fef85
JS
2942lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2943 struct lpfc_iocbq *rspiocb)
dea3101e 2944{
2e0fef85 2945 struct lpfc_vport *vport = cmdiocb->vport;
dea3101e
JB
2946 IOCB_t *irsp;
2947
2948 irsp = &rspiocb->iocb;
2949
858c9f6c
JS
2950 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2951 "ELS cmd cmpl: status:x%x/x%x did:x%x",
2952 irsp->ulpStatus, irsp->un.ulpWord[4],
2953 irsp->un.elsreq64.remoteID);
dea3101e 2954 /* ELS cmd tag <ulpIoTag> completes */
e8b62011
JS
2955 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2956 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2957 irsp->ulpIoTag, irsp->ulpStatus,
2958 irsp->un.ulpWord[4], irsp->ulpTimeout);
dea3101e 2959 /* Check to see if link went down during discovery */
2e0fef85 2960 lpfc_els_chk_latt(vport);
dea3101e
JB
2961 lpfc_els_free_iocb(phba, cmdiocb);
2962 return;
2963}
2964
e59058c4 2965/**
3621a710 2966 * lpfc_issue_els_scr - Issue a scr to an node on a vport
e59058c4
JS
2967 * @vport: pointer to a host virtual N_Port data structure.
2968 * @nportid: N_Port identifier to the remote node.
2969 * @retry: number of retries to the command IOCB.
2970 *
2971 * This routine issues a State Change Request (SCR) to a fabric node
2972 * on a @vport. The remote node @nportid is passed into the function. It
2973 * first search the @vport node list to find the matching ndlp. If no such
2974 * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2975 * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2976 * routine is invoked to send the SCR IOCB.
2977 *
2978 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2979 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2980 * will be stored into the context1 field of the IOCB for the completion
2981 * callback function to the SCR ELS command.
2982 *
2983 * Return code
2984 * 0 - Successfully issued scr command
2985 * 1 - Failed to issue scr command
2986 **/
dea3101e 2987int
2e0fef85 2988lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
dea3101e 2989{
2e0fef85 2990 struct lpfc_hba *phba = vport->phba;
dea3101e 2991 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2992 uint8_t *pcmd;
2993 uint16_t cmdsize;
2994 struct lpfc_nodelist *ndlp;
2995
92d7f7b0 2996 cmdsize = (sizeof(uint32_t) + sizeof(SCR));
dea3101e 2997
e47c9093
JS
2998 ndlp = lpfc_findnode_did(vport, nportid);
2999 if (!ndlp) {
9d3d340d 3000 ndlp = lpfc_nlp_init(vport, nportid);
e47c9093
JS
3001 if (!ndlp)
3002 return 1;
e47c9093
JS
3003 lpfc_enqueue_node(vport, ndlp);
3004 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3005 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3006 if (!ndlp)
3007 return 1;
3008 }
2e0fef85
JS
3009
3010 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3011 ndlp->nlp_DID, ELS_CMD_SCR);
dea3101e 3012
488d1469 3013 if (!elsiocb) {
fa4066b6
JS
3014 /* This will trigger the release of the node just
3015 * allocated
3016 */
329f9bc7 3017 lpfc_nlp_put(ndlp);
c9f8735b 3018 return 1;
dea3101e
JB
3019 }
3020
dea3101e
JB
3021 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3022
3023 *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
92d7f7b0 3024 pcmd += sizeof(uint32_t);
dea3101e
JB
3025
3026 /* For SCR, remainder of payload is SCR parameter page */
92d7f7b0 3027 memset(pcmd, 0, sizeof(SCR));
dea3101e
JB
3028 ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3029
858c9f6c
JS
3030 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3031 "Issue SCR: did:x%x",
3032 ndlp->nlp_DID, 0, 0);
3033
dea3101e
JB
3034 phba->fc_stat.elsXmitSCR++;
3035 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3772a991
JS
3036 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3037 IOCB_ERROR) {
fa4066b6
JS
3038 /* The additional lpfc_nlp_put will cause the following
3039 * lpfc_els_free_iocb routine to trigger the rlease of
3040 * the node.
3041 */
329f9bc7 3042 lpfc_nlp_put(ndlp);
dea3101e 3043 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 3044 return 1;
dea3101e 3045 }
fa4066b6
JS
3046 /* This will cause the callback-function lpfc_cmpl_els_cmd to
3047 * trigger the release of node.
3048 */
b7e50c53
JS
3049 if (!(vport->fc_flag & FC_PT2PT))
3050 lpfc_nlp_put(ndlp);
c9f8735b 3051 return 0;
dea3101e
JB
3052}
3053
e59058c4 3054/**
3621a710 3055 * lpfc_issue_els_farpr - Issue a farp to an node on a vport
e59058c4
JS
3056 * @vport: pointer to a host virtual N_Port data structure.
3057 * @nportid: N_Port identifier to the remote node.
3058 * @retry: number of retries to the command IOCB.
3059 *
3060 * This routine issues a Fibre Channel Address Resolution Response
3061 * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3062 * is passed into the function. It first search the @vport node list to find
3063 * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3064 * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3065 * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3066 *
3067 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3068 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3069 * will be stored into the context1 field of the IOCB for the completion
3070 * callback function to the PARPR ELS command.
3071 *
3072 * Return code
3073 * 0 - Successfully issued farpr command
3074 * 1 - Failed to issue farpr command
3075 **/
dea3101e 3076static int
2e0fef85 3077lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
dea3101e 3078{
2e0fef85 3079 struct lpfc_hba *phba = vport->phba;
dea3101e 3080 struct lpfc_iocbq *elsiocb;
dea3101e
JB
3081 FARP *fp;
3082 uint8_t *pcmd;
3083 uint32_t *lp;
3084 uint16_t cmdsize;
3085 struct lpfc_nodelist *ondlp;
3086 struct lpfc_nodelist *ndlp;
3087
92d7f7b0 3088 cmdsize = (sizeof(uint32_t) + sizeof(FARP));
dea3101e 3089
e47c9093
JS
3090 ndlp = lpfc_findnode_did(vport, nportid);
3091 if (!ndlp) {
9d3d340d 3092 ndlp = lpfc_nlp_init(vport, nportid);
e47c9093
JS
3093 if (!ndlp)
3094 return 1;
e47c9093
JS
3095 lpfc_enqueue_node(vport, ndlp);
3096 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3097 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3098 if (!ndlp)
3099 return 1;
3100 }
2e0fef85
JS
3101
3102 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3103 ndlp->nlp_DID, ELS_CMD_RNID);
488d1469 3104 if (!elsiocb) {
fa4066b6
JS
3105 /* This will trigger the release of the node just
3106 * allocated
3107 */
329f9bc7 3108 lpfc_nlp_put(ndlp);
c9f8735b 3109 return 1;
dea3101e
JB
3110 }
3111
dea3101e
JB
3112 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3113
3114 *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
92d7f7b0 3115 pcmd += sizeof(uint32_t);
dea3101e
JB
3116
3117 /* Fill in FARPR payload */
3118 fp = (FARP *) (pcmd);
92d7f7b0 3119 memset(fp, 0, sizeof(FARP));
dea3101e
JB
3120 lp = (uint32_t *) pcmd;
3121 *lp++ = be32_to_cpu(nportid);
2e0fef85 3122 *lp++ = be32_to_cpu(vport->fc_myDID);
dea3101e
JB
3123 fp->Rflags = 0;
3124 fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3125
92d7f7b0
JS
3126 memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3127 memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 3128 ondlp = lpfc_findnode_did(vport, nportid);
e47c9093 3129 if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
dea3101e 3130 memcpy(&fp->OportName, &ondlp->nlp_portname,
92d7f7b0 3131 sizeof(struct lpfc_name));
dea3101e 3132 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
92d7f7b0 3133 sizeof(struct lpfc_name));
dea3101e
JB
3134 }
3135
858c9f6c
JS
3136 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3137 "Issue FARPR: did:x%x",
3138 ndlp->nlp_DID, 0, 0);
3139
dea3101e
JB
3140 phba->fc_stat.elsXmitFARPR++;
3141 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3772a991
JS
3142 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3143 IOCB_ERROR) {
fa4066b6
JS
3144 /* The additional lpfc_nlp_put will cause the following
3145 * lpfc_els_free_iocb routine to trigger the release of
3146 * the node.
3147 */
329f9bc7 3148 lpfc_nlp_put(ndlp);
dea3101e 3149 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 3150 return 1;
dea3101e 3151 }
fa4066b6
JS
3152 /* This will cause the callback-function lpfc_cmpl_els_cmd to
3153 * trigger the release of the node.
3154 */
329f9bc7 3155 lpfc_nlp_put(ndlp);
c9f8735b 3156 return 0;
dea3101e
JB
3157}
3158
e59058c4 3159/**
3621a710 3160 * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
e59058c4
JS
3161 * @vport: pointer to a host virtual N_Port data structure.
3162 * @nlp: pointer to a node-list data structure.
3163 *
3164 * This routine cancels the timer with a delayed IOCB-command retry for
3165 * a @vport's @ndlp. It stops the timer for the delayed function retrial and
3166 * removes the ELS retry event if it presents. In addition, if the
3167 * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
3168 * commands are sent for the @vport's nodes that require issuing discovery
3169 * ADISC.
3170 **/
fdcebe28 3171void
2e0fef85 3172lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
fdcebe28 3173{
2e0fef85 3174 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
e47c9093 3175 struct lpfc_work_evt *evtp;
2e0fef85 3176
0d2b6b83
JS
3177 if (!(nlp->nlp_flag & NLP_DELAY_TMO))
3178 return;
2e0fef85 3179 spin_lock_irq(shost->host_lock);
fdcebe28 3180 nlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 3181 spin_unlock_irq(shost->host_lock);
fdcebe28
JS
3182 del_timer_sync(&nlp->nlp_delayfunc);
3183 nlp->nlp_last_elscmd = 0;
e47c9093 3184 if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
fdcebe28 3185 list_del_init(&nlp->els_retry_evt.evt_listp);
e47c9093
JS
3186 /* Decrement nlp reference count held for the delayed retry */
3187 evtp = &nlp->els_retry_evt;
3188 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
3189 }
fdcebe28 3190 if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2e0fef85 3191 spin_lock_irq(shost->host_lock);
fdcebe28 3192 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2e0fef85
JS
3193 spin_unlock_irq(shost->host_lock);
3194 if (vport->num_disc_nodes) {
0d2b6b83
JS
3195 if (vport->port_state < LPFC_VPORT_READY) {
3196 /* Check if there are more ADISCs to be sent */
3197 lpfc_more_adisc(vport);
0d2b6b83
JS
3198 } else {
3199 /* Check if there are more PLOGIs to be sent */
3200 lpfc_more_plogi(vport);
90160e01
JS
3201 if (vport->num_disc_nodes == 0) {
3202 spin_lock_irq(shost->host_lock);
3203 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3204 spin_unlock_irq(shost->host_lock);
3205 lpfc_can_disctmo(vport);
3206 lpfc_end_rscn(vport);
3207 }
fdcebe28
JS
3208 }
3209 }
3210 }
3211 return;
3212}
3213
e59058c4 3214/**
3621a710 3215 * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
e59058c4
JS
3216 * @ptr: holder for the pointer to the timer function associated data (ndlp).
3217 *
3218 * This routine is invoked by the ndlp delayed-function timer to check
3219 * whether there is any pending ELS retry event(s) with the node. If not, it
3220 * simply returns. Otherwise, if there is at least one ELS delayed event, it
3221 * adds the delayed events to the HBA work list and invokes the
3222 * lpfc_worker_wake_up() routine to wake up worker thread to process the
3223 * event. Note that lpfc_nlp_get() is called before posting the event to
3224 * the work list to hold reference count of ndlp so that it guarantees the
3225 * reference to ndlp will still be available when the worker thread gets
3226 * to the event associated with the ndlp.
3227 **/
dea3101e 3228void
f22eb4d3 3229lpfc_els_retry_delay(struct timer_list *t)
dea3101e 3230{
f22eb4d3 3231 struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
2e0fef85 3232 struct lpfc_vport *vport = ndlp->vport;
2e0fef85 3233 struct lpfc_hba *phba = vport->phba;
92d7f7b0 3234 unsigned long flags;
2e0fef85 3235 struct lpfc_work_evt *evtp = &ndlp->els_retry_evt;
dea3101e 3236
92d7f7b0 3237 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 3238 if (!list_empty(&evtp->evt_listp)) {
92d7f7b0 3239 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e
JB
3240 return;
3241 }
3242
fa4066b6
JS
3243 /* We need to hold the node by incrementing the reference
3244 * count until the queued work is done
3245 */
3246 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
5e9d9b82
JS
3247 if (evtp->evt_arg1) {
3248 evtp->evt = LPFC_EVT_ELS_RETRY;
3249 list_add_tail(&evtp->evt_listp, &phba->work_list);
92d7f7b0 3250 lpfc_worker_wake_up(phba);
5e9d9b82 3251 }
92d7f7b0 3252 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e
JB
3253 return;
3254}
3255
e59058c4 3256/**
3621a710 3257 * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
e59058c4
JS
3258 * @ndlp: pointer to a node-list data structure.
3259 *
3260 * This routine is the worker-thread handler for processing the @ndlp delayed
3261 * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3262 * the last ELS command from the associated ndlp and invokes the proper ELS
3263 * function according to the delayed ELS command to retry the command.
3264 **/
dea3101e
JB
3265void
3266lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3267{
2e0fef85
JS
3268 struct lpfc_vport *vport = ndlp->vport;
3269 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
eb016566 3270 uint32_t cmd, retry;
dea3101e 3271
2e0fef85 3272 spin_lock_irq(shost->host_lock);
5024ab17
JW
3273 cmd = ndlp->nlp_last_elscmd;
3274 ndlp->nlp_last_elscmd = 0;
dea3101e
JB
3275
3276 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2e0fef85 3277 spin_unlock_irq(shost->host_lock);
dea3101e
JB
3278 return;
3279 }
3280
3281 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 3282 spin_unlock_irq(shost->host_lock);
1a169689
JS
3283 /*
3284 * If a discovery event readded nlp_delayfunc after timer
3285 * firing and before processing the timer, cancel the
3286 * nlp_delayfunc.
3287 */
3288 del_timer_sync(&ndlp->nlp_delayfunc);
dea3101e 3289 retry = ndlp->nlp_retry;
4d9ab994 3290 ndlp->nlp_retry = 0;
dea3101e
JB
3291
3292 switch (cmd) {
3293 case ELS_CMD_FLOGI:
2e0fef85 3294 lpfc_issue_els_flogi(vport, ndlp, retry);
dea3101e
JB
3295 break;
3296 case ELS_CMD_PLOGI:
2e0fef85 3297 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
5024ab17 3298 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3299 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6ad42535 3300 }
dea3101e
JB
3301 break;
3302 case ELS_CMD_ADISC:
2e0fef85 3303 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
5024ab17 3304 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3305 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6ad42535 3306 }
dea3101e
JB
3307 break;
3308 case ELS_CMD_PRLI:
a0f2d3ef 3309 case ELS_CMD_NVMEPRLI:
2e0fef85 3310 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
5024ab17 3311 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3312 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
6ad42535 3313 }
dea3101e
JB
3314 break;
3315 case ELS_CMD_LOGO:
2e0fef85 3316 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
5024ab17 3317 ndlp->nlp_prev_state = ndlp->nlp_state;
086a345f 3318 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
6ad42535 3319 }
dea3101e 3320 break;
92d7f7b0 3321 case ELS_CMD_FDISC:
fedd3b7b
JS
3322 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3323 lpfc_issue_els_fdisc(vport, ndlp, retry);
92d7f7b0 3324 break;
dea3101e
JB
3325 }
3326 return;
3327}
3328
5cca2ab1
JS
3329/**
3330 * lpfc_link_reset - Issue link reset
3331 * @vport: pointer to a virtual N_Port data structure.
3332 *
3333 * This routine performs link reset by sending INIT_LINK mailbox command.
3334 * For SLI-3 adapter, link attention interrupt is enabled before issuing
3335 * INIT_LINK mailbox command.
3336 *
3337 * Return code
3338 * 0 - Link reset initiated successfully
3339 * 1 - Failed to initiate link reset
3340 **/
3341int
3342lpfc_link_reset(struct lpfc_vport *vport)
3343{
3344 struct lpfc_hba *phba = vport->phba;
3345 LPFC_MBOXQ_t *mbox;
3346 uint32_t control;
3347 int rc;
3348
3349 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3350 "2851 Attempt link reset\n");
3351 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3352 if (!mbox) {
3353 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3354 "2852 Failed to allocate mbox memory");
3355 return 1;
3356 }
3357
3358 /* Enable Link attention interrupts */
3359 if (phba->sli_rev <= LPFC_SLI_REV3) {
3360 spin_lock_irq(&phba->hbalock);
3361 phba->sli.sli_flag |= LPFC_PROCESS_LA;
3362 control = readl(phba->HCregaddr);
3363 control |= HC_LAINT_ENA;
3364 writel(control, phba->HCregaddr);
3365 readl(phba->HCregaddr); /* flush */
3366 spin_unlock_irq(&phba->hbalock);
3367 }
3368
3369 lpfc_init_link(phba, mbox, phba->cfg_topology,
3370 phba->cfg_link_speed);
3371 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3372 mbox->vport = vport;
3373 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3374 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
3375 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
3376 "2853 Failed to issue INIT_LINK "
3377 "mbox command, rc:x%x\n", rc);
3378 mempool_free(mbox, phba->mbox_mem_pool);
3379 return 1;
3380 }
3381
3382 return 0;
3383}
3384
e59058c4 3385/**
3621a710 3386 * lpfc_els_retry - Make retry decision on an els command iocb
e59058c4
JS
3387 * @phba: pointer to lpfc hba data structure.
3388 * @cmdiocb: pointer to lpfc command iocb data structure.
3389 * @rspiocb: pointer to lpfc response iocb data structure.
3390 *
3391 * This routine makes a retry decision on an ELS command IOCB, which has
3392 * failed. The following ELS IOCBs use this function for retrying the command
3393 * when previously issued command responsed with error status: FLOGI, PLOGI,
3394 * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3395 * returned error status, it makes the decision whether a retry shall be
3396 * issued for the command, and whether a retry shall be made immediately or
3397 * delayed. In the former case, the corresponding ELS command issuing-function
3398 * is called to retry the command. In the later case, the ELS command shall
3399 * be posted to the ndlp delayed event and delayed function timer set to the
3400 * ndlp for the delayed command issusing.
3401 *
3402 * Return code
3403 * 0 - No retry of els command is made
3404 * 1 - Immediate or delayed retry of els command is made
3405 **/
dea3101e 3406static int
2e0fef85
JS
3407lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3408 struct lpfc_iocbq *rspiocb)
dea3101e 3409{
2e0fef85
JS
3410 struct lpfc_vport *vport = cmdiocb->vport;
3411 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3412 IOCB_t *irsp = &rspiocb->iocb;
3413 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3414 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
dea3101e
JB
3415 uint32_t *elscmd;
3416 struct ls_rjt stat;
2e0fef85 3417 int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
98c9ea5c 3418 int logerr = 0;
2e0fef85 3419 uint32_t cmd = 0;
488d1469 3420 uint32_t did;
5cca2ab1 3421 int link_reset = 0, rc;
dea3101e 3422
488d1469 3423
dea3101e
JB
3424 /* Note: context2 may be 0 for internal driver abort
3425 * of delays ELS command.
3426 */
3427
3428 if (pcmd && pcmd->virt) {
3429 elscmd = (uint32_t *) (pcmd->virt);
3430 cmd = *elscmd++;
3431 }
3432
e47c9093 3433 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
488d1469
JS
3434 did = ndlp->nlp_DID;
3435 else {
3436 /* We should only hit this case for retrying PLOGI */
3437 did = irsp->un.elsreq64.remoteID;
2e0fef85 3438 ndlp = lpfc_findnode_did(vport, did);
e47c9093
JS
3439 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3440 && (cmd != ELS_CMD_PLOGI))
488d1469
JS
3441 return 1;
3442 }
3443
858c9f6c
JS
3444 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3445 "Retry ELS: wd7:x%x wd4:x%x did:x%x",
3446 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3447
dea3101e
JB
3448 switch (irsp->ulpStatus) {
3449 case IOSTAT_FCP_RSP_ERROR:
1151e3ec 3450 break;
dea3101e 3451 case IOSTAT_REMOTE_STOP:
1151e3ec
JS
3452 if (phba->sli_rev == LPFC_SLI_REV4) {
3453 /* This IO was aborted by the target, we don't
3454 * know the rxid and because we did not send the
3455 * ABTS we cannot generate and RRQ.
3456 */
3457 lpfc_set_rrq_active(phba, ndlp,
ee0f4fe1 3458 cmdiocb->sli4_lxritag, 0, 0);
1151e3ec 3459 }
dea3101e 3460 break;
dea3101e 3461 case IOSTAT_LOCAL_REJECT:
e3d2b802 3462 switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
dea3101e 3463 case IOERR_LOOP_OPEN_FAILURE:
eaf15d5b
JS
3464 if (cmd == ELS_CMD_FLOGI) {
3465 if (PCI_DEVICE_ID_HORNET ==
3466 phba->pcidev->device) {
76a95d75 3467 phba->fc_topology = LPFC_TOPOLOGY_LOOP;
eaf15d5b
JS
3468 phba->pport->fc_myDID = 0;
3469 phba->alpa_map[0] = 0;
3470 phba->alpa_map[1] = 0;
3471 }
3472 }
2e0fef85 3473 if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
92d7f7b0 3474 delay = 1000;
dea3101e
JB
3475 retry = 1;
3476 break;
3477
92d7f7b0 3478 case IOERR_ILLEGAL_COMMAND:
7f5f3d0d
JS
3479 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3480 "0124 Retry illegal cmd x%x "
3481 "retry:x%x delay:x%x\n",
3482 cmd, cmdiocb->retry, delay);
3483 retry = 1;
3484 /* All command's retry policy */
3485 maxretry = 8;
3486 if (cmdiocb->retry > 2)
3487 delay = 1000;
92d7f7b0
JS
3488 break;
3489
dea3101e 3490 case IOERR_NO_RESOURCES:
98c9ea5c 3491 logerr = 1; /* HBA out of resources */
858c9f6c
JS
3492 retry = 1;
3493 if (cmdiocb->retry > 100)
3494 delay = 100;
3495 maxretry = 250;
3496 break;
3497
3498 case IOERR_ILLEGAL_FRAME:
92d7f7b0 3499 delay = 100;
dea3101e
JB
3500 retry = 1;
3501 break;
3502
3503 case IOERR_INVALID_RPI:
5b5b36a9
JS
3504 if (cmd == ELS_CMD_PLOGI &&
3505 did == NameServer_DID) {
3506 /* Continue forever if plogi to */
3507 /* the nameserver fails */
3508 maxretry = 0;
3509 delay = 100;
3510 }
dea3101e
JB
3511 retry = 1;
3512 break;
5cca2ab1
JS
3513
3514 case IOERR_SEQUENCE_TIMEOUT:
3515 if (cmd == ELS_CMD_PLOGI &&
3516 did == NameServer_DID &&
3517 (cmdiocb->retry + 1) == maxretry) {
3518 /* Reset the Link */
3519 link_reset = 1;
3520 break;
3521 }
3522 retry = 1;
3523 delay = 100;
3524 break;
dea3101e
JB
3525 }
3526 break;
3527
3528 case IOSTAT_NPORT_RJT:
3529 case IOSTAT_FABRIC_RJT:
3530 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3531 retry = 1;
3532 break;
3533 }
3534 break;
3535
3536 case IOSTAT_NPORT_BSY:
3537 case IOSTAT_FABRIC_BSY:
98c9ea5c 3538 logerr = 1; /* Fabric / Remote NPort out of resources */
dea3101e
JB
3539 retry = 1;
3540 break;
3541
3542 case IOSTAT_LS_RJT:
3543 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3544 /* Added for Vendor specifc support
3545 * Just keep retrying for these Rsn / Exp codes
3546 */
3547 switch (stat.un.b.lsRjtRsnCode) {
3548 case LSRJT_UNABLE_TPC:
dea37e82
JS
3549 /* The driver has a VALID PLOGI but the rport has
3550 * rejected the PRLI - can't do it now. Delay
3551 * for 1 second and try again - don't care about
3552 * the explanation.
3553 */
3554 if (cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) {
3555 delay = 1000;
3556 maxretry = lpfc_max_els_tries + 1;
3557 retry = 1;
3558 break;
3559 }
3560
3561 /* Legacy bug fix code for targets with PLOGI delays. */
dea3101e
JB
3562 if (stat.un.b.lsRjtRsnCodeExp ==
3563 LSEXP_CMD_IN_PROGRESS) {
3564 if (cmd == ELS_CMD_PLOGI) {
92d7f7b0 3565 delay = 1000;
dea3101e
JB
3566 maxretry = 48;
3567 }
3568 retry = 1;
3569 break;
3570 }
ffc95493
JS
3571 if (stat.un.b.lsRjtRsnCodeExp ==
3572 LSEXP_CANT_GIVE_DATA) {
3573 if (cmd == ELS_CMD_PLOGI) {
3574 delay = 1000;
3575 maxretry = 48;
3576 }
3577 retry = 1;
3578 break;
3579 }
dea37e82 3580 if (cmd == ELS_CMD_PLOGI) {
92d7f7b0 3581 delay = 1000;
dea3101e
JB
3582 maxretry = lpfc_max_els_tries + 1;
3583 retry = 1;
3584 break;
3585 }
92d7f7b0
JS
3586 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3587 (cmd == ELS_CMD_FDISC) &&
3588 (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
e8b62011
JS
3589 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3590 "0125 FDISC Failed (x%x). "
3591 "Fabric out of resources\n",
3592 stat.un.lsRjtError);
92d7f7b0
JS
3593 lpfc_vport_set_state(vport,
3594 FC_VPORT_NO_FABRIC_RSCS);
3595 }
dea3101e
JB
3596 break;
3597
3598 case LSRJT_LOGICAL_BSY:
858c9f6c 3599 if ((cmd == ELS_CMD_PLOGI) ||
a0f2d3ef
JS
3600 (cmd == ELS_CMD_PRLI) ||
3601 (cmd == ELS_CMD_NVMEPRLI)) {
92d7f7b0 3602 delay = 1000;
dea3101e 3603 maxretry = 48;
92d7f7b0 3604 } else if (cmd == ELS_CMD_FDISC) {
51ef4c26
JS
3605 /* FDISC retry policy */
3606 maxretry = 48;
3607 if (cmdiocb->retry >= 32)
3608 delay = 1000;
dea3101e
JB
3609 }
3610 retry = 1;
3611 break;
92d7f7b0
JS
3612
3613 case LSRJT_LOGICAL_ERR:
7f5f3d0d
JS
3614 /* There are some cases where switches return this
3615 * error when they are not ready and should be returning
3616 * Logical Busy. We should delay every time.
3617 */
3618 if (cmd == ELS_CMD_FDISC &&
3619 stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3620 maxretry = 3;
3621 delay = 1000;
3622 retry = 1;
44fd7fe3
JS
3623 } else if (cmd == ELS_CMD_FLOGI &&
3624 stat.un.b.lsRjtRsnCodeExp ==
3625 LSEXP_NOTHING_MORE) {
3626 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
3627 retry = 1;
3628 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3629 "0820 FLOGI Failed (x%x). "
3630 "BBCredit Not Supported\n",
3631 stat.un.lsRjtError);
7f5f3d0d 3632 }
44fd7fe3
JS
3633 break;
3634
92d7f7b0
JS
3635 case LSRJT_PROTOCOL_ERR:
3636 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3637 (cmd == ELS_CMD_FDISC) &&
3638 ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3639 (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3640 ) {
e8b62011 3641 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
d7c255b2 3642 "0122 FDISC Failed (x%x). "
e8b62011
JS
3643 "Fabric Detected Bad WWN\n",
3644 stat.un.lsRjtError);
92d7f7b0
JS
3645 lpfc_vport_set_state(vport,
3646 FC_VPORT_FABRIC_REJ_WWN);
3647 }
3648 break;
7bdedb34
JS
3649 case LSRJT_VENDOR_UNIQUE:
3650 if ((stat.un.b.vendorUnique == 0x45) &&
3651 (cmd == ELS_CMD_FLOGI)) {
3652 goto out_retry;
3653 }
3654 break;
8db1c2b3
DK
3655 case LSRJT_CMD_UNSUPPORTED:
3656 /* lpfc nvmet returns this type of LS_RJT when it
3657 * receives an FCP PRLI because lpfc nvmet only
3658 * support NVME. ELS request is terminated for FCP4
3659 * on this rport.
3660 */
3661 if (stat.un.b.lsRjtRsnCodeExp ==
3662 LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
3663 spin_lock_irq(shost->host_lock);
3664 ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
3665 spin_unlock_irq(shost->host_lock);
3666 retry = 0;
3667 goto out_retry;
3668 }
3669 break;
dea3101e
JB
3670 }
3671 break;
3672
3673 case IOSTAT_INTERMED_RSP:
3674 case IOSTAT_BA_RJT:
3675 break;
3676
3677 default:
3678 break;
3679 }
3680
5cca2ab1
JS
3681 if (link_reset) {
3682 rc = lpfc_link_reset(vport);
3683 if (rc) {
3684 /* Do not give up. Retry PLOGI one more time and attempt
3685 * link reset if PLOGI fails again.
3686 */
3687 retry = 1;
3688 delay = 100;
3689 goto out_retry;
3690 }
3691 return 1;
3692 }
3693
488d1469 3694 if (did == FDMI_DID)
dea3101e 3695 retry = 1;
dea3101e 3696
df9e1b59 3697 if ((cmd == ELS_CMD_FLOGI) &&
76a95d75 3698 (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
1b32f6aa 3699 !lpfc_error_lost_link(irsp)) {
98c9ea5c
JS
3700 /* FLOGI retry policy */
3701 retry = 1;
df9e1b59 3702 /* retry FLOGI forever */
6eae4303
JS
3703 if (phba->link_flag != LS_LOOPBACK_MODE)
3704 maxretry = 0;
3705 else
3706 maxretry = 2;
3707
6669f9bb
JS
3708 if (cmdiocb->retry >= 100)
3709 delay = 5000;
3710 else if (cmdiocb->retry >= 32)
98c9ea5c 3711 delay = 1000;
df9e1b59
JS
3712 } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
3713 /* retry FDISCs every second up to devloss */
3714 retry = 1;
3715 maxretry = vport->cfg_devloss_tmo;
3716 delay = 1000;
98c9ea5c
JS
3717 }
3718
6669f9bb
JS
3719 cmdiocb->retry++;
3720 if (maxretry && (cmdiocb->retry >= maxretry)) {
dea3101e
JB
3721 phba->fc_stat.elsRetryExceeded++;
3722 retry = 0;
3723 }
3724
ed957684
JS
3725 if ((vport->load_flag & FC_UNLOADING) != 0)
3726 retry = 0;
3727
7bdedb34 3728out_retry:
dea3101e 3729 if (retry) {
38b92ef8
JS
3730 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3731 /* Stop retrying PLOGI and FDISC if in FCF discovery */
3732 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3733 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3734 "2849 Stop retry ELS command "
3735 "x%x to remote NPORT x%x, "
3736 "Data: x%x x%x\n", cmd, did,
3737 cmdiocb->retry, delay);
3738 return 0;
3739 }
3740 }
dea3101e
JB
3741
3742 /* Retry ELS command <elsCmd> to remote NPORT <did> */
e8b62011
JS
3743 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3744 "0107 Retry ELS command x%x to remote "
3745 "NPORT x%x Data: x%x x%x\n",
3746 cmd, did, cmdiocb->retry, delay);
dea3101e 3747
858c9f6c
JS
3748 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3749 ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
e3d2b802
JS
3750 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
3751 IOERR_NO_RESOURCES))) {
858c9f6c
JS
3752 /* Don't reset timer for no resources */
3753
dea3101e 3754 /* If discovery / RSCN timer is running, reset it */
2e0fef85 3755 if (timer_pending(&vport->fc_disctmo) ||
92d7f7b0 3756 (vport->fc_flag & FC_RSCN_MODE))
2e0fef85 3757 lpfc_set_disctmo(vport);
dea3101e
JB
3758 }
3759
3760 phba->fc_stat.elsXmitRetry++;
58da1ffb 3761 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
dea3101e
JB
3762 phba->fc_stat.elsDelayRetry++;
3763 ndlp->nlp_retry = cmdiocb->retry;
3764
92d7f7b0
JS
3765 /* delay is specified in milliseconds */
3766 mod_timer(&ndlp->nlp_delayfunc,
3767 jiffies + msecs_to_jiffies(delay));
2e0fef85 3768 spin_lock_irq(shost->host_lock);
dea3101e 3769 ndlp->nlp_flag |= NLP_DELAY_TMO;
2e0fef85 3770 spin_unlock_irq(shost->host_lock);
dea3101e 3771
5024ab17 3772 ndlp->nlp_prev_state = ndlp->nlp_state;
a0f2d3ef
JS
3773 if ((cmd == ELS_CMD_PRLI) ||
3774 (cmd == ELS_CMD_NVMEPRLI))
858c9f6c 3775 lpfc_nlp_set_state(vport, ndlp,
4c1b64ba 3776 NLP_STE_PRLI_ISSUE);
858c9f6c
JS
3777 else
3778 lpfc_nlp_set_state(vport, ndlp,
3779 NLP_STE_NPR_NODE);
dea3101e
JB
3780 ndlp->nlp_last_elscmd = cmd;
3781
c9f8735b 3782 return 1;
dea3101e
JB
3783 }
3784 switch (cmd) {
3785 case ELS_CMD_FLOGI:
2e0fef85 3786 lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
c9f8735b 3787 return 1;
92d7f7b0
JS
3788 case ELS_CMD_FDISC:
3789 lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3790 return 1;
dea3101e 3791 case ELS_CMD_PLOGI:
58da1ffb 3792 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
488d1469 3793 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3794 lpfc_nlp_set_state(vport, ndlp,
de0c5b32 3795 NLP_STE_PLOGI_ISSUE);
488d1469 3796 }
2e0fef85 3797 lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
c9f8735b 3798 return 1;
dea3101e 3799 case ELS_CMD_ADISC:
5024ab17 3800 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3801 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3802 lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
c9f8735b 3803 return 1;
dea3101e 3804 case ELS_CMD_PRLI:
a0f2d3ef 3805 case ELS_CMD_NVMEPRLI:
5024ab17 3806 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3807 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3808 lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
c9f8735b 3809 return 1;
dea3101e 3810 case ELS_CMD_LOGO:
5024ab17 3811 ndlp->nlp_prev_state = ndlp->nlp_state;
086a345f 3812 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
2e0fef85 3813 lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
c9f8735b 3814 return 1;
dea3101e
JB
3815 }
3816 }
dea3101e 3817 /* No retry ELS command <elsCmd> to remote NPORT <did> */
98c9ea5c
JS
3818 if (logerr) {
3819 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3820 "0137 No retry ELS command x%x to remote "
3821 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
3822 cmd, did, irsp->ulpStatus,
3823 irsp->un.ulpWord[4]);
3824 }
3825 else {
3826 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
a58cbd52
JS
3827 "0108 No retry ELS command x%x to remote "
3828 "NPORT x%x Retried:%d Error:x%x/%x\n",
3829 cmd, did, cmdiocb->retry, irsp->ulpStatus,
3830 irsp->un.ulpWord[4]);
98c9ea5c 3831 }
c9f8735b 3832 return 0;
dea3101e
JB
3833}
3834
e59058c4 3835/**
3621a710 3836 * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
e59058c4
JS
3837 * @phba: pointer to lpfc hba data structure.
3838 * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3839 *
3840 * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3841 * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3842 * checks to see whether there is a lpfc DMA buffer associated with the
3843 * response of the command IOCB. If so, it will be released before releasing
3844 * the lpfc DMA buffer associated with the IOCB itself.
3845 *
3846 * Return code
3847 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3848 **/
09372820 3849static int
87af33fe
JS
3850lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3851{
3852 struct lpfc_dmabuf *buf_ptr;
3853
e59058c4 3854 /* Free the response before processing the command. */
87af33fe
JS
3855 if (!list_empty(&buf_ptr1->list)) {
3856 list_remove_head(&buf_ptr1->list, buf_ptr,
3857 struct lpfc_dmabuf,
3858 list);
3859 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3860 kfree(buf_ptr);
3861 }
3862 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3863 kfree(buf_ptr1);
3864 return 0;
3865}
3866
e59058c4 3867/**
3621a710 3868 * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
e59058c4
JS
3869 * @phba: pointer to lpfc hba data structure.
3870 * @buf_ptr: pointer to the lpfc dma buffer data structure.
3871 *
3872 * This routine releases the lpfc Direct Memory Access (DMA) buffer
3873 * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3874 * pool.
3875 *
3876 * Return code
3877 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3878 **/
09372820 3879static int
87af33fe
JS
3880lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3881{
3882 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3883 kfree(buf_ptr);
3884 return 0;
3885}
3886
e59058c4 3887/**
3621a710 3888 * lpfc_els_free_iocb - Free a command iocb and its associated resources
e59058c4
JS
3889 * @phba: pointer to lpfc hba data structure.
3890 * @elsiocb: pointer to lpfc els command iocb data structure.
3891 *
3892 * This routine frees a command IOCB and its associated resources. The
3893 * command IOCB data structure contains the reference to various associated
3894 * resources, these fields must be set to NULL if the associated reference
3895 * not present:
3896 * context1 - reference to ndlp
3897 * context2 - reference to cmd
3898 * context2->next - reference to rsp
3899 * context3 - reference to bpl
3900 *
3901 * It first properly decrements the reference count held on ndlp for the
3902 * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3903 * set, it invokes the lpfc_els_free_data() routine to release the Direct
3904 * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3905 * adds the DMA buffer the @phba data structure for the delayed release.
3906 * If reference to the Buffer Pointer List (BPL) is present, the
3907 * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3908 * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3909 * invoked to release the IOCB data structure back to @phba IOCBQ list.
3910 *
3911 * Return code
3912 * 0 - Success (currently, always return 0)
3913 **/
dea3101e 3914int
329f9bc7 3915lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
dea3101e
JB
3916{
3917 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
a8adb832
JS
3918 struct lpfc_nodelist *ndlp;
3919
3920 ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3921 if (ndlp) {
3922 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3923 lpfc_nlp_put(ndlp);
dea3101e 3924
a8adb832
JS
3925 /* If the ndlp is not being used by another discovery
3926 * thread, free it.
3927 */
3928 if (!lpfc_nlp_not_used(ndlp)) {
3929 /* If ndlp is being used by another discovery
3930 * thread, just clear NLP_DEFER_RM
3931 */
3932 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3933 }
3934 }
3935 else
3936 lpfc_nlp_put(ndlp);
329f9bc7
JS
3937 elsiocb->context1 = NULL;
3938 }
dea3101e
JB
3939 /* context2 = cmd, context2->next = rsp, context3 = bpl */
3940 if (elsiocb->context2) {
0ff10d46
JS
3941 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3942 /* Firmware could still be in progress of DMAing
3943 * payload, so don't free data buffer till after
3944 * a hbeat.
3945 */
3946 elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3947 buf_ptr = elsiocb->context2;
3948 elsiocb->context2 = NULL;
3949 if (buf_ptr) {
3950 buf_ptr1 = NULL;
3951 spin_lock_irq(&phba->hbalock);
3952 if (!list_empty(&buf_ptr->list)) {
3953 list_remove_head(&buf_ptr->list,
3954 buf_ptr1, struct lpfc_dmabuf,
3955 list);
3956 INIT_LIST_HEAD(&buf_ptr1->list);
3957 list_add_tail(&buf_ptr1->list,
3958 &phba->elsbuf);
3959 phba->elsbuf_cnt++;
3960 }
3961 INIT_LIST_HEAD(&buf_ptr->list);
3962 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3963 phba->elsbuf_cnt++;
3964 spin_unlock_irq(&phba->hbalock);
3965 }
3966 } else {
3967 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3968 lpfc_els_free_data(phba, buf_ptr1);
8667f515 3969 elsiocb->context2 = NULL;
0ff10d46 3970 }
dea3101e
JB
3971 }
3972
3973 if (elsiocb->context3) {
3974 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
87af33fe 3975 lpfc_els_free_bpl(phba, buf_ptr);
8667f515 3976 elsiocb->context3 = NULL;
dea3101e 3977 }
604a3e30 3978 lpfc_sli_release_iocbq(phba, elsiocb);
dea3101e
JB
3979 return 0;
3980}
3981
e59058c4 3982/**
3621a710 3983 * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
e59058c4
JS
3984 * @phba: pointer to lpfc hba data structure.
3985 * @cmdiocb: pointer to lpfc command iocb data structure.
3986 * @rspiocb: pointer to lpfc response iocb data structure.
3987 *
3988 * This routine is the completion callback function to the Logout (LOGO)
3989 * Accept (ACC) Response ELS command. This routine is invoked to indicate
3990 * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3991 * release the ndlp if it has the last reference remaining (reference count
3992 * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3993 * field to NULL to inform the following lpfc_els_free_iocb() routine no
3994 * ndlp reference count needs to be decremented. Otherwise, the ndlp
3995 * reference use-count shall be decremented by the lpfc_els_free_iocb()
3996 * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3997 * IOCB data structure.
3998 **/
dea3101e 3999static void
2e0fef85
JS
4000lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4001 struct lpfc_iocbq *rspiocb)
dea3101e 4002{
2e0fef85
JS
4003 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4004 struct lpfc_vport *vport = cmdiocb->vport;
858c9f6c
JS
4005 IOCB_t *irsp;
4006
4007 irsp = &rspiocb->iocb;
4008 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4009 "ACC LOGO cmpl: status:x%x/x%x did:x%x",
4010 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
dea3101e 4011 /* ACC to LOGO completes to NPort <nlp_DID> */
e8b62011
JS
4012 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4013 "0109 ACC to LOGO completes to NPort x%x "
4014 "Data: x%x x%x x%x\n",
4015 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4016 ndlp->nlp_rpi);
87af33fe
JS
4017
4018 if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
4019 /* NPort Recovery mode or node is just allocated */
4020 if (!lpfc_nlp_not_used(ndlp)) {
4021 /* If the ndlp is being used by another discovery
4022 * thread, just unregister the RPI.
4023 */
4024 lpfc_unreg_rpi(vport, ndlp);
fa4066b6
JS
4025 } else {
4026 /* Indicate the node has already released, should
4027 * not reference to it from within lpfc_els_free_iocb.
4028 */
4029 cmdiocb->context1 = NULL;
87af33fe 4030 }
dea3101e 4031 }
73d91e50
JS
4032
4033 /*
4034 * The driver received a LOGO from the rport and has ACK'd it.
df9e1b59 4035 * At this point, the driver is done so release the IOCB
73d91e50 4036 */
dea3101e 4037 lpfc_els_free_iocb(phba, cmdiocb);
dea3101e
JB
4038}
4039
e59058c4 4040/**
3621a710 4041 * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
e59058c4
JS
4042 * @phba: pointer to lpfc hba data structure.
4043 * @pmb: pointer to the driver internal queue element for mailbox command.
4044 *
4045 * This routine is the completion callback function for unregister default
4046 * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
4047 * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
4048 * decrements the ndlp reference count held for this completion callback
4049 * function. After that, it invokes the lpfc_nlp_not_used() to check
4050 * whether there is only one reference left on the ndlp. If so, it will
4051 * perform one more decrement and trigger the release of the ndlp.
4052 **/
858c9f6c
JS
4053void
4054lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4055{
3e1f0718
JS
4056 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4057 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
858c9f6c 4058
3e1f0718
JS
4059 pmb->ctx_buf = NULL;
4060 pmb->ctx_ndlp = NULL;
d439d286 4061
858c9f6c
JS
4062 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4063 kfree(mp);
4064 mempool_free(pmb, phba->mbox_mem_pool);
086a345f 4065 if (ndlp) {
be6bb941
JS
4066 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4067 "0006 rpi%x DID:%x flg:%x %d map:%x %p\n",
4068 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
2c935bc5 4069 kref_read(&ndlp->kref),
be6bb941 4070 ndlp->nlp_usg_map, ndlp);
086a345f
JS
4071 if (NLP_CHK_NODE_ACT(ndlp)) {
4072 lpfc_nlp_put(ndlp);
4073 /* This is the end of the default RPI cleanup logic for
4074 * this ndlp. If no other discovery threads are using
4075 * this ndlp, free all resources associated with it.
4076 */
4077 lpfc_nlp_not_used(ndlp);
4078 } else {
4079 lpfc_drop_node(ndlp->vport, ndlp);
4080 }
a8adb832 4081 }
3772a991 4082
858c9f6c
JS
4083 return;
4084}
4085
e59058c4 4086/**
3621a710 4087 * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
e59058c4
JS
4088 * @phba: pointer to lpfc hba data structure.
4089 * @cmdiocb: pointer to lpfc command iocb data structure.
4090 * @rspiocb: pointer to lpfc response iocb data structure.
4091 *
4092 * This routine is the completion callback function for ELS Response IOCB
4093 * command. In normal case, this callback function just properly sets the
4094 * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
4095 * field in the command IOCB is not NULL, the referred mailbox command will
4096 * be send out, and then invokes the lpfc_els_free_iocb() routine to release
4097 * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
4098 * link down event occurred during the discovery, the lpfc_nlp_not_used()
4099 * routine shall be invoked trying to release the ndlp if no other threads
4100 * are currently referring it.
4101 **/
dea3101e 4102static void
858c9f6c 4103lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
329f9bc7 4104 struct lpfc_iocbq *rspiocb)
dea3101e 4105{
2e0fef85
JS
4106 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4107 struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
4108 struct Scsi_Host *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
87af33fe
JS
4109 IOCB_t *irsp;
4110 uint8_t *pcmd;
dea3101e 4111 LPFC_MBOXQ_t *mbox = NULL;
2e0fef85 4112 struct lpfc_dmabuf *mp = NULL;
87af33fe 4113 uint32_t ls_rjt = 0;
dea3101e 4114
33ccf8d1
JS
4115 irsp = &rspiocb->iocb;
4116
dea3101e
JB
4117 if (cmdiocb->context_un.mbox)
4118 mbox = cmdiocb->context_un.mbox;
4119
fa4066b6
JS
4120 /* First determine if this is a LS_RJT cmpl. Note, this callback
4121 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
4122 */
87af33fe 4123 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
58da1ffb
JS
4124 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4125 (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
fa4066b6 4126 /* A LS_RJT associated with Default RPI cleanup has its own
3ad2f3fb 4127 * separate code path.
87af33fe
JS
4128 */
4129 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4130 ls_rjt = 1;
4131 }
4132
dea3101e 4133 /* Check to see if link went down during discovery */
58da1ffb 4134 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
dea3101e 4135 if (mbox) {
3e1f0718 4136 mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
14691150
JS
4137 if (mp) {
4138 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4139 kfree(mp);
4140 }
329f9bc7 4141 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 4142 }
58da1ffb
JS
4143 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4144 (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
fa4066b6 4145 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 4146 ndlp = NULL;
fa4066b6
JS
4147 /* Indicate the node has already released,
4148 * should not reference to it from within
4149 * the routine lpfc_els_free_iocb.
4150 */
4151 cmdiocb->context1 = NULL;
4152 }
dea3101e
JB
4153 goto out;
4154 }
4155
858c9f6c 4156 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
51ef4c26 4157 "ELS rsp cmpl: status:x%x/x%x did:x%x",
858c9f6c 4158 irsp->ulpStatus, irsp->un.ulpWord[4],
51ef4c26 4159 cmdiocb->iocb.un.elsreq64.remoteID);
dea3101e 4160 /* ELS response tag <ulpIoTag> completes */
e8b62011
JS
4161 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4162 "0110 ELS response tag x%x completes "
4163 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
4164 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
4165 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
4166 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4167 ndlp->nlp_rpi);
dea3101e
JB
4168 if (mbox) {
4169 if ((rspiocb->iocb.ulpStatus == 0)
4170 && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
1fe68477
DK
4171 if (!lpfc_unreg_rpi(vport, ndlp) &&
4172 (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
4173 ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE)) {
4174 lpfc_printf_vlog(vport, KERN_INFO,
4175 LOG_DISCOVERY,
4176 "0314 PLOGI recov DID x%x "
4177 "Data: x%x x%x x%x\n",
4178 ndlp->nlp_DID, ndlp->nlp_state,
4179 ndlp->nlp_rpi, ndlp->nlp_flag);
3e1f0718 4180 mp = mbox->ctx_buf;
1fe68477
DK
4181 if (mp) {
4182 lpfc_mbuf_free(phba, mp->virt,
4183 mp->phys);
4184 kfree(mp);
4185 }
4186 mempool_free(mbox, phba->mbox_mem_pool);
4187 goto out;
4188 }
4189
e47c9093
JS
4190 /* Increment reference count to ndlp to hold the
4191 * reference to ndlp for the callback function.
4192 */
3e1f0718 4193 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
2e0fef85 4194 mbox->vport = vport;
858c9f6c
JS
4195 if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
4196 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4197 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4198 }
4199 else {
4200 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
4201 ndlp->nlp_prev_state = ndlp->nlp_state;
4202 lpfc_nlp_set_state(vport, ndlp,
2e0fef85 4203 NLP_STE_REG_LOGIN_ISSUE);
858c9f6c 4204 }
4b7789b7
JS
4205
4206 ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
0b727fea 4207 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
e47c9093 4208 != MBX_NOT_FINISHED)
dea3101e 4209 goto out;
4b7789b7
JS
4210
4211 /* Decrement the ndlp reference count we
4212 * set for this failed mailbox command.
4213 */
4214 lpfc_nlp_put(ndlp);
4215 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
98c9ea5c
JS
4216
4217 /* ELS rsp: Cannot issue reg_login for <NPortid> */
4218 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4219 "0138 ELS rsp: Cannot issue reg_login for x%x "
4220 "Data: x%x x%x x%x\n",
4221 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4222 ndlp->nlp_rpi);
4223
fa4066b6 4224 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 4225 ndlp = NULL;
fa4066b6
JS
4226 /* Indicate node has already been released,
4227 * should not reference to it from within
4228 * the routine lpfc_els_free_iocb.
4229 */
4230 cmdiocb->context1 = NULL;
4231 }
dea3101e 4232 } else {
858c9f6c
JS
4233 /* Do not drop node for lpfc_els_abort'ed ELS cmds */
4234 if (!lpfc_error_lost_link(irsp) &&
4235 ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
fa4066b6 4236 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 4237 ndlp = NULL;
fa4066b6
JS
4238 /* Indicate node has already been
4239 * released, should not reference
4240 * to it from within the routine
4241 * lpfc_els_free_iocb.
4242 */
4243 cmdiocb->context1 = NULL;
4244 }
dea3101e
JB
4245 }
4246 }
3e1f0718 4247 mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
14691150
JS
4248 if (mp) {
4249 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4250 kfree(mp);
4251 }
4252 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e
JB
4253 }
4254out:
58da1ffb 4255 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2e0fef85 4256 spin_lock_irq(shost->host_lock);
858c9f6c 4257 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
2e0fef85 4258 spin_unlock_irq(shost->host_lock);
87af33fe
JS
4259
4260 /* If the node is not being used by another discovery thread,
4261 * and we are sending a reject, we are done with it.
4262 * Release driver reference count here and free associated
4263 * resources.
4264 */
4265 if (ls_rjt)
fa4066b6
JS
4266 if (lpfc_nlp_not_used(ndlp))
4267 /* Indicate node has already been released,
4268 * should not reference to it from within
4269 * the routine lpfc_els_free_iocb.
4270 */
4271 cmdiocb->context1 = NULL;
4b7789b7 4272
dea3101e 4273 }
87af33fe 4274
dea3101e
JB
4275 lpfc_els_free_iocb(phba, cmdiocb);
4276 return;
4277}
4278
e59058c4 4279/**
3621a710 4280 * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
e59058c4
JS
4281 * @vport: pointer to a host virtual N_Port data structure.
4282 * @flag: the els command code to be accepted.
4283 * @oldiocb: pointer to the original lpfc command iocb data structure.
4284 * @ndlp: pointer to a node-list data structure.
4285 * @mbox: pointer to the driver internal queue element for mailbox command.
4286 *
4287 * This routine prepares and issues an Accept (ACC) response IOCB
4288 * command. It uses the @flag to properly set up the IOCB field for the
4289 * specific ACC response command to be issued and invokes the
4290 * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
4291 * @mbox pointer is passed in, it will be put into the context_un.mbox
4292 * field of the IOCB for the completion callback function to issue the
4293 * mailbox command to the HBA later when callback is invoked.
4294 *
4295 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4296 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4297 * will be stored into the context1 field of the IOCB for the completion
4298 * callback function to the corresponding response ELS IOCB command.
4299 *
4300 * Return code
4301 * 0 - Successfully issued acc response
4302 * 1 - Failed to issue acc response
4303 **/
dea3101e 4304int
2e0fef85
JS
4305lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
4306 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
51ef4c26 4307 LPFC_MBOXQ_t *mbox)
dea3101e 4308{
2e0fef85
JS
4309 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4310 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
4311 IOCB_t *icmd;
4312 IOCB_t *oldcmd;
4313 struct lpfc_iocbq *elsiocb;
dea3101e 4314 uint8_t *pcmd;
d6de08cc 4315 struct serv_parm *sp;
dea3101e
JB
4316 uint16_t cmdsize;
4317 int rc;
82d9a2a2 4318 ELS_PKT *els_pkt_ptr;
dea3101e 4319
dea3101e
JB
4320 oldcmd = &oldiocb->iocb;
4321
4322 switch (flag) {
4323 case ELS_CMD_ACC:
92d7f7b0 4324 cmdsize = sizeof(uint32_t);
2e0fef85
JS
4325 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4326 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4327 if (!elsiocb) {
2e0fef85 4328 spin_lock_irq(shost->host_lock);
5024ab17 4329 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2e0fef85 4330 spin_unlock_irq(shost->host_lock);
c9f8735b 4331 return 1;
dea3101e 4332 }
2e0fef85 4333
dea3101e 4334 icmd = &elsiocb->iocb;
7851fe2c
JS
4335 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4336 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e
JB
4337 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4338 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4339 pcmd += sizeof(uint32_t);
858c9f6c
JS
4340
4341 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4342 "Issue ACC: did:x%x flg:x%x",
4343 ndlp->nlp_DID, ndlp->nlp_flag, 0);
dea3101e 4344 break;
d6de08cc 4345 case ELS_CMD_FLOGI:
dea3101e 4346 case ELS_CMD_PLOGI:
92d7f7b0 4347 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
2e0fef85
JS
4348 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4349 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4350 if (!elsiocb)
c9f8735b 4351 return 1;
488d1469 4352
dea3101e 4353 icmd = &elsiocb->iocb;
7851fe2c
JS
4354 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4355 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e
JB
4356 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4357
4358 if (mbox)
4359 elsiocb->context_un.mbox = mbox;
4360
4361 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4362 pcmd += sizeof(uint32_t);
d6de08cc
JS
4363 sp = (struct serv_parm *)pcmd;
4364
4365 if (flag == ELS_CMD_FLOGI) {
4366 /* Copy the received service parameters back */
4367 memcpy(sp, &phba->fc_fabparam,
4368 sizeof(struct serv_parm));
4369
4370 /* Clear the F_Port bit */
4371 sp->cmn.fPort = 0;
4372
4373 /* Mark all class service parameters as invalid */
4374 sp->cls1.classValid = 0;
4375 sp->cls2.classValid = 0;
4376 sp->cls3.classValid = 0;
4377 sp->cls4.classValid = 0;
4378
4379 /* Copy our worldwide names */
4380 memcpy(&sp->portName, &vport->fc_sparam.portName,
4381 sizeof(struct lpfc_name));
4382 memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
4383 sizeof(struct lpfc_name));
8c258641 4384 } else {
d6de08cc
JS
4385 memcpy(pcmd, &vport->fc_sparam,
4386 sizeof(struct serv_parm));
e0165f20 4387
8c258641
JS
4388 sp->cmn.valid_vendor_ver_level = 0;
4389 memset(sp->un.vendorVersion, 0,
4390 sizeof(sp->un.vendorVersion));
44fd7fe3 4391 sp->cmn.bbRcvSizeMsb &= 0xF;
8c258641
JS
4392
4393 /* If our firmware supports this feature, convey that
4394 * info to the target using the vendor specific field.
4395 */
4396 if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
4397 sp->cmn.valid_vendor_ver_level = 1;
4398 sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
4399 sp->un.vv.flags =
4400 cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
4401 }
4402 }
4403
858c9f6c 4404 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
d6de08cc 4405 "Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
858c9f6c 4406 ndlp->nlp_DID, ndlp->nlp_flag, 0);
dea3101e 4407 break;
82d9a2a2 4408 case ELS_CMD_PRLO:
92d7f7b0 4409 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
2e0fef85 4410 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
82d9a2a2
JS
4411 ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
4412 if (!elsiocb)
4413 return 1;
4414
4415 icmd = &elsiocb->iocb;
7851fe2c
JS
4416 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4417 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
82d9a2a2
JS
4418 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4419
4420 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
92d7f7b0 4421 sizeof(uint32_t) + sizeof(PRLO));
82d9a2a2
JS
4422 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
4423 els_pkt_ptr = (ELS_PKT *) pcmd;
4424 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
858c9f6c
JS
4425
4426 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4427 "Issue ACC PRLO: did:x%x flg:x%x",
4428 ndlp->nlp_DID, ndlp->nlp_flag, 0);
82d9a2a2 4429 break;
dea3101e 4430 default:
c9f8735b 4431 return 1;
dea3101e 4432 }
dea3101e 4433 if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2e0fef85 4434 spin_lock_irq(shost->host_lock);
7c5e518c
JS
4435 if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4436 ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
4437 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2e0fef85 4438 spin_unlock_irq(shost->host_lock);
dea3101e
JB
4439 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4440 } else {
858c9f6c 4441 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e
JB
4442 }
4443
4444 phba->fc_stat.elsXmitACC++;
3772a991 4445 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
4446 if (rc == IOCB_ERROR) {
4447 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4448 return 1;
dea3101e 4449 }
c9f8735b 4450 return 0;
dea3101e
JB
4451}
4452
e59058c4 4453/**
3621a710 4454 * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
e59058c4
JS
4455 * @vport: pointer to a virtual N_Port data structure.
4456 * @rejectError:
4457 * @oldiocb: pointer to the original lpfc command iocb data structure.
4458 * @ndlp: pointer to a node-list data structure.
4459 * @mbox: pointer to the driver internal queue element for mailbox command.
4460 *
4461 * This routine prepares and issue an Reject (RJT) response IOCB
4462 * command. If a @mbox pointer is passed in, it will be put into the
4463 * context_un.mbox field of the IOCB for the completion callback function
4464 * to issue to the HBA later.
4465 *
4466 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4467 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4468 * will be stored into the context1 field of the IOCB for the completion
4469 * callback function to the reject response ELS IOCB command.
4470 *
4471 * Return code
4472 * 0 - Successfully issued reject response
4473 * 1 - Failed to issue reject response
4474 **/
dea3101e 4475int
2e0fef85 4476lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
858c9f6c
JS
4477 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4478 LPFC_MBOXQ_t *mbox)
dea3101e 4479{
2e0fef85 4480 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
4481 IOCB_t *icmd;
4482 IOCB_t *oldcmd;
4483 struct lpfc_iocbq *elsiocb;
dea3101e
JB
4484 uint8_t *pcmd;
4485 uint16_t cmdsize;
4486 int rc;
4487
92d7f7b0 4488 cmdsize = 2 * sizeof(uint32_t);
2e0fef85
JS
4489 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4490 ndlp->nlp_DID, ELS_CMD_LS_RJT);
488d1469 4491 if (!elsiocb)
c9f8735b 4492 return 1;
dea3101e
JB
4493
4494 icmd = &elsiocb->iocb;
4495 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4496 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4497 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e
JB
4498 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4499
4500 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
92d7f7b0 4501 pcmd += sizeof(uint32_t);
dea3101e
JB
4502 *((uint32_t *) (pcmd)) = rejectError;
4503
51ef4c26 4504 if (mbox)
858c9f6c 4505 elsiocb->context_un.mbox = mbox;
858c9f6c 4506
dea3101e 4507 /* Xmit ELS RJT <err> response tag <ulpIoTag> */
e8b62011
JS
4508 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4509 "0129 Xmit ELS RJT x%x response tag x%x "
4510 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4511 "rpi x%x\n",
4512 rejectError, elsiocb->iotag,
4513 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4514 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
858c9f6c
JS
4515 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4516 "Issue LS_RJT: did:x%x flg:x%x err:x%x",
4517 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4518
dea3101e 4519 phba->fc_stat.elsXmitLSRJT++;
858c9f6c 4520 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3772a991 4521 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
51ef4c26 4522
dea3101e
JB
4523 if (rc == IOCB_ERROR) {
4524 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4525 return 1;
dea3101e 4526 }
c9f8735b 4527 return 0;
dea3101e
JB
4528}
4529
e59058c4 4530/**
3621a710 4531 * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
e59058c4
JS
4532 * @vport: pointer to a virtual N_Port data structure.
4533 * @oldiocb: pointer to the original lpfc command iocb data structure.
4534 * @ndlp: pointer to a node-list data structure.
4535 *
4536 * This routine prepares and issues an Accept (ACC) response to Address
4537 * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4538 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4539 *
4540 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4541 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4542 * will be stored into the context1 field of the IOCB for the completion
4543 * callback function to the ADISC Accept response ELS IOCB command.
4544 *
4545 * Return code
4546 * 0 - Successfully issued acc adisc response
4547 * 1 - Failed to issue adisc acc response
4548 **/
dea3101e 4549int
2e0fef85
JS
4550lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4551 struct lpfc_nodelist *ndlp)
dea3101e 4552{
2e0fef85 4553 struct lpfc_hba *phba = vport->phba;
dea3101e 4554 ADISC *ap;
2e0fef85 4555 IOCB_t *icmd, *oldcmd;
dea3101e 4556 struct lpfc_iocbq *elsiocb;
dea3101e
JB
4557 uint8_t *pcmd;
4558 uint16_t cmdsize;
4559 int rc;
4560
92d7f7b0 4561 cmdsize = sizeof(uint32_t) + sizeof(ADISC);
2e0fef85
JS
4562 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4563 ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4564 if (!elsiocb)
c9f8735b 4565 return 1;
dea3101e 4566
5b8bd0c9
JS
4567 icmd = &elsiocb->iocb;
4568 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4569 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4570 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5b8bd0c9 4571
dea3101e 4572 /* Xmit ADISC ACC response tag <ulpIoTag> */
e8b62011
JS
4573 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4574 "0130 Xmit ADISC ACC response iotag x%x xri: "
4575 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4576 elsiocb->iotag, elsiocb->iocb.ulpContext,
4577 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4578 ndlp->nlp_rpi);
dea3101e
JB
4579 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4580
4581 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4582 pcmd += sizeof(uint32_t);
dea3101e
JB
4583
4584 ap = (ADISC *) (pcmd);
4585 ap->hardAL_PA = phba->fc_pref_ALPA;
92d7f7b0
JS
4586 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4587 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 4588 ap->DID = be32_to_cpu(vport->fc_myDID);
dea3101e 4589
858c9f6c
JS
4590 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4591 "Issue ACC ADISC: did:x%x flg:x%x",
4592 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4593
dea3101e 4594 phba->fc_stat.elsXmitACC++;
858c9f6c 4595 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3772a991 4596 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
4597 if (rc == IOCB_ERROR) {
4598 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4599 return 1;
dea3101e 4600 }
d496b9a7
JS
4601
4602 /* Xmit ELS ACC response tag <ulpIoTag> */
4603 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4604 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
4605 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
4606 "RPI: x%x, fc_flag x%x\n",
4607 rc, elsiocb->iotag, elsiocb->sli4_xritag,
4608 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4609 ndlp->nlp_rpi, vport->fc_flag);
c9f8735b 4610 return 0;
dea3101e
JB
4611}
4612
e59058c4 4613/**
3621a710 4614 * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
e59058c4
JS
4615 * @vport: pointer to a virtual N_Port data structure.
4616 * @oldiocb: pointer to the original lpfc command iocb data structure.
4617 * @ndlp: pointer to a node-list data structure.
4618 *
4619 * This routine prepares and issues an Accept (ACC) response to Process
4620 * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
4621 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4622 *
4623 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4624 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4625 * will be stored into the context1 field of the IOCB for the completion
4626 * callback function to the PRLI Accept response ELS IOCB command.
4627 *
4628 * Return code
4629 * 0 - Successfully issued acc prli response
4630 * 1 - Failed to issue acc prli response
4631 **/
dea3101e 4632int
2e0fef85 4633lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5b8bd0c9 4634 struct lpfc_nodelist *ndlp)
dea3101e 4635{
2e0fef85 4636 struct lpfc_hba *phba = vport->phba;
dea3101e 4637 PRLI *npr;
a0f2d3ef 4638 struct lpfc_nvme_prli *npr_nvme;
dea3101e
JB
4639 lpfc_vpd_t *vpd;
4640 IOCB_t *icmd;
4641 IOCB_t *oldcmd;
4642 struct lpfc_iocbq *elsiocb;
dea3101e
JB
4643 uint8_t *pcmd;
4644 uint16_t cmdsize;
a0f2d3ef
JS
4645 uint32_t prli_fc4_req, *req_payload;
4646 struct lpfc_dmabuf *req_buf;
dea3101e 4647 int rc;
a0f2d3ef
JS
4648 u32 elsrspcmd;
4649
4650 /* Need the incoming PRLI payload to determine if the ACC is for an
4651 * FC4 or NVME PRLI type. The PRLI type is at word 1.
4652 */
4653 req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
4654 req_payload = (((uint32_t *)req_buf->virt) + 1);
4655
4656 /* PRLI type payload is at byte 3 for FCP or NVME. */
4657 prli_fc4_req = be32_to_cpu(*req_payload);
4658 prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
4659 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4660 "6127 PRLI_ACC: Req Type x%x, Word1 x%08x\n",
4661 prli_fc4_req, *((uint32_t *)req_payload));
4662
4663 if (prli_fc4_req == PRLI_FCP_TYPE) {
4664 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
4665 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
4666 } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4667 cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
4668 elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
4669 } else {
4670 return 1;
4671 }
dea3101e 4672
2e0fef85 4673 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
a0f2d3ef 4674 ndlp->nlp_DID, elsrspcmd);
c9f8735b
JW
4675 if (!elsiocb)
4676 return 1;
dea3101e 4677
5b8bd0c9
JS
4678 icmd = &elsiocb->iocb;
4679 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4680 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4681 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4682
dea3101e 4683 /* Xmit PRLI ACC response tag <ulpIoTag> */
e8b62011
JS
4684 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4685 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
4686 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4687 elsiocb->iotag, elsiocb->iocb.ulpContext,
4688 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4689 ndlp->nlp_rpi);
dea3101e 4690 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
a0f2d3ef 4691 memset(pcmd, 0, cmdsize);
dea3101e 4692
c07f10cd 4693 *((uint32_t *)(pcmd)) = elsrspcmd;
92d7f7b0 4694 pcmd += sizeof(uint32_t);
dea3101e
JB
4695
4696 /* For PRLI, remainder of payload is PRLI parameter page */
dea3101e 4697 vpd = &phba->vpd;
dea3101e 4698
a0f2d3ef
JS
4699 if (prli_fc4_req == PRLI_FCP_TYPE) {
4700 /*
4701 * If the remote port is a target and our firmware version
4702 * is 3.20 or later, set the following bits for FC-TAPE
4703 * support.
4704 */
4705 npr = (PRLI *) pcmd;
4706 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4707 (vpd->rev.feaLevelHigh >= 0x02)) {
4708 npr->ConfmComplAllowed = 1;
4709 npr->Retry = 1;
4710 npr->TaskRetryIdReq = 1;
4711 }
4712 npr->acceptRspCode = PRLI_REQ_EXECUTED;
4713 npr->estabImagePair = 1;
4714 npr->readXferRdyDis = 1;
4715 npr->ConfmComplAllowed = 1;
4716 npr->prliType = PRLI_FCP_TYPE;
4717 npr->initiatorFunc = 1;
4718 } else if (prli_fc4_req & PRLI_NVME_TYPE) {
4719 /* Respond with an NVME PRLI Type */
4720 npr_nvme = (struct lpfc_nvme_prli *) pcmd;
4721 bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
4722 bf_set(prli_estabImagePair, npr_nvme, 0); /* Should be 0 */
4723 bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
8c258641
JS
4724 if (phba->nvmet_support) {
4725 bf_set(prli_tgt, npr_nvme, 1);
4726 bf_set(prli_disc, npr_nvme, 1);
4727 if (phba->cfg_nvme_enable_fb) {
4728 bf_set(prli_fba, npr_nvme, 1);
4729
4730 /* TBD. Target mode needs to post buffers
4731 * that support the configured first burst
4732 * byte size.
4733 */
4734 bf_set(prli_fb_sz, npr_nvme,
4735 phba->cfg_nvmet_fb_size);
4736 }
4737 } else {
4738 bf_set(prli_init, npr_nvme, 1);
4739 }
a0f2d3ef
JS
4740
4741 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
4742 "6015 NVME issue PRLI ACC word1 x%08x "
4743 "word4 x%08x word5 x%08x flag x%x, "
4744 "fcp_info x%x nlp_type x%x\n",
4745 npr_nvme->word1, npr_nvme->word4,
4746 npr_nvme->word5, ndlp->nlp_flag,
4747 ndlp->nlp_fcp_info, ndlp->nlp_type);
4748 npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
4749 npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
4750 npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
4751 } else
4752 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4753 "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
4754 prli_fc4_req, ndlp->nlp_fc4_type,
4755 ndlp->nlp_DID);
dea3101e 4756
858c9f6c
JS
4757 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4758 "Issue ACC PRLI: did:x%x flg:x%x",
4759 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4760
dea3101e 4761 phba->fc_stat.elsXmitACC++;
858c9f6c 4762 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e 4763
3772a991 4764 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
4765 if (rc == IOCB_ERROR) {
4766 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4767 return 1;
dea3101e 4768 }
c9f8735b 4769 return 0;
dea3101e
JB
4770}
4771
e59058c4 4772/**
3621a710 4773 * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
e59058c4
JS
4774 * @vport: pointer to a virtual N_Port data structure.
4775 * @format: rnid command format.
4776 * @oldiocb: pointer to the original lpfc command iocb data structure.
4777 * @ndlp: pointer to a node-list data structure.
4778 *
4779 * This routine issues a Request Node Identification Data (RNID) Accept
4780 * (ACC) response. It constructs the RNID ACC response command according to
4781 * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4782 * issue the response. Note that this command does not need to hold the ndlp
4783 * reference count for the callback. So, the ndlp reference count taken by
4784 * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4785 * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4786 * there is no ndlp reference available.
4787 *
4788 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4789 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4790 * will be stored into the context1 field of the IOCB for the completion
4791 * callback function. However, for the RNID Accept Response ELS command,
4792 * this is undone later by this routine after the IOCB is allocated.
4793 *
4794 * Return code
4795 * 0 - Successfully issued acc rnid response
4796 * 1 - Failed to issue acc rnid response
4797 **/
dea3101e 4798static int
2e0fef85 4799lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
329f9bc7 4800 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
dea3101e 4801{
2e0fef85 4802 struct lpfc_hba *phba = vport->phba;
dea3101e 4803 RNID *rn;
2e0fef85 4804 IOCB_t *icmd, *oldcmd;
dea3101e 4805 struct lpfc_iocbq *elsiocb;
dea3101e
JB
4806 uint8_t *pcmd;
4807 uint16_t cmdsize;
4808 int rc;
4809
92d7f7b0
JS
4810 cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4811 + (2 * sizeof(struct lpfc_name));
dea3101e 4812 if (format)
92d7f7b0 4813 cmdsize += sizeof(RNID_TOP_DISC);
dea3101e 4814
2e0fef85
JS
4815 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4816 ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4817 if (!elsiocb)
c9f8735b 4818 return 1;
dea3101e 4819
5b8bd0c9
JS
4820 icmd = &elsiocb->iocb;
4821 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4822 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4823 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4824
dea3101e 4825 /* Xmit RNID ACC response tag <ulpIoTag> */
e8b62011
JS
4826 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4827 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
4828 elsiocb->iotag, elsiocb->iocb.ulpContext);
dea3101e 4829 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
dea3101e 4830 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4831 pcmd += sizeof(uint32_t);
dea3101e 4832
92d7f7b0 4833 memset(pcmd, 0, sizeof(RNID));
dea3101e
JB
4834 rn = (RNID *) (pcmd);
4835 rn->Format = format;
92d7f7b0
JS
4836 rn->CommonLen = (2 * sizeof(struct lpfc_name));
4837 memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4838 memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
dea3101e
JB
4839 switch (format) {
4840 case 0:
4841 rn->SpecificLen = 0;
4842 break;
4843 case RNID_TOPOLOGY_DISC:
92d7f7b0 4844 rn->SpecificLen = sizeof(RNID_TOP_DISC);
dea3101e 4845 memcpy(&rn->un.topologyDisc.portName,
92d7f7b0 4846 &vport->fc_portname, sizeof(struct lpfc_name));
dea3101e
JB
4847 rn->un.topologyDisc.unitType = RNID_HBA;
4848 rn->un.topologyDisc.physPort = 0;
4849 rn->un.topologyDisc.attachedNodes = 0;
4850 break;
4851 default:
4852 rn->CommonLen = 0;
4853 rn->SpecificLen = 0;
4854 break;
4855 }
4856
858c9f6c
JS
4857 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4858 "Issue ACC RNID: did:x%x flg:x%x",
4859 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4860
dea3101e 4861 phba->fc_stat.elsXmitACC++;
858c9f6c 4862 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e 4863
3772a991 4864 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
4865 if (rc == IOCB_ERROR) {
4866 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4867 return 1;
dea3101e 4868 }
c9f8735b 4869 return 0;
dea3101e
JB
4870}
4871
19ca7609
JS
4872/**
4873 * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
4874 * @vport: pointer to a virtual N_Port data structure.
4875 * @iocb: pointer to the lpfc command iocb data structure.
4876 * @ndlp: pointer to a node-list data structure.
4877 *
4878 * Return
4879 **/
4880static void
4881lpfc_els_clear_rrq(struct lpfc_vport *vport,
a0f2d3ef 4882 struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
19ca7609
JS
4883{
4884 struct lpfc_hba *phba = vport->phba;
4885 uint8_t *pcmd;
4886 struct RRQ *rrq;
4887 uint16_t rxid;
1151e3ec 4888 uint16_t xri;
19ca7609
JS
4889 struct lpfc_node_rrq *prrq;
4890
4891
4892 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
4893 pcmd += sizeof(uint32_t);
4894 rrq = (struct RRQ *)pcmd;
1151e3ec 4895 rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
9589b062 4896 rxid = bf_get(rrq_rxid, rrq);
19ca7609
JS
4897
4898 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4899 "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
4900 " x%x x%x\n",
1151e3ec 4901 be32_to_cpu(bf_get(rrq_did, rrq)),
9589b062 4902 bf_get(rrq_oxid, rrq),
19ca7609
JS
4903 rxid,
4904 iocb->iotag, iocb->iocb.ulpContext);
4905
4906 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4907 "Clear RRQ: did:x%x flg:x%x exchg:x%.08x",
4908 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
1151e3ec 4909 if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
9589b062 4910 xri = bf_get(rrq_oxid, rrq);
1151e3ec
JS
4911 else
4912 xri = rxid;
4913 prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
19ca7609 4914 if (prrq)
1151e3ec 4915 lpfc_clr_rrq_active(phba, xri, prrq);
19ca7609
JS
4916 return;
4917}
4918
12265f68
JS
4919/**
4920 * lpfc_els_rsp_echo_acc - Issue echo acc response
4921 * @vport: pointer to a virtual N_Port data structure.
4922 * @data: pointer to echo data to return in the accept.
4923 * @oldiocb: pointer to the original lpfc command iocb data structure.
4924 * @ndlp: pointer to a node-list data structure.
4925 *
4926 * Return code
4927 * 0 - Successfully issued acc echo response
4928 * 1 - Failed to issue acc echo response
4929 **/
4930static int
4931lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
4932 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4933{
4934 struct lpfc_hba *phba = vport->phba;
4935 struct lpfc_iocbq *elsiocb;
12265f68
JS
4936 uint8_t *pcmd;
4937 uint16_t cmdsize;
4938 int rc;
4939
12265f68
JS
4940 cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
4941
bf08611b
JS
4942 /* The accumulated length can exceed the BPL_SIZE. For
4943 * now, use this as the limit
4944 */
4945 if (cmdsize > LPFC_BPL_SIZE)
4946 cmdsize = LPFC_BPL_SIZE;
12265f68
JS
4947 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4948 ndlp->nlp_DID, ELS_CMD_ACC);
4949 if (!elsiocb)
4950 return 1;
4951
7851fe2c
JS
4952 elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext; /* Xri / rx_id */
4953 elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
4954
12265f68
JS
4955 /* Xmit ECHO ACC response tag <ulpIoTag> */
4956 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4957 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
4958 elsiocb->iotag, elsiocb->iocb.ulpContext);
4959 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4960 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4961 pcmd += sizeof(uint32_t);
4962 memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
4963
4964 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4965 "Issue ACC ECHO: did:x%x flg:x%x",
4966 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4967
4968 phba->fc_stat.elsXmitACC++;
4969 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
12265f68
JS
4970
4971 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4972 if (rc == IOCB_ERROR) {
4973 lpfc_els_free_iocb(phba, elsiocb);
4974 return 1;
4975 }
4976 return 0;
4977}
4978
e59058c4 4979/**
3621a710 4980 * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
e59058c4
JS
4981 * @vport: pointer to a host virtual N_Port data structure.
4982 *
4983 * This routine issues Address Discover (ADISC) ELS commands to those
4984 * N_Ports which are in node port recovery state and ADISC has not been issued
4985 * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
4986 * lpfc_issue_els_adisc() routine, the per @vport number of discover count
4987 * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
4988 * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
4989 * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4990 * IOCBs quit for later pick up. On the other hand, after walking through
4991 * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4992 * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4993 * no more ADISC need to be sent.
4994 *
4995 * Return code
4996 * The number of N_Ports with adisc issued.
4997 **/
dea3101e 4998int
2e0fef85 4999lpfc_els_disc_adisc(struct lpfc_vport *vport)
dea3101e 5000{
2e0fef85 5001 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 5002 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 5003 int sentadisc = 0;
dea3101e 5004
685f0bf7 5005 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2e0fef85 5006 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
5007 if (!NLP_CHK_NODE_ACT(ndlp))
5008 continue;
685f0bf7
JS
5009 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5010 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5011 (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
2e0fef85 5012 spin_lock_irq(shost->host_lock);
685f0bf7 5013 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2e0fef85 5014 spin_unlock_irq(shost->host_lock);
685f0bf7 5015 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
5016 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
5017 lpfc_issue_els_adisc(vport, ndlp, 0);
685f0bf7 5018 sentadisc++;
2e0fef85
JS
5019 vport->num_disc_nodes++;
5020 if (vport->num_disc_nodes >=
3de2a653 5021 vport->cfg_discovery_threads) {
2e0fef85
JS
5022 spin_lock_irq(shost->host_lock);
5023 vport->fc_flag |= FC_NLP_MORE;
5024 spin_unlock_irq(shost->host_lock);
685f0bf7 5025 break;
dea3101e
JB
5026 }
5027 }
5028 }
5029 if (sentadisc == 0) {
2e0fef85
JS
5030 spin_lock_irq(shost->host_lock);
5031 vport->fc_flag &= ~FC_NLP_MORE;
5032 spin_unlock_irq(shost->host_lock);
dea3101e 5033 }
2fe165b6 5034 return sentadisc;
dea3101e
JB
5035}
5036
e59058c4 5037/**
3621a710 5038 * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
e59058c4
JS
5039 * @vport: pointer to a host virtual N_Port data structure.
5040 *
5041 * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
5042 * which are in node port recovery state, with a @vport. Each time an ELS
5043 * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
5044 * the per @vport number of discover count (num_disc_nodes) shall be
5045 * incremented. If the num_disc_nodes reaches a pre-configured threshold
5046 * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
5047 * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
5048 * later pick up. On the other hand, after walking through all the ndlps with
5049 * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
5050 * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
5051 * PLOGI need to be sent.
5052 *
5053 * Return code
5054 * The number of N_Ports with plogi issued.
5055 **/
dea3101e 5056int
2e0fef85 5057lpfc_els_disc_plogi(struct lpfc_vport *vport)
dea3101e 5058{
2e0fef85 5059 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 5060 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 5061 int sentplogi = 0;
dea3101e 5062
2e0fef85
JS
5063 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
5064 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
5065 if (!NLP_CHK_NODE_ACT(ndlp))
5066 continue;
685f0bf7 5067 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
8b017a30
JS
5068 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5069 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
5070 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
685f0bf7 5071 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
5072 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5073 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
685f0bf7 5074 sentplogi++;
2e0fef85
JS
5075 vport->num_disc_nodes++;
5076 if (vport->num_disc_nodes >=
8b017a30 5077 vport->cfg_discovery_threads) {
2e0fef85
JS
5078 spin_lock_irq(shost->host_lock);
5079 vport->fc_flag |= FC_NLP_MORE;
5080 spin_unlock_irq(shost->host_lock);
685f0bf7 5081 break;
dea3101e
JB
5082 }
5083 }
5084 }
87af33fe
JS
5085 if (sentplogi) {
5086 lpfc_set_disctmo(vport);
5087 }
5088 else {
2e0fef85
JS
5089 spin_lock_irq(shost->host_lock);
5090 vport->fc_flag &= ~FC_NLP_MORE;
5091 spin_unlock_irq(shost->host_lock);
dea3101e 5092 }
2fe165b6 5093 return sentplogi;
dea3101e
JB
5094}
5095
bd4b3e5c 5096static uint32_t
86478875
JS
5097lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
5098 uint32_t word0)
5099{
5100
5101 desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
5102 desc->payload.els_req = word0;
5103 desc->length = cpu_to_be32(sizeof(desc->payload));
6c92d1d0
JS
5104
5105 return sizeof(struct fc_rdp_link_service_desc);
86478875
JS
5106}
5107
bd4b3e5c 5108static uint32_t
86478875
JS
5109lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
5110 uint8_t *page_a0, uint8_t *page_a2)
5111{
5112 uint16_t wavelength;
5113 uint16_t temperature;
5114 uint16_t rx_power;
5115 uint16_t tx_bias;
5116 uint16_t tx_power;
5117 uint16_t vcc;
5118 uint16_t flag = 0;
5119 struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
5120 struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
5121
5122 desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
5123
5124 trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
5125 &page_a0[SSF_TRANSCEIVER_CODE_B4];
5126 trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
5127 &page_a0[SSF_TRANSCEIVER_CODE_B5];
5128
5129 if ((trasn_code_byte4->fc_sw_laser) ||
5130 (trasn_code_byte5->fc_sw_laser_sl) ||
5131 (trasn_code_byte5->fc_sw_laser_sn)) { /* check if its short WL */
5132 flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
5133 } else if (trasn_code_byte4->fc_lw_laser) {
5134 wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
5135 page_a0[SSF_WAVELENGTH_B0];
5136 if (wavelength == SFP_WAVELENGTH_LC1310)
5137 flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
5138 if (wavelength == SFP_WAVELENGTH_LL1550)
5139 flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
5140 }
5141 /* check if its SFP+ */
5142 flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
5143 SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
5144 << SFP_FLAG_CT_SHIFT;
5145
5146 /* check if its OPTICAL */
5147 flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
5148 SFP_FLAG_IS_OPTICAL_PORT : 0)
5149 << SFP_FLAG_IS_OPTICAL_SHIFT;
5150
5151 temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
5152 page_a2[SFF_TEMPERATURE_B0]);
5153 vcc = (page_a2[SFF_VCC_B1] << 8 |
5154 page_a2[SFF_VCC_B0]);
5155 tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
5156 page_a2[SFF_TXPOWER_B0]);
5157 tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
5158 page_a2[SFF_TX_BIAS_CURRENT_B0]);
5159 rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
5160 page_a2[SFF_RXPOWER_B0]);
5161 desc->sfp_info.temperature = cpu_to_be16(temperature);
5162 desc->sfp_info.rx_power = cpu_to_be16(rx_power);
5163 desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
5164 desc->sfp_info.tx_power = cpu_to_be16(tx_power);
5165 desc->sfp_info.vcc = cpu_to_be16(vcc);
5166
5167 desc->sfp_info.flags = cpu_to_be16(flag);
5168 desc->length = cpu_to_be32(sizeof(desc->sfp_info));
6c92d1d0
JS
5169
5170 return sizeof(struct fc_rdp_sfp_desc);
86478875
JS
5171}
5172
bd4b3e5c 5173static uint32_t
86478875
JS
5174lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
5175 READ_LNK_VAR *stat)
5176{
5177 uint32_t type;
5178
5179 desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
5180
5181 type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
5182
5183 desc->info.port_type = cpu_to_be32(type);
5184
5185 desc->info.link_status.link_failure_cnt =
5186 cpu_to_be32(stat->linkFailureCnt);
5187 desc->info.link_status.loss_of_synch_cnt =
5188 cpu_to_be32(stat->lossSyncCnt);
5189 desc->info.link_status.loss_of_signal_cnt =
5190 cpu_to_be32(stat->lossSignalCnt);
5191 desc->info.link_status.primitive_seq_proto_err =
5192 cpu_to_be32(stat->primSeqErrCnt);
5193 desc->info.link_status.invalid_trans_word =
5194 cpu_to_be32(stat->invalidXmitWord);
5195 desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
5196
5197 desc->length = cpu_to_be32(sizeof(desc->info));
6c92d1d0
JS
5198
5199 return sizeof(struct fc_rdp_link_error_status_desc);
86478875
JS
5200}
5201
bd4b3e5c 5202static uint32_t
56204984
JS
5203lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
5204 struct lpfc_vport *vport)
5205{
3aaaa314
JS
5206 uint32_t bbCredit;
5207
56204984
JS
5208 desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
5209
3aaaa314
JS
5210 bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
5211 (vport->fc_sparam.cmn.bbCreditMsb << 8);
5212 desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
5213 if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
5214 bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
5215 (vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
5216 desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
5217 } else {
56204984 5218 desc->bbc_info.attached_port_bbc = 0;
3aaaa314 5219 }
56204984
JS
5220
5221 desc->bbc_info.rtt = 0;
5222 desc->length = cpu_to_be32(sizeof(desc->bbc_info));
6c92d1d0
JS
5223
5224 return sizeof(struct fc_rdp_bbc_desc);
56204984
JS
5225}
5226
bd4b3e5c 5227static uint32_t
310429ef
JS
5228lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
5229 struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
56204984 5230{
310429ef 5231 uint32_t flags = 0;
56204984
JS
5232
5233 desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5234
3aaaa314
JS
5235 desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
5236 desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
5237 desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
5238 desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
310429ef
JS
5239
5240 if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5241 flags |= RDP_OET_HIGH_ALARM;
5242 if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5243 flags |= RDP_OET_LOW_ALARM;
5244 if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5245 flags |= RDP_OET_HIGH_WARNING;
5246 if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5247 flags |= RDP_OET_LOW_WARNING;
5248
56204984
JS
5249 flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
5250 desc->oed_info.function_flags = cpu_to_be32(flags);
5251 desc->length = cpu_to_be32(sizeof(desc->oed_info));
6c92d1d0 5252 return sizeof(struct fc_rdp_oed_sfp_desc);
56204984
JS
5253}
5254
bd4b3e5c 5255static uint32_t
310429ef
JS
5256lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
5257 struct fc_rdp_oed_sfp_desc *desc,
56204984
JS
5258 uint8_t *page_a2)
5259{
310429ef 5260 uint32_t flags = 0;
56204984
JS
5261
5262 desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5263
3aaaa314
JS
5264 desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
5265 desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
5266 desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
5267 desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
310429ef
JS
5268
5269 if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5270 flags |= RDP_OET_HIGH_ALARM;
5271 if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5272 flags |= RDP_OET_LOW_ALARM;
5273 if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5274 flags |= RDP_OET_HIGH_WARNING;
5275 if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5276 flags |= RDP_OET_LOW_WARNING;
5277
56204984
JS
5278 flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
5279 desc->oed_info.function_flags = cpu_to_be32(flags);
5280 desc->length = cpu_to_be32(sizeof(desc->oed_info));
6c92d1d0 5281 return sizeof(struct fc_rdp_oed_sfp_desc);
56204984
JS
5282}
5283
bd4b3e5c 5284static uint32_t
310429ef
JS
5285lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
5286 struct fc_rdp_oed_sfp_desc *desc,
56204984
JS
5287 uint8_t *page_a2)
5288{
310429ef 5289 uint32_t flags = 0;
56204984
JS
5290
5291 desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5292
3aaaa314
JS
5293 desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
5294 desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
5295 desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
5296 desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
310429ef
JS
5297
5298 if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5299 flags |= RDP_OET_HIGH_ALARM;
5300 if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
5301 flags |= RDP_OET_LOW_ALARM;
5302 if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5303 flags |= RDP_OET_HIGH_WARNING;
5304 if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
5305 flags |= RDP_OET_LOW_WARNING;
5306
56204984
JS
5307 flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
5308 desc->oed_info.function_flags = cpu_to_be32(flags);
5309 desc->length = cpu_to_be32(sizeof(desc->oed_info));
6c92d1d0 5310 return sizeof(struct fc_rdp_oed_sfp_desc);
56204984
JS
5311}
5312
bd4b3e5c 5313static uint32_t
310429ef
JS
5314lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
5315 struct fc_rdp_oed_sfp_desc *desc,
56204984
JS
5316 uint8_t *page_a2)
5317{
310429ef 5318 uint32_t flags = 0;
56204984
JS
5319
5320 desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5321
3aaaa314
JS
5322 desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
5323 desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
5324 desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
5325 desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
310429ef
JS
5326
5327 if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5328 flags |= RDP_OET_HIGH_ALARM;
5329 if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
5330 flags |= RDP_OET_LOW_ALARM;
5331 if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5332 flags |= RDP_OET_HIGH_WARNING;
5333 if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
5334 flags |= RDP_OET_LOW_WARNING;
5335
56204984
JS
5336 flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
5337 desc->oed_info.function_flags = cpu_to_be32(flags);
5338 desc->length = cpu_to_be32(sizeof(desc->oed_info));
6c92d1d0 5339 return sizeof(struct fc_rdp_oed_sfp_desc);
56204984
JS
5340}
5341
5342
bd4b3e5c 5343static uint32_t
310429ef
JS
5344lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
5345 struct fc_rdp_oed_sfp_desc *desc,
56204984
JS
5346 uint8_t *page_a2)
5347{
310429ef 5348 uint32_t flags = 0;
56204984
JS
5349
5350 desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5351
3aaaa314
JS
5352 desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
5353 desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
5354 desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
5355 desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
310429ef
JS
5356
5357 if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5358 flags |= RDP_OET_HIGH_ALARM;
5359 if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
5360 flags |= RDP_OET_LOW_ALARM;
5361 if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5362 flags |= RDP_OET_HIGH_WARNING;
5363 if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
5364 flags |= RDP_OET_LOW_WARNING;
5365
56204984
JS
5366 flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
5367 desc->oed_info.function_flags = cpu_to_be32(flags);
5368 desc->length = cpu_to_be32(sizeof(desc->oed_info));
6c92d1d0 5369 return sizeof(struct fc_rdp_oed_sfp_desc);
56204984
JS
5370}
5371
bd4b3e5c 5372static uint32_t
56204984
JS
5373lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
5374 uint8_t *page_a0, struct lpfc_vport *vport)
5375{
5376 desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
5377 memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
5378 memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
5379 memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
a0f2d3ef 5380 memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
56204984
JS
5381 memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
5382 desc->length = cpu_to_be32(sizeof(desc->opd_info));
6c92d1d0 5383 return sizeof(struct fc_rdp_opd_sfp_desc);
56204984
JS
5384}
5385
bd4b3e5c 5386static uint32_t
4258e98e
JS
5387lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
5388{
5389 if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
5390 return 0;
5391 desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
5392
5393 desc->info.CorrectedBlocks =
5394 cpu_to_be32(stat->fecCorrBlkCount);
5395 desc->info.UncorrectableBlocks =
5396 cpu_to_be32(stat->fecUncorrBlkCount);
5397
5398 desc->length = cpu_to_be32(sizeof(desc->info));
5399
5400 return sizeof(struct fc_fec_rdp_desc);
5401}
5402
bd4b3e5c 5403static uint32_t
86478875
JS
5404lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
5405{
5406 uint16_t rdp_cap = 0;
5407 uint16_t rdp_speed;
5408
5409 desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
5410
81e75177
JS
5411 switch (phba->fc_linkspeed) {
5412 case LPFC_LINK_SPEED_1GHZ:
86478875
JS
5413 rdp_speed = RDP_PS_1GB;
5414 break;
81e75177 5415 case LPFC_LINK_SPEED_2GHZ:
86478875
JS
5416 rdp_speed = RDP_PS_2GB;
5417 break;
81e75177 5418 case LPFC_LINK_SPEED_4GHZ:
86478875
JS
5419 rdp_speed = RDP_PS_4GB;
5420 break;
81e75177 5421 case LPFC_LINK_SPEED_8GHZ:
86478875
JS
5422 rdp_speed = RDP_PS_8GB;
5423 break;
81e75177 5424 case LPFC_LINK_SPEED_10GHZ:
86478875
JS
5425 rdp_speed = RDP_PS_10GB;
5426 break;
81e75177 5427 case LPFC_LINK_SPEED_16GHZ:
86478875
JS
5428 rdp_speed = RDP_PS_16GB;
5429 break;
a085e87c
JS
5430 case LPFC_LINK_SPEED_32GHZ:
5431 rdp_speed = RDP_PS_32GB;
5432 break;
fbd8a6ba
JS
5433 case LPFC_LINK_SPEED_64GHZ:
5434 rdp_speed = RDP_PS_64GB;
5435 break;
86478875
JS
5436 default:
5437 rdp_speed = RDP_PS_UNKNOWN;
5438 break;
5439 }
5440
5441 desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
5442
1dc5ec24
JS
5443 if (phba->lmt & LMT_128Gb)
5444 rdp_cap |= RDP_PS_128GB;
fbd8a6ba
JS
5445 if (phba->lmt & LMT_64Gb)
5446 rdp_cap |= RDP_PS_64GB;
d38dd52c
JS
5447 if (phba->lmt & LMT_32Gb)
5448 rdp_cap |= RDP_PS_32GB;
86478875
JS
5449 if (phba->lmt & LMT_16Gb)
5450 rdp_cap |= RDP_PS_16GB;
5451 if (phba->lmt & LMT_10Gb)
5452 rdp_cap |= RDP_PS_10GB;
5453 if (phba->lmt & LMT_8Gb)
5454 rdp_cap |= RDP_PS_8GB;
5455 if (phba->lmt & LMT_4Gb)
5456 rdp_cap |= RDP_PS_4GB;
5457 if (phba->lmt & LMT_2Gb)
5458 rdp_cap |= RDP_PS_2GB;
5459 if (phba->lmt & LMT_1Gb)
5460 rdp_cap |= RDP_PS_1GB;
5461
5462 if (rdp_cap == 0)
5463 rdp_cap = RDP_CAP_UNKNOWN;
56204984
JS
5464 if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
5465 rdp_cap |= RDP_CAP_USER_CONFIGURED;
86478875
JS
5466
5467 desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
5468 desc->length = cpu_to_be32(sizeof(desc->info));
6c92d1d0 5469 return sizeof(struct fc_rdp_port_speed_desc);
86478875
JS
5470}
5471
bd4b3e5c 5472static uint32_t
86478875 5473lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
cd46cded 5474 struct lpfc_vport *vport)
86478875
JS
5475{
5476
5477 desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5478
cd46cded 5479 memcpy(desc->port_names.wwnn, &vport->fc_nodename,
86478875
JS
5480 sizeof(desc->port_names.wwnn));
5481
cd46cded 5482 memcpy(desc->port_names.wwpn, &vport->fc_portname,
86478875
JS
5483 sizeof(desc->port_names.wwpn));
5484
5485 desc->length = cpu_to_be32(sizeof(desc->port_names));
6c92d1d0 5486 return sizeof(struct fc_rdp_port_name_desc);
86478875
JS
5487}
5488
bd4b3e5c 5489static uint32_t
86478875
JS
5490lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
5491 struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5492{
5493
5494 desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5495 if (vport->fc_flag & FC_FABRIC) {
5496 memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
5497 sizeof(desc->port_names.wwnn));
5498
5499 memcpy(desc->port_names.wwpn, &vport->fabric_portname,
5500 sizeof(desc->port_names.wwpn));
5501 } else { /* Point to Point */
5502 memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
5503 sizeof(desc->port_names.wwnn));
5504
5505 memcpy(desc->port_names.wwnn, &ndlp->nlp_portname,
5506 sizeof(desc->port_names.wwpn));
5507 }
5508
5509 desc->length = cpu_to_be32(sizeof(desc->port_names));
6c92d1d0 5510 return sizeof(struct fc_rdp_port_name_desc);
86478875
JS
5511}
5512
bd4b3e5c 5513static void
86478875
JS
5514lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
5515 int status)
5516{
5517 struct lpfc_nodelist *ndlp = rdp_context->ndlp;
5518 struct lpfc_vport *vport = ndlp->vport;
5519 struct lpfc_iocbq *elsiocb;
eb8d68c9 5520 struct ulp_bde64 *bpl;
86478875
JS
5521 IOCB_t *icmd;
5522 uint8_t *pcmd;
5523 struct ls_rjt *stat;
5524 struct fc_rdp_res_frame *rdp_res;
6c92d1d0 5525 uint32_t cmdsize, len;
310429ef 5526 uint16_t *flag_ptr;
6c92d1d0 5527 int rc;
86478875
JS
5528
5529 if (status != SUCCESS)
5530 goto error;
eb8d68c9
JS
5531
5532 /* This will change once we know the true size of the RDP payload */
86478875
JS
5533 cmdsize = sizeof(struct fc_rdp_res_frame);
5534
5535 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
5536 lpfc_max_els_tries, rdp_context->ndlp,
5537 rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
5538 lpfc_nlp_put(ndlp);
5539 if (!elsiocb)
5540 goto free_rdp_context;
5541
5542 icmd = &elsiocb->iocb;
5543 icmd->ulpContext = rdp_context->rx_id;
5544 icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5545
5546 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5547 "2171 Xmit RDP response tag x%x xri x%x, "
5548 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
5549 elsiocb->iotag, elsiocb->iocb.ulpContext,
5550 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5551 ndlp->nlp_rpi);
5552 rdp_res = (struct fc_rdp_res_frame *)
5553 (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5554 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5555 memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
5556 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5557
310429ef
JS
5558 /* Update Alarm and Warning */
5559 flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
5560 phba->sfp_alarm |= *flag_ptr;
5561 flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
5562 phba->sfp_warning |= *flag_ptr;
5563
86478875 5564 /* For RDP payload */
6c92d1d0
JS
5565 len = 8;
5566 len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
5567 (len + pcmd), ELS_CMD_RDP);
86478875 5568
6c92d1d0 5569 len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
86478875 5570 rdp_context->page_a0, rdp_context->page_a2);
6c92d1d0
JS
5571 len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
5572 phba);
5573 len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
5574 (len + pcmd), &rdp_context->link_stat);
5575 len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
cd46cded 5576 (len + pcmd), vport);
6c92d1d0
JS
5577 len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
5578 (len + pcmd), vport, ndlp);
5579 len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
4258e98e 5580 &rdp_context->link_stat);
6c92d1d0
JS
5581 len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
5582 &rdp_context->link_stat, vport);
5583 len += lpfc_rdp_res_oed_temp_desc(phba,
5584 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5585 rdp_context->page_a2);
5586 len += lpfc_rdp_res_oed_voltage_desc(phba,
5587 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5588 rdp_context->page_a2);
5589 len += lpfc_rdp_res_oed_txbias_desc(phba,
5590 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5591 rdp_context->page_a2);
5592 len += lpfc_rdp_res_oed_txpower_desc(phba,
5593 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5594 rdp_context->page_a2);
5595 len += lpfc_rdp_res_oed_rxpower_desc(phba,
5596 (struct fc_rdp_oed_sfp_desc *)(len + pcmd),
5597 rdp_context->page_a2);
5598 len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
5599 rdp_context->page_a0, vport);
5600
6c92d1d0 5601 rdp_res->length = cpu_to_be32(len - 8);
86478875
JS
5602 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5603
eb8d68c9
JS
5604 /* Now that we know the true size of the payload, update the BPL */
5605 bpl = (struct ulp_bde64 *)
5606 (((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
6c92d1d0 5607 bpl->tus.f.bdeSize = len;
eb8d68c9
JS
5608 bpl->tus.f.bdeFlags = 0;
5609 bpl->tus.w = le32_to_cpu(bpl->tus.w);
5610
86478875
JS
5611 phba->fc_stat.elsXmitACC++;
5612 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5613 if (rc == IOCB_ERROR)
5614 lpfc_els_free_iocb(phba, elsiocb);
5615
5616 kfree(rdp_context);
5617
5618 return;
5619error:
5620 cmdsize = 2 * sizeof(uint32_t);
5621 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
5622 ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
5623 lpfc_nlp_put(ndlp);
5624 if (!elsiocb)
5625 goto free_rdp_context;
5626
5627 icmd = &elsiocb->iocb;
5628 icmd->ulpContext = rdp_context->rx_id;
5629 icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5630 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5631
5632 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
5633 stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5634 stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5635
5636 phba->fc_stat.elsXmitLSRJT++;
5637 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5638 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5639
5640 if (rc == IOCB_ERROR)
5641 lpfc_els_free_iocb(phba, elsiocb);
5642free_rdp_context:
5643 kfree(rdp_context);
5644}
5645
bd4b3e5c 5646static int
86478875
JS
5647lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
5648{
5649 LPFC_MBOXQ_t *mbox = NULL;
5650 int rc;
5651
5652 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5653 if (!mbox) {
5654 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
5655 "7105 failed to allocate mailbox memory");
5656 return 1;
5657 }
5658
5659 if (lpfc_sli4_dump_page_a0(phba, mbox))
5660 goto prep_mbox_fail;
5661 mbox->vport = rdp_context->ndlp->vport;
5662 mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
3e1f0718 5663 mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
86478875
JS
5664 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5665 if (rc == MBX_NOT_FINISHED)
5666 goto issue_mbox_fail;
5667
5668 return 0;
5669
5670prep_mbox_fail:
5671issue_mbox_fail:
5672 mempool_free(mbox, phba->mbox_mem_pool);
5673 return 1;
5674}
5675
5676/*
5677 * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
5678 * @vport: pointer to a host virtual N_Port data structure.
5679 * @cmdiocb: pointer to lpfc command iocb data structure.
5680 * @ndlp: pointer to a node-list data structure.
5681 *
5682 * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
5683 * IOCB. First, the payload of the unsolicited RDP is checked.
5684 * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
5685 * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
5686 * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
5687 * gather all data and send RDP response.
5688 *
5689 * Return code
5690 * 0 - Sent the acc response
5691 * 1 - Sent the reject response.
5692 */
5693static int
5694lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5695 struct lpfc_nodelist *ndlp)
5696{
5697 struct lpfc_hba *phba = vport->phba;
5698 struct lpfc_dmabuf *pcmd;
5699 uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
5700 struct fc_rdp_req_frame *rdp_req;
5701 struct lpfc_rdp_context *rdp_context;
5702 IOCB_t *cmd = NULL;
5703 struct ls_rjt stat;
5704
5705 if (phba->sli_rev < LPFC_SLI_REV4 ||
a0f2d3ef
JS
5706 bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
5707 LPFC_SLI_INTF_IF_TYPE_2) {
86478875
JS
5708 rjt_err = LSRJT_UNABLE_TPC;
5709 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5710 goto error;
5711 }
5712
5713 if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
5714 rjt_err = LSRJT_UNABLE_TPC;
5715 rjt_expl = LSEXP_REQ_UNSUPPORTED;
5716 goto error;
5717 }
5718
5719 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5720 rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
5721
86478875
JS
5722 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5723 "2422 ELS RDP Request "
5724 "dec len %d tag x%x port_id %d len %d\n",
5725 be32_to_cpu(rdp_req->rdp_des_length),
5726 be32_to_cpu(rdp_req->nport_id_desc.tag),
5727 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
5728 be32_to_cpu(rdp_req->nport_id_desc.length));
5729
5730 if (sizeof(struct fc_rdp_nport_desc) !=
5731 be32_to_cpu(rdp_req->rdp_des_length))
5732 goto rjt_logerr;
5733 if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
5734 goto rjt_logerr;
5735 if (RDP_NPORT_ID_SIZE !=
5736 be32_to_cpu(rdp_req->nport_id_desc.length))
5737 goto rjt_logerr;
699acd62 5738 rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
86478875
JS
5739 if (!rdp_context) {
5740 rjt_err = LSRJT_UNABLE_TPC;
5741 goto error;
5742 }
5743
86478875
JS
5744 cmd = &cmdiocb->iocb;
5745 rdp_context->ndlp = lpfc_nlp_get(ndlp);
5746 rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
5747 rdp_context->rx_id = cmd->ulpContext;
5748 rdp_context->cmpl = lpfc_els_rdp_cmpl;
5749 if (lpfc_get_rdp_info(phba, rdp_context)) {
5750 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
5751 "2423 Unable to send mailbox");
5752 kfree(rdp_context);
5753 rjt_err = LSRJT_UNABLE_TPC;
5754 lpfc_nlp_put(ndlp);
5755 goto error;
5756 }
5757
5758 return 0;
5759
5760rjt_logerr:
5761 rjt_err = LSRJT_LOGICAL_ERR;
5762
5763error:
5764 memset(&stat, 0, sizeof(stat));
5765 stat.un.b.lsRjtRsnCode = rjt_err;
5766 stat.un.b.lsRjtRsnCodeExp = rjt_expl;
5767 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5768 return 1;
5769}
5770
5771
8b017a30
JS
5772static void
5773lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5774{
5775 MAILBOX_t *mb;
5776 IOCB_t *icmd;
5777 uint8_t *pcmd;
5778 struct lpfc_iocbq *elsiocb;
5779 struct lpfc_nodelist *ndlp;
5780 struct ls_rjt *stat;
481ad967 5781 union lpfc_sli4_cfg_shdr *shdr;
8b017a30
JS
5782 struct lpfc_lcb_context *lcb_context;
5783 struct fc_lcb_res_frame *lcb_res;
481ad967 5784 uint32_t cmdsize, shdr_status, shdr_add_status;
8b017a30
JS
5785 int rc;
5786
5787 mb = &pmb->u.mb;
3e1f0718 5788 lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
8b017a30 5789 ndlp = lcb_context->ndlp;
3e1f0718
JS
5790 pmb->ctx_ndlp = NULL;
5791 pmb->ctx_buf = NULL;
8b017a30 5792
481ad967
JS
5793 shdr = (union lpfc_sli4_cfg_shdr *)
5794 &pmb->u.mqe.un.beacon_config.header.cfg_shdr;
5795 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5796 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5797
5798 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
5799 "0194 SET_BEACON_CONFIG mailbox "
5800 "completed with status x%x add_status x%x,"
5801 " mbx status x%x\n",
5802 shdr_status, shdr_add_status, mb->mbxStatus);
5803
66e9e6bf
JS
5804 if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
5805 (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
5806 (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
8b017a30
JS
5807 mempool_free(pmb, phba->mbox_mem_pool);
5808 goto error;
5809 }
5810
5811 mempool_free(pmb, phba->mbox_mem_pool);
8b017a30
JS
5812 cmdsize = sizeof(struct fc_lcb_res_frame);
5813 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5814 lpfc_max_els_tries, ndlp,
5815 ndlp->nlp_DID, ELS_CMD_ACC);
5816
5817 /* Decrement the ndlp reference count from previous mbox command */
5818 lpfc_nlp_put(ndlp);
5819
5820 if (!elsiocb)
5821 goto free_lcb_context;
5822
5823 lcb_res = (struct fc_lcb_res_frame *)
5824 (((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5825
24bc3119 5826 memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
8b017a30
JS
5827 icmd = &elsiocb->iocb;
5828 icmd->ulpContext = lcb_context->rx_id;
5829 icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
5830
5831 pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5832 *((uint32_t *)(pcmd)) = ELS_CMD_ACC;
5833 lcb_res->lcb_sub_command = lcb_context->sub_command;
5834 lcb_res->lcb_type = lcb_context->type;
24bc3119 5835 lcb_res->capability = lcb_context->capability;
8b017a30 5836 lcb_res->lcb_frequency = lcb_context->frequency;
66e9e6bf 5837 lcb_res->lcb_duration = lcb_context->duration;
8b017a30
JS
5838 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5839 phba->fc_stat.elsXmitACC++;
5840 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5841 if (rc == IOCB_ERROR)
5842 lpfc_els_free_iocb(phba, elsiocb);
5843
5844 kfree(lcb_context);
5845 return;
5846
5847error:
5848 cmdsize = sizeof(struct fc_lcb_res_frame);
5849 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5850 lpfc_max_els_tries, ndlp,
5851 ndlp->nlp_DID, ELS_CMD_LS_RJT);
5852 lpfc_nlp_put(ndlp);
5853 if (!elsiocb)
5854 goto free_lcb_context;
5855
5856 icmd = &elsiocb->iocb;
5857 icmd->ulpContext = lcb_context->rx_id;
5858 icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
5859 pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
5860
5861 *((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
5862 stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
5863 stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5864
b114d900
JS
5865 if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
5866 stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
5867
8b017a30
JS
5868 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5869 phba->fc_stat.elsXmitLSRJT++;
5870 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5871 if (rc == IOCB_ERROR)
5872 lpfc_els_free_iocb(phba, elsiocb);
5873free_lcb_context:
5874 kfree(lcb_context);
5875}
5876
5877static int
5878lpfc_sli4_set_beacon(struct lpfc_vport *vport,
5879 struct lpfc_lcb_context *lcb_context,
5880 uint32_t beacon_state)
5881{
5882 struct lpfc_hba *phba = vport->phba;
66e9e6bf 5883 union lpfc_sli4_cfg_shdr *cfg_shdr;
8b017a30
JS
5884 LPFC_MBOXQ_t *mbox = NULL;
5885 uint32_t len;
5886 int rc;
5887
5888 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5889 if (!mbox)
5890 return 1;
5891
66e9e6bf 5892 cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
8b017a30
JS
5893 len = sizeof(struct lpfc_mbx_set_beacon_config) -
5894 sizeof(struct lpfc_sli4_cfg_mhdr);
5895 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5896 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
5897 LPFC_SLI4_MBX_EMBED);
3e1f0718 5898 mbox->ctx_ndlp = (void *)lcb_context;
8b017a30
JS
5899 mbox->vport = phba->pport;
5900 mbox->mbox_cmpl = lpfc_els_lcb_rsp;
5901 bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
5902 phba->sli4_hba.physical_port);
5903 bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
5904 beacon_state);
66e9e6bf
JS
5905 mbox->u.mqe.un.beacon_config.word5 = 0; /* Reserved */
5906
5907 /*
5908 * Check bv1s bit before issuing the mailbox
5909 * if bv1s == 1, LCB V1 supported
5910 * else, LCB V0 supported
5911 */
5912
5913 if (phba->sli4_hba.pc_sli4_params.bv1s) {
5914 /* COMMON_SET_BEACON_CONFIG_V1 */
5915 cfg_shdr->request.word9 = BEACON_VERSION_V1;
5916 lcb_context->capability |= LCB_CAPABILITY_DURATION;
5917 bf_set(lpfc_mbx_set_beacon_port_type,
5918 &mbox->u.mqe.un.beacon_config, 0);
5919 bf_set(lpfc_mbx_set_beacon_duration_v1,
5920 &mbox->u.mqe.un.beacon_config,
5921 be16_to_cpu(lcb_context->duration));
5922 } else {
5923 /* COMMON_SET_BEACON_CONFIG_V0 */
5924 if (be16_to_cpu(lcb_context->duration) != 0) {
5925 mempool_free(mbox, phba->mbox_mem_pool);
5926 return 1;
5927 }
5928 cfg_shdr->request.word9 = BEACON_VERSION_V0;
5929 lcb_context->capability &= ~(LCB_CAPABILITY_DURATION);
5930 bf_set(lpfc_mbx_set_beacon_state,
5931 &mbox->u.mqe.un.beacon_config, beacon_state);
5932 bf_set(lpfc_mbx_set_beacon_port_type,
5933 &mbox->u.mqe.un.beacon_config, 1);
5934 bf_set(lpfc_mbx_set_beacon_duration,
5935 &mbox->u.mqe.un.beacon_config,
5936 be16_to_cpu(lcb_context->duration));
5937 }
5938
8b017a30
JS
5939 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5940 if (rc == MBX_NOT_FINISHED) {
5941 mempool_free(mbox, phba->mbox_mem_pool);
5942 return 1;
5943 }
5944
5945 return 0;
5946}
5947
5948
5949/**
5950 * lpfc_els_rcv_lcb - Process an unsolicited LCB
5951 * @vport: pointer to a host virtual N_Port data structure.
5952 * @cmdiocb: pointer to lpfc command iocb data structure.
5953 * @ndlp: pointer to a node-list data structure.
5954 *
5955 * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
5956 * First, the payload of the unsolicited LCB is checked.
5957 * Then based on Subcommand beacon will either turn on or off.
5958 *
5959 * Return code
5960 * 0 - Sent the acc response
5961 * 1 - Sent the reject response.
5962 **/
5963static int
5964lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5965 struct lpfc_nodelist *ndlp)
5966{
5967 struct lpfc_hba *phba = vport->phba;
5968 struct lpfc_dmabuf *pcmd;
8b017a30
JS
5969 uint8_t *lp;
5970 struct fc_lcb_request_frame *beacon;
5971 struct lpfc_lcb_context *lcb_context;
5972 uint8_t state, rjt_err;
5973 struct ls_rjt stat;
5974
8b017a30
JS
5975 pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
5976 lp = (uint8_t *)pcmd->virt;
5977 beacon = (struct fc_lcb_request_frame *)pcmd->virt;
5978
5979 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5980 "0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
5981 "type x%x frequency %x duration x%x\n",
5982 lp[0], lp[1], lp[2],
5983 beacon->lcb_command,
5984 beacon->lcb_sub_command,
5985 beacon->lcb_type,
5986 beacon->lcb_frequency,
5987 be16_to_cpu(beacon->lcb_duration));
5988
d6564e52
JS
5989 if (beacon->lcb_sub_command != LPFC_LCB_ON &&
5990 beacon->lcb_sub_command != LPFC_LCB_OFF) {
8b017a30
JS
5991 rjt_err = LSRJT_CMD_UNSUPPORTED;
5992 goto rjt;
5993 }
66e9e6bf
JS
5994
5995 if (phba->sli_rev < LPFC_SLI_REV4 ||
5996 phba->hba_flag & HBA_FCOE_MODE ||
5997 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
5998 LPFC_SLI_INTF_IF_TYPE_2)) {
8b017a30
JS
5999 rjt_err = LSRJT_CMD_UNSUPPORTED;
6000 goto rjt;
6001 }
6002
e7950423
SM
6003 lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
6004 if (!lcb_context) {
6005 rjt_err = LSRJT_UNABLE_TPC;
6006 goto rjt;
6007 }
6008
8b017a30
JS
6009 state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
6010 lcb_context->sub_command = beacon->lcb_sub_command;
66e9e6bf 6011 lcb_context->capability = 0;
8b017a30
JS
6012 lcb_context->type = beacon->lcb_type;
6013 lcb_context->frequency = beacon->lcb_frequency;
66e9e6bf 6014 lcb_context->duration = beacon->lcb_duration;
8b017a30
JS
6015 lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6016 lcb_context->rx_id = cmdiocb->iocb.ulpContext;
6017 lcb_context->ndlp = lpfc_nlp_get(ndlp);
6018 if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
6019 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
6020 LOG_ELS, "0193 failed to send mail box");
e7950423 6021 kfree(lcb_context);
8b017a30
JS
6022 lpfc_nlp_put(ndlp);
6023 rjt_err = LSRJT_UNABLE_TPC;
6024 goto rjt;
6025 }
6026 return 0;
6027rjt:
6028 memset(&stat, 0, sizeof(stat));
6029 stat.un.b.lsRjtRsnCode = rjt_err;
6030 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6031 return 1;
6032}
6033
6034
e59058c4 6035/**
3621a710 6036 * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
e59058c4
JS
6037 * @vport: pointer to a host virtual N_Port data structure.
6038 *
6039 * This routine cleans up any Registration State Change Notification
6040 * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
6041 * @vport together with the host_lock is used to prevent multiple thread
6042 * trying to access the RSCN array on a same @vport at the same time.
6043 **/
92d7f7b0 6044void
2e0fef85 6045lpfc_els_flush_rscn(struct lpfc_vport *vport)
dea3101e 6046{
2e0fef85
JS
6047 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6048 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
6049 int i;
6050
7f5f3d0d
JS
6051 spin_lock_irq(shost->host_lock);
6052 if (vport->fc_rscn_flush) {
6053 /* Another thread is walking fc_rscn_id_list on this vport */
6054 spin_unlock_irq(shost->host_lock);
6055 return;
6056 }
6057 /* Indicate we are walking lpfc_els_flush_rscn on this vport */
6058 vport->fc_rscn_flush = 1;
6059 spin_unlock_irq(shost->host_lock);
6060
2e0fef85 6061 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
92d7f7b0 6062 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
2e0fef85 6063 vport->fc_rscn_id_list[i] = NULL;
dea3101e 6064 }
2e0fef85
JS
6065 spin_lock_irq(shost->host_lock);
6066 vport->fc_rscn_id_cnt = 0;
6067 vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
6068 spin_unlock_irq(shost->host_lock);
6069 lpfc_can_disctmo(vport);
7f5f3d0d
JS
6070 /* Indicate we are done walking this fc_rscn_id_list */
6071 vport->fc_rscn_flush = 0;
dea3101e
JB
6072}
6073
e59058c4 6074/**
3621a710 6075 * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
e59058c4
JS
6076 * @vport: pointer to a host virtual N_Port data structure.
6077 * @did: remote destination port identifier.
6078 *
6079 * This routine checks whether there is any pending Registration State
6080 * Configuration Notification (RSCN) to a @did on @vport.
6081 *
6082 * Return code
6083 * None zero - The @did matched with a pending rscn
6084 * 0 - not able to match @did with a pending rscn
6085 **/
dea3101e 6086int
2e0fef85 6087lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
dea3101e
JB
6088{
6089 D_ID ns_did;
6090 D_ID rscn_did;
dea3101e 6091 uint32_t *lp;
92d7f7b0 6092 uint32_t payload_len, i;
7f5f3d0d 6093 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
6094
6095 ns_did.un.word = did;
dea3101e
JB
6096
6097 /* Never match fabric nodes for RSCNs */
6098 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2e0fef85 6099 return 0;
dea3101e
JB
6100
6101 /* If we are doing a FULL RSCN rediscovery, match everything */
2e0fef85 6102 if (vport->fc_flag & FC_RSCN_DISCOVERY)
c9f8735b 6103 return did;
dea3101e 6104
7f5f3d0d
JS
6105 spin_lock_irq(shost->host_lock);
6106 if (vport->fc_rscn_flush) {
6107 /* Another thread is walking fc_rscn_id_list on this vport */
6108 spin_unlock_irq(shost->host_lock);
6109 return 0;
6110 }
6111 /* Indicate we are walking fc_rscn_id_list on this vport */
6112 vport->fc_rscn_flush = 1;
6113 spin_unlock_irq(shost->host_lock);
2e0fef85 6114 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
92d7f7b0
JS
6115 lp = vport->fc_rscn_id_list[i]->virt;
6116 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6117 payload_len -= sizeof(uint32_t); /* take off word 0 */
dea3101e 6118 while (payload_len) {
92d7f7b0
JS
6119 rscn_did.un.word = be32_to_cpu(*lp++);
6120 payload_len -= sizeof(uint32_t);
eaf15d5b
JS
6121 switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
6122 case RSCN_ADDRESS_FORMAT_PORT:
6fb120a7
JS
6123 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6124 && (ns_did.un.b.area == rscn_did.un.b.area)
6125 && (ns_did.un.b.id == rscn_did.un.b.id))
7f5f3d0d 6126 goto return_did_out;
dea3101e 6127 break;
eaf15d5b 6128 case RSCN_ADDRESS_FORMAT_AREA:
dea3101e
JB
6129 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6130 && (ns_did.un.b.area == rscn_did.un.b.area))
7f5f3d0d 6131 goto return_did_out;
dea3101e 6132 break;
eaf15d5b 6133 case RSCN_ADDRESS_FORMAT_DOMAIN:
dea3101e 6134 if (ns_did.un.b.domain == rscn_did.un.b.domain)
7f5f3d0d 6135 goto return_did_out;
dea3101e 6136 break;
eaf15d5b 6137 case RSCN_ADDRESS_FORMAT_FABRIC:
7f5f3d0d 6138 goto return_did_out;
dea3101e
JB
6139 }
6140 }
92d7f7b0 6141 }
7f5f3d0d
JS
6142 /* Indicate we are done with walking fc_rscn_id_list on this vport */
6143 vport->fc_rscn_flush = 0;
92d7f7b0 6144 return 0;
7f5f3d0d
JS
6145return_did_out:
6146 /* Indicate we are done with walking fc_rscn_id_list on this vport */
6147 vport->fc_rscn_flush = 0;
6148 return did;
dea3101e
JB
6149}
6150
e59058c4 6151/**
3621a710 6152 * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
e59058c4
JS
6153 * @vport: pointer to a host virtual N_Port data structure.
6154 *
6155 * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
6156 * state machine for a @vport's nodes that are with pending RSCN (Registration
6157 * State Change Notification).
6158 *
6159 * Return code
6160 * 0 - Successful (currently alway return 0)
6161 **/
dea3101e 6162static int
2e0fef85 6163lpfc_rscn_recovery_check(struct lpfc_vport *vport)
dea3101e 6164{
685f0bf7 6165 struct lpfc_nodelist *ndlp = NULL;
dea3101e 6166
0d2b6b83 6167 /* Move all affected nodes by pending RSCNs to NPR state. */
2e0fef85 6168 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093 6169 if (!NLP_CHK_NODE_ACT(ndlp) ||
0d2b6b83
JS
6170 (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
6171 !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
685f0bf7 6172 continue;
1c5b12f7
JS
6173
6174 /* NVME Target mode does not do RSCN Recovery. */
8c258641
JS
6175 if (vport->phba->nvmet_support)
6176 continue;
1c5b12f7 6177
dea16bda
JS
6178 /* If we are in the process of doing discovery on this
6179 * NPort, let it continue on its own.
6180 */
6181 switch (ndlp->nlp_state) {
6182 case NLP_STE_PLOGI_ISSUE:
6183 case NLP_STE_ADISC_ISSUE:
6184 case NLP_STE_REG_LOGIN_ISSUE:
6185 case NLP_STE_PRLI_ISSUE:
6186 case NLP_STE_LOGO_ISSUE:
6187 continue;
6188 }
6189
6190
2e0fef85 6191 lpfc_disc_state_machine(vport, ndlp, NULL,
0d2b6b83
JS
6192 NLP_EVT_DEVICE_RECOVERY);
6193 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea3101e 6194 }
c9f8735b 6195 return 0;
dea3101e
JB
6196}
6197
ddcc50f0 6198/**
3621a710 6199 * lpfc_send_rscn_event - Send an RSCN event to management application
ddcc50f0
JS
6200 * @vport: pointer to a host virtual N_Port data structure.
6201 * @cmdiocb: pointer to lpfc command iocb data structure.
6202 *
6203 * lpfc_send_rscn_event sends an RSCN netlink event to management
6204 * applications.
6205 */
6206static void
6207lpfc_send_rscn_event(struct lpfc_vport *vport,
6208 struct lpfc_iocbq *cmdiocb)
6209{
6210 struct lpfc_dmabuf *pcmd;
6211 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6212 uint32_t *payload_ptr;
6213 uint32_t payload_len;
6214 struct lpfc_rscn_event_header *rscn_event_data;
6215
6216 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6217 payload_ptr = (uint32_t *) pcmd->virt;
6218 payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
6219
6220 rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
6221 payload_len, GFP_KERNEL);
6222 if (!rscn_event_data) {
6223 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6224 "0147 Failed to allocate memory for RSCN event\n");
6225 return;
6226 }
6227 rscn_event_data->event_type = FC_REG_RSCN_EVENT;
6228 rscn_event_data->payload_length = payload_len;
6229 memcpy(rscn_event_data->rscn_payload, payload_ptr,
6230 payload_len);
6231
6232 fc_host_post_vendor_event(shost,
6233 fc_get_event_number(),
6599eaaa 6234 sizeof(struct lpfc_rscn_event_header) + payload_len,
ddcc50f0
JS
6235 (char *)rscn_event_data,
6236 LPFC_NL_VENDOR_ID);
6237
6238 kfree(rscn_event_data);
6239}
6240
e59058c4 6241/**
3621a710 6242 * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
e59058c4
JS
6243 * @vport: pointer to a host virtual N_Port data structure.
6244 * @cmdiocb: pointer to lpfc command iocb data structure.
6245 * @ndlp: pointer to a node-list data structure.
6246 *
6247 * This routine processes an unsolicited RSCN (Registration State Change
6248 * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
6249 * to invoke fc_host_post_event() routine to the FC transport layer. If the
6250 * discover state machine is about to begin discovery, it just accepts the
6251 * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
6252 * contains N_Port IDs for other vports on this HBA, it just accepts the
6253 * RSCN and ignore processing it. If the state machine is in the recovery
6254 * state, the fc_rscn_id_list of this @vport is walked and the
6255 * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
6256 * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
6257 * routine is invoked to handle the RSCN event.
6258 *
6259 * Return code
6260 * 0 - Just sent the acc response
6261 * 1 - Sent the acc response and waited for name server completion
6262 **/
dea3101e 6263static int
2e0fef85 6264lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
51ef4c26 6265 struct lpfc_nodelist *ndlp)
dea3101e 6266{
2e0fef85
JS
6267 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6268 struct lpfc_hba *phba = vport->phba;
dea3101e 6269 struct lpfc_dmabuf *pcmd;
92d7f7b0 6270 uint32_t *lp, *datap;
92d7f7b0 6271 uint32_t payload_len, length, nportid, *cmd;
7f5f3d0d 6272 int rscn_cnt;
92d7f7b0 6273 int rscn_id = 0, hba_id = 0;
d2873e4c 6274 int i;
dea3101e 6275
dea3101e
JB
6276 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6277 lp = (uint32_t *) pcmd->virt;
6278
92d7f7b0
JS
6279 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6280 payload_len -= sizeof(uint32_t); /* take off word 0 */
dea3101e 6281 /* RSCN received */
e8b62011
JS
6282 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6283 "0214 RSCN received Data: x%x x%x x%x x%x\n",
7f5f3d0d
JS
6284 vport->fc_flag, payload_len, *lp,
6285 vport->fc_rscn_id_cnt);
ddcc50f0
JS
6286
6287 /* Send an RSCN event to the management application */
6288 lpfc_send_rscn_event(vport, cmdiocb);
6289
d2873e4c 6290 for (i = 0; i < payload_len/sizeof(uint32_t); i++)
2e0fef85 6291 fc_host_post_event(shost, fc_get_event_number(),
d2873e4c
JS
6292 FCH_EVT_RSCN, lp[i]);
6293
dea3101e
JB
6294 /* If we are about to begin discovery, just ACC the RSCN.
6295 * Discovery processing will satisfy it.
6296 */
2e0fef85 6297 if (vport->port_state <= LPFC_NS_QRY) {
858c9f6c
JS
6298 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6299 "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
6300 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6301
51ef4c26 6302 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
c9f8735b 6303 return 0;
dea3101e
JB
6304 }
6305
92d7f7b0
JS
6306 /* If this RSCN just contains NPortIDs for other vports on this HBA,
6307 * just ACC and ignore it.
6308 */
6309 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3de2a653 6310 !(vport->cfg_peer_port_login)) {
92d7f7b0
JS
6311 i = payload_len;
6312 datap = lp;
6313 while (i > 0) {
6314 nportid = *datap++;
6315 nportid = ((be32_to_cpu(nportid)) & Mask_DID);
6316 i -= sizeof(uint32_t);
6317 rscn_id++;
549e55cd
JS
6318 if (lpfc_find_vport_by_did(phba, nportid))
6319 hba_id++;
92d7f7b0
JS
6320 }
6321 if (rscn_id == hba_id) {
6322 /* ALL NPortIDs in RSCN are on HBA */
e8b62011 6323 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
d7c255b2 6324 "0219 Ignore RSCN "
e8b62011
JS
6325 "Data: x%x x%x x%x x%x\n",
6326 vport->fc_flag, payload_len,
7f5f3d0d 6327 *lp, vport->fc_rscn_id_cnt);
858c9f6c
JS
6328 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6329 "RCV RSCN vport: did:x%x/ste:x%x flg:x%x",
6330 ndlp->nlp_DID, vport->port_state,
6331 ndlp->nlp_flag);
6332
92d7f7b0 6333 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
51ef4c26 6334 ndlp, NULL);
92d7f7b0
JS
6335 return 0;
6336 }
6337 }
6338
7f5f3d0d
JS
6339 spin_lock_irq(shost->host_lock);
6340 if (vport->fc_rscn_flush) {
6341 /* Another thread is walking fc_rscn_id_list on this vport */
7f5f3d0d 6342 vport->fc_flag |= FC_RSCN_DISCOVERY;
97957244 6343 spin_unlock_irq(shost->host_lock);
58da1ffb
JS
6344 /* Send back ACC */
6345 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7f5f3d0d
JS
6346 return 0;
6347 }
6348 /* Indicate we are walking fc_rscn_id_list on this vport */
6349 vport->fc_rscn_flush = 1;
6350 spin_unlock_irq(shost->host_lock);
af901ca1 6351 /* Get the array count after successfully have the token */
7f5f3d0d 6352 rscn_cnt = vport->fc_rscn_id_cnt;
dea3101e
JB
6353 /* If we are already processing an RSCN, save the received
6354 * RSCN payload buffer, cmdiocb->context2 to process later.
6355 */
2e0fef85 6356 if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
858c9f6c
JS
6357 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6358 "RCV RSCN defer: did:x%x/ste:x%x flg:x%x",
6359 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6360
09372820 6361 spin_lock_irq(shost->host_lock);
92d7f7b0
JS
6362 vport->fc_flag |= FC_RSCN_DEFERRED;
6363 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
2e0fef85 6364 !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
2e0fef85
JS
6365 vport->fc_flag |= FC_RSCN_MODE;
6366 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
6367 if (rscn_cnt) {
6368 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
6369 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
6370 }
6371 if ((rscn_cnt) &&
6372 (payload_len + length <= LPFC_BPL_SIZE)) {
6373 *cmd &= ELS_CMD_MASK;
7f5f3d0d 6374 *cmd |= cpu_to_be32(payload_len + length);
92d7f7b0
JS
6375 memcpy(((uint8_t *)cmd) + length, lp,
6376 payload_len);
6377 } else {
6378 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
6379 vport->fc_rscn_id_cnt++;
6380 /* If we zero, cmdiocb->context2, the calling
6381 * routine will not try to free it.
6382 */
6383 cmdiocb->context2 = NULL;
6384 }
dea3101e 6385 /* Deferred RSCN */
e8b62011
JS
6386 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6387 "0235 Deferred RSCN "
6388 "Data: x%x x%x x%x\n",
6389 vport->fc_rscn_id_cnt, vport->fc_flag,
6390 vport->port_state);
dea3101e 6391 } else {
2e0fef85
JS
6392 vport->fc_flag |= FC_RSCN_DISCOVERY;
6393 spin_unlock_irq(shost->host_lock);
dea3101e 6394 /* ReDiscovery RSCN */
e8b62011
JS
6395 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6396 "0234 ReDiscovery RSCN "
6397 "Data: x%x x%x x%x\n",
6398 vport->fc_rscn_id_cnt, vport->fc_flag,
6399 vport->port_state);
dea3101e 6400 }
7f5f3d0d
JS
6401 /* Indicate we are done walking fc_rscn_id_list on this vport */
6402 vport->fc_rscn_flush = 0;
dea3101e 6403 /* Send back ACC */
51ef4c26 6404 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 6405 /* send RECOVERY event for ALL nodes that match RSCN payload */
2e0fef85 6406 lpfc_rscn_recovery_check(vport);
c9f8735b 6407 return 0;
dea3101e 6408 }
858c9f6c
JS
6409 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6410 "RCV RSCN: did:x%x/ste:x%x flg:x%x",
6411 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6412
2e0fef85
JS
6413 spin_lock_irq(shost->host_lock);
6414 vport->fc_flag |= FC_RSCN_MODE;
6415 spin_unlock_irq(shost->host_lock);
6416 vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
7f5f3d0d
JS
6417 /* Indicate we are done walking fc_rscn_id_list on this vport */
6418 vport->fc_rscn_flush = 0;
dea3101e
JB
6419 /*
6420 * If we zero, cmdiocb->context2, the calling routine will
6421 * not try to free it.
6422 */
6423 cmdiocb->context2 = NULL;
2e0fef85 6424 lpfc_set_disctmo(vport);
dea3101e 6425 /* Send back ACC */
51ef4c26 6426 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 6427 /* send RECOVERY event for ALL nodes that match RSCN payload */
2e0fef85 6428 lpfc_rscn_recovery_check(vport);
2e0fef85 6429 return lpfc_els_handle_rscn(vport);
dea3101e
JB
6430}
6431
e59058c4 6432/**
3621a710 6433 * lpfc_els_handle_rscn - Handle rscn for a vport
e59058c4
JS
6434 * @vport: pointer to a host virtual N_Port data structure.
6435 *
6436 * This routine handles the Registration State Configuration Notification
6437 * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
6438 * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
6439 * if the ndlp to NameServer exists, a Common Transport (CT) command to the
6440 * NameServer shall be issued. If CT command to the NameServer fails to be
6441 * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
6442 * RSCN activities with the @vport.
6443 *
6444 * Return code
6445 * 0 - Cleaned up rscn on the @vport
6446 * 1 - Wait for plogi to name server before proceed
6447 **/
dea3101e 6448int
2e0fef85 6449lpfc_els_handle_rscn(struct lpfc_vport *vport)
dea3101e
JB
6450{
6451 struct lpfc_nodelist *ndlp;
7ea92eb4 6452 struct lpfc_hba *phba = vport->phba;
dea3101e 6453
92d7f7b0
JS
6454 /* Ignore RSCN if the port is being torn down. */
6455 if (vport->load_flag & FC_UNLOADING) {
6456 lpfc_els_flush_rscn(vport);
6457 return 0;
6458 }
6459
dea3101e 6460 /* Start timer for RSCN processing */
2e0fef85 6461 lpfc_set_disctmo(vport);
dea3101e
JB
6462
6463 /* RSCN processed */
e8b62011
JS
6464 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6465 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
6466 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
6467 vport->port_state);
dea3101e
JB
6468
6469 /* To process RSCN, first compare RSCN data with NameServer */
2e0fef85 6470 vport->fc_ns_retry = 0;
0ff10d46
JS
6471 vport->num_disc_nodes = 0;
6472
2e0fef85 6473 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093
JS
6474 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
6475 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
1c5b12f7
JS
6476 /* Good ndlp, issue CT Request to NameServer. Need to
6477 * know how many gidfts were issued. If none, then just
6478 * flush the RSCN. Otherwise, the outstanding requests
6479 * need to complete.
6480 */
7ea92eb4
JS
6481 if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
6482 if (lpfc_issue_gidft(vport) > 0)
6483 return 1;
6484 } else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
6485 if (lpfc_issue_gidpt(vport) > 0)
6486 return 1;
6487 } else {
c9f8735b 6488 return 1;
7ea92eb4 6489 }
dea3101e 6490 } else {
1c5b12f7 6491 /* Nameserver login in question. Revalidate. */
e47c9093
JS
6492 if (ndlp) {
6493 ndlp = lpfc_enable_node(vport, ndlp,
6494 NLP_STE_PLOGI_ISSUE);
6495 if (!ndlp) {
6496 lpfc_els_flush_rscn(vport);
6497 return 0;
6498 }
6499 ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
dea3101e 6500 } else {
9d3d340d 6501 ndlp = lpfc_nlp_init(vport, NameServer_DID);
e47c9093
JS
6502 if (!ndlp) {
6503 lpfc_els_flush_rscn(vport);
6504 return 0;
6505 }
5024ab17 6506 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 6507 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
dea3101e 6508 }
e47c9093
JS
6509 ndlp->nlp_type |= NLP_FABRIC;
6510 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
6511 /* Wait for NameServer login cmpl before we can
6512 * continue
6513 */
6514 return 1;
dea3101e
JB
6515 }
6516
2e0fef85 6517 lpfc_els_flush_rscn(vport);
c9f8735b 6518 return 0;
dea3101e
JB
6519}
6520
e59058c4 6521/**
3621a710 6522 * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
e59058c4
JS
6523 * @vport: pointer to a host virtual N_Port data structure.
6524 * @cmdiocb: pointer to lpfc command iocb data structure.
6525 * @ndlp: pointer to a node-list data structure.
6526 *
6527 * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
6528 * unsolicited event. An unsolicited FLOGI can be received in a point-to-
6529 * point topology. As an unsolicited FLOGI should not be received in a loop
6530 * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
6531 * lpfc_check_sparm() routine is invoked to check the parameters in the
6532 * unsolicited FLOGI. If parameters validation failed, the routine
6533 * lpfc_els_rsp_reject() shall be called with reject reason code set to
6534 * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
6535 * FLOGI shall be compared with the Port WWN of the @vport to determine who
6536 * will initiate PLOGI. The higher lexicographical value party shall has
6537 * higher priority (as the winning port) and will initiate PLOGI and
6538 * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
6539 * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
6540 * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
6541 *
6542 * Return code
6543 * 0 - Successfully processed the unsolicited flogi
6544 * 1 - Failed to process the unsolicited flogi
6545 **/
dea3101e 6546static int
2e0fef85 6547lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
51ef4c26 6548 struct lpfc_nodelist *ndlp)
dea3101e 6549{
2e0fef85
JS
6550 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6551 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
6552 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6553 uint32_t *lp = (uint32_t *) pcmd->virt;
6554 IOCB_t *icmd = &cmdiocb->iocb;
6555 struct serv_parm *sp;
6556 LPFC_MBOXQ_t *mbox;
dea3101e
JB
6557 uint32_t cmd, did;
6558 int rc;
e74c03c8
JS
6559 uint32_t fc_flag = 0;
6560 uint32_t port_state = 0;
dea3101e
JB
6561
6562 cmd = *lp++;
6563 sp = (struct serv_parm *) lp;
6564
6565 /* FLOGI received */
6566
2e0fef85 6567 lpfc_set_disctmo(vport);
dea3101e 6568
76a95d75 6569 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
dea3101e
JB
6570 /* We should never receive a FLOGI in loop mode, ignore it */
6571 did = icmd->un.elsreq64.remoteID;
6572
6573 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
6574 Loop Mode */
e8b62011
JS
6575 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6576 "0113 An FLOGI ELS command x%x was "
6577 "received from DID x%x in Loop Mode\n",
6578 cmd, did);
c9f8735b 6579 return 1;
dea3101e
JB
6580 }
6581
d6de08cc 6582 (void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
dea3101e 6583
d6de08cc
JS
6584 /*
6585 * If our portname is greater than the remote portname,
6586 * then we initiate Nport login.
6587 */
939723a4 6588
d6de08cc
JS
6589 rc = memcmp(&vport->fc_portname, &sp->portName,
6590 sizeof(struct lpfc_name));
939723a4 6591
d6de08cc
JS
6592 if (!rc) {
6593 if (phba->sli_rev < LPFC_SLI_REV4) {
6594 mbox = mempool_alloc(phba->mbox_mem_pool,
6595 GFP_KERNEL);
6596 if (!mbox)
6597 return 1;
6598 lpfc_linkdown(phba);
6599 lpfc_init_link(phba, mbox,
6600 phba->cfg_topology,
6601 phba->cfg_link_speed);
6602 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6603 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6604 mbox->vport = vport;
6605 rc = lpfc_sli_issue_mbox(phba, mbox,
6606 MBX_NOWAIT);
6607 lpfc_set_loopback_flag(phba);
6608 if (rc == MBX_NOT_FINISHED)
6609 mempool_free(mbox, phba->mbox_mem_pool);
6610 return 1;
6611 }
6612
6613 /* abort the flogi coming back to ourselves
6614 * due to external loopback on the port.
939723a4 6615 */
d6de08cc
JS
6616 lpfc_els_abort_flogi(phba);
6617 return 0;
6618
6619 } else if (rc > 0) { /* greater than */
2e0fef85 6620 spin_lock_irq(shost->host_lock);
d6de08cc 6621 vport->fc_flag |= FC_PT2PT_PLOGI;
2e0fef85 6622 spin_unlock_irq(shost->host_lock);
939723a4 6623
d6de08cc
JS
6624 /* If we have the high WWPN we can assign our own
6625 * myDID; otherwise, we have to WAIT for a PLOGI
6626 * from the remote NPort to find out what it
6627 * will be.
939723a4 6628 */
d6de08cc 6629 vport->fc_myDID = PT2PT_LocalID;
dea3101e 6630 } else {
d6de08cc
JS
6631 vport->fc_myDID = PT2PT_RemoteID;
6632 }
939723a4 6633
d6de08cc
JS
6634 /*
6635 * The vport state should go to LPFC_FLOGI only
6636 * AFTER we issue a FLOGI, not receive one.
6637 */
6638 spin_lock_irq(shost->host_lock);
6639 fc_flag = vport->fc_flag;
6640 port_state = vport->port_state;
6641 vport->fc_flag |= FC_PT2PT;
6642 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
d496b9a7
JS
6643
6644 /* Acking an unsol FLOGI. Count 1 for link bounce
6645 * work-around.
6646 */
6647 vport->rcv_flogi_cnt++;
d6de08cc
JS
6648 spin_unlock_irq(shost->host_lock);
6649 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6650 "3311 Rcv Flogi PS x%x new PS x%x "
6651 "fc_flag x%x new fc_flag x%x\n",
6652 port_state, vport->port_state,
6653 fc_flag, vport->fc_flag);
939723a4 6654
d6de08cc
JS
6655 /*
6656 * We temporarily set fc_myDID to make it look like we are
6657 * a Fabric. This is done just so we end up with the right
6658 * did / sid on the FLOGI ACC rsp.
6659 */
6660 did = vport->fc_myDID;
6661 vport->fc_myDID = Fabric_DID;
dea3101e 6662
d6de08cc 6663 memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
eec3d312 6664
dea3101e 6665 /* Send back ACC */
d6de08cc 6666 lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
dea3101e 6667
939723a4
JS
6668 /* Now lets put fc_myDID back to what its supposed to be */
6669 vport->fc_myDID = did;
6670
c9f8735b 6671 return 0;
dea3101e
JB
6672}
6673
e59058c4 6674/**
3621a710 6675 * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
e59058c4
JS
6676 * @vport: pointer to a host virtual N_Port data structure.
6677 * @cmdiocb: pointer to lpfc command iocb data structure.
6678 * @ndlp: pointer to a node-list data structure.
6679 *
6680 * This routine processes Request Node Identification Data (RNID) IOCB
6681 * received as an ELS unsolicited event. Only when the RNID specified format
6682 * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
6683 * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
6684 * Accept (ACC) the RNID ELS command. All the other RNID formats are
6685 * rejected by invoking the lpfc_els_rsp_reject() routine.
6686 *
6687 * Return code
6688 * 0 - Successfully processed rnid iocb (currently always return 0)
6689 **/
dea3101e 6690static int
2e0fef85
JS
6691lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6692 struct lpfc_nodelist *ndlp)
dea3101e
JB
6693{
6694 struct lpfc_dmabuf *pcmd;
6695 uint32_t *lp;
dea3101e
JB
6696 RNID *rn;
6697 struct ls_rjt stat;
eb016566 6698 uint32_t cmd;
dea3101e 6699
dea3101e
JB
6700 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6701 lp = (uint32_t *) pcmd->virt;
6702
6703 cmd = *lp++;
6704 rn = (RNID *) lp;
6705
6706 /* RNID received */
6707
6708 switch (rn->Format) {
6709 case 0:
6710 case RNID_TOPOLOGY_DISC:
6711 /* Send back ACC */
2e0fef85 6712 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
dea3101e
JB
6713 break;
6714 default:
6715 /* Reject this request because format not supported */
6716 stat.un.b.lsRjtRsvd0 = 0;
6717 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6718 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6719 stat.un.b.vendorUnique = 0;
858c9f6c
JS
6720 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6721 NULL);
dea3101e 6722 }
c9f8735b 6723 return 0;
dea3101e
JB
6724}
6725
12265f68
JS
6726/**
6727 * lpfc_els_rcv_echo - Process an unsolicited echo iocb
6728 * @vport: pointer to a host virtual N_Port data structure.
6729 * @cmdiocb: pointer to lpfc command iocb data structure.
6730 * @ndlp: pointer to a node-list data structure.
6731 *
6732 * Return code
6733 * 0 - Successfully processed echo iocb (currently always return 0)
6734 **/
6735static int
6736lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6737 struct lpfc_nodelist *ndlp)
6738{
6739 uint8_t *pcmd;
6740
6741 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
6742
6743 /* skip over first word of echo command to find echo data */
6744 pcmd += sizeof(uint32_t);
6745
6746 lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
6747 return 0;
6748}
6749
e59058c4 6750/**
3621a710 6751 * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
e59058c4
JS
6752 * @vport: pointer to a host virtual N_Port data structure.
6753 * @cmdiocb: pointer to lpfc command iocb data structure.
6754 * @ndlp: pointer to a node-list data structure.
6755 *
6756 * This routine processes a Link Incident Report Registration(LIRR) IOCB
6757 * received as an ELS unsolicited event. Currently, this function just invokes
6758 * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
6759 *
6760 * Return code
6761 * 0 - Successfully processed lirr iocb (currently always return 0)
6762 **/
dea3101e 6763static int
2e0fef85
JS
6764lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6765 struct lpfc_nodelist *ndlp)
7bb3b137
JW
6766{
6767 struct ls_rjt stat;
6768
6769 /* For now, unconditionally reject this command */
6770 stat.un.b.lsRjtRsvd0 = 0;
6771 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6772 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6773 stat.un.b.vendorUnique = 0;
858c9f6c 6774 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7bb3b137
JW
6775 return 0;
6776}
6777
5ffc266e
JS
6778/**
6779 * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
6780 * @vport: pointer to a host virtual N_Port data structure.
6781 * @cmdiocb: pointer to lpfc command iocb data structure.
6782 * @ndlp: pointer to a node-list data structure.
6783 *
6784 * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
6785 * received as an ELS unsolicited event. A request to RRQ shall only
6786 * be accepted if the Originator Nx_Port N_Port_ID or the Responder
6787 * Nx_Port N_Port_ID of the target Exchange is the same as the
6788 * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
6789 * not accepted, an LS_RJT with reason code "Unable to perform
6790 * command request" and reason code explanation "Invalid Originator
6791 * S_ID" shall be returned. For now, we just unconditionally accept
6792 * RRQ from the target.
6793 **/
6794static void
6795lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6796 struct lpfc_nodelist *ndlp)
6797{
6798 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
19ca7609
JS
6799 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6800 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
5ffc266e
JS
6801}
6802
12265f68
JS
6803/**
6804 * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
6805 * @phba: pointer to lpfc hba data structure.
6806 * @pmb: pointer to the driver internal queue element for mailbox command.
6807 *
6808 * This routine is the completion callback function for the MBX_READ_LNK_STAT
6809 * mailbox command. This callback function is to actually send the Accept
6810 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6811 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6812 * mailbox command, constructs the RPS response with the link statistics
6813 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6814 * response to the RPS.
6815 *
6816 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6817 * will be incremented by 1 for holding the ndlp and the reference to ndlp
6818 * will be stored into the context1 field of the IOCB for the completion
6819 * callback function to the RPS Accept Response ELS IOCB command.
6820 *
6821 **/
6822static void
6823lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6824{
6825 MAILBOX_t *mb;
6826 IOCB_t *icmd;
6827 struct RLS_RSP *rls_rsp;
6828 uint8_t *pcmd;
6829 struct lpfc_iocbq *elsiocb;
6830 struct lpfc_nodelist *ndlp;
7851fe2c
JS
6831 uint16_t oxid;
6832 uint16_t rxid;
12265f68
JS
6833 uint32_t cmdsize;
6834
6835 mb = &pmb->u.mb;
6836
3e1f0718
JS
6837 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6838 rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
6839 oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
6840 pmb->ctx_buf = NULL;
6841 pmb->ctx_ndlp = NULL;
12265f68
JS
6842
6843 if (mb->mbxStatus) {
6844 mempool_free(pmb, phba->mbox_mem_pool);
6845 return;
6846 }
6847
6848 cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
12265f68
JS
6849 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6850 lpfc_max_els_tries, ndlp,
6851 ndlp->nlp_DID, ELS_CMD_ACC);
6852
6853 /* Decrement the ndlp reference count from previous mbox command */
6854 lpfc_nlp_put(ndlp);
6855
37db57e3
JS
6856 if (!elsiocb) {
6857 mempool_free(pmb, phba->mbox_mem_pool);
12265f68 6858 return;
37db57e3 6859 }
12265f68
JS
6860
6861 icmd = &elsiocb->iocb;
7851fe2c
JS
6862 icmd->ulpContext = rxid;
6863 icmd->unsli3.rcvsli3.ox_id = oxid;
12265f68
JS
6864
6865 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6866 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
6867 pcmd += sizeof(uint32_t); /* Skip past command */
6868 rls_rsp = (struct RLS_RSP *)pcmd;
6869
6870 rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6871 rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6872 rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6873 rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6874 rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6875 rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
37db57e3 6876 mempool_free(pmb, phba->mbox_mem_pool);
12265f68
JS
6877 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
6878 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6879 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
6880 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6881 elsiocb->iotag, elsiocb->iocb.ulpContext,
6882 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6883 ndlp->nlp_rpi);
6884 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6885 phba->fc_stat.elsXmitACC++;
6886 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
6887 lpfc_els_free_iocb(phba, elsiocb);
6888}
6889
e59058c4 6890/**
3621a710 6891 * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
e59058c4
JS
6892 * @phba: pointer to lpfc hba data structure.
6893 * @pmb: pointer to the driver internal queue element for mailbox command.
6894 *
6895 * This routine is the completion callback function for the MBX_READ_LNK_STAT
6896 * mailbox command. This callback function is to actually send the Accept
6897 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
6898 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
6899 * mailbox command, constructs the RPS response with the link statistics
6900 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
6901 * response to the RPS.
6902 *
6903 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6904 * will be incremented by 1 for holding the ndlp and the reference to ndlp
6905 * will be stored into the context1 field of the IOCB for the completion
6906 * callback function to the RPS Accept Response ELS IOCB command.
6907 *
6908 **/
082c0266 6909static void
329f9bc7 6910lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7bb3b137 6911{
7bb3b137
JW
6912 MAILBOX_t *mb;
6913 IOCB_t *icmd;
6914 RPS_RSP *rps_rsp;
6915 uint8_t *pcmd;
6916 struct lpfc_iocbq *elsiocb;
6917 struct lpfc_nodelist *ndlp;
7851fe2c
JS
6918 uint16_t status;
6919 uint16_t oxid;
6920 uint16_t rxid;
7bb3b137
JW
6921 uint32_t cmdsize;
6922
04c68496 6923 mb = &pmb->u.mb;
7bb3b137 6924
3e1f0718
JS
6925 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6926 rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
6927 oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
6928 pmb->ctx_ndlp = NULL;
6929 pmb->ctx_buf = NULL;
7bb3b137
JW
6930
6931 if (mb->mbxStatus) {
329f9bc7 6932 mempool_free(pmb, phba->mbox_mem_pool);
7bb3b137
JW
6933 return;
6934 }
6935
6936 cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
329f9bc7 6937 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85
JS
6938 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6939 lpfc_max_els_tries, ndlp,
6940 ndlp->nlp_DID, ELS_CMD_ACC);
fa4066b6
JS
6941
6942 /* Decrement the ndlp reference count from previous mbox command */
329f9bc7 6943 lpfc_nlp_put(ndlp);
fa4066b6 6944
c9f8735b 6945 if (!elsiocb)
7bb3b137 6946 return;
7bb3b137
JW
6947
6948 icmd = &elsiocb->iocb;
7851fe2c
JS
6949 icmd->ulpContext = rxid;
6950 icmd->unsli3.rcvsli3.ox_id = oxid;
7bb3b137
JW
6951
6952 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6953 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 6954 pcmd += sizeof(uint32_t); /* Skip past command */
7bb3b137
JW
6955 rps_rsp = (RPS_RSP *)pcmd;
6956
76a95d75 6957 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
7bb3b137
JW
6958 status = 0x10;
6959 else
6960 status = 0x8;
2e0fef85 6961 if (phba->pport->fc_flag & FC_FABRIC)
7bb3b137
JW
6962 status |= 0x4;
6963
6964 rps_rsp->rsvd1 = 0;
09372820
JS
6965 rps_rsp->portStatus = cpu_to_be16(status);
6966 rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
6967 rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
6968 rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
6969 rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
6970 rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
6971 rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7bb3b137 6972 /* Xmit ELS RPS ACC response tag <ulpIoTag> */
e8b62011
JS
6973 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
6974 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
6975 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
6976 elsiocb->iotag, elsiocb->iocb.ulpContext,
6977 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
6978 ndlp->nlp_rpi);
858c9f6c 6979 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7bb3b137 6980 phba->fc_stat.elsXmitACC++;
3772a991 6981 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7bb3b137 6982 lpfc_els_free_iocb(phba, elsiocb);
7bb3b137
JW
6983 return;
6984}
6985
e59058c4 6986/**
12265f68
JS
6987 * lpfc_els_rcv_rls - Process an unsolicited rls iocb
6988 * @vport: pointer to a host virtual N_Port data structure.
6989 * @cmdiocb: pointer to lpfc command iocb data structure.
6990 * @ndlp: pointer to a node-list data structure.
6991 *
6992 * This routine processes Read Port Status (RPL) IOCB received as an
6993 * ELS unsolicited event. It first checks the remote port state. If the
6994 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
6995 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
6996 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
6997 * for reading the HBA link statistics. It is for the callback function,
6998 * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
6999 * to actually sending out RPL Accept (ACC) response.
7000 *
7001 * Return codes
7002 * 0 - Successfully processed rls iocb (currently always return 0)
7003 **/
7004static int
7005lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7006 struct lpfc_nodelist *ndlp)
7007{
7008 struct lpfc_hba *phba = vport->phba;
7009 LPFC_MBOXQ_t *mbox;
12265f68
JS
7010 struct ls_rjt stat;
7011
7012 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7013 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7014 /* reject the unsolicited RPS request and done with it */
7015 goto reject_out;
7016
12265f68
JS
7017 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7018 if (mbox) {
7019 lpfc_read_lnk_stat(phba, mbox);
3e1f0718 7020 mbox->ctx_buf = (void *)((unsigned long)
7851fe2c
JS
7021 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7022 cmdiocb->iocb.ulpContext)); /* rx_id */
3e1f0718 7023 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
12265f68
JS
7024 mbox->vport = vport;
7025 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
7026 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7027 != MBX_NOT_FINISHED)
7028 /* Mbox completion will send ELS Response */
7029 return 0;
7030 /* Decrement reference count used for the failed mbox
7031 * command.
7032 */
7033 lpfc_nlp_put(ndlp);
7034 mempool_free(mbox, phba->mbox_mem_pool);
7035 }
7036reject_out:
7037 /* issue rejection response */
7038 stat.un.b.lsRjtRsvd0 = 0;
7039 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7040 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7041 stat.un.b.vendorUnique = 0;
7042 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7043 return 0;
7044}
7045
7046/**
7047 * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
7048 * @vport: pointer to a host virtual N_Port data structure.
7049 * @cmdiocb: pointer to lpfc command iocb data structure.
7050 * @ndlp: pointer to a node-list data structure.
7051 *
7052 * This routine processes Read Timout Value (RTV) IOCB received as an
7053 * ELS unsolicited event. It first checks the remote port state. If the
7054 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7055 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7056 * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
7057 * Value (RTV) unsolicited IOCB event.
7058 *
7059 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7060 * will be incremented by 1 for holding the ndlp and the reference to ndlp
7061 * will be stored into the context1 field of the IOCB for the completion
7062 * callback function to the RPS Accept Response ELS IOCB command.
7063 *
7064 * Return codes
7065 * 0 - Successfully processed rtv iocb (currently always return 0)
7066 **/
7067static int
7068lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7069 struct lpfc_nodelist *ndlp)
7070{
7071 struct lpfc_hba *phba = vport->phba;
7072 struct ls_rjt stat;
7073 struct RTV_RSP *rtv_rsp;
7074 uint8_t *pcmd;
7075 struct lpfc_iocbq *elsiocb;
7076 uint32_t cmdsize;
7077
7078
7079 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7080 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7081 /* reject the unsolicited RPS request and done with it */
7082 goto reject_out;
7083
7084 cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
7085 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7086 lpfc_max_els_tries, ndlp,
7087 ndlp->nlp_DID, ELS_CMD_ACC);
7088
7089 if (!elsiocb)
7090 return 1;
7091
7092 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
8fd03fd1 7093 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
12265f68
JS
7094 pcmd += sizeof(uint32_t); /* Skip past command */
7095
7096 /* use the command's xri in the response */
7851fe2c
JS
7097 elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext; /* Xri / rx_id */
7098 elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
12265f68
JS
7099
7100 rtv_rsp = (struct RTV_RSP *)pcmd;
7101
7102 /* populate RTV payload */
7103 rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
7104 rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
7105 bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
7106 bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
7107 rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
7108
7109 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
7110 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7111 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
7112 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
7113 "Data: x%x x%x x%x\n",
7114 elsiocb->iotag, elsiocb->iocb.ulpContext,
7115 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7116 ndlp->nlp_rpi,
7117 rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
7118 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7119 phba->fc_stat.elsXmitACC++;
7120 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7121 lpfc_els_free_iocb(phba, elsiocb);
7122 return 0;
7123
7124reject_out:
7125 /* issue rejection response */
7126 stat.un.b.lsRjtRsvd0 = 0;
7127 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7128 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7129 stat.un.b.vendorUnique = 0;
7130 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7131 return 0;
7132}
7133
7134/* lpfc_els_rcv_rps - Process an unsolicited rps iocb
e59058c4
JS
7135 * @vport: pointer to a host virtual N_Port data structure.
7136 * @cmdiocb: pointer to lpfc command iocb data structure.
7137 * @ndlp: pointer to a node-list data structure.
7138 *
7139 * This routine processes Read Port Status (RPS) IOCB received as an
7140 * ELS unsolicited event. It first checks the remote port state. If the
7141 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7142 * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
7143 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7144 * for reading the HBA link statistics. It is for the callback function,
7145 * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
7146 * to actually sending out RPS Accept (ACC) response.
7147 *
7148 * Return codes
7149 * 0 - Successfully processed rps iocb (currently always return 0)
7150 **/
7bb3b137 7151static int
2e0fef85
JS
7152lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7153 struct lpfc_nodelist *ndlp)
dea3101e 7154{
2e0fef85 7155 struct lpfc_hba *phba = vport->phba;
dea3101e 7156 uint32_t *lp;
7bb3b137
JW
7157 uint8_t flag;
7158 LPFC_MBOXQ_t *mbox;
7159 struct lpfc_dmabuf *pcmd;
7160 RPS *rps;
7161 struct ls_rjt stat;
7162
2fe165b6 7163 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
90160e01
JS
7164 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7165 /* reject the unsolicited RPS request and done with it */
7166 goto reject_out;
7bb3b137
JW
7167
7168 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7169 lp = (uint32_t *) pcmd->virt;
7170 flag = (be32_to_cpu(*lp++) & 0xf);
7171 rps = (RPS *) lp;
7172
7173 if ((flag == 0) ||
7174 ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
2e0fef85 7175 ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
92d7f7b0 7176 sizeof(struct lpfc_name)) == 0))) {
2e0fef85 7177
92d7f7b0
JS
7178 printk("Fix me....\n");
7179 dump_stack();
2e0fef85
JS
7180 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7181 if (mbox) {
7bb3b137 7182 lpfc_read_lnk_stat(phba, mbox);
3e1f0718 7183 mbox->ctx_buf = (void *)((unsigned long)
7851fe2c
JS
7184 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7185 cmdiocb->iocb.ulpContext)); /* rx_id */
3e1f0718 7186 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
92d7f7b0 7187 mbox->vport = vport;
7bb3b137 7188 mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
fa4066b6 7189 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
0b727fea 7190 != MBX_NOT_FINISHED)
7bb3b137
JW
7191 /* Mbox completion will send ELS Response */
7192 return 0;
fa4066b6
JS
7193 /* Decrement reference count used for the failed mbox
7194 * command.
7195 */
329f9bc7 7196 lpfc_nlp_put(ndlp);
7bb3b137
JW
7197 mempool_free(mbox, phba->mbox_mem_pool);
7198 }
7199 }
90160e01
JS
7200
7201reject_out:
7202 /* issue rejection response */
7bb3b137
JW
7203 stat.un.b.lsRjtRsvd0 = 0;
7204 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7205 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7206 stat.un.b.vendorUnique = 0;
858c9f6c 7207 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7bb3b137
JW
7208 return 0;
7209}
7210
19ca7609
JS
7211/* lpfc_issue_els_rrq - Process an unsolicited rps iocb
7212 * @vport: pointer to a host virtual N_Port data structure.
7213 * @ndlp: pointer to a node-list data structure.
7214 * @did: DID of the target.
7215 * @rrq: Pointer to the rrq struct.
7216 *
7217 * Build a ELS RRQ command and send it to the target. If the issue_iocb is
7218 * Successful the the completion handler will clear the RRQ.
7219 *
7220 * Return codes
7221 * 0 - Successfully sent rrq els iocb.
7222 * 1 - Failed to send rrq els iocb.
7223 **/
7224static int
7225lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7226 uint32_t did, struct lpfc_node_rrq *rrq)
7227{
7228 struct lpfc_hba *phba = vport->phba;
7229 struct RRQ *els_rrq;
19ca7609
JS
7230 struct lpfc_iocbq *elsiocb;
7231 uint8_t *pcmd;
7232 uint16_t cmdsize;
7233 int ret;
7234
7235
7236 if (ndlp != rrq->ndlp)
7237 ndlp = rrq->ndlp;
7238 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7239 return 1;
7240
7241 /* If ndlp is not NULL, we will bump the reference count on it */
7242 cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
7243 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
7244 ELS_CMD_RRQ);
7245 if (!elsiocb)
7246 return 1;
7247
19ca7609
JS
7248 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7249
7250 /* For RRQ request, remainder of payload is Exchange IDs */
7251 *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
7252 pcmd += sizeof(uint32_t);
7253 els_rrq = (struct RRQ *) pcmd;
7254
ee0f4fe1 7255 bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
19ca7609
JS
7256 bf_set(rrq_rxid, els_rrq, rrq->rxid);
7257 bf_set(rrq_did, els_rrq, vport->fc_myDID);
7258 els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
7259 els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
7260
7261
7262 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7263 "Issue RRQ: did:x%x",
7264 did, rrq->xritag, rrq->rxid);
7265 elsiocb->context_un.rrq = rrq;
7266 elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
7267 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7268
7269 if (ret == IOCB_ERROR) {
7270 lpfc_els_free_iocb(phba, elsiocb);
7271 return 1;
7272 }
7273 return 0;
7274}
7275
7276/**
7277 * lpfc_send_rrq - Sends ELS RRQ if needed.
7278 * @phba: pointer to lpfc hba data structure.
7279 * @rrq: pointer to the active rrq.
7280 *
7281 * This routine will call the lpfc_issue_els_rrq if the rrq is
7282 * still active for the xri. If this function returns a failure then
7283 * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
7284 *
7285 * Returns 0 Success.
7286 * 1 Failure.
7287 **/
7288int
7289lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
7290{
7291 struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
7292 rrq->nlp_DID);
7293 if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
7294 return lpfc_issue_els_rrq(rrq->vport, ndlp,
7295 rrq->nlp_DID, rrq);
7296 else
7297 return 1;
7298}
7299
e59058c4 7300/**
3621a710 7301 * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
e59058c4
JS
7302 * @vport: pointer to a host virtual N_Port data structure.
7303 * @cmdsize: size of the ELS command.
7304 * @oldiocb: pointer to the original lpfc command iocb data structure.
7305 * @ndlp: pointer to a node-list data structure.
7306 *
7307 * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
7308 * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
7309 *
7310 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7311 * will be incremented by 1 for holding the ndlp and the reference to ndlp
7312 * will be stored into the context1 field of the IOCB for the completion
7313 * callback function to the RPL Accept Response ELS command.
7314 *
7315 * Return code
7316 * 0 - Successfully issued ACC RPL ELS command
7317 * 1 - Failed to issue ACC RPL ELS command
7318 **/
082c0266 7319static int
2e0fef85
JS
7320lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
7321 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7bb3b137 7322{
2e0fef85
JS
7323 struct lpfc_hba *phba = vport->phba;
7324 IOCB_t *icmd, *oldcmd;
7bb3b137
JW
7325 RPL_RSP rpl_rsp;
7326 struct lpfc_iocbq *elsiocb;
7bb3b137 7327 uint8_t *pcmd;
dea3101e 7328
2e0fef85
JS
7329 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
7330 ndlp->nlp_DID, ELS_CMD_ACC);
7bb3b137 7331
488d1469 7332 if (!elsiocb)
7bb3b137 7333 return 1;
488d1469 7334
7bb3b137
JW
7335 icmd = &elsiocb->iocb;
7336 oldcmd = &oldiocb->iocb;
7851fe2c
JS
7337 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
7338 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7bb3b137
JW
7339
7340 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7341 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 7342 pcmd += sizeof(uint16_t);
7bb3b137
JW
7343 *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
7344 pcmd += sizeof(uint16_t);
7345
7346 /* Setup the RPL ACC payload */
7347 rpl_rsp.listLen = be32_to_cpu(1);
7348 rpl_rsp.index = 0;
7349 rpl_rsp.port_num_blk.portNum = 0;
2e0fef85
JS
7350 rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
7351 memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7bb3b137 7352 sizeof(struct lpfc_name));
7bb3b137 7353 memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7bb3b137 7354 /* Xmit ELS RPL ACC response tag <ulpIoTag> */
e8b62011
JS
7355 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7356 "0120 Xmit ELS RPL ACC response tag x%x "
7357 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
7358 "rpi x%x\n",
7359 elsiocb->iotag, elsiocb->iocb.ulpContext,
7360 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7361 ndlp->nlp_rpi);
858c9f6c 7362 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7bb3b137 7363 phba->fc_stat.elsXmitACC++;
3772a991
JS
7364 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7365 IOCB_ERROR) {
7bb3b137
JW
7366 lpfc_els_free_iocb(phba, elsiocb);
7367 return 1;
7368 }
7369 return 0;
7370}
7371
e59058c4 7372/**
3621a710 7373 * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
e59058c4
JS
7374 * @vport: pointer to a host virtual N_Port data structure.
7375 * @cmdiocb: pointer to lpfc command iocb data structure.
7376 * @ndlp: pointer to a node-list data structure.
7377 *
7378 * This routine processes Read Port List (RPL) IOCB received as an ELS
7379 * unsolicited event. It first checks the remote port state. If the remote
7380 * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
7381 * invokes the lpfc_els_rsp_reject() routine to send reject response.
7382 * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
7383 * to accept the RPL.
7384 *
7385 * Return code
7386 * 0 - Successfully processed rpl iocb (currently always return 0)
7387 **/
7bb3b137 7388static int
2e0fef85
JS
7389lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7390 struct lpfc_nodelist *ndlp)
7bb3b137
JW
7391{
7392 struct lpfc_dmabuf *pcmd;
7393 uint32_t *lp;
7394 uint32_t maxsize;
7395 uint16_t cmdsize;
7396 RPL *rpl;
7397 struct ls_rjt stat;
7398
2fe165b6
JW
7399 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7400 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
90160e01 7401 /* issue rejection response */
7bb3b137
JW
7402 stat.un.b.lsRjtRsvd0 = 0;
7403 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7404 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7405 stat.un.b.vendorUnique = 0;
858c9f6c
JS
7406 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
7407 NULL);
90160e01
JS
7408 /* rejected the unsolicited RPL request and done with it */
7409 return 0;
7bb3b137
JW
7410 }
7411
dea3101e
JB
7412 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7413 lp = (uint32_t *) pcmd->virt;
7bb3b137 7414 rpl = (RPL *) (lp + 1);
7bb3b137 7415 maxsize = be32_to_cpu(rpl->maxsize);
dea3101e 7416
7bb3b137
JW
7417 /* We support only one port */
7418 if ((rpl->index == 0) &&
7419 ((maxsize == 0) ||
7420 ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
7421 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
2fe165b6 7422 } else {
7bb3b137
JW
7423 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
7424 }
2e0fef85 7425 lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
dea3101e
JB
7426
7427 return 0;
7428}
7429
e59058c4 7430/**
3621a710 7431 * lpfc_els_rcv_farp - Process an unsolicited farp request els command
e59058c4
JS
7432 * @vport: pointer to a virtual N_Port data structure.
7433 * @cmdiocb: pointer to lpfc command iocb data structure.
7434 * @ndlp: pointer to a node-list data structure.
7435 *
7436 * This routine processes Fibre Channel Address Resolution Protocol
7437 * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
7438 * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
7439 * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
7440 * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
7441 * remote PortName is compared against the FC PortName stored in the @vport
7442 * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
7443 * compared against the FC NodeName stored in the @vport data structure.
7444 * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
7445 * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
7446 * invoked to send out FARP Response to the remote node. Before sending the
7447 * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
7448 * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
7449 * routine is invoked to log into the remote port first.
7450 *
7451 * Return code
7452 * 0 - Either the FARP Match Mode not supported or successfully processed
7453 **/
dea3101e 7454static int
2e0fef85
JS
7455lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7456 struct lpfc_nodelist *ndlp)
dea3101e
JB
7457{
7458 struct lpfc_dmabuf *pcmd;
7459 uint32_t *lp;
7460 IOCB_t *icmd;
7461 FARP *fp;
7462 uint32_t cmd, cnt, did;
7463
7464 icmd = &cmdiocb->iocb;
7465 did = icmd->un.elsreq64.remoteID;
7466 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7467 lp = (uint32_t *) pcmd->virt;
7468
7469 cmd = *lp++;
7470 fp = (FARP *) lp;
dea3101e 7471 /* FARP-REQ received from DID <did> */
e8b62011
JS
7472 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7473 "0601 FARP-REQ received from DID x%x\n", did);
dea3101e
JB
7474 /* We will only support match on WWPN or WWNN */
7475 if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
c9f8735b 7476 return 0;
dea3101e
JB
7477 }
7478
7479 cnt = 0;
7480 /* If this FARP command is searching for my portname */
7481 if (fp->Mflags & FARP_MATCH_PORT) {
2e0fef85 7482 if (memcmp(&fp->RportName, &vport->fc_portname,
92d7f7b0 7483 sizeof(struct lpfc_name)) == 0)
dea3101e
JB
7484 cnt = 1;
7485 }
7486
7487 /* If this FARP command is searching for my nodename */
7488 if (fp->Mflags & FARP_MATCH_NODE) {
2e0fef85 7489 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
92d7f7b0 7490 sizeof(struct lpfc_name)) == 0)
dea3101e
JB
7491 cnt = 1;
7492 }
7493
7494 if (cnt) {
7495 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
7496 (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
7497 /* Log back into the node before sending the FARP. */
7498 if (fp->Rflags & FARP_REQUEST_PLOGI) {
5024ab17 7499 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 7500 lpfc_nlp_set_state(vport, ndlp,
de0c5b32 7501 NLP_STE_PLOGI_ISSUE);
2e0fef85 7502 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
dea3101e
JB
7503 }
7504
7505 /* Send a FARP response to that node */
2e0fef85
JS
7506 if (fp->Rflags & FARP_REQUEST_FARPR)
7507 lpfc_issue_els_farpr(vport, did, 0);
dea3101e
JB
7508 }
7509 }
c9f8735b 7510 return 0;
dea3101e
JB
7511}
7512
e59058c4 7513/**
3621a710 7514 * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
e59058c4
JS
7515 * @vport: pointer to a host virtual N_Port data structure.
7516 * @cmdiocb: pointer to lpfc command iocb data structure.
7517 * @ndlp: pointer to a node-list data structure.
7518 *
7519 * This routine processes Fibre Channel Address Resolution Protocol
7520 * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
7521 * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
7522 * the FARP response request.
7523 *
7524 * Return code
7525 * 0 - Successfully processed FARPR IOCB (currently always return 0)
7526 **/
dea3101e 7527static int
2e0fef85
JS
7528lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7529 struct lpfc_nodelist *ndlp)
dea3101e
JB
7530{
7531 struct lpfc_dmabuf *pcmd;
7532 uint32_t *lp;
7533 IOCB_t *icmd;
7534 uint32_t cmd, did;
7535
7536 icmd = &cmdiocb->iocb;
7537 did = icmd->un.elsreq64.remoteID;
7538 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7539 lp = (uint32_t *) pcmd->virt;
7540
7541 cmd = *lp++;
7542 /* FARP-RSP received from DID <did> */
e8b62011
JS
7543 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7544 "0600 FARP-RSP received from DID x%x\n", did);
dea3101e 7545 /* ACCEPT the Farp resp request */
51ef4c26 7546 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e
JB
7547
7548 return 0;
7549}
7550
e59058c4 7551/**
3621a710 7552 * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
e59058c4
JS
7553 * @vport: pointer to a host virtual N_Port data structure.
7554 * @cmdiocb: pointer to lpfc command iocb data structure.
7555 * @fan_ndlp: pointer to a node-list data structure.
7556 *
7557 * This routine processes a Fabric Address Notification (FAN) IOCB
7558 * command received as an ELS unsolicited event. The FAN ELS command will
7559 * only be processed on a physical port (i.e., the @vport represents the
7560 * physical port). The fabric NodeName and PortName from the FAN IOCB are
7561 * compared against those in the phba data structure. If any of those is
7562 * different, the lpfc_initial_flogi() routine is invoked to initialize
7563 * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
7564 * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
7565 * is invoked to register login to the fabric.
7566 *
7567 * Return code
7568 * 0 - Successfully processed fan iocb (currently always return 0).
7569 **/
dea3101e 7570static int
2e0fef85
JS
7571lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7572 struct lpfc_nodelist *fan_ndlp)
dea3101e 7573{
0d2b6b83 7574 struct lpfc_hba *phba = vport->phba;
dea3101e 7575 uint32_t *lp;
5024ab17 7576 FAN *fp;
dea3101e 7577
0d2b6b83
JS
7578 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
7579 lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
7580 fp = (FAN *) ++lp;
5024ab17 7581 /* FAN received; Fan does not have a reply sequence */
0d2b6b83
JS
7582 if ((vport == phba->pport) &&
7583 (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5024ab17 7584 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
0d2b6b83 7585 sizeof(struct lpfc_name))) ||
5024ab17 7586 (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
0d2b6b83
JS
7587 sizeof(struct lpfc_name)))) {
7588 /* This port has switched fabrics. FLOGI is required */
76a95d75 7589 lpfc_issue_init_vfi(vport);
0d2b6b83
JS
7590 } else {
7591 /* FAN verified - skip FLOGI */
7592 vport->fc_myDID = vport->fc_prevDID;
6fb120a7
JS
7593 if (phba->sli_rev < LPFC_SLI_REV4)
7594 lpfc_issue_fabric_reglogin(vport);
1b51197d
JS
7595 else {
7596 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7597 "3138 Need register VFI: (x%x/%x)\n",
7598 vport->fc_prevDID, vport->fc_myDID);
6fb120a7 7599 lpfc_issue_reg_vfi(vport);
1b51197d 7600 }
5024ab17 7601 }
dea3101e 7602 }
c9f8735b 7603 return 0;
dea3101e
JB
7604}
7605
e59058c4 7606/**
3621a710 7607 * lpfc_els_timeout - Handler funciton to the els timer
e59058c4
JS
7608 * @ptr: holder for the timer function associated data.
7609 *
7610 * This routine is invoked by the ELS timer after timeout. It posts the ELS
7611 * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
7612 * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
7613 * up the worker thread. It is for the worker thread to invoke the routine
7614 * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
7615 **/
dea3101e 7616void
f22eb4d3 7617lpfc_els_timeout(struct timer_list *t)
dea3101e 7618{
f22eb4d3 7619 struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
2e0fef85 7620 struct lpfc_hba *phba = vport->phba;
5e9d9b82 7621 uint32_t tmo_posted;
dea3101e
JB
7622 unsigned long iflag;
7623
2e0fef85 7624 spin_lock_irqsave(&vport->work_port_lock, iflag);
5e9d9b82 7625 tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
06918ac5 7626 if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
2e0fef85 7627 vport->work_port_events |= WORKER_ELS_TMO;
5e9d9b82 7628 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
92d7f7b0 7629
06918ac5 7630 if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
5e9d9b82 7631 lpfc_worker_wake_up(phba);
dea3101e
JB
7632 return;
7633}
7634
2a9bf3d0 7635
e59058c4 7636/**
3621a710 7637 * lpfc_els_timeout_handler - Process an els timeout event
e59058c4
JS
7638 * @vport: pointer to a virtual N_Port data structure.
7639 *
7640 * This routine is the actual handler function that processes an ELS timeout
7641 * event. It walks the ELS ring to get and abort all the IOCBs (except the
7642 * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
7643 * invoking the lpfc_sli_issue_abort_iotag() routine.
7644 **/
dea3101e 7645void
2e0fef85 7646lpfc_els_timeout_handler(struct lpfc_vport *vport)
dea3101e 7647{
2e0fef85 7648 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
7649 struct lpfc_sli_ring *pring;
7650 struct lpfc_iocbq *tmp_iocb, *piocb;
7651 IOCB_t *cmd = NULL;
7652 struct lpfc_dmabuf *pcmd;
2e0fef85 7653 uint32_t els_command = 0;
dea3101e 7654 uint32_t timeout;
2e0fef85 7655 uint32_t remote_ID = 0xffffffff;
2a9bf3d0
JS
7656 LIST_HEAD(abort_list);
7657
dea3101e 7658
dea3101e
JB
7659 timeout = (uint32_t)(phba->fc_ratov << 1);
7660
895427bd 7661 pring = lpfc_phba_elsring(phba);
1234a6d5
DK
7662 if (unlikely(!pring))
7663 return;
895427bd 7664
06918ac5
JS
7665 if ((phba->pport->load_flag & FC_UNLOADING))
7666 return;
2a9bf3d0 7667 spin_lock_irq(&phba->hbalock);
0976e1a6
JS
7668 if (phba->sli_rev == LPFC_SLI_REV4)
7669 spin_lock(&pring->ring_lock);
2a9bf3d0 7670
06918ac5
JS
7671 if ((phba->pport->load_flag & FC_UNLOADING)) {
7672 if (phba->sli_rev == LPFC_SLI_REV4)
7673 spin_unlock(&pring->ring_lock);
7674 spin_unlock_irq(&phba->hbalock);
7675 return;
7676 }
7677
0976e1a6 7678 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
dea3101e
JB
7679 cmd = &piocb->iocb;
7680
2e0fef85
JS
7681 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
7682 piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7683 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
dea3101e 7684 continue;
2e0fef85
JS
7685
7686 if (piocb->vport != vport)
7687 continue;
7688
dea3101e 7689 pcmd = (struct lpfc_dmabuf *) piocb->context2;
2e0fef85
JS
7690 if (pcmd)
7691 els_command = *(uint32_t *) (pcmd->virt);
dea3101e 7692
92d7f7b0
JS
7693 if (els_command == ELS_CMD_FARP ||
7694 els_command == ELS_CMD_FARPR ||
7695 els_command == ELS_CMD_FDISC)
7696 continue;
7697
dea3101e 7698 if (piocb->drvrTimeout > 0) {
92d7f7b0 7699 if (piocb->drvrTimeout >= timeout)
dea3101e 7700 piocb->drvrTimeout -= timeout;
92d7f7b0 7701 else
dea3101e 7702 piocb->drvrTimeout = 0;
dea3101e
JB
7703 continue;
7704 }
7705
2e0fef85
JS
7706 remote_ID = 0xffffffff;
7707 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
dea3101e 7708 remote_ID = cmd->un.elsreq64.remoteID;
2e0fef85
JS
7709 else {
7710 struct lpfc_nodelist *ndlp;
7711 ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
58da1ffb 7712 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2e0fef85 7713 remote_ID = ndlp->nlp_DID;
dea3101e 7714 }
2a9bf3d0
JS
7715 list_add_tail(&piocb->dlist, &abort_list);
7716 }
0976e1a6
JS
7717 if (phba->sli_rev == LPFC_SLI_REV4)
7718 spin_unlock(&pring->ring_lock);
2a9bf3d0
JS
7719 spin_unlock_irq(&phba->hbalock);
7720
7721 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
15026c9e 7722 cmd = &piocb->iocb;
e8b62011 7723 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2a9bf3d0
JS
7724 "0127 ELS timeout Data: x%x x%x x%x "
7725 "x%x\n", els_command,
7726 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
7727 spin_lock_irq(&phba->hbalock);
7728 list_del_init(&piocb->dlist);
07951076 7729 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
2a9bf3d0 7730 spin_unlock_irq(&phba->hbalock);
dea3101e 7731 }
5a0e326d 7732
895427bd 7733 if (!list_empty(&pring->txcmplq))
06918ac5
JS
7734 if (!(phba->pport->load_flag & FC_UNLOADING))
7735 mod_timer(&vport->els_tmofunc,
7736 jiffies + msecs_to_jiffies(1000 * timeout));
dea3101e
JB
7737}
7738
e59058c4 7739/**
3621a710 7740 * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
e59058c4
JS
7741 * @vport: pointer to a host virtual N_Port data structure.
7742 *
7743 * This routine is used to clean up all the outstanding ELS commands on a
7744 * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
7745 * routine. After that, it walks the ELS transmit queue to remove all the
7746 * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
7747 * the IOCBs with a non-NULL completion callback function, the callback
7748 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7749 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
7750 * callback function, the IOCB will simply be released. Finally, it walks
7751 * the ELS transmit completion queue to issue an abort IOCB to any transmit
7752 * completion queue IOCB that is associated with the @vport and is not
7753 * an IOCB from libdfc (i.e., the management plane IOCBs that are not
7754 * part of the discovery state machine) out to HBA by invoking the
7755 * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
7756 * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
7757 * the IOCBs are aborted when this function returns.
7758 **/
dea3101e 7759void
2e0fef85 7760lpfc_els_flush_cmd(struct lpfc_vport *vport)
dea3101e 7761{
0976e1a6 7762 LIST_HEAD(abort_list);
2e0fef85 7763 struct lpfc_hba *phba = vport->phba;
895427bd 7764 struct lpfc_sli_ring *pring;
dea3101e
JB
7765 struct lpfc_iocbq *tmp_iocb, *piocb;
7766 IOCB_t *cmd = NULL;
92d7f7b0
JS
7767
7768 lpfc_fabric_abort_vport(vport);
0976e1a6
JS
7769 /*
7770 * For SLI3, only the hbalock is required. But SLI4 needs to coordinate
7771 * with the ring insert operation. Because lpfc_sli_issue_abort_iotag
7772 * ultimately grabs the ring_lock, the driver must splice the list into
7773 * a working list and release the locks before calling the abort.
7774 */
7775 spin_lock_irq(&phba->hbalock);
895427bd 7776 pring = lpfc_phba_elsring(phba);
53cf29d3
GP
7777
7778 /* Bail out if we've no ELS wq, like in PCI error recovery case. */
7779 if (unlikely(!pring)) {
7780 spin_unlock_irq(&phba->hbalock);
7781 return;
7782 }
7783
0976e1a6
JS
7784 if (phba->sli_rev == LPFC_SLI_REV4)
7785 spin_lock(&pring->ring_lock);
7786
7787 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7788 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
7789 continue;
7790
7791 if (piocb->vport != vport)
7792 continue;
7793 list_add_tail(&piocb->dlist, &abort_list);
7794 }
7795 if (phba->sli_rev == LPFC_SLI_REV4)
7796 spin_unlock(&pring->ring_lock);
7797 spin_unlock_irq(&phba->hbalock);
7798 /* Abort each iocb on the aborted list and remove the dlist links. */
7799 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
7800 spin_lock_irq(&phba->hbalock);
7801 list_del_init(&piocb->dlist);
7802 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
7803 spin_unlock_irq(&phba->hbalock);
7804 }
7805 if (!list_empty(&abort_list))
7806 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7807 "3387 abort list for txq not empty\n");
7808 INIT_LIST_HEAD(&abort_list);
dea3101e 7809
2e0fef85 7810 spin_lock_irq(&phba->hbalock);
0976e1a6
JS
7811 if (phba->sli_rev == LPFC_SLI_REV4)
7812 spin_lock(&pring->ring_lock);
7813
dea3101e
JB
7814 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
7815 cmd = &piocb->iocb;
7816
7817 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
7818 continue;
7819 }
7820
7821 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
329f9bc7
JS
7822 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
7823 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
7824 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7825 cmd->ulpCommand == CMD_ABORT_XRI_CN)
dea3101e 7826 continue;
dea3101e 7827
2e0fef85
JS
7828 if (piocb->vport != vport)
7829 continue;
7830
0976e1a6
JS
7831 list_del_init(&piocb->list);
7832 list_add_tail(&piocb->list, &abort_list);
dea3101e 7833 }
0976e1a6
JS
7834 if (phba->sli_rev == LPFC_SLI_REV4)
7835 spin_unlock(&pring->ring_lock);
2e0fef85 7836 spin_unlock_irq(&phba->hbalock);
2534ba75 7837
a257bf90 7838 /* Cancell all the IOCBs from the completions list */
0976e1a6
JS
7839 lpfc_sli_cancel_iocbs(phba, &abort_list,
7840 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
2534ba75 7841
dea3101e
JB
7842 return;
7843}
7844
e59058c4 7845/**
3621a710 7846 * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
e59058c4
JS
7847 * @phba: pointer to lpfc hba data structure.
7848 *
7849 * This routine is used to clean up all the outstanding ELS commands on a
7850 * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
7851 * routine. After that, it walks the ELS transmit queue to remove all the
7852 * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
7853 * the IOCBs with the completion callback function associated, the callback
7854 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
7855 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
7856 * callback function associated, the IOCB will simply be released. Finally,
7857 * it walks the ELS transmit completion queue to issue an abort IOCB to any
7858 * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
7859 * management plane IOCBs that are not part of the discovery state machine)
7860 * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
7861 **/
549e55cd
JS
7862void
7863lpfc_els_flush_all_cmd(struct lpfc_hba *phba)
7864{
0976e1a6 7865 struct lpfc_vport *vport;
523128e5
JS
7866
7867 spin_lock_irq(&phba->port_list_lock);
0976e1a6
JS
7868 list_for_each_entry(vport, &phba->port_list, listentry)
7869 lpfc_els_flush_cmd(vport);
523128e5 7870 spin_unlock_irq(&phba->port_list_lock);
a257bf90 7871
549e55cd
JS
7872 return;
7873}
7874
ea2151b4 7875/**
3621a710 7876 * lpfc_send_els_failure_event - Posts an ELS command failure event
ea2151b4
JS
7877 * @phba: Pointer to hba context object.
7878 * @cmdiocbp: Pointer to command iocb which reported error.
7879 * @rspiocbp: Pointer to response iocb which reported error.
7880 *
7881 * This function sends an event when there is an ELS command
7882 * failure.
7883 **/
7884void
7885lpfc_send_els_failure_event(struct lpfc_hba *phba,
7886 struct lpfc_iocbq *cmdiocbp,
7887 struct lpfc_iocbq *rspiocbp)
7888{
7889 struct lpfc_vport *vport = cmdiocbp->vport;
7890 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7891 struct lpfc_lsrjt_event lsrjt_event;
7892 struct lpfc_fabric_event_header fabric_event;
7893 struct ls_rjt stat;
7894 struct lpfc_nodelist *ndlp;
7895 uint32_t *pcmd;
7896
7897 ndlp = cmdiocbp->context1;
7898 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7899 return;
7900
7901 if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
7902 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
7903 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
7904 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
7905 sizeof(struct lpfc_name));
7906 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
7907 sizeof(struct lpfc_name));
7908 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
7909 cmdiocbp->context2)->virt);
49198b37 7910 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
ea2151b4
JS
7911 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
7912 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
7913 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
7914 fc_host_post_vendor_event(shost,
7915 fc_get_event_number(),
7916 sizeof(lsrjt_event),
7917 (char *)&lsrjt_event,
ddcc50f0 7918 LPFC_NL_VENDOR_ID);
ea2151b4
JS
7919 return;
7920 }
7921 if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
7922 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
7923 fabric_event.event_type = FC_REG_FABRIC_EVENT;
7924 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
7925 fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
7926 else
7927 fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
7928 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
7929 sizeof(struct lpfc_name));
7930 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
7931 sizeof(struct lpfc_name));
7932 fc_host_post_vendor_event(shost,
7933 fc_get_event_number(),
7934 sizeof(fabric_event),
7935 (char *)&fabric_event,
ddcc50f0 7936 LPFC_NL_VENDOR_ID);
ea2151b4
JS
7937 return;
7938 }
7939
7940}
7941
7942/**
3621a710 7943 * lpfc_send_els_event - Posts unsolicited els event
ea2151b4
JS
7944 * @vport: Pointer to vport object.
7945 * @ndlp: Pointer FC node object.
7946 * @cmd: ELS command code.
7947 *
7948 * This function posts an event when there is an incoming
7949 * unsolicited ELS command.
7950 **/
7951static void
7952lpfc_send_els_event(struct lpfc_vport *vport,
7953 struct lpfc_nodelist *ndlp,
ddcc50f0 7954 uint32_t *payload)
ea2151b4 7955{
ddcc50f0
JS
7956 struct lpfc_els_event_header *els_data = NULL;
7957 struct lpfc_logo_event *logo_data = NULL;
ea2151b4
JS
7958 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7959
ddcc50f0
JS
7960 if (*payload == ELS_CMD_LOGO) {
7961 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
7962 if (!logo_data) {
7963 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7964 "0148 Failed to allocate memory "
7965 "for LOGO event\n");
7966 return;
7967 }
7968 els_data = &logo_data->header;
7969 } else {
7970 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
7971 GFP_KERNEL);
7972 if (!els_data) {
7973 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7974 "0149 Failed to allocate memory "
7975 "for ELS event\n");
7976 return;
7977 }
7978 }
7979 els_data->event_type = FC_REG_ELS_EVENT;
7980 switch (*payload) {
ea2151b4 7981 case ELS_CMD_PLOGI:
ddcc50f0 7982 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
ea2151b4
JS
7983 break;
7984 case ELS_CMD_PRLO:
ddcc50f0 7985 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
ea2151b4
JS
7986 break;
7987 case ELS_CMD_ADISC:
ddcc50f0
JS
7988 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
7989 break;
7990 case ELS_CMD_LOGO:
7991 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
7992 /* Copy the WWPN in the LOGO payload */
7993 memcpy(logo_data->logo_wwpn, &payload[2],
7994 sizeof(struct lpfc_name));
ea2151b4
JS
7995 break;
7996 default:
e916141c 7997 kfree(els_data);
ea2151b4
JS
7998 return;
7999 }
ddcc50f0
JS
8000 memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
8001 memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
8002 if (*payload == ELS_CMD_LOGO) {
8003 fc_host_post_vendor_event(shost,
8004 fc_get_event_number(),
8005 sizeof(struct lpfc_logo_event),
8006 (char *)logo_data,
8007 LPFC_NL_VENDOR_ID);
8008 kfree(logo_data);
8009 } else {
8010 fc_host_post_vendor_event(shost,
8011 fc_get_event_number(),
8012 sizeof(struct lpfc_els_event_header),
8013 (char *)els_data,
8014 LPFC_NL_VENDOR_ID);
8015 kfree(els_data);
8016 }
ea2151b4
JS
8017
8018 return;
8019}
8020
8021
e59058c4 8022/**
3621a710 8023 * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
e59058c4
JS
8024 * @phba: pointer to lpfc hba data structure.
8025 * @pring: pointer to a SLI ring.
8026 * @vport: pointer to a host virtual N_Port data structure.
8027 * @elsiocb: pointer to lpfc els command iocb data structure.
8028 *
8029 * This routine is used for processing the IOCB associated with a unsolicited
8030 * event. It first determines whether there is an existing ndlp that matches
8031 * the DID from the unsolicited IOCB. If not, it will create a new one with
8032 * the DID from the unsolicited IOCB. The ELS command from the unsolicited
8033 * IOCB is then used to invoke the proper routine and to set up proper state
8034 * of the discovery state machine.
8035 **/
ed957684
JS
8036static void
8037lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
92d7f7b0 8038 struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
dea3101e 8039{
87af33fe 8040 struct Scsi_Host *shost;
dea3101e 8041 struct lpfc_nodelist *ndlp;
dea3101e 8042 struct ls_rjt stat;
92d7f7b0 8043 uint32_t *payload;
303f2f9c 8044 uint32_t cmd, did, newnode;
d496b9a7 8045 uint8_t rjt_exp, rjt_err = 0, init_link = 0;
ed957684 8046 IOCB_t *icmd = &elsiocb->iocb;
d496b9a7 8047 LPFC_MBOXQ_t *mbox;
dea3101e 8048
e47c9093 8049 if (!vport || !(elsiocb->context2))
dea3101e 8050 goto dropit;
2e0fef85 8051
dea3101e 8052 newnode = 0;
92d7f7b0
JS
8053 payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
8054 cmd = *payload;
ed957684 8055 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
495a714c 8056 lpfc_post_buffer(phba, pring, 1);
dea3101e 8057
858c9f6c
JS
8058 did = icmd->un.rcvels.remoteID;
8059 if (icmd->ulpStatus) {
8060 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8061 "RCV Unsol ELS: status:x%x/x%x did:x%x",
8062 icmd->ulpStatus, icmd->un.ulpWord[4], did);
dea3101e 8063 goto dropit;
858c9f6c 8064 }
dea3101e
JB
8065
8066 /* Check to see if link went down during discovery */
ed957684 8067 if (lpfc_els_chk_latt(vport))
dea3101e 8068 goto dropit;
dea3101e 8069
c868595d 8070 /* Ignore traffic received during vport shutdown. */
92d7f7b0
JS
8071 if (vport->load_flag & FC_UNLOADING)
8072 goto dropit;
8073
92494144
JS
8074 /* If NPort discovery is delayed drop incoming ELS */
8075 if ((vport->fc_flag & FC_DISC_DELAYED) &&
8076 (cmd != ELS_CMD_PLOGI))
8077 goto dropit;
8078
2e0fef85 8079 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 8080 if (!ndlp) {
dea3101e 8081 /* Cannot find existing Fabric ndlp, so allocate a new one */
9d3d340d 8082 ndlp = lpfc_nlp_init(vport, did);
ed957684 8083 if (!ndlp)
dea3101e 8084 goto dropit;
98c9ea5c 8085 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
dea3101e 8086 newnode = 1;
e47c9093 8087 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
dea3101e 8088 ndlp->nlp_type |= NLP_FABRIC;
58da1ffb
JS
8089 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
8090 ndlp = lpfc_enable_node(vport, ndlp,
8091 NLP_STE_UNUSED_NODE);
8092 if (!ndlp)
8093 goto dropit;
8094 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8095 newnode = 1;
8096 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8097 ndlp->nlp_type |= NLP_FABRIC;
8098 } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
8099 /* This is similar to the new node path */
8100 ndlp = lpfc_nlp_get(ndlp);
8101 if (!ndlp)
8102 goto dropit;
8103 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8104 newnode = 1;
87af33fe 8105 }
dea3101e
JB
8106
8107 phba->fc_stat.elsRcvFrame++;
e47c9093 8108
12838e74
JS
8109 /*
8110 * Do not process any unsolicited ELS commands
8111 * if the ndlp is in DEV_LOSS
8112 */
466e840b
JS
8113 shost = lpfc_shost_from_vport(vport);
8114 spin_lock_irq(shost->host_lock);
8115 if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
8116 spin_unlock_irq(shost->host_lock);
12838e74 8117 goto dropit;
466e840b
JS
8118 }
8119 spin_unlock_irq(shost->host_lock);
12838e74 8120
329f9bc7 8121 elsiocb->context1 = lpfc_nlp_get(ndlp);
2e0fef85 8122 elsiocb->vport = vport;
dea3101e
JB
8123
8124 if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
8125 cmd &= ELS_CMD_MASK;
8126 }
8127 /* ELS command <elsCmd> received from NPORT <did> */
e8b62011
JS
8128 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8129 "0112 ELS command x%x received from NPORT x%x "
e74c03c8
JS
8130 "Data: x%x x%x x%x x%x\n",
8131 cmd, did, vport->port_state, vport->fc_flag,
8132 vport->fc_myDID, vport->fc_prevDID);
eec3d312
JS
8133
8134 /* reject till our FLOGI completes */
8135 if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
d6de08cc 8136 (cmd != ELS_CMD_FLOGI)) {
401304cc 8137 rjt_err = LSRJT_LOGICAL_BSY;
eec3d312
JS
8138 rjt_exp = LSEXP_NOTHING_MORE;
8139 goto lsrjt;
8140 }
8141
dea3101e
JB
8142 switch (cmd) {
8143 case ELS_CMD_PLOGI:
858c9f6c
JS
8144 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8145 "RCV PLOGI: did:x%x/ste:x%x flg:x%x",
8146 did, vport->port_state, ndlp->nlp_flag);
8147
dea3101e 8148 phba->fc_stat.elsRcvPLOGI++;
858c9f6c 8149 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
e74c03c8
JS
8150 if (phba->sli_rev == LPFC_SLI_REV4 &&
8151 (phba->pport->fc_flag & FC_PT2PT)) {
8152 vport->fc_prevDID = vport->fc_myDID;
8153 /* Our DID needs to be updated before registering
8154 * the vfi. This is done in lpfc_rcv_plogi but
8155 * that is called after the reg_vfi.
8156 */
8157 vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
8158 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8159 "3312 Remote port assigned DID x%x "
8160 "%x\n", vport->fc_myDID,
8161 vport->fc_prevDID);
8162 }
858c9f6c 8163
ddcc50f0 8164 lpfc_send_els_event(vport, ndlp, payload);
92494144
JS
8165
8166 /* If Nport discovery is delayed, reject PLOGIs */
8167 if (vport->fc_flag & FC_DISC_DELAYED) {
8168 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8169 rjt_exp = LSEXP_NOTHING_MORE;
92494144
JS
8170 break;
8171 }
d6de08cc 8172
858c9f6c 8173 if (vport->port_state < LPFC_DISC_AUTH) {
1b32f6aa
JS
8174 if (!(phba->pport->fc_flag & FC_PT2PT) ||
8175 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
8176 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8177 rjt_exp = LSEXP_NOTHING_MORE;
1b32f6aa
JS
8178 break;
8179 }
dea3101e 8180 }
87af33fe 8181
87af33fe
JS
8182 spin_lock_irq(shost->host_lock);
8183 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
8184 spin_unlock_irq(shost->host_lock);
8185
2e0fef85
JS
8186 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8187 NLP_EVT_RCV_PLOGI);
858c9f6c 8188
dea3101e
JB
8189 break;
8190 case ELS_CMD_FLOGI:
858c9f6c
JS
8191 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8192 "RCV FLOGI: did:x%x/ste:x%x flg:x%x",
8193 did, vport->port_state, ndlp->nlp_flag);
8194
dea3101e 8195 phba->fc_stat.elsRcvFLOGI++;
d496b9a7
JS
8196
8197 /* If the driver believes fabric discovery is done and is ready,
8198 * bounce the link. There is some descrepancy.
8199 */
8200 if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
8201 vport->fc_flag & FC_PT2PT &&
8202 vport->rcv_flogi_cnt >= 1) {
8203 rjt_err = LSRJT_LOGICAL_BSY;
8204 rjt_exp = LSEXP_NOTHING_MORE;
8205 init_link++;
8206 goto lsrjt;
8207 }
8208
51ef4c26 8209 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
87af33fe 8210 if (newnode)
98c9ea5c 8211 lpfc_nlp_put(ndlp);
dea3101e
JB
8212 break;
8213 case ELS_CMD_LOGO:
858c9f6c
JS
8214 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8215 "RCV LOGO: did:x%x/ste:x%x flg:x%x",
8216 did, vport->port_state, ndlp->nlp_flag);
8217
dea3101e 8218 phba->fc_stat.elsRcvLOGO++;
ddcc50f0 8219 lpfc_send_els_event(vport, ndlp, payload);
2e0fef85 8220 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 8221 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8222 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e
JB
8223 break;
8224 }
2e0fef85 8225 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
dea3101e
JB
8226 break;
8227 case ELS_CMD_PRLO:
858c9f6c
JS
8228 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8229 "RCV PRLO: did:x%x/ste:x%x flg:x%x",
8230 did, vport->port_state, ndlp->nlp_flag);
8231
dea3101e 8232 phba->fc_stat.elsRcvPRLO++;
ddcc50f0 8233 lpfc_send_els_event(vport, ndlp, payload);
2e0fef85 8234 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 8235 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8236 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e
JB
8237 break;
8238 }
2e0fef85 8239 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
dea3101e 8240 break;
8b017a30
JS
8241 case ELS_CMD_LCB:
8242 phba->fc_stat.elsRcvLCB++;
8243 lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
8244 break;
86478875
JS
8245 case ELS_CMD_RDP:
8246 phba->fc_stat.elsRcvRDP++;
8247 lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
8248 break;
dea3101e
JB
8249 case ELS_CMD_RSCN:
8250 phba->fc_stat.elsRcvRSCN++;
51ef4c26 8251 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
87af33fe 8252 if (newnode)
98c9ea5c 8253 lpfc_nlp_put(ndlp);
dea3101e
JB
8254 break;
8255 case ELS_CMD_ADISC:
858c9f6c
JS
8256 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8257 "RCV ADISC: did:x%x/ste:x%x flg:x%x",
8258 did, vport->port_state, ndlp->nlp_flag);
8259
ddcc50f0 8260 lpfc_send_els_event(vport, ndlp, payload);
dea3101e 8261 phba->fc_stat.elsRcvADISC++;
2e0fef85 8262 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 8263 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8264 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e
JB
8265 break;
8266 }
2e0fef85
JS
8267 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8268 NLP_EVT_RCV_ADISC);
dea3101e
JB
8269 break;
8270 case ELS_CMD_PDISC:
858c9f6c
JS
8271 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8272 "RCV PDISC: did:x%x/ste:x%x flg:x%x",
8273 did, vport->port_state, ndlp->nlp_flag);
8274
dea3101e 8275 phba->fc_stat.elsRcvPDISC++;
2e0fef85 8276 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 8277 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8278 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e
JB
8279 break;
8280 }
2e0fef85
JS
8281 lpfc_disc_state_machine(vport, ndlp, elsiocb,
8282 NLP_EVT_RCV_PDISC);
dea3101e
JB
8283 break;
8284 case ELS_CMD_FARPR:
858c9f6c
JS
8285 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8286 "RCV FARPR: did:x%x/ste:x%x flg:x%x",
8287 did, vport->port_state, ndlp->nlp_flag);
8288
dea3101e 8289 phba->fc_stat.elsRcvFARPR++;
2e0fef85 8290 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
dea3101e
JB
8291 break;
8292 case ELS_CMD_FARP:
858c9f6c
JS
8293 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8294 "RCV FARP: did:x%x/ste:x%x flg:x%x",
8295 did, vport->port_state, ndlp->nlp_flag);
8296
dea3101e 8297 phba->fc_stat.elsRcvFARP++;
2e0fef85 8298 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
dea3101e
JB
8299 break;
8300 case ELS_CMD_FAN:
858c9f6c
JS
8301 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8302 "RCV FAN: did:x%x/ste:x%x flg:x%x",
8303 did, vport->port_state, ndlp->nlp_flag);
8304
dea3101e 8305 phba->fc_stat.elsRcvFAN++;
2e0fef85 8306 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
dea3101e 8307 break;
dea3101e 8308 case ELS_CMD_PRLI:
a0f2d3ef 8309 case ELS_CMD_NVMEPRLI:
858c9f6c
JS
8310 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8311 "RCV PRLI: did:x%x/ste:x%x flg:x%x",
8312 did, vport->port_state, ndlp->nlp_flag);
8313
dea3101e 8314 phba->fc_stat.elsRcvPRLI++;
a71e3cdc
DK
8315 if ((vport->port_state < LPFC_DISC_AUTH) &&
8316 (vport->fc_flag & FC_FABRIC)) {
858c9f6c 8317 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 8318 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e
JB
8319 break;
8320 }
2e0fef85 8321 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
dea3101e 8322 break;
7bb3b137 8323 case ELS_CMD_LIRR:
858c9f6c
JS
8324 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8325 "RCV LIRR: did:x%x/ste:x%x flg:x%x",
8326 did, vport->port_state, ndlp->nlp_flag);
8327
7bb3b137 8328 phba->fc_stat.elsRcvLIRR++;
2e0fef85 8329 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
87af33fe 8330 if (newnode)
98c9ea5c 8331 lpfc_nlp_put(ndlp);
7bb3b137 8332 break;
12265f68
JS
8333 case ELS_CMD_RLS:
8334 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8335 "RCV RLS: did:x%x/ste:x%x flg:x%x",
8336 did, vport->port_state, ndlp->nlp_flag);
8337
8338 phba->fc_stat.elsRcvRLS++;
8339 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
8340 if (newnode)
8341 lpfc_nlp_put(ndlp);
8342 break;
7bb3b137 8343 case ELS_CMD_RPS:
858c9f6c
JS
8344 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8345 "RCV RPS: did:x%x/ste:x%x flg:x%x",
8346 did, vport->port_state, ndlp->nlp_flag);
8347
7bb3b137 8348 phba->fc_stat.elsRcvRPS++;
2e0fef85 8349 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
87af33fe 8350 if (newnode)
98c9ea5c 8351 lpfc_nlp_put(ndlp);
7bb3b137
JW
8352 break;
8353 case ELS_CMD_RPL:
858c9f6c
JS
8354 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8355 "RCV RPL: did:x%x/ste:x%x flg:x%x",
8356 did, vport->port_state, ndlp->nlp_flag);
8357
7bb3b137 8358 phba->fc_stat.elsRcvRPL++;
2e0fef85 8359 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
87af33fe 8360 if (newnode)
98c9ea5c 8361 lpfc_nlp_put(ndlp);
7bb3b137 8362 break;
dea3101e 8363 case ELS_CMD_RNID:
858c9f6c
JS
8364 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8365 "RCV RNID: did:x%x/ste:x%x flg:x%x",
8366 did, vport->port_state, ndlp->nlp_flag);
8367
dea3101e 8368 phba->fc_stat.elsRcvRNID++;
2e0fef85 8369 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
87af33fe 8370 if (newnode)
98c9ea5c 8371 lpfc_nlp_put(ndlp);
dea3101e 8372 break;
12265f68
JS
8373 case ELS_CMD_RTV:
8374 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8375 "RCV RTV: did:x%x/ste:x%x flg:x%x",
8376 did, vport->port_state, ndlp->nlp_flag);
8377 phba->fc_stat.elsRcvRTV++;
8378 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
8379 if (newnode)
8380 lpfc_nlp_put(ndlp);
8381 break;
5ffc266e
JS
8382 case ELS_CMD_RRQ:
8383 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8384 "RCV RRQ: did:x%x/ste:x%x flg:x%x",
8385 did, vport->port_state, ndlp->nlp_flag);
8386
8387 phba->fc_stat.elsRcvRRQ++;
8388 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
8389 if (newnode)
8390 lpfc_nlp_put(ndlp);
8391 break;
12265f68
JS
8392 case ELS_CMD_ECHO:
8393 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8394 "RCV ECHO: did:x%x/ste:x%x flg:x%x",
8395 did, vport->port_state, ndlp->nlp_flag);
8396
8397 phba->fc_stat.elsRcvECHO++;
8398 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
8399 if (newnode)
8400 lpfc_nlp_put(ndlp);
8401 break;
303f2f9c 8402 case ELS_CMD_REC:
8fd03fd1
CIK
8403 /* receive this due to exchange closed */
8404 rjt_err = LSRJT_UNABLE_TPC;
8405 rjt_exp = LSEXP_INVALID_OX_RX;
303f2f9c 8406 break;
dea3101e 8407 default:
858c9f6c
JS
8408 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8409 "RCV ELS cmd: cmd:x%x did:x%x/ste:x%x",
8410 cmd, did, vport->port_state);
8411
dea3101e 8412 /* Unsupported ELS command, reject */
63e801ce 8413 rjt_err = LSRJT_CMD_UNSUPPORTED;
303f2f9c 8414 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e
JB
8415
8416 /* Unknown ELS command <elsCmd> received from NPORT <did> */
e8b62011
JS
8417 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8418 "0115 Unknown ELS command x%x "
8419 "received from NPORT x%x\n", cmd, did);
87af33fe 8420 if (newnode)
98c9ea5c 8421 lpfc_nlp_put(ndlp);
dea3101e
JB
8422 break;
8423 }
8424
eec3d312 8425lsrjt:
dea3101e
JB
8426 /* check if need to LS_RJT received ELS cmd */
8427 if (rjt_err) {
92d7f7b0 8428 memset(&stat, 0, sizeof(stat));
858c9f6c 8429 stat.un.b.lsRjtRsnCode = rjt_err;
303f2f9c 8430 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
858c9f6c
JS
8431 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
8432 NULL);
dea3101e
JB
8433 }
8434
d7c255b2
JS
8435 lpfc_nlp_put(elsiocb->context1);
8436 elsiocb->context1 = NULL;
d496b9a7
JS
8437
8438 /* Special case. Driver received an unsolicited command that
8439 * unsupportable given the driver's current state. Reset the
8440 * link and start over.
8441 */
8442 if (init_link) {
8443 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8444 if (!mbox)
8445 return;
8446 lpfc_linkdown(phba);
8447 lpfc_init_link(phba, mbox,
8448 phba->cfg_topology,
8449 phba->cfg_link_speed);
8450 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8451 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8452 mbox->vport = vport;
8453 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
8454 MBX_NOT_FINISHED)
8455 mempool_free(mbox, phba->mbox_mem_pool);
8456 }
8457
ed957684
JS
8458 return;
8459
8460dropit:
98c9ea5c 8461 if (vport && !(vport->load_flag & FC_UNLOADING))
6fb120a7
JS
8462 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8463 "0111 Dropping received ELS cmd "
ed957684 8464 "Data: x%x x%x x%x\n",
6fb120a7 8465 icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
ed957684
JS
8466 phba->fc_stat.elsRcvDrop++;
8467}
8468
e59058c4 8469/**
3621a710 8470 * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
e59058c4
JS
8471 * @phba: pointer to lpfc hba data structure.
8472 * @pring: pointer to a SLI ring.
8473 * @elsiocb: pointer to lpfc els iocb data structure.
8474 *
8475 * This routine is used to process an unsolicited event received from a SLI
8476 * (Service Level Interface) ring. The actual processing of the data buffer
8477 * associated with the unsolicited event is done by invoking the routine
8478 * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
8479 * SLI ring on which the unsolicited event was received.
8480 **/
ed957684
JS
8481void
8482lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8483 struct lpfc_iocbq *elsiocb)
8484{
8485 struct lpfc_vport *vport = phba->pport;
ed957684 8486 IOCB_t *icmd = &elsiocb->iocb;
ed957684 8487 dma_addr_t paddr;
92d7f7b0
JS
8488 struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
8489 struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
8490
d7c255b2 8491 elsiocb->context1 = NULL;
92d7f7b0
JS
8492 elsiocb->context2 = NULL;
8493 elsiocb->context3 = NULL;
ed957684 8494
92d7f7b0
JS
8495 if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
8496 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
8497 } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
e3d2b802
JS
8498 (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
8499 IOERR_RCV_BUFFER_WAITING) {
ed957684
JS
8500 phba->fc_stat.NoRcvBuf++;
8501 /* Not enough posted buffers; Try posting more buffers */
92d7f7b0 8502 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
495a714c 8503 lpfc_post_buffer(phba, pring, 0);
ed957684
JS
8504 return;
8505 }
8506
92d7f7b0
JS
8507 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
8508 (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
8509 icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
8510 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
8511 vport = phba->pport;
6fb120a7
JS
8512 else
8513 vport = lpfc_find_vport_by_vpid(phba,
6d368e53 8514 icmd->unsli3.rcvsli3.vpi);
92d7f7b0 8515 }
6d368e53 8516
7f5f3d0d
JS
8517 /* If there are no BDEs associated
8518 * with this IOCB, there is nothing to do.
8519 */
ed957684
JS
8520 if (icmd->ulpBdeCount == 0)
8521 return;
8522
7f5f3d0d
JS
8523 /* type of ELS cmd is first 32bit word
8524 * in packet
8525 */
ed957684 8526 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
92d7f7b0 8527 elsiocb->context2 = bdeBuf1;
ed957684
JS
8528 } else {
8529 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
8530 icmd->un.cont64[0].addrLow);
92d7f7b0
JS
8531 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
8532 paddr);
ed957684
JS
8533 }
8534
92d7f7b0
JS
8535 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8536 /*
8537 * The different unsolicited event handlers would tell us
8538 * if they are done with "mp" by setting context2 to NULL.
8539 */
dea3101e 8540 if (elsiocb->context2) {
92d7f7b0
JS
8541 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
8542 elsiocb->context2 = NULL;
dea3101e 8543 }
ed957684
JS
8544
8545 /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
92d7f7b0 8546 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
ed957684 8547 icmd->ulpBdeCount == 2) {
92d7f7b0
JS
8548 elsiocb->context2 = bdeBuf2;
8549 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
ed957684
JS
8550 /* free mp if we are done with it */
8551 if (elsiocb->context2) {
92d7f7b0
JS
8552 lpfc_in_buf_free(phba, elsiocb->context2);
8553 elsiocb->context2 = NULL;
8554 }
8555 }
8556}
8557
bd4b3e5c 8558static void
4258e98e
JS
8559lpfc_start_fdmi(struct lpfc_vport *vport)
8560{
4258e98e
JS
8561 struct lpfc_nodelist *ndlp;
8562
8563 /* If this is the first time, allocate an ndlp and initialize
8564 * it. Otherwise, make sure the node is enabled and then do the
8565 * login.
8566 */
8567 ndlp = lpfc_findnode_did(vport, FDMI_DID);
8568 if (!ndlp) {
9d3d340d 8569 ndlp = lpfc_nlp_init(vport, FDMI_DID);
4258e98e 8570 if (ndlp) {
4258e98e
JS
8571 ndlp->nlp_type |= NLP_FABRIC;
8572 } else {
8573 return;
8574 }
8575 }
8576 if (!NLP_CHK_NODE_ACT(ndlp))
8577 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
8578
8579 if (ndlp) {
8580 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8581 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
8582 }
8583}
8584
e59058c4 8585/**
3621a710 8586 * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
e59058c4
JS
8587 * @phba: pointer to lpfc hba data structure.
8588 * @vport: pointer to a virtual N_Port data structure.
8589 *
8590 * This routine issues a Port Login (PLOGI) to the Name Server with
8591 * State Change Request (SCR) for a @vport. This routine will create an
8592 * ndlp for the Name Server associated to the @vport if such node does
8593 * not already exist. The PLOGI to Name Server is issued by invoking the
8594 * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
8595 * (FDMI) is configured to the @vport, a FDMI node will be created and
8596 * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
8597 **/
92d7f7b0
JS
8598void
8599lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
8600{
4258e98e 8601 struct lpfc_nodelist *ndlp;
92494144
JS
8602 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8603
8604 /*
8605 * If lpfc_delay_discovery parameter is set and the clean address
8606 * bit is cleared and fc fabric parameters chenged, delay FC NPort
8607 * discovery.
8608 */
8609 spin_lock_irq(shost->host_lock);
8610 if (vport->fc_flag & FC_DISC_DELAYED) {
8611 spin_unlock_irq(shost->host_lock);
18775708
JS
8612 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
8613 "3334 Delay fc port discovery for %d seconds\n",
8614 phba->fc_ratov);
92494144 8615 mod_timer(&vport->delayed_disc_tmo,
256ec0d0 8616 jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
92494144
JS
8617 return;
8618 }
8619 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
8620
8621 ndlp = lpfc_findnode_did(vport, NameServer_DID);
8622 if (!ndlp) {
9d3d340d 8623 ndlp = lpfc_nlp_init(vport, NameServer_DID);
92d7f7b0 8624 if (!ndlp) {
76a95d75 8625 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
92d7f7b0
JS
8626 lpfc_disc_start(vport);
8627 return;
8628 }
8629 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
8630 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8631 "0251 NameServer login: no memory\n");
92d7f7b0
JS
8632 return;
8633 }
e47c9093
JS
8634 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
8635 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
8636 if (!ndlp) {
76a95d75 8637 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
e47c9093
JS
8638 lpfc_disc_start(vport);
8639 return;
8640 }
8641 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8642 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8643 "0348 NameServer login: node freed\n");
8644 return;
8645 }
92d7f7b0 8646 }
58da1ffb 8647 ndlp->nlp_type |= NLP_FABRIC;
92d7f7b0
JS
8648
8649 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
8650
8651 if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
8652 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
8653 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
8654 "0252 Cannot issue NameServer login\n");
92d7f7b0
JS
8655 return;
8656 }
8657
8663cbbe
JS
8658 if ((phba->cfg_enable_SmartSAN ||
8659 (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
8660 (vport->load_flag & FC_ALLOW_FDMI))
4258e98e 8661 lpfc_start_fdmi(vport);
92d7f7b0
JS
8662}
8663
e59058c4 8664/**
3621a710 8665 * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
e59058c4
JS
8666 * @phba: pointer to lpfc hba data structure.
8667 * @pmb: pointer to the driver internal queue element for mailbox command.
8668 *
8669 * This routine is the completion callback function to register new vport
8670 * mailbox command. If the new vport mailbox command completes successfully,
8671 * the fabric registration login shall be performed on physical port (the
8672 * new vport created is actually a physical port, with VPI 0) or the port
8673 * login to Name Server for State Change Request (SCR) will be performed
8674 * on virtual port (real virtual port, with VPI greater than 0).
8675 **/
92d7f7b0
JS
8676static void
8677lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
8678{
8679 struct lpfc_vport *vport = pmb->vport;
8680 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3e1f0718 8681 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
04c68496 8682 MAILBOX_t *mb = &pmb->u.mb;
695a814e 8683 int rc;
92d7f7b0 8684
09372820 8685 spin_lock_irq(shost->host_lock);
92d7f7b0 8686 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
09372820 8687 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
8688
8689 if (mb->mbxStatus) {
e8b62011 8690 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
38b92ef8
JS
8691 "0915 Register VPI failed : Status: x%x"
8692 " upd bit: x%x \n", mb->mbxStatus,
8693 mb->un.varRegVpi.upd);
8694 if (phba->sli_rev == LPFC_SLI_REV4 &&
8695 mb->un.varRegVpi.upd)
8696 goto mbox_err_exit ;
92d7f7b0
JS
8697
8698 switch (mb->mbxStatus) {
8699 case 0x11: /* unsupported feature */
8700 case 0x9603: /* max_vpi exceeded */
7f5f3d0d 8701 case 0x9602: /* Link event since CLEAR_LA */
92d7f7b0
JS
8702 /* giving up on vport registration */
8703 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8704 spin_lock_irq(shost->host_lock);
8705 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
8706 spin_unlock_irq(shost->host_lock);
8707 lpfc_can_disctmo(vport);
8708 break;
695a814e
JS
8709 /* If reg_vpi fail with invalid VPI status, re-init VPI */
8710 case 0x20:
8711 spin_lock_irq(shost->host_lock);
8712 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
8713 spin_unlock_irq(shost->host_lock);
8714 lpfc_init_vpi(phba, pmb, vport->vpi);
8715 pmb->vport = vport;
8716 pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
8717 rc = lpfc_sli_issue_mbox(phba, pmb,
8718 MBX_NOWAIT);
8719 if (rc == MBX_NOT_FINISHED) {
8720 lpfc_printf_vlog(vport,
8721 KERN_ERR, LOG_MBOX,
8722 "2732 Failed to issue INIT_VPI"
8723 " mailbox command\n");
8724 } else {
8725 lpfc_nlp_put(ndlp);
8726 return;
8727 }
8728
92d7f7b0
JS
8729 default:
8730 /* Try to recover from this error */
5af5eee7
JS
8731 if (phba->sli_rev == LPFC_SLI_REV4)
8732 lpfc_sli4_unreg_all_rpis(vport);
92d7f7b0 8733 lpfc_mbx_unreg_vpi(vport);
09372820 8734 spin_lock_irq(shost->host_lock);
92d7f7b0 8735 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
09372820 8736 spin_unlock_irq(shost->host_lock);
5d181531
JS
8737 if (mb->mbxStatus == MBX_NOT_FINISHED)
8738 break;
8739 if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
8740 !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
8741 if (phba->sli_rev == LPFC_SLI_REV4)
8742 lpfc_issue_init_vfi(vport);
8743 else
8744 lpfc_initial_flogi(vport);
8745 } else {
7f5f3d0d 8746 lpfc_initial_fdisc(vport);
5d181531 8747 }
92d7f7b0
JS
8748 break;
8749 }
92d7f7b0 8750 } else {
695a814e 8751 spin_lock_irq(shost->host_lock);
1987807d 8752 vport->vpi_state |= LPFC_VPI_REGISTERED;
695a814e
JS
8753 spin_unlock_irq(shost->host_lock);
8754 if (vport == phba->pport) {
6fb120a7
JS
8755 if (phba->sli_rev < LPFC_SLI_REV4)
8756 lpfc_issue_fabric_reglogin(vport);
695a814e 8757 else {
fc2b989b
JS
8758 /*
8759 * If the physical port is instantiated using
8760 * FDISC, do not start vport discovery.
8761 */
8762 if (vport->port_state != LPFC_FDISC)
8763 lpfc_start_fdiscs(phba);
695a814e
JS
8764 lpfc_do_scr_ns_plogi(phba, vport);
8765 }
8766 } else
92d7f7b0
JS
8767 lpfc_do_scr_ns_plogi(phba, vport);
8768 }
38b92ef8 8769mbox_err_exit:
fa4066b6
JS
8770 /* Now, we decrement the ndlp reference count held for this
8771 * callback function
8772 */
8773 lpfc_nlp_put(ndlp);
8774
92d7f7b0
JS
8775 mempool_free(pmb, phba->mbox_mem_pool);
8776 return;
8777}
8778
e59058c4 8779/**
3621a710 8780 * lpfc_register_new_vport - Register a new vport with a HBA
e59058c4
JS
8781 * @phba: pointer to lpfc hba data structure.
8782 * @vport: pointer to a host virtual N_Port data structure.
8783 * @ndlp: pointer to a node-list data structure.
8784 *
8785 * This routine registers the @vport as a new virtual port with a HBA.
8786 * It is done through a registering vpi mailbox command.
8787 **/
695a814e 8788void
92d7f7b0
JS
8789lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
8790 struct lpfc_nodelist *ndlp)
8791{
09372820 8792 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
8793 LPFC_MBOXQ_t *mbox;
8794
8795 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8796 if (mbox) {
6fb120a7 8797 lpfc_reg_vpi(vport, mbox);
92d7f7b0 8798 mbox->vport = vport;
3e1f0718 8799 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
92d7f7b0 8800 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
0b727fea 8801 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
92d7f7b0 8802 == MBX_NOT_FINISHED) {
fa4066b6
JS
8803 /* mailbox command not success, decrement ndlp
8804 * reference count for this command
8805 */
8806 lpfc_nlp_put(ndlp);
92d7f7b0 8807 mempool_free(mbox, phba->mbox_mem_pool);
92d7f7b0 8808
e8b62011
JS
8809 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
8810 "0253 Register VPI: Can't send mbox\n");
fa4066b6 8811 goto mbox_err_exit;
92d7f7b0
JS
8812 }
8813 } else {
e8b62011
JS
8814 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
8815 "0254 Register VPI: no memory\n");
fa4066b6 8816 goto mbox_err_exit;
92d7f7b0 8817 }
fa4066b6
JS
8818 return;
8819
8820mbox_err_exit:
8821 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
8822 spin_lock_irq(shost->host_lock);
8823 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
8824 spin_unlock_irq(shost->host_lock);
8825 return;
92d7f7b0
JS
8826}
8827
695a814e 8828/**
0c9ab6f5 8829 * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
695a814e
JS
8830 * @phba: pointer to lpfc hba data structure.
8831 *
0c9ab6f5 8832 * This routine cancels the retry delay timers to all the vports.
695a814e
JS
8833 **/
8834void
0c9ab6f5 8835lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
695a814e
JS
8836{
8837 struct lpfc_vport **vports;
8838 struct lpfc_nodelist *ndlp;
695a814e 8839 uint32_t link_state;
0c9ab6f5 8840 int i;
695a814e
JS
8841
8842 /* Treat this failure as linkdown for all vports */
8843 link_state = phba->link_state;
8844 lpfc_linkdown(phba);
8845 phba->link_state = link_state;
8846
8847 vports = lpfc_create_vport_work_array(phba);
8848
8849 if (vports) {
8850 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
8851 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
8852 if (ndlp)
8853 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
8854 lpfc_els_flush_cmd(vports[i]);
8855 }
8856 lpfc_destroy_vport_work_array(phba, vports);
8857 }
0c9ab6f5
JS
8858}
8859
8860/**
8861 * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
8862 * @phba: pointer to lpfc hba data structure.
8863 *
8864 * This routine abort all pending discovery commands and
8865 * start a timer to retry FLOGI for the physical port
8866 * discovery.
8867 **/
8868void
8869lpfc_retry_pport_discovery(struct lpfc_hba *phba)
8870{
8871 struct lpfc_nodelist *ndlp;
8872 struct Scsi_Host *shost;
8873
8874 /* Cancel the all vports retry delay retry timers */
8875 lpfc_cancel_all_vport_retry_delay_timer(phba);
695a814e
JS
8876
8877 /* If fabric require FLOGI, then re-instantiate physical login */
8878 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
8879 if (!ndlp)
8880 return;
8881
695a814e 8882 shost = lpfc_shost_from_vport(phba->pport);
256ec0d0 8883 mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
695a814e
JS
8884 spin_lock_irq(shost->host_lock);
8885 ndlp->nlp_flag |= NLP_DELAY_TMO;
8886 spin_unlock_irq(shost->host_lock);
8887 ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
8888 phba->pport->port_state = LPFC_FLOGI;
8889 return;
8890}
8891
8892/**
8893 * lpfc_fabric_login_reqd - Check if FLOGI required.
8894 * @phba: pointer to lpfc hba data structure.
8895 * @cmdiocb: pointer to FDISC command iocb.
8896 * @rspiocb: pointer to FDISC response iocb.
8897 *
8898 * This routine checks if a FLOGI is reguired for FDISC
8899 * to succeed.
8900 **/
8901static int
8902lpfc_fabric_login_reqd(struct lpfc_hba *phba,
8903 struct lpfc_iocbq *cmdiocb,
8904 struct lpfc_iocbq *rspiocb)
8905{
8906
8907 if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
8908 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
8909 return 0;
8910 else
8911 return 1;
8912}
8913
e59058c4 8914/**
3621a710 8915 * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
e59058c4
JS
8916 * @phba: pointer to lpfc hba data structure.
8917 * @cmdiocb: pointer to lpfc command iocb data structure.
8918 * @rspiocb: pointer to lpfc response iocb data structure.
8919 *
8920 * This routine is the completion callback function to a Fabric Discover
8921 * (FDISC) ELS command. Since all the FDISC ELS commands are issued
8922 * single threaded, each FDISC completion callback function will reset
8923 * the discovery timer for all vports such that the timers will not get
8924 * unnecessary timeout. The function checks the FDISC IOCB status. If error
8925 * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
8926 * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
8927 * assigned to the vport has been changed with the completion of the FDISC
8928 * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
8929 * are unregistered from the HBA, and then the lpfc_register_new_vport()
8930 * routine is invoked to register new vport with the HBA. Otherwise, the
8931 * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
8932 * Server for State Change Request (SCR).
8933 **/
92d7f7b0
JS
8934static void
8935lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8936 struct lpfc_iocbq *rspiocb)
8937{
8938 struct lpfc_vport *vport = cmdiocb->vport;
8939 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8940 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
8941 struct lpfc_nodelist *np;
8942 struct lpfc_nodelist *next_np;
8943 IOCB_t *irsp = &rspiocb->iocb;
8944 struct lpfc_iocbq *piocb;
92494144
JS
8945 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
8946 struct serv_parm *sp;
8947 uint8_t fabric_param_changed;
92d7f7b0 8948
e8b62011
JS
8949 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8950 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
8951 irsp->ulpStatus, irsp->un.ulpWord[4],
8952 vport->fc_prevDID);
92d7f7b0
JS
8953 /* Since all FDISCs are being single threaded, we
8954 * must reset the discovery timer for ALL vports
8955 * waiting to send FDISC when one completes.
8956 */
8957 list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
8958 lpfc_set_disctmo(piocb->vport);
8959 }
8960
858c9f6c
JS
8961 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
8962 "FDISC cmpl: status:x%x/x%x prevdid:x%x",
8963 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
8964
92d7f7b0 8965 if (irsp->ulpStatus) {
695a814e
JS
8966
8967 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
8968 lpfc_retry_pport_discovery(phba);
8969 goto out;
8970 }
8971
92d7f7b0
JS
8972 /* Check for retry */
8973 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
8974 goto out;
92d7f7b0 8975 /* FDISC failed */
e8b62011 8976 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6b5151fd 8977 "0126 FDISC failed. (x%x/x%x)\n",
e8b62011 8978 irsp->ulpStatus, irsp->un.ulpWord[4]);
d7c255b2
JS
8979 goto fdisc_failed;
8980 }
d7c255b2 8981 spin_lock_irq(shost->host_lock);
695a814e 8982 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
4b40c59e 8983 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
d7c255b2 8984 vport->fc_flag |= FC_FABRIC;
76a95d75 8985 if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
d7c255b2
JS
8986 vport->fc_flag |= FC_PUBLIC_LOOP;
8987 spin_unlock_irq(shost->host_lock);
92d7f7b0 8988
d7c255b2
JS
8989 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
8990 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
92494144 8991 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
a2fc4aef
JS
8992 if (!prsp)
8993 goto out;
92494144
JS
8994 sp = prsp->virt + sizeof(uint32_t);
8995 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
8996 memcpy(&vport->fabric_portname, &sp->portName,
8997 sizeof(struct lpfc_name));
8998 memcpy(&vport->fabric_nodename, &sp->nodeName,
8999 sizeof(struct lpfc_name));
9000 if (fabric_param_changed &&
d7c255b2
JS
9001 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9002 /* If our NportID changed, we need to ensure all
9003 * remaining NPORTs get unreg_login'ed so we can
9004 * issue unreg_vpi.
9005 */
9006 list_for_each_entry_safe(np, next_np,
9007 &vport->fc_nodes, nlp_listp) {
9008 if (!NLP_CHK_NODE_ACT(ndlp) ||
9009 (np->nlp_state != NLP_STE_NPR_NODE) ||
9010 !(np->nlp_flag & NLP_NPR_ADISC))
9011 continue;
09372820 9012 spin_lock_irq(shost->host_lock);
d7c255b2 9013 np->nlp_flag &= ~NLP_NPR_ADISC;
09372820 9014 spin_unlock_irq(shost->host_lock);
d7c255b2 9015 lpfc_unreg_rpi(vport, np);
92d7f7b0 9016 }
78730cfe 9017 lpfc_cleanup_pending_mbox(vport);
5af5eee7
JS
9018
9019 if (phba->sli_rev == LPFC_SLI_REV4)
9020 lpfc_sli4_unreg_all_rpis(vport);
9021
d7c255b2
JS
9022 lpfc_mbx_unreg_vpi(vport);
9023 spin_lock_irq(shost->host_lock);
9024 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
0f65ff68
JS
9025 if (phba->sli_rev == LPFC_SLI_REV4)
9026 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
4b40c59e
JS
9027 else
9028 vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
d7c255b2 9029 spin_unlock_irq(shost->host_lock);
38b92ef8
JS
9030 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
9031 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9032 /*
9033 * Driver needs to re-reg VPI in order for f/w
9034 * to update the MAC address.
9035 */
9036 lpfc_register_new_vport(phba, vport, ndlp);
5ac6b303 9037 goto out;
92d7f7b0
JS
9038 }
9039
ecfd03c6
JS
9040 if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
9041 lpfc_issue_init_vpi(vport);
9042 else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
d7c255b2
JS
9043 lpfc_register_new_vport(phba, vport, ndlp);
9044 else
9045 lpfc_do_scr_ns_plogi(phba, vport);
9046 goto out;
9047fdisc_failed:
845d9e8d
JS
9048 if (vport->fc_vport &&
9049 (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
c84163d1 9050 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
d7c255b2
JS
9051 /* Cancel discovery timer */
9052 lpfc_can_disctmo(vport);
9053 lpfc_nlp_put(ndlp);
92d7f7b0
JS
9054out:
9055 lpfc_els_free_iocb(phba, cmdiocb);
9056}
9057
e59058c4 9058/**
3621a710 9059 * lpfc_issue_els_fdisc - Issue a fdisc iocb command
e59058c4
JS
9060 * @vport: pointer to a virtual N_Port data structure.
9061 * @ndlp: pointer to a node-list data structure.
9062 * @retry: number of retries to the command IOCB.
9063 *
9064 * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
9065 * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
9066 * routine to issue the IOCB, which makes sure only one outstanding fabric
9067 * IOCB will be sent off HBA at any given time.
9068 *
9069 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9070 * will be incremented by 1 for holding the ndlp and the reference to ndlp
9071 * will be stored into the context1 field of the IOCB for the completion
9072 * callback function to the FDISC ELS command.
9073 *
9074 * Return code
9075 * 0 - Successfully issued fdisc iocb command
9076 * 1 - Failed to issue fdisc iocb command
9077 **/
a6ababd2 9078static int
92d7f7b0
JS
9079lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
9080 uint8_t retry)
9081{
9082 struct lpfc_hba *phba = vport->phba;
9083 IOCB_t *icmd;
9084 struct lpfc_iocbq *elsiocb;
9085 struct serv_parm *sp;
9086 uint8_t *pcmd;
9087 uint16_t cmdsize;
9088 int did = ndlp->nlp_DID;
9089 int rc;
92d7f7b0 9090
5ffc266e 9091 vport->port_state = LPFC_FDISC;
6b5151fd 9092 vport->fc_myDID = 0;
92d7f7b0
JS
9093 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
9094 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
9095 ELS_CMD_FDISC);
9096 if (!elsiocb) {
92d7f7b0 9097 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
9098 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9099 "0255 Issue FDISC: no IOCB\n");
92d7f7b0
JS
9100 return 1;
9101 }
9102
9103 icmd = &elsiocb->iocb;
9104 icmd->un.elsreq64.myID = 0;
9105 icmd->un.elsreq64.fl = 1;
9106
73d91e50
JS
9107 /*
9108 * SLI3 ports require a different context type value than SLI4.
9109 * Catch SLI3 ports here and override the prep.
9110 */
9111 if (phba->sli_rev == LPFC_SLI_REV3) {
f1126688
JS
9112 icmd->ulpCt_h = 1;
9113 icmd->ulpCt_l = 0;
9114 }
92d7f7b0
JS
9115
9116 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9117 *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
9118 pcmd += sizeof(uint32_t); /* CSP Word 1 */
9119 memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
9120 sp = (struct serv_parm *) pcmd;
9121 /* Setup CSPs accordingly for Fabric */
9122 sp->cmn.e_d_tov = 0;
9123 sp->cmn.w2.r_a_tov = 0;
df9e1b59 9124 sp->cmn.virtual_fabric_support = 0;
92d7f7b0
JS
9125 sp->cls1.classValid = 0;
9126 sp->cls2.seqDelivery = 1;
9127 sp->cls3.seqDelivery = 1;
9128
9129 pcmd += sizeof(uint32_t); /* CSP Word 2 */
9130 pcmd += sizeof(uint32_t); /* CSP Word 3 */
9131 pcmd += sizeof(uint32_t); /* CSP Word 4 */
9132 pcmd += sizeof(uint32_t); /* Port Name */
9133 memcpy(pcmd, &vport->fc_portname, 8);
9134 pcmd += sizeof(uint32_t); /* Node Name */
9135 pcmd += sizeof(uint32_t); /* Node Name */
9136 memcpy(pcmd, &vport->fc_nodename, 8);
176de5bb 9137 sp->cmn.valid_vendor_ver_level = 0;
aeb3c817 9138 memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
92d7f7b0
JS
9139 lpfc_set_disctmo(vport);
9140
9141 phba->fc_stat.elsXmitFDISC++;
9142 elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
9143
858c9f6c
JS
9144 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9145 "Issue FDISC: did:x%x",
9146 did, 0, 0);
9147
92d7f7b0
JS
9148 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
9149 if (rc == IOCB_ERROR) {
9150 lpfc_els_free_iocb(phba, elsiocb);
92d7f7b0 9151 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
9152 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
9153 "0256 Issue FDISC: Cannot send IOCB\n");
92d7f7b0
JS
9154 return 1;
9155 }
9156 lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
92d7f7b0
JS
9157 return 0;
9158}
9159
e59058c4 9160/**
3621a710 9161 * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
e59058c4
JS
9162 * @phba: pointer to lpfc hba data structure.
9163 * @cmdiocb: pointer to lpfc command iocb data structure.
9164 * @rspiocb: pointer to lpfc response iocb data structure.
9165 *
9166 * This routine is the completion callback function to the issuing of a LOGO
9167 * ELS command off a vport. It frees the command IOCB and then decrement the
9168 * reference count held on ndlp for this completion function, indicating that
9169 * the reference to the ndlp is no long needed. Note that the
9170 * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
9171 * callback function and an additional explicit ndlp reference decrementation
9172 * will trigger the actual release of the ndlp.
9173 **/
92d7f7b0
JS
9174static void
9175lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9176 struct lpfc_iocbq *rspiocb)
9177{
9178 struct lpfc_vport *vport = cmdiocb->vport;
858c9f6c 9179 IOCB_t *irsp;
e47c9093 9180 struct lpfc_nodelist *ndlp;
9589b062 9181 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
858c9f6c 9182
9589b062 9183 ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
858c9f6c
JS
9184 irsp = &rspiocb->iocb;
9185 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9186 "LOGO npiv cmpl: status:x%x/x%x did:x%x",
9187 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
92d7f7b0
JS
9188
9189 lpfc_els_free_iocb(phba, cmdiocb);
9190 vport->unreg_vpi_cmpl = VPORT_ERROR;
e47c9093
JS
9191
9192 /* Trigger the release of the ndlp after logo */
9193 lpfc_nlp_put(ndlp);
9589b062
JS
9194
9195 /* NPIV LOGO completes to NPort <nlp_DID> */
9196 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9197 "2928 NPIV LOGO completes to NPort x%x "
9198 "Data: x%x x%x x%x x%x\n",
9199 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
9200 irsp->ulpTimeout, vport->num_disc_nodes);
9201
9202 if (irsp->ulpStatus == IOSTAT_SUCCESS) {
9203 spin_lock_irq(shost->host_lock);
73dc0dbe 9204 vport->fc_flag &= ~FC_NDISC_ACTIVE;
9589b062
JS
9205 vport->fc_flag &= ~FC_FABRIC;
9206 spin_unlock_irq(shost->host_lock);
73dc0dbe 9207 lpfc_can_disctmo(vport);
9589b062 9208 }
92d7f7b0
JS
9209}
9210
e59058c4 9211/**
3621a710 9212 * lpfc_issue_els_npiv_logo - Issue a logo off a vport
e59058c4
JS
9213 * @vport: pointer to a virtual N_Port data structure.
9214 * @ndlp: pointer to a node-list data structure.
9215 *
9216 * This routine issues a LOGO ELS command to an @ndlp off a @vport.
9217 *
9218 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9219 * will be incremented by 1 for holding the ndlp and the reference to ndlp
9220 * will be stored into the context1 field of the IOCB for the completion
9221 * callback function to the LOGO ELS command.
9222 *
9223 * Return codes
9224 * 0 - Successfully issued logo off the @vport
9225 * 1 - Failed to issue logo off the @vport
9226 **/
92d7f7b0
JS
9227int
9228lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
9229{
9230 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9231 struct lpfc_hba *phba = vport->phba;
92d7f7b0
JS
9232 struct lpfc_iocbq *elsiocb;
9233 uint8_t *pcmd;
9234 uint16_t cmdsize;
9235
9236 cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
9237 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
9238 ELS_CMD_LOGO);
9239 if (!elsiocb)
9240 return 1;
9241
92d7f7b0
JS
9242 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9243 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
9244 pcmd += sizeof(uint32_t);
9245
9246 /* Fill in LOGO payload */
9247 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
9248 pcmd += sizeof(uint32_t);
9249 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
9250
858c9f6c
JS
9251 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9252 "Issue LOGO npiv did:x%x flg:x%x",
9253 ndlp->nlp_DID, ndlp->nlp_flag, 0);
9254
92d7f7b0
JS
9255 elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
9256 spin_lock_irq(shost->host_lock);
9257 ndlp->nlp_flag |= NLP_LOGO_SND;
9258 spin_unlock_irq(shost->host_lock);
3772a991
JS
9259 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
9260 IOCB_ERROR) {
92d7f7b0
JS
9261 spin_lock_irq(shost->host_lock);
9262 ndlp->nlp_flag &= ~NLP_LOGO_SND;
9263 spin_unlock_irq(shost->host_lock);
9264 lpfc_els_free_iocb(phba, elsiocb);
9265 return 1;
9266 }
9267 return 0;
9268}
9269
e59058c4 9270/**
3621a710 9271 * lpfc_fabric_block_timeout - Handler function to the fabric block timer
e59058c4
JS
9272 * @ptr: holder for the timer function associated data.
9273 *
9274 * This routine is invoked by the fabric iocb block timer after
9275 * timeout. It posts the fabric iocb block timeout event by setting the
9276 * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
9277 * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
9278 * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
9279 * posted event WORKER_FABRIC_BLOCK_TMO.
9280 **/
92d7f7b0 9281void
f22eb4d3 9282lpfc_fabric_block_timeout(struct timer_list *t)
92d7f7b0 9283{
f22eb4d3 9284 struct lpfc_hba *phba = from_timer(phba, t, fabric_block_timer);
92d7f7b0
JS
9285 unsigned long iflags;
9286 uint32_t tmo_posted;
5e9d9b82 9287
92d7f7b0
JS
9288 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9289 tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
9290 if (!tmo_posted)
9291 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
9292 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9293
5e9d9b82
JS
9294 if (!tmo_posted)
9295 lpfc_worker_wake_up(phba);
9296 return;
92d7f7b0
JS
9297}
9298
e59058c4 9299/**
3621a710 9300 * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
e59058c4
JS
9301 * @phba: pointer to lpfc hba data structure.
9302 *
9303 * This routine issues one fabric iocb from the driver internal list to
9304 * the HBA. It first checks whether it's ready to issue one fabric iocb to
9305 * the HBA (whether there is no outstanding fabric iocb). If so, it shall
9306 * remove one pending fabric iocb from the driver internal list and invokes
9307 * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
9308 **/
92d7f7b0
JS
9309static void
9310lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
9311{
9312 struct lpfc_iocbq *iocb;
9313 unsigned long iflags;
9314 int ret;
92d7f7b0
JS
9315 IOCB_t *cmd;
9316
9317repeat:
9318 iocb = NULL;
9319 spin_lock_irqsave(&phba->hbalock, iflags);
7f5f3d0d 9320 /* Post any pending iocb to the SLI layer */
92d7f7b0
JS
9321 if (atomic_read(&phba->fabric_iocb_count) == 0) {
9322 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
9323 list);
9324 if (iocb)
7f5f3d0d 9325 /* Increment fabric iocb count to hold the position */
92d7f7b0
JS
9326 atomic_inc(&phba->fabric_iocb_count);
9327 }
9328 spin_unlock_irqrestore(&phba->hbalock, iflags);
9329 if (iocb) {
9330 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9331 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9332 iocb->iocb_flag |= LPFC_IO_FABRIC;
9333
858c9f6c
JS
9334 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9335 "Fabric sched1: ste:x%x",
9336 iocb->vport->port_state, 0, 0);
9337
3772a991 9338 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
92d7f7b0
JS
9339
9340 if (ret == IOCB_ERROR) {
9341 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9342 iocb->fabric_iocb_cmpl = NULL;
9343 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9344 cmd = &iocb->iocb;
9345 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
9346 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
9347 iocb->iocb_cmpl(phba, iocb, iocb);
9348
9349 atomic_dec(&phba->fabric_iocb_count);
9350 goto repeat;
9351 }
9352 }
9353
9354 return;
9355}
9356
e59058c4 9357/**
3621a710 9358 * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
e59058c4
JS
9359 * @phba: pointer to lpfc hba data structure.
9360 *
9361 * This routine unblocks the issuing fabric iocb command. The function
9362 * will clear the fabric iocb block bit and then invoke the routine
9363 * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
9364 * from the driver internal fabric iocb list.
9365 **/
92d7f7b0
JS
9366void
9367lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
9368{
9369 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9370
9371 lpfc_resume_fabric_iocbs(phba);
9372 return;
9373}
9374
e59058c4 9375/**
3621a710 9376 * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
e59058c4
JS
9377 * @phba: pointer to lpfc hba data structure.
9378 *
9379 * This routine blocks the issuing fabric iocb for a specified amount of
9380 * time (currently 100 ms). This is done by set the fabric iocb block bit
9381 * and set up a timeout timer for 100ms. When the block bit is set, no more
9382 * fabric iocb will be issued out of the HBA.
9383 **/
92d7f7b0
JS
9384static void
9385lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
9386{
9387 int blocked;
9388
9389 blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7f5f3d0d 9390 /* Start a timer to unblock fabric iocbs after 100ms */
92d7f7b0 9391 if (!blocked)
256ec0d0
JS
9392 mod_timer(&phba->fabric_block_timer,
9393 jiffies + msecs_to_jiffies(100));
92d7f7b0
JS
9394
9395 return;
9396}
9397
e59058c4 9398/**
3621a710 9399 * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
e59058c4
JS
9400 * @phba: pointer to lpfc hba data structure.
9401 * @cmdiocb: pointer to lpfc command iocb data structure.
9402 * @rspiocb: pointer to lpfc response iocb data structure.
9403 *
9404 * This routine is the callback function that is put to the fabric iocb's
9405 * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
9406 * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
9407 * function first restores and invokes the original iocb's callback function
9408 * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
9409 * fabric bound iocb from the driver internal fabric iocb list onto the wire.
9410 **/
92d7f7b0
JS
9411static void
9412lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9413 struct lpfc_iocbq *rspiocb)
9414{
9415 struct ls_rjt stat;
9416
e01ea5e2 9417 BUG_ON((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
92d7f7b0
JS
9418
9419 switch (rspiocb->iocb.ulpStatus) {
9420 case IOSTAT_NPORT_RJT:
9421 case IOSTAT_FABRIC_RJT:
9422 if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
9423 lpfc_block_fabric_iocbs(phba);
ed957684 9424 }
92d7f7b0
JS
9425 break;
9426
9427 case IOSTAT_NPORT_BSY:
9428 case IOSTAT_FABRIC_BSY:
9429 lpfc_block_fabric_iocbs(phba);
9430 break;
9431
9432 case IOSTAT_LS_RJT:
9433 stat.un.lsRjtError =
9434 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
9435 if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
9436 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
9437 lpfc_block_fabric_iocbs(phba);
9438 break;
9439 }
9440
a0f2d3ef 9441 BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
92d7f7b0
JS
9442
9443 cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
9444 cmdiocb->fabric_iocb_cmpl = NULL;
9445 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
9446 cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
9447
9448 atomic_dec(&phba->fabric_iocb_count);
9449 if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
7f5f3d0d
JS
9450 /* Post any pending iocbs to HBA */
9451 lpfc_resume_fabric_iocbs(phba);
92d7f7b0
JS
9452 }
9453}
9454
e59058c4 9455/**
3621a710 9456 * lpfc_issue_fabric_iocb - Issue a fabric iocb command
e59058c4
JS
9457 * @phba: pointer to lpfc hba data structure.
9458 * @iocb: pointer to lpfc command iocb data structure.
9459 *
9460 * This routine is used as the top-level API for issuing a fabric iocb command
9461 * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
9462 * function makes sure that only one fabric bound iocb will be outstanding at
9463 * any given time. As such, this function will first check to see whether there
9464 * is already an outstanding fabric iocb on the wire. If so, it will put the
9465 * newly issued iocb onto the driver internal fabric iocb list, waiting to be
9466 * issued later. Otherwise, it will issue the iocb on the wire and update the
9467 * fabric iocb count it indicate that there is one fabric iocb on the wire.
9468 *
9469 * Note, this implementation has a potential sending out fabric IOCBs out of
9470 * order. The problem is caused by the construction of the "ready" boolen does
9471 * not include the condition that the internal fabric IOCB list is empty. As
9472 * such, it is possible a fabric IOCB issued by this routine might be "jump"
9473 * ahead of the fabric IOCBs in the internal list.
9474 *
9475 * Return code
9476 * IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
9477 * IOCB_ERROR - failed to issue fabric iocb
9478 **/
a6ababd2 9479static int
92d7f7b0
JS
9480lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
9481{
9482 unsigned long iflags;
92d7f7b0
JS
9483 int ready;
9484 int ret;
9485
a0f2d3ef 9486 BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
92d7f7b0
JS
9487
9488 spin_lock_irqsave(&phba->hbalock, iflags);
9489 ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
9490 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9491
7f5f3d0d
JS
9492 if (ready)
9493 /* Increment fabric iocb count to hold the position */
9494 atomic_inc(&phba->fabric_iocb_count);
92d7f7b0
JS
9495 spin_unlock_irqrestore(&phba->hbalock, iflags);
9496 if (ready) {
9497 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9498 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9499 iocb->iocb_flag |= LPFC_IO_FABRIC;
9500
858c9f6c
JS
9501 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9502 "Fabric sched2: ste:x%x",
9503 iocb->vport->port_state, 0, 0);
9504
3772a991 9505 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
92d7f7b0
JS
9506
9507 if (ret == IOCB_ERROR) {
9508 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9509 iocb->fabric_iocb_cmpl = NULL;
9510 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9511 atomic_dec(&phba->fabric_iocb_count);
9512 }
9513 } else {
9514 spin_lock_irqsave(&phba->hbalock, iflags);
9515 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
9516 spin_unlock_irqrestore(&phba->hbalock, iflags);
9517 ret = IOCB_SUCCESS;
9518 }
9519 return ret;
9520}
9521
e59058c4 9522/**
3621a710 9523 * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
e59058c4
JS
9524 * @vport: pointer to a virtual N_Port data structure.
9525 *
9526 * This routine aborts all the IOCBs associated with a @vport from the
9527 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9528 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9529 * list, removes each IOCB associated with the @vport off the list, set the
9530 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9531 * associated with the IOCB.
9532 **/
a6ababd2 9533static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
92d7f7b0
JS
9534{
9535 LIST_HEAD(completions);
9536 struct lpfc_hba *phba = vport->phba;
9537 struct lpfc_iocbq *tmp_iocb, *piocb;
92d7f7b0
JS
9538
9539 spin_lock_irq(&phba->hbalock);
9540 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9541 list) {
9542
9543 if (piocb->vport != vport)
9544 continue;
9545
9546 list_move_tail(&piocb->list, &completions);
9547 }
9548 spin_unlock_irq(&phba->hbalock);
9549
a257bf90
JS
9550 /* Cancel all the IOCBs from the completions list */
9551 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9552 IOERR_SLI_ABORTED);
92d7f7b0
JS
9553}
9554
e59058c4 9555/**
3621a710 9556 * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
e59058c4
JS
9557 * @ndlp: pointer to a node-list data structure.
9558 *
9559 * This routine aborts all the IOCBs associated with an @ndlp from the
9560 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9561 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9562 * list, removes each IOCB associated with the @ndlp off the list, set the
9563 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9564 * associated with the IOCB.
9565 **/
92d7f7b0
JS
9566void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
9567{
9568 LIST_HEAD(completions);
a257bf90 9569 struct lpfc_hba *phba = ndlp->phba;
92d7f7b0 9570 struct lpfc_iocbq *tmp_iocb, *piocb;
895427bd
JS
9571 struct lpfc_sli_ring *pring;
9572
9573 pring = lpfc_phba_elsring(phba);
92d7f7b0 9574
1234a6d5
DK
9575 if (unlikely(!pring))
9576 return;
9577
92d7f7b0
JS
9578 spin_lock_irq(&phba->hbalock);
9579 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9580 list) {
9581 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
9582
9583 list_move_tail(&piocb->list, &completions);
ed957684 9584 }
dea3101e 9585 }
92d7f7b0
JS
9586 spin_unlock_irq(&phba->hbalock);
9587
a257bf90
JS
9588 /* Cancel all the IOCBs from the completions list */
9589 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9590 IOERR_SLI_ABORTED);
92d7f7b0
JS
9591}
9592
e59058c4 9593/**
3621a710 9594 * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
e59058c4
JS
9595 * @phba: pointer to lpfc hba data structure.
9596 *
9597 * This routine aborts all the IOCBs currently on the driver internal
9598 * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
9599 * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
9600 * list, removes IOCBs off the list, set the status feild to
9601 * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
9602 * the IOCB.
9603 **/
92d7f7b0
JS
9604void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
9605{
9606 LIST_HEAD(completions);
92d7f7b0
JS
9607
9608 spin_lock_irq(&phba->hbalock);
9609 list_splice_init(&phba->fabric_iocb_list, &completions);
9610 spin_unlock_irq(&phba->hbalock);
9611
a257bf90
JS
9612 /* Cancel all the IOCBs from the completions list */
9613 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9614 IOERR_SLI_ABORTED);
dea3101e 9615}
6fb120a7 9616
1151e3ec
JS
9617/**
9618 * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
9619 * @vport: pointer to lpfc vport data structure.
9620 *
9621 * This routine is invoked by the vport cleanup for deletions and the cleanup
9622 * for an ndlp on removal.
9623 **/
9624void
9625lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
9626{
9627 struct lpfc_hba *phba = vport->phba;
9628 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
9629 unsigned long iflag = 0;
9630
9631 spin_lock_irqsave(&phba->hbalock, iflag);
895427bd 9632 spin_lock(&phba->sli4_hba.sgl_list_lock);
1151e3ec
JS
9633 list_for_each_entry_safe(sglq_entry, sglq_next,
9634 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
9635 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
9636 sglq_entry->ndlp = NULL;
9637 }
895427bd 9638 spin_unlock(&phba->sli4_hba.sgl_list_lock);
1151e3ec
JS
9639 spin_unlock_irqrestore(&phba->hbalock, iflag);
9640 return;
9641}
9642
6fb120a7
JS
9643/**
9644 * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
9645 * @phba: pointer to lpfc hba data structure.
9646 * @axri: pointer to the els xri abort wcqe structure.
9647 *
9648 * This routine is invoked by the worker thread to process a SLI4 slow-path
9649 * ELS aborted xri.
9650 **/
9651void
9652lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
9653 struct sli4_wcqe_xri_aborted *axri)
9654{
9655 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
19ca7609 9656 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
7851fe2c 9657 uint16_t lxri = 0;
19ca7609 9658
6fb120a7
JS
9659 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
9660 unsigned long iflag = 0;
19ca7609 9661 struct lpfc_nodelist *ndlp;
895427bd
JS
9662 struct lpfc_sli_ring *pring;
9663
9664 pring = lpfc_phba_elsring(phba);
6fb120a7 9665
0f65ff68 9666 spin_lock_irqsave(&phba->hbalock, iflag);
895427bd 9667 spin_lock(&phba->sli4_hba.sgl_list_lock);
6fb120a7
JS
9668 list_for_each_entry_safe(sglq_entry, sglq_next,
9669 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
9670 if (sglq_entry->sli4_xritag == xri) {
9671 list_del(&sglq_entry->list);
19ca7609
JS
9672 ndlp = sglq_entry->ndlp;
9673 sglq_entry->ndlp = NULL;
6fb120a7 9674 list_add_tail(&sglq_entry->list,
895427bd 9675 &phba->sli4_hba.lpfc_els_sgl_list);
0f65ff68 9676 sglq_entry->state = SGL_FREED;
895427bd 9677 spin_unlock(&phba->sli4_hba.sgl_list_lock);
6fb120a7 9678 spin_unlock_irqrestore(&phba->hbalock, iflag);
ee0f4fe1
JS
9679 lpfc_set_rrq_active(phba, ndlp,
9680 sglq_entry->sli4_lxritag,
9681 rxid, 1);
589a52d6
JS
9682
9683 /* Check if TXQ queue needs to be serviced */
1234a6d5 9684 if (pring && !list_empty(&pring->txq))
589a52d6 9685 lpfc_worker_wake_up(phba);
6fb120a7
JS
9686 return;
9687 }
9688 }
895427bd 9689 spin_unlock(&phba->sli4_hba.sgl_list_lock);
7851fe2c
JS
9690 lxri = lpfc_sli4_xri_inrange(phba, xri);
9691 if (lxri == NO_XRI) {
9692 spin_unlock_irqrestore(&phba->hbalock, iflag);
9693 return;
9694 }
895427bd 9695 spin_lock(&phba->sli4_hba.sgl_list_lock);
7851fe2c 9696 sglq_entry = __lpfc_get_active_sglq(phba, lxri);
0f65ff68 9697 if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
895427bd 9698 spin_unlock(&phba->sli4_hba.sgl_list_lock);
0f65ff68
JS
9699 spin_unlock_irqrestore(&phba->hbalock, iflag);
9700 return;
9701 }
9702 sglq_entry->state = SGL_XRI_ABORTED;
895427bd 9703 spin_unlock(&phba->sli4_hba.sgl_list_lock);
0f65ff68
JS
9704 spin_unlock_irqrestore(&phba->hbalock, iflag);
9705 return;
6fb120a7 9706}
086a345f
JS
9707
9708/* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
9709 * @vport: pointer to virtual port object.
9710 * @ndlp: nodelist pointer for the impacted node.
9711 *
9712 * The driver calls this routine in response to an SLI4 XRI ABORT CQE
9713 * or an SLI3 ASYNC_STATUS_CN event from the port. For either event,
9714 * the driver is required to send a LOGO to the remote node before it
9715 * attempts to recover its login to the remote node.
9716 */
9717void
9718lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
9719 struct lpfc_nodelist *ndlp)
9720{
9721 struct Scsi_Host *shost;
9722 struct lpfc_hba *phba;
9723 unsigned long flags = 0;
9724
9725 shost = lpfc_shost_from_vport(vport);
9726 phba = vport->phba;
9727 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
9728 lpfc_printf_log(phba, KERN_INFO,
9729 LOG_SLI, "3093 No rport recovery needed. "
9730 "rport in state 0x%x\n", ndlp->nlp_state);
9731 return;
9732 }
30e196ca
JS
9733 lpfc_printf_log(phba, KERN_ERR,
9734 LOG_ELS | LOG_FCP_ERROR | LOG_NVME_IOERR,
086a345f
JS
9735 "3094 Start rport recovery on shost id 0x%x "
9736 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
9737 "flags 0x%x\n",
9738 shost->host_no, ndlp->nlp_DID,
9739 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
9740 ndlp->nlp_flag);
9741 /*
9742 * The rport is not responding. Remove the FCP-2 flag to prevent
9743 * an ADISC in the follow-up recovery code.
9744 */
9745 spin_lock_irqsave(shost->host_lock, flags);
9746 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
30e196ca 9747 ndlp->nlp_flag |= NLP_ISSUE_LOGO;
086a345f 9748 spin_unlock_irqrestore(shost->host_lock, flags);
30e196ca 9749 lpfc_unreg_rpi(vport, ndlp);
086a345f
JS
9750}
9751