]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/lpfc/lpfc_els.c
[SCSI] be2iscsi: Add pci_disable device
[mirror_ubuntu-artful-kernel.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. *
92494144 4 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e
JB
8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e 20 *******************************************************************/
09372820 21/* See Fibre Channel protocol T11 FC-LS for details */
dea3101e
JB
22#include <linux/blkdev.h>
23#include <linux/pci.h>
5a0e3ad6 24#include <linux/slab.h>
dea3101e
JB
25#include <linux/interrupt.h>
26
91886523 27#include <scsi/scsi.h>
dea3101e
JB
28#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
da0436e9 32#include "lpfc_hw4.h"
dea3101e
JB
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
da0436e9 35#include "lpfc_sli4.h"
ea2151b4 36#include "lpfc_nl.h"
dea3101e
JB
37#include "lpfc_disc.h"
38#include "lpfc_scsi.h"
39#include "lpfc.h"
40#include "lpfc_logmsg.h"
41#include "lpfc_crtn.h"
92d7f7b0 42#include "lpfc_vport.h"
858c9f6c 43#include "lpfc_debugfs.h"
dea3101e
JB
44
45static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
46 struct lpfc_iocbq *);
92d7f7b0
JS
47static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
48 struct lpfc_iocbq *);
a6ababd2
AB
49static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
50static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
51 struct lpfc_nodelist *ndlp, uint8_t retry);
52static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
53 struct lpfc_iocbq *iocb);
92d7f7b0 54
dea3101e
JB
55static int lpfc_max_els_tries = 3;
56
e59058c4 57/**
3621a710 58 * lpfc_els_chk_latt - Check host link attention event for a vport
e59058c4
JS
59 * @vport: pointer to a host virtual N_Port data structure.
60 *
61 * This routine checks whether there is an outstanding host link
62 * attention event during the discovery process with the @vport. It is done
63 * by reading the HBA's Host Attention (HA) register. If there is any host
64 * link attention events during this @vport's discovery process, the @vport
65 * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
66 * be issued if the link state is not already in host link cleared state,
67 * and a return code shall indicate whether the host link attention event
68 * had happened.
69 *
70 * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
71 * state in LPFC_VPORT_READY, the request for checking host link attention
72 * event will be ignored and a return code shall indicate no host link
73 * attention event had happened.
74 *
75 * Return codes
76 * 0 - no host link attention event happened
77 * 1 - host link attention event happened
78 **/
858c9f6c 79int
2e0fef85 80lpfc_els_chk_latt(struct lpfc_vport *vport)
dea3101e 81{
2e0fef85
JS
82 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
83 struct lpfc_hba *phba = vport->phba;
dea3101e 84 uint32_t ha_copy;
dea3101e 85
2e0fef85 86 if (vport->port_state >= LPFC_VPORT_READY ||
3772a991
JS
87 phba->link_state == LPFC_LINK_DOWN ||
88 phba->sli_rev > LPFC_SLI_REV3)
dea3101e
JB
89 return 0;
90
91 /* Read the HBA Host Attention Register */
9940b97b
JS
92 if (lpfc_readl(phba->HAregaddr, &ha_copy))
93 return 1;
dea3101e
JB
94
95 if (!(ha_copy & HA_LATT))
96 return 0;
97
98 /* Pending Link Event during Discovery */
e8b62011
JS
99 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
100 "0237 Pending Link Event during "
101 "Discovery: State x%x\n",
102 phba->pport->port_state);
dea3101e
JB
103
104 /* CLEAR_LA should re-enable link attention events and
25985edc 105 * we should then immediately take a LATT event. The
dea3101e
JB
106 * LATT processing should call lpfc_linkdown() which
107 * will cleanup any left over in-progress discovery
108 * events.
109 */
2e0fef85
JS
110 spin_lock_irq(shost->host_lock);
111 vport->fc_flag |= FC_ABORT_DISCOVERY;
112 spin_unlock_irq(shost->host_lock);
dea3101e 113
92d7f7b0 114 if (phba->link_state != LPFC_CLEAR_LA)
ed957684 115 lpfc_issue_clear_la(phba, vport);
dea3101e 116
c9f8735b 117 return 1;
dea3101e
JB
118}
119
e59058c4 120/**
3621a710 121 * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
e59058c4
JS
122 * @vport: pointer to a host virtual N_Port data structure.
123 * @expectRsp: flag indicating whether response is expected.
124 * @cmdSize: size of the ELS command.
125 * @retry: number of retries to the command IOCB when it fails.
126 * @ndlp: pointer to a node-list data structure.
127 * @did: destination identifier.
128 * @elscmd: the ELS command code.
129 *
130 * This routine is used for allocating a lpfc-IOCB data structure from
131 * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
132 * passed into the routine for discovery state machine to issue an Extended
133 * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
134 * and preparation routine that is used by all the discovery state machine
135 * routines and the ELS command-specific fields will be later set up by
136 * the individual discovery machine routines after calling this routine
137 * allocating and preparing a generic IOCB data structure. It fills in the
138 * Buffer Descriptor Entries (BDEs), allocates buffers for both command
139 * payload and response payload (if expected). The reference count on the
140 * ndlp is incremented by 1 and the reference to the ndlp is put into
141 * context1 of the IOCB data structure for this IOCB to hold the ndlp
142 * reference for the command's callback function to access later.
143 *
144 * Return code
145 * Pointer to the newly allocated/prepared els iocb data structure
146 * NULL - when els iocb data structure allocation/preparation failed
147 **/
f1c3b0fc 148struct lpfc_iocbq *
2e0fef85
JS
149lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
150 uint16_t cmdSize, uint8_t retry,
151 struct lpfc_nodelist *ndlp, uint32_t did,
152 uint32_t elscmd)
dea3101e 153{
2e0fef85 154 struct lpfc_hba *phba = vport->phba;
0bd4ca25 155 struct lpfc_iocbq *elsiocb;
dea3101e
JB
156 struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
157 struct ulp_bde64 *bpl;
158 IOCB_t *icmd;
159
dea3101e 160
2e0fef85
JS
161 if (!lpfc_is_link_up(phba))
162 return NULL;
dea3101e 163
dea3101e 164 /* Allocate buffer for command iocb */
0bd4ca25 165 elsiocb = lpfc_sli_get_iocbq(phba);
dea3101e
JB
166
167 if (elsiocb == NULL)
168 return NULL;
e47c9093 169
0c287589
JS
170 /*
171 * If this command is for fabric controller and HBA running
172 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
173 */
174 if ((did == Fabric_DID) &&
45ed1190 175 (phba->hba_flag & HBA_FIP_SUPPORT) &&
0c287589
JS
176 ((elscmd == ELS_CMD_FLOGI) ||
177 (elscmd == ELS_CMD_FDISC) ||
178 (elscmd == ELS_CMD_LOGO)))
c868595d
JS
179 switch (elscmd) {
180 case ELS_CMD_FLOGI:
f0d9bccc
JS
181 elsiocb->iocb_flag |=
182 ((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
c868595d
JS
183 & LPFC_FIP_ELS_ID_MASK);
184 break;
185 case ELS_CMD_FDISC:
f0d9bccc
JS
186 elsiocb->iocb_flag |=
187 ((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
c868595d
JS
188 & LPFC_FIP_ELS_ID_MASK);
189 break;
190 case ELS_CMD_LOGO:
f0d9bccc
JS
191 elsiocb->iocb_flag |=
192 ((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
c868595d
JS
193 & LPFC_FIP_ELS_ID_MASK);
194 break;
195 }
0c287589 196 else
c868595d 197 elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
0c287589 198
dea3101e
JB
199 icmd = &elsiocb->iocb;
200
201 /* fill in BDEs for command */
202 /* Allocate buffer for command payload */
98c9ea5c
JS
203 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
204 if (pcmd)
205 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
fa4066b6
JS
206 if (!pcmd || !pcmd->virt)
207 goto els_iocb_free_pcmb_exit;
dea3101e
JB
208
209 INIT_LIST_HEAD(&pcmd->list);
210
211 /* Allocate buffer for response payload */
212 if (expectRsp) {
92d7f7b0 213 prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
dea3101e
JB
214 if (prsp)
215 prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
216 &prsp->phys);
fa4066b6
JS
217 if (!prsp || !prsp->virt)
218 goto els_iocb_free_prsp_exit;
dea3101e 219 INIT_LIST_HEAD(&prsp->list);
e47c9093 220 } else
dea3101e 221 prsp = NULL;
dea3101e
JB
222
223 /* Allocate buffer for Buffer ptr list */
92d7f7b0 224 pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
dea3101e 225 if (pbuflist)
ed957684
JS
226 pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
227 &pbuflist->phys);
fa4066b6
JS
228 if (!pbuflist || !pbuflist->virt)
229 goto els_iocb_free_pbuf_exit;
dea3101e
JB
230
231 INIT_LIST_HEAD(&pbuflist->list);
232
233 icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
234 icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
34b02dcd 235 icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2e0fef85 236 icmd->un.elsreq64.remoteID = did; /* DID */
dea3101e 237 if (expectRsp) {
92d7f7b0 238 icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
dea3101e 239 icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
2680eeaa 240 icmd->ulpTimeout = phba->fc_ratov * 2;
dea3101e 241 } else {
92d7f7b0 242 icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
dea3101e
JB
243 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
244 }
dea3101e
JB
245 icmd->ulpBdeCount = 1;
246 icmd->ulpLe = 1;
247 icmd->ulpClass = CLASS3;
248
92d7f7b0
JS
249 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
250 icmd->un.elsreq64.myID = vport->fc_myDID;
251
252 /* For ELS_REQUEST64_CR, use the VPI by default */
6d368e53 253 icmd->ulpContext = phba->vpi_ids[vport->vpi];
92d7f7b0 254 icmd->ulpCt_h = 0;
eada272d
JS
255 /* The CT field must be 0=INVALID_RPI for the ECHO cmd */
256 if (elscmd == ELS_CMD_ECHO)
257 icmd->ulpCt_l = 0; /* context = invalid RPI */
258 else
259 icmd->ulpCt_l = 1; /* context = VPI */
92d7f7b0
JS
260 }
261
dea3101e
JB
262 bpl = (struct ulp_bde64 *) pbuflist->virt;
263 bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
264 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
265 bpl->tus.f.bdeSize = cmdSize;
266 bpl->tus.f.bdeFlags = 0;
267 bpl->tus.w = le32_to_cpu(bpl->tus.w);
268
269 if (expectRsp) {
270 bpl++;
271 bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
272 bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
273 bpl->tus.f.bdeSize = FCELSSIZE;
34b02dcd 274 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
dea3101e
JB
275 bpl->tus.w = le32_to_cpu(bpl->tus.w);
276 }
277
fa4066b6 278 /* prevent preparing iocb with NULL ndlp reference */
51ef4c26 279 elsiocb->context1 = lpfc_nlp_get(ndlp);
fa4066b6
JS
280 if (!elsiocb->context1)
281 goto els_iocb_free_pbuf_exit;
329f9bc7
JS
282 elsiocb->context2 = pcmd;
283 elsiocb->context3 = pbuflist;
dea3101e 284 elsiocb->retry = retry;
2e0fef85 285 elsiocb->vport = vport;
dea3101e
JB
286 elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
287
288 if (prsp) {
289 list_add(&prsp->list, &pcmd->list);
290 }
dea3101e
JB
291 if (expectRsp) {
292 /* Xmit ELS command <elsCmd> to remote NPORT <did> */
e8b62011
JS
293 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
294 "0116 Xmit ELS command x%x to remote "
295 "NPORT x%x I/O tag: x%x, port state: x%x\n",
296 elscmd, did, elsiocb->iotag,
297 vport->port_state);
dea3101e
JB
298 } else {
299 /* Xmit ELS response <elsCmd> to remote NPORT <did> */
e8b62011
JS
300 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
301 "0117 Xmit ELS response x%x to remote "
302 "NPORT x%x I/O tag: x%x, size: x%x\n",
303 elscmd, ndlp->nlp_DID, elsiocb->iotag,
304 cmdSize);
dea3101e 305 }
c9f8735b 306 return elsiocb;
dea3101e 307
fa4066b6 308els_iocb_free_pbuf_exit:
eaf15d5b
JS
309 if (expectRsp)
310 lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
fa4066b6
JS
311 kfree(pbuflist);
312
313els_iocb_free_prsp_exit:
314 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
315 kfree(prsp);
316
317els_iocb_free_pcmb_exit:
318 kfree(pcmd);
319 lpfc_sli_release_iocbq(phba, elsiocb);
320 return NULL;
321}
dea3101e 322
e59058c4 323/**
3621a710 324 * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
e59058c4
JS
325 * @vport: pointer to a host virtual N_Port data structure.
326 *
327 * This routine issues a fabric registration login for a @vport. An
328 * active ndlp node with Fabric_DID must already exist for this @vport.
329 * The routine invokes two mailbox commands to carry out fabric registration
330 * login through the HBA firmware: the first mailbox command requests the
331 * HBA to perform link configuration for the @vport; and the second mailbox
332 * command requests the HBA to perform the actual fabric registration login
333 * with the @vport.
334 *
335 * Return code
336 * 0 - successfully issued fabric registration login for @vport
337 * -ENXIO -- failed to issue fabric registration login for @vport
338 **/
3772a991 339int
92d7f7b0 340lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
dea3101e 341{
2e0fef85 342 struct lpfc_hba *phba = vport->phba;
dea3101e 343 LPFC_MBOXQ_t *mbox;
14691150 344 struct lpfc_dmabuf *mp;
92d7f7b0
JS
345 struct lpfc_nodelist *ndlp;
346 struct serv_parm *sp;
dea3101e 347 int rc;
98c9ea5c 348 int err = 0;
dea3101e 349
92d7f7b0
JS
350 sp = &phba->fc_fabparam;
351 ndlp = lpfc_findnode_did(vport, Fabric_DID);
e47c9093 352 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
98c9ea5c 353 err = 1;
92d7f7b0 354 goto fail;
98c9ea5c 355 }
92d7f7b0
JS
356
357 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
98c9ea5c
JS
358 if (!mbox) {
359 err = 2;
92d7f7b0 360 goto fail;
98c9ea5c 361 }
92d7f7b0
JS
362
363 vport->port_state = LPFC_FABRIC_CFG_LINK;
364 lpfc_config_link(phba, mbox);
365 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
366 mbox->vport = vport;
367
0b727fea 368 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
98c9ea5c
JS
369 if (rc == MBX_NOT_FINISHED) {
370 err = 3;
92d7f7b0 371 goto fail_free_mbox;
98c9ea5c 372 }
92d7f7b0
JS
373
374 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
98c9ea5c
JS
375 if (!mbox) {
376 err = 4;
92d7f7b0 377 goto fail;
98c9ea5c 378 }
4042629e
JS
379 rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
380 ndlp->nlp_rpi);
98c9ea5c
JS
381 if (rc) {
382 err = 5;
92d7f7b0 383 goto fail_free_mbox;
98c9ea5c 384 }
92d7f7b0
JS
385
386 mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
387 mbox->vport = vport;
e47c9093
JS
388 /* increment the reference count on ndlp to hold reference
389 * for the callback routine.
390 */
92d7f7b0
JS
391 mbox->context2 = lpfc_nlp_get(ndlp);
392
0b727fea 393 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
98c9ea5c
JS
394 if (rc == MBX_NOT_FINISHED) {
395 err = 6;
92d7f7b0 396 goto fail_issue_reg_login;
98c9ea5c 397 }
92d7f7b0
JS
398
399 return 0;
400
401fail_issue_reg_login:
e47c9093
JS
402 /* decrement the reference count on ndlp just incremented
403 * for the failed mbox command.
404 */
92d7f7b0
JS
405 lpfc_nlp_put(ndlp);
406 mp = (struct lpfc_dmabuf *) mbox->context1;
407 lpfc_mbuf_free(phba, mp->virt, mp->phys);
408 kfree(mp);
409fail_free_mbox:
410 mempool_free(mbox, phba->mbox_mem_pool);
411
412fail:
413 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011 414 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
98c9ea5c 415 "0249 Cannot issue Register Fabric login: Err %d\n", err);
92d7f7b0
JS
416 return -ENXIO;
417}
418
6fb120a7
JS
419/**
420 * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
421 * @vport: pointer to a host virtual N_Port data structure.
422 *
423 * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
424 * the @vport. This mailbox command is necessary for FCoE only.
425 *
426 * Return code
427 * 0 - successfully issued REG_VFI for @vport
428 * A failure code otherwise.
429 **/
430static int
431lpfc_issue_reg_vfi(struct lpfc_vport *vport)
432{
433 struct lpfc_hba *phba = vport->phba;
434 LPFC_MBOXQ_t *mboxq;
435 struct lpfc_nodelist *ndlp;
436 struct serv_parm *sp;
437 struct lpfc_dmabuf *dmabuf;
438 int rc = 0;
439
440 sp = &phba->fc_fabparam;
441 ndlp = lpfc_findnode_did(vport, Fabric_DID);
442 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
443 rc = -ENODEV;
444 goto fail;
445 }
446
447 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
448 if (!dmabuf) {
449 rc = -ENOMEM;
450 goto fail;
451 }
452 dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
453 if (!dmabuf->virt) {
454 rc = -ENOMEM;
455 goto fail_free_dmabuf;
456 }
6d368e53 457
6fb120a7
JS
458 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
459 if (!mboxq) {
460 rc = -ENOMEM;
461 goto fail_free_coherent;
462 }
463 vport->port_state = LPFC_FABRIC_CFG_LINK;
464 memcpy(dmabuf->virt, &phba->fc_fabparam, sizeof(vport->fc_sparam));
465 lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
466 mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
467 mboxq->vport = vport;
468 mboxq->context1 = dmabuf;
469 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
470 if (rc == MBX_NOT_FINISHED) {
471 rc = -ENXIO;
472 goto fail_free_mbox;
473 }
474 return 0;
475
476fail_free_mbox:
477 mempool_free(mboxq, phba->mbox_mem_pool);
478fail_free_coherent:
479 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
480fail_free_dmabuf:
481 kfree(dmabuf);
482fail:
483 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
484 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
485 "0289 Issue Register VFI failed: Err %d\n", rc);
486 return rc;
487}
488
92494144
JS
489/**
490 * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
491 * @vport: pointer to a host virtual N_Port data structure.
492 * @sp: pointer to service parameter data structure.
493 *
494 * This routine is called from FLOGI/FDISC completion handler functions.
495 * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
496 * node nodename is changed in the completion service parameter else return
497 * 0. This function also set flag in the vport data structure to delay
498 * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
499 * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
500 * node nodename is changed in the completion service parameter.
501 *
502 * Return code
503 * 0 - FCID and Fabric Nodename and Fabric portname is not changed.
504 * 1 - FCID or Fabric Nodename or Fabric portname is changed.
505 *
506 **/
507static uint8_t
508lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
509 struct serv_parm *sp)
510{
511 uint8_t fabric_param_changed = 0;
512 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
513
514 if ((vport->fc_prevDID != vport->fc_myDID) ||
515 memcmp(&vport->fabric_portname, &sp->portName,
516 sizeof(struct lpfc_name)) ||
517 memcmp(&vport->fabric_nodename, &sp->nodeName,
518 sizeof(struct lpfc_name)))
519 fabric_param_changed = 1;
520
521 /*
522 * Word 1 Bit 31 in common service parameter is overloaded.
523 * Word 1 Bit 31 in FLOGI request is multiple NPort request
524 * Word 1 Bit 31 in FLOGI response is clean address bit
525 *
526 * If fabric parameter is changed and clean address bit is
527 * cleared delay nport discovery if
528 * - vport->fc_prevDID != 0 (not initial discovery) OR
529 * - lpfc_delay_discovery module parameter is set.
530 */
531 if (fabric_param_changed && !sp->cmn.clean_address_bit &&
532 (vport->fc_prevDID || lpfc_delay_discovery)) {
533 spin_lock_irq(shost->host_lock);
534 vport->fc_flag |= FC_DISC_DELAYED;
535 spin_unlock_irq(shost->host_lock);
536 }
537
538 return fabric_param_changed;
539}
540
541
e59058c4 542/**
3621a710 543 * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
e59058c4
JS
544 * @vport: pointer to a host virtual N_Port data structure.
545 * @ndlp: pointer to a node-list data structure.
546 * @sp: pointer to service parameter data structure.
547 * @irsp: pointer to the IOCB within the lpfc response IOCB.
548 *
549 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
550 * function to handle the completion of a Fabric Login (FLOGI) into a fabric
551 * port in a fabric topology. It properly sets up the parameters to the @ndlp
552 * from the IOCB response. It also check the newly assigned N_Port ID to the
553 * @vport against the previously assigned N_Port ID. If it is different from
554 * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
555 * is invoked on all the remaining nodes with the @vport to unregister the
556 * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
557 * is invoked to register login to the fabric.
558 *
559 * Return code
560 * 0 - Success (currently, always return 0)
561 **/
92d7f7b0
JS
562static int
563lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
564 struct serv_parm *sp, IOCB_t *irsp)
565{
566 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
567 struct lpfc_hba *phba = vport->phba;
568 struct lpfc_nodelist *np;
569 struct lpfc_nodelist *next_np;
92494144 570 uint8_t fabric_param_changed;
92d7f7b0 571
2e0fef85
JS
572 spin_lock_irq(shost->host_lock);
573 vport->fc_flag |= FC_FABRIC;
574 spin_unlock_irq(shost->host_lock);
dea3101e
JB
575
576 phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
577 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
578 phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
579
12265f68 580 phba->fc_edtovResol = sp->cmn.edtovResolution;
dea3101e
JB
581 phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
582
76a95d75 583 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
2e0fef85
JS
584 spin_lock_irq(shost->host_lock);
585 vport->fc_flag |= FC_PUBLIC_LOOP;
586 spin_unlock_irq(shost->host_lock);
dea3101e
JB
587 }
588
2e0fef85 589 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
dea3101e 590 memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
92d7f7b0 591 memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
dea3101e
JB
592 ndlp->nlp_class_sup = 0;
593 if (sp->cls1.classValid)
594 ndlp->nlp_class_sup |= FC_COS_CLASS1;
595 if (sp->cls2.classValid)
596 ndlp->nlp_class_sup |= FC_COS_CLASS2;
597 if (sp->cls3.classValid)
598 ndlp->nlp_class_sup |= FC_COS_CLASS3;
599 if (sp->cls4.classValid)
600 ndlp->nlp_class_sup |= FC_COS_CLASS4;
601 ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
602 sp->cmn.bbRcvSizeLsb;
92494144
JS
603
604 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
605 memcpy(&vport->fabric_portname, &sp->portName,
606 sizeof(struct lpfc_name));
607 memcpy(&vport->fabric_nodename, &sp->nodeName,
608 sizeof(struct lpfc_name));
dea3101e
JB
609 memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
610
92d7f7b0
JS
611 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
612 if (sp->cmn.response_multiple_NPort) {
e8b62011
JS
613 lpfc_printf_vlog(vport, KERN_WARNING,
614 LOG_ELS | LOG_VPORT,
615 "1816 FLOGI NPIV supported, "
616 "response data 0x%x\n",
617 sp->cmn.response_multiple_NPort);
92d7f7b0 618 phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
92d7f7b0
JS
619 } else {
620 /* Because we asked f/w for NPIV it still expects us
e8b62011
JS
621 to call reg_vnpid atleast for the physcial host */
622 lpfc_printf_vlog(vport, KERN_WARNING,
623 LOG_ELS | LOG_VPORT,
624 "1817 Fabric does not support NPIV "
625 "- configuring single port mode.\n");
92d7f7b0
JS
626 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
627 }
628 }
dea3101e 629
92494144 630 if (fabric_param_changed &&
92d7f7b0 631 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
dea3101e 632
92d7f7b0
JS
633 /* If our NportID changed, we need to ensure all
634 * remaining NPORTs get unreg_login'ed.
635 */
636 list_for_each_entry_safe(np, next_np,
637 &vport->fc_nodes, nlp_listp) {
d7c255b2 638 if (!NLP_CHK_NODE_ACT(np))
e47c9093 639 continue;
92d7f7b0
JS
640 if ((np->nlp_state != NLP_STE_NPR_NODE) ||
641 !(np->nlp_flag & NLP_NPR_ADISC))
642 continue;
643 spin_lock_irq(shost->host_lock);
644 np->nlp_flag &= ~NLP_NPR_ADISC;
645 spin_unlock_irq(shost->host_lock);
646 lpfc_unreg_rpi(vport, np);
647 }
78730cfe 648 lpfc_cleanup_pending_mbox(vport);
5af5eee7 649
5248a749 650 if (phba->sli_rev == LPFC_SLI_REV4) {
5af5eee7 651 lpfc_sli4_unreg_all_rpis(vport);
92d7f7b0 652 lpfc_mbx_unreg_vpi(vport);
09372820 653 spin_lock_irq(shost->host_lock);
92d7f7b0 654 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5248a749
JS
655 /*
656 * If VPI is unreged, driver need to do INIT_VPI
657 * before re-registering
658 */
ecfd03c6
JS
659 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
660 spin_unlock_irq(shost->host_lock);
661 }
38b92ef8
JS
662 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
663 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
664 /*
665 * Driver needs to re-reg VPI in order for f/w
666 * to update the MAC address.
667 */
9589b062 668 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
38b92ef8
JS
669 lpfc_register_new_vport(phba, vport, ndlp);
670 return 0;
92d7f7b0 671 }
dea3101e 672
6fb120a7
JS
673 if (phba->sli_rev < LPFC_SLI_REV4) {
674 lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
675 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
676 vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
677 lpfc_register_new_vport(phba, vport, ndlp);
678 else
679 lpfc_issue_fabric_reglogin(vport);
680 } else {
681 ndlp->nlp_type |= NLP_FABRIC;
682 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
695a814e
JS
683 if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
684 (vport->vpi_state & LPFC_VPI_REGISTERED)) {
6fb120a7
JS
685 lpfc_start_fdiscs(phba);
686 lpfc_do_scr_ns_plogi(phba, vport);
695a814e 687 } else if (vport->fc_flag & FC_VFI_REGISTERED)
ecfd03c6 688 lpfc_issue_init_vpi(vport);
695a814e 689 else
6fb120a7 690 lpfc_issue_reg_vfi(vport);
92d7f7b0 691 }
dea3101e 692 return 0;
dea3101e 693}
e59058c4 694/**
3621a710 695 * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
e59058c4
JS
696 * @vport: pointer to a host virtual N_Port data structure.
697 * @ndlp: pointer to a node-list data structure.
698 * @sp: pointer to service parameter data structure.
699 *
700 * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
701 * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
702 * in a point-to-point topology. First, the @vport's N_Port Name is compared
703 * with the received N_Port Name: if the @vport's N_Port Name is greater than
704 * the received N_Port Name lexicographically, this node shall assign local
705 * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
706 * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
707 * this node shall just wait for the remote node to issue PLOGI and assign
708 * N_Port IDs.
709 *
710 * Return code
711 * 0 - Success
712 * -ENXIO - Fail
713 **/
dea3101e 714static int
2e0fef85
JS
715lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
716 struct serv_parm *sp)
dea3101e 717{
2e0fef85
JS
718 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
719 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
720 LPFC_MBOXQ_t *mbox;
721 int rc;
722
2e0fef85
JS
723 spin_lock_irq(shost->host_lock);
724 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
725 spin_unlock_irq(shost->host_lock);
dea3101e
JB
726
727 phba->fc_edtov = FF_DEF_EDTOV;
728 phba->fc_ratov = FF_DEF_RATOV;
2e0fef85 729 rc = memcmp(&vport->fc_portname, &sp->portName,
92d7f7b0 730 sizeof(vport->fc_portname));
dea3101e
JB
731 if (rc >= 0) {
732 /* This side will initiate the PLOGI */
2e0fef85
JS
733 spin_lock_irq(shost->host_lock);
734 vport->fc_flag |= FC_PT2PT_PLOGI;
735 spin_unlock_irq(shost->host_lock);
dea3101e
JB
736
737 /*
738 * N_Port ID cannot be 0, set our to LocalID the other
739 * side will be RemoteID.
740 */
741
742 /* not equal */
743 if (rc)
2e0fef85 744 vport->fc_myDID = PT2PT_LocalID;
dea3101e
JB
745
746 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
747 if (!mbox)
748 goto fail;
749
750 lpfc_config_link(phba, mbox);
751
752 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
ed957684 753 mbox->vport = vport;
0b727fea 754 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
dea3101e
JB
755 if (rc == MBX_NOT_FINISHED) {
756 mempool_free(mbox, phba->mbox_mem_pool);
757 goto fail;
758 }
e47c9093
JS
759 /* Decrement ndlp reference count indicating that ndlp can be
760 * safely released when other references to it are done.
761 */
329f9bc7 762 lpfc_nlp_put(ndlp);
dea3101e 763
2e0fef85 764 ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
dea3101e
JB
765 if (!ndlp) {
766 /*
767 * Cannot find existing Fabric ndlp, so allocate a
768 * new one
769 */
770 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
771 if (!ndlp)
772 goto fail;
2e0fef85 773 lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
e47c9093
JS
774 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
775 ndlp = lpfc_enable_node(vport, ndlp,
776 NLP_STE_UNUSED_NODE);
777 if(!ndlp)
778 goto fail;
dea3101e
JB
779 }
780
781 memcpy(&ndlp->nlp_portname, &sp->portName,
2e0fef85 782 sizeof(struct lpfc_name));
dea3101e 783 memcpy(&ndlp->nlp_nodename, &sp->nodeName,
2e0fef85 784 sizeof(struct lpfc_name));
e47c9093 785 /* Set state will put ndlp onto node list if not already done */
2e0fef85
JS
786 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
787 spin_lock_irq(shost->host_lock);
dea3101e 788 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 789 spin_unlock_irq(shost->host_lock);
e47c9093
JS
790 } else
791 /* This side will wait for the PLOGI, decrement ndlp reference
792 * count indicating that ndlp can be released when other
793 * references to it are done.
794 */
329f9bc7 795 lpfc_nlp_put(ndlp);
dea3101e 796
09372820
JS
797 /* If we are pt2pt with another NPort, force NPIV off! */
798 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
799
2e0fef85
JS
800 spin_lock_irq(shost->host_lock);
801 vport->fc_flag |= FC_PT2PT;
802 spin_unlock_irq(shost->host_lock);
dea3101e
JB
803
804 /* Start discovery - this should just do CLEAR_LA */
2e0fef85 805 lpfc_disc_start(vport);
dea3101e 806 return 0;
92d7f7b0 807fail:
dea3101e
JB
808 return -ENXIO;
809}
810
e59058c4 811/**
3621a710 812 * lpfc_cmpl_els_flogi - Completion callback function for flogi
e59058c4
JS
813 * @phba: pointer to lpfc hba data structure.
814 * @cmdiocb: pointer to lpfc command iocb data structure.
815 * @rspiocb: pointer to lpfc response iocb data structure.
816 *
817 * This routine is the top-level completion callback function for issuing
818 * a Fabric Login (FLOGI) command. If the response IOCB reported error,
819 * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
820 * retry has been made (either immediately or delayed with lpfc_els_retry()
821 * returning 1), the command IOCB will be released and function returned.
822 * If the retry attempt has been given up (possibly reach the maximum
823 * number of retries), one additional decrement of ndlp reference shall be
824 * invoked before going out after releasing the command IOCB. This will
825 * actually release the remote node (Note, lpfc_els_free_iocb() will also
826 * invoke one decrement of ndlp reference count). If no error reported in
827 * the IOCB status, the command Port ID field is used to determine whether
828 * this is a point-to-point topology or a fabric topology: if the Port ID
829 * field is assigned, it is a fabric topology; otherwise, it is a
830 * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
831 * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
832 * specific topology completion conditions.
833 **/
dea3101e 834static void
329f9bc7
JS
835lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
836 struct lpfc_iocbq *rspiocb)
dea3101e 837{
2e0fef85
JS
838 struct lpfc_vport *vport = cmdiocb->vport;
839 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
840 IOCB_t *irsp = &rspiocb->iocb;
841 struct lpfc_nodelist *ndlp = cmdiocb->context1;
842 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
843 struct serv_parm *sp;
0c9ab6f5 844 uint16_t fcf_index;
dea3101e
JB
845 int rc;
846
847 /* Check to see if link went down during discovery */
2e0fef85 848 if (lpfc_els_chk_latt(vport)) {
fa4066b6
JS
849 /* One additional decrement on node reference count to
850 * trigger the release of the node
851 */
329f9bc7 852 lpfc_nlp_put(ndlp);
dea3101e
JB
853 goto out;
854 }
855
858c9f6c
JS
856 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
857 "FLOGI cmpl: status:x%x/x%x state:x%x",
858 irsp->ulpStatus, irsp->un.ulpWord[4],
859 vport->port_state);
860
dea3101e 861 if (irsp->ulpStatus) {
0c9ab6f5 862 /*
a93ff37a 863 * In case of FIP mode, perform roundrobin FCF failover
0c9ab6f5
JS
864 * due to new FCF discovery
865 */
866 if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
dbb6b3ab 867 (phba->fcf.fcf_flag & FCF_DISCOVERY) &&
9589b062
JS
868 !((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
869 (irsp->un.ulpWord[4] == IOERR_SLI_ABORTED))) {
0c9ab6f5 870 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
a93ff37a
JS
871 "2611 FLOGI failed on FCF (x%x), "
872 "status:x%x/x%x, tmo:x%x, perform "
873 "roundrobin FCF failover\n",
38b92ef8
JS
874 phba->fcf.current_rec.fcf_indx,
875 irsp->ulpStatus, irsp->un.ulpWord[4],
876 irsp->ulpTimeout);
7d791df7
JS
877 lpfc_sli4_set_fcf_flogi_fail(phba,
878 phba->fcf.current_rec.fcf_indx);
0c9ab6f5 879 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
a93ff37a
JS
880 rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
881 if (rc)
882 goto out;
0c9ab6f5
JS
883 }
884
38b92ef8
JS
885 /* FLOGI failure */
886 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
887 "2858 FLOGI failure Status:x%x/x%x TMO:x%x\n",
888 irsp->ulpStatus, irsp->un.ulpWord[4],
889 irsp->ulpTimeout);
890
dea3101e 891 /* Check for retry */
2e0fef85 892 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
dea3101e 893 goto out;
2e0fef85 894
76a95d75
JS
895 /* FLOGI failure */
896 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
897 "0100 FLOGI failure Status:x%x/x%x TMO:x%x\n",
898 irsp->ulpStatus, irsp->un.ulpWord[4],
899 irsp->ulpTimeout);
900
dea3101e 901 /* FLOGI failed, so there is no fabric */
2e0fef85
JS
902 spin_lock_irq(shost->host_lock);
903 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
904 spin_unlock_irq(shost->host_lock);
dea3101e 905
329f9bc7 906 /* If private loop, then allow max outstanding els to be
dea3101e
JB
907 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
908 * alpa map would take too long otherwise.
909 */
910 if (phba->alpa_map[0] == 0) {
3de2a653 911 vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
76a95d75
JS
912 if ((phba->sli_rev == LPFC_SLI_REV4) &&
913 (!(vport->fc_flag & FC_VFI_REGISTERED) ||
914 (vport->fc_prevDID != vport->fc_myDID))) {
915 if (vport->fc_flag & FC_VFI_REGISTERED)
916 lpfc_sli4_unreg_all_rpis(vport);
917 lpfc_issue_reg_vfi(vport);
918 lpfc_nlp_put(ndlp);
919 goto out;
920 }
dea3101e 921 }
dea3101e
JB
922 goto flogifail;
923 }
695a814e
JS
924 spin_lock_irq(shost->host_lock);
925 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
4b40c59e 926 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
695a814e 927 spin_unlock_irq(shost->host_lock);
dea3101e
JB
928
929 /*
930 * The FLogI succeeded. Sync the data for the CPU before
931 * accessing it.
932 */
933 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
934
935 sp = prsp->virt + sizeof(uint32_t);
936
937 /* FLOGI completes successfully */
e8b62011 938 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
af901ca1 939 "0101 FLOGI completes successfully "
e8b62011
JS
940 "Data: x%x x%x x%x x%x\n",
941 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
942 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
dea3101e 943
2e0fef85 944 if (vport->port_state == LPFC_FLOGI) {
dea3101e
JB
945 /*
946 * If Common Service Parameters indicate Nport
947 * we are point to point, if Fport we are Fabric.
948 */
949 if (sp->cmn.fPort)
2e0fef85 950 rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
76a95d75 951 else if (!(phba->hba_flag & HBA_FCOE_MODE))
2e0fef85 952 rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
dbb6b3ab
JS
953 else {
954 lpfc_printf_vlog(vport, KERN_ERR,
955 LOG_FIP | LOG_ELS,
956 "2831 FLOGI response with cleared Fabric "
957 "bit fcf_index 0x%x "
958 "Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
959 "Fabric Name "
960 "%02x%02x%02x%02x%02x%02x%02x%02x\n",
961 phba->fcf.current_rec.fcf_indx,
962 phba->fcf.current_rec.switch_name[0],
963 phba->fcf.current_rec.switch_name[1],
964 phba->fcf.current_rec.switch_name[2],
965 phba->fcf.current_rec.switch_name[3],
966 phba->fcf.current_rec.switch_name[4],
967 phba->fcf.current_rec.switch_name[5],
968 phba->fcf.current_rec.switch_name[6],
969 phba->fcf.current_rec.switch_name[7],
970 phba->fcf.current_rec.fabric_name[0],
971 phba->fcf.current_rec.fabric_name[1],
972 phba->fcf.current_rec.fabric_name[2],
973 phba->fcf.current_rec.fabric_name[3],
974 phba->fcf.current_rec.fabric_name[4],
975 phba->fcf.current_rec.fabric_name[5],
976 phba->fcf.current_rec.fabric_name[6],
977 phba->fcf.current_rec.fabric_name[7]);
978 lpfc_nlp_put(ndlp);
979 spin_lock_irq(&phba->hbalock);
980 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
a93ff37a 981 phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
dbb6b3ab
JS
982 spin_unlock_irq(&phba->hbalock);
983 goto out;
984 }
0c9ab6f5
JS
985 if (!rc) {
986 /* Mark the FCF discovery process done */
999d813f
JS
987 if (phba->hba_flag & HBA_FIP_SUPPORT)
988 lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
989 LOG_ELS,
a93ff37a
JS
990 "2769 FLOGI to FCF (x%x) "
991 "completed successfully\n",
999d813f 992 phba->fcf.current_rec.fcf_indx);
0c9ab6f5
JS
993 spin_lock_irq(&phba->hbalock);
994 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
a93ff37a 995 phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
0c9ab6f5 996 spin_unlock_irq(&phba->hbalock);
dea3101e 997 goto out;
0c9ab6f5 998 }
dea3101e
JB
999 }
1000
1001flogifail:
329f9bc7 1002 lpfc_nlp_put(ndlp);
dea3101e 1003
858c9f6c 1004 if (!lpfc_error_lost_link(irsp)) {
dea3101e 1005 /* FLOGI failed, so just use loop map to make discovery list */
2e0fef85 1006 lpfc_disc_list_loopmap(vport);
dea3101e
JB
1007
1008 /* Start discovery */
2e0fef85 1009 lpfc_disc_start(vport);
87af33fe
JS
1010 } else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1011 ((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
1012 (irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
1013 (phba->link_state != LPFC_CLEAR_LA)) {
1014 /* If FLOGI failed enable link interrupt. */
1015 lpfc_issue_clear_la(phba, vport);
dea3101e 1016 }
dea3101e
JB
1017out:
1018 lpfc_els_free_iocb(phba, cmdiocb);
1019}
1020
e59058c4 1021/**
3621a710 1022 * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
e59058c4
JS
1023 * @vport: pointer to a host virtual N_Port data structure.
1024 * @ndlp: pointer to a node-list data structure.
1025 * @retry: number of retries to the command IOCB.
1026 *
1027 * This routine issues a Fabric Login (FLOGI) Request ELS command
1028 * for a @vport. The initiator service parameters are put into the payload
1029 * of the FLOGI Request IOCB and the top-level callback function pointer
1030 * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1031 * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1032 * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1033 *
1034 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1035 * will be incremented by 1 for holding the ndlp and the reference to ndlp
1036 * will be stored into the context1 field of the IOCB for the completion
1037 * callback function to the FLOGI ELS command.
1038 *
1039 * Return code
1040 * 0 - successfully issued flogi iocb for @vport
1041 * 1 - failed to issue flogi iocb for @vport
1042 **/
dea3101e 1043static int
2e0fef85 1044lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
1045 uint8_t retry)
1046{
2e0fef85 1047 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
1048 struct serv_parm *sp;
1049 IOCB_t *icmd;
1050 struct lpfc_iocbq *elsiocb;
1051 struct lpfc_sli_ring *pring;
1052 uint8_t *pcmd;
1053 uint16_t cmdsize;
1054 uint32_t tmo;
1055 int rc;
1056
1057 pring = &phba->sli.ring[LPFC_ELS_RING];
1058
92d7f7b0 1059 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2e0fef85
JS
1060 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1061 ndlp->nlp_DID, ELS_CMD_FLOGI);
92d7f7b0 1062
488d1469 1063 if (!elsiocb)
c9f8735b 1064 return 1;
dea3101e
JB
1065
1066 icmd = &elsiocb->iocb;
1067 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1068
1069 /* For FLOGI request, remainder of payload is service parameters */
1070 *((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
92d7f7b0
JS
1071 pcmd += sizeof(uint32_t);
1072 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
dea3101e
JB
1073 sp = (struct serv_parm *) pcmd;
1074
1075 /* Setup CSPs accordingly for Fabric */
1076 sp->cmn.e_d_tov = 0;
1077 sp->cmn.w2.r_a_tov = 0;
1078 sp->cls1.classValid = 0;
1079 sp->cls2.seqDelivery = 1;
1080 sp->cls3.seqDelivery = 1;
1081 if (sp->cmn.fcphLow < FC_PH3)
1082 sp->cmn.fcphLow = FC_PH3;
1083 if (sp->cmn.fcphHigh < FC_PH3)
1084 sp->cmn.fcphHigh = FC_PH3;
1085
c31098ce
JS
1086 if (phba->sli_rev == LPFC_SLI_REV4) {
1087 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1088 LPFC_SLI_INTF_IF_TYPE_0) {
1089 elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1090 elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1091 /* FLOGI needs to be 3 for WQE FCFI */
1092 /* Set the fcfi to the fcfi we registered with */
1093 elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1094 }
5248a749
JS
1095 } else {
1096 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1097 sp->cmn.request_multiple_Nport = 1;
1098 /* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1099 icmd->ulpCt_h = 1;
1100 icmd->ulpCt_l = 0;
1101 } else
1102 sp->cmn.request_multiple_Nport = 0;
92d7f7b0
JS
1103 }
1104
76a95d75 1105 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
858c9f6c
JS
1106 icmd->un.elsreq64.myID = 0;
1107 icmd->un.elsreq64.fl = 1;
1108 }
1109
dea3101e
JB
1110 tmo = phba->fc_ratov;
1111 phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
2e0fef85 1112 lpfc_set_disctmo(vport);
dea3101e
JB
1113 phba->fc_ratov = tmo;
1114
1115 phba->fc_stat.elsXmitFLOGI++;
1116 elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
858c9f6c
JS
1117
1118 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1119 "Issue FLOGI: opt:x%x",
1120 phba->sli3_options, 0, 0);
1121
92d7f7b0 1122 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
dea3101e
JB
1123 if (rc == IOCB_ERROR) {
1124 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 1125 return 1;
dea3101e 1126 }
c9f8735b 1127 return 0;
dea3101e
JB
1128}
1129
e59058c4 1130/**
3621a710 1131 * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
e59058c4
JS
1132 * @phba: pointer to lpfc hba data structure.
1133 *
1134 * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1135 * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1136 * list and issues an abort IOCB commond on each outstanding IOCB that
1137 * contains a active Fabric_DID ndlp. Note that this function is to issue
1138 * the abort IOCB command on all the outstanding IOCBs, thus when this
1139 * function returns, it does not guarantee all the IOCBs are actually aborted.
1140 *
1141 * Return code
3ad2f3fb 1142 * 0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
e59058c4 1143 **/
dea3101e 1144int
2e0fef85 1145lpfc_els_abort_flogi(struct lpfc_hba *phba)
dea3101e
JB
1146{
1147 struct lpfc_sli_ring *pring;
1148 struct lpfc_iocbq *iocb, *next_iocb;
1149 struct lpfc_nodelist *ndlp;
1150 IOCB_t *icmd;
1151
1152 /* Abort outstanding I/O on NPort <nlp_DID> */
1153 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
e8b62011
JS
1154 "0201 Abort outstanding I/O on NPort x%x\n",
1155 Fabric_DID);
dea3101e
JB
1156
1157 pring = &phba->sli.ring[LPFC_ELS_RING];
1158
1159 /*
1160 * Check the txcmplq for an iocb that matches the nport the driver is
1161 * searching for.
1162 */
2e0fef85 1163 spin_lock_irq(&phba->hbalock);
dea3101e
JB
1164 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1165 icmd = &iocb->iocb;
2e0fef85
JS
1166 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
1167 icmd->un.elsreq64.bdl.ulpIoTag32) {
dea3101e 1168 ndlp = (struct lpfc_nodelist *)(iocb->context1);
58da1ffb
JS
1169 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1170 (ndlp->nlp_DID == Fabric_DID))
07951076 1171 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e
JB
1172 }
1173 }
2e0fef85 1174 spin_unlock_irq(&phba->hbalock);
dea3101e
JB
1175
1176 return 0;
1177}
1178
e59058c4 1179/**
3621a710 1180 * lpfc_initial_flogi - Issue an initial fabric login for a vport
e59058c4
JS
1181 * @vport: pointer to a host virtual N_Port data structure.
1182 *
1183 * This routine issues an initial Fabric Login (FLOGI) for the @vport
1184 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1185 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1186 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1187 * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1188 * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1189 * @vport.
1190 *
1191 * Return code
1192 * 0 - failed to issue initial flogi for @vport
1193 * 1 - successfully issued initial flogi for @vport
1194 **/
dea3101e 1195int
2e0fef85 1196lpfc_initial_flogi(struct lpfc_vport *vport)
dea3101e 1197{
2e0fef85 1198 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
1199 struct lpfc_nodelist *ndlp;
1200
98c9ea5c
JS
1201 vport->port_state = LPFC_FLOGI;
1202 lpfc_set_disctmo(vport);
1203
c9f8735b 1204 /* First look for the Fabric ndlp */
2e0fef85 1205 ndlp = lpfc_findnode_did(vport, Fabric_DID);
c9f8735b 1206 if (!ndlp) {
dea3101e 1207 /* Cannot find existing Fabric ndlp, so allocate a new one */
c9f8735b
JW
1208 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1209 if (!ndlp)
1210 return 0;
2e0fef85 1211 lpfc_nlp_init(vport, ndlp, Fabric_DID);
6fb120a7
JS
1212 /* Set the node type */
1213 ndlp->nlp_type |= NLP_FABRIC;
e47c9093
JS
1214 /* Put ndlp onto node list */
1215 lpfc_enqueue_node(vport, ndlp);
1216 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1217 /* re-setup ndlp without removing from node list */
1218 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1219 if (!ndlp)
1220 return 0;
dea3101e 1221 }
87af33fe 1222
5ac6b303 1223 if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
fa4066b6
JS
1224 /* This decrement of reference count to node shall kick off
1225 * the release of the node.
1226 */
329f9bc7 1227 lpfc_nlp_put(ndlp);
5ac6b303
JS
1228 return 0;
1229 }
c9f8735b 1230 return 1;
dea3101e
JB
1231}
1232
e59058c4 1233/**
3621a710 1234 * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
e59058c4
JS
1235 * @vport: pointer to a host virtual N_Port data structure.
1236 *
1237 * This routine issues an initial Fabric Discover (FDISC) for the @vport
1238 * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1239 * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1240 * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1241 * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1242 * is then invoked with the @vport and the ndlp to perform the FDISC for the
1243 * @vport.
1244 *
1245 * Return code
1246 * 0 - failed to issue initial fdisc for @vport
1247 * 1 - successfully issued initial fdisc for @vport
1248 **/
92d7f7b0
JS
1249int
1250lpfc_initial_fdisc(struct lpfc_vport *vport)
1251{
1252 struct lpfc_hba *phba = vport->phba;
1253 struct lpfc_nodelist *ndlp;
1254
1255 /* First look for the Fabric ndlp */
1256 ndlp = lpfc_findnode_did(vport, Fabric_DID);
1257 if (!ndlp) {
1258 /* Cannot find existing Fabric ndlp, so allocate a new one */
1259 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
1260 if (!ndlp)
1261 return 0;
1262 lpfc_nlp_init(vport, ndlp, Fabric_DID);
e47c9093
JS
1263 /* Put ndlp onto node list */
1264 lpfc_enqueue_node(vport, ndlp);
1265 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
1266 /* re-setup ndlp without removing from node list */
1267 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1268 if (!ndlp)
1269 return 0;
92d7f7b0 1270 }
e47c9093 1271
92d7f7b0 1272 if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
fa4066b6
JS
1273 /* decrement node reference count to trigger the release of
1274 * the node.
1275 */
92d7f7b0 1276 lpfc_nlp_put(ndlp);
fa4066b6 1277 return 0;
92d7f7b0
JS
1278 }
1279 return 1;
1280}
87af33fe 1281
e59058c4 1282/**
3621a710 1283 * lpfc_more_plogi - Check and issue remaining plogis for a vport
e59058c4
JS
1284 * @vport: pointer to a host virtual N_Port data structure.
1285 *
1286 * This routine checks whether there are more remaining Port Logins
1287 * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1288 * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1289 * to issue ELS PLOGIs up to the configured discover threads with the
1290 * @vport (@vport->cfg_discovery_threads). The function also decrement
1291 * the @vport's num_disc_node by 1 if it is not already 0.
1292 **/
87af33fe 1293void
2e0fef85 1294lpfc_more_plogi(struct lpfc_vport *vport)
dea3101e
JB
1295{
1296 int sentplogi;
1297
2e0fef85
JS
1298 if (vport->num_disc_nodes)
1299 vport->num_disc_nodes--;
dea3101e
JB
1300
1301 /* Continue discovery with <num_disc_nodes> PLOGIs to go */
e8b62011
JS
1302 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1303 "0232 Continue discovery with %d PLOGIs to go "
1304 "Data: x%x x%x x%x\n",
1305 vport->num_disc_nodes, vport->fc_plogi_cnt,
1306 vport->fc_flag, vport->port_state);
dea3101e 1307 /* Check to see if there are more PLOGIs to be sent */
2e0fef85
JS
1308 if (vport->fc_flag & FC_NLP_MORE)
1309 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
1310 sentplogi = lpfc_els_disc_plogi(vport);
1311
dea3101e
JB
1312 return;
1313}
1314
e59058c4 1315/**
3621a710 1316 * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
e59058c4
JS
1317 * @phba: pointer to lpfc hba data structure.
1318 * @prsp: pointer to response IOCB payload.
1319 * @ndlp: pointer to a node-list data structure.
1320 *
1321 * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1322 * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1323 * The following cases are considered N_Port confirmed:
1324 * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1325 * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1326 * it does not have WWPN assigned either. If the WWPN is confirmed, the
1327 * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1328 * 1) if there is a node on vport list other than the @ndlp with the same
1329 * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1330 * on that node to release the RPI associated with the node; 2) if there is
1331 * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1332 * into, a new node shall be allocated (or activated). In either case, the
1333 * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1334 * be released and the new_ndlp shall be put on to the vport node list and
1335 * its pointer returned as the confirmed node.
1336 *
1337 * Note that before the @ndlp got "released", the keepDID from not-matching
1338 * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1339 * of the @ndlp. This is because the release of @ndlp is actually to put it
1340 * into an inactive state on the vport node list and the vport node list
1341 * management algorithm does not allow two node with a same DID.
1342 *
1343 * Return code
1344 * pointer to the PLOGI N_Port @ndlp
1345 **/
488d1469 1346static struct lpfc_nodelist *
92d7f7b0 1347lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
488d1469
JS
1348 struct lpfc_nodelist *ndlp)
1349{
2e0fef85 1350 struct lpfc_vport *vport = ndlp->vport;
488d1469 1351 struct lpfc_nodelist *new_ndlp;
0ff10d46
JS
1352 struct lpfc_rport_data *rdata;
1353 struct fc_rport *rport;
488d1469 1354 struct serv_parm *sp;
92d7f7b0 1355 uint8_t name[sizeof(struct lpfc_name)];
58da1ffb 1356 uint32_t rc, keepDID = 0;
38b92ef8
JS
1357 int put_node;
1358 int put_rport;
19ca7609 1359 struct lpfc_node_rrqs rrq;
488d1469 1360
2fb9bd8b
JS
1361 /* Fabric nodes can have the same WWPN so we don't bother searching
1362 * by WWPN. Just return the ndlp that was given to us.
1363 */
1364 if (ndlp->nlp_type & NLP_FABRIC)
1365 return ndlp;
1366
92d7f7b0 1367 sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
685f0bf7 1368 memset(name, 0, sizeof(struct lpfc_name));
488d1469 1369
685f0bf7 1370 /* Now we find out if the NPort we are logging into, matches the WWPN
488d1469
JS
1371 * we have for that ndlp. If not, we have some work to do.
1372 */
2e0fef85 1373 new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
488d1469 1374
e47c9093 1375 if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
488d1469 1376 return ndlp;
19ca7609 1377 memset(&rrq.xri_bitmap, 0, sizeof(new_ndlp->active_rrqs.xri_bitmap));
488d1469
JS
1378
1379 if (!new_ndlp) {
2e0fef85
JS
1380 rc = memcmp(&ndlp->nlp_portname, name,
1381 sizeof(struct lpfc_name));
92795650
JS
1382 if (!rc)
1383 return ndlp;
488d1469
JS
1384 new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1385 if (!new_ndlp)
1386 return ndlp;
2e0fef85 1387 lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
e47c9093 1388 } else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
58da1ffb
JS
1389 rc = memcmp(&ndlp->nlp_portname, name,
1390 sizeof(struct lpfc_name));
1391 if (!rc)
1392 return ndlp;
e47c9093
JS
1393 new_ndlp = lpfc_enable_node(vport, new_ndlp,
1394 NLP_STE_UNUSED_NODE);
1395 if (!new_ndlp)
1396 return ndlp;
58da1ffb 1397 keepDID = new_ndlp->nlp_DID;
19ca7609
JS
1398 if (phba->sli_rev == LPFC_SLI_REV4)
1399 memcpy(&rrq.xri_bitmap,
1400 &new_ndlp->active_rrqs.xri_bitmap,
1401 sizeof(new_ndlp->active_rrqs.xri_bitmap));
1402 } else {
58da1ffb 1403 keepDID = new_ndlp->nlp_DID;
19ca7609
JS
1404 if (phba->sli_rev == LPFC_SLI_REV4)
1405 memcpy(&rrq.xri_bitmap,
1406 &new_ndlp->active_rrqs.xri_bitmap,
1407 sizeof(new_ndlp->active_rrqs.xri_bitmap));
1408 }
488d1469 1409
2e0fef85 1410 lpfc_unreg_rpi(vport, new_ndlp);
488d1469 1411 new_ndlp->nlp_DID = ndlp->nlp_DID;
92795650 1412 new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
19ca7609
JS
1413 if (phba->sli_rev == LPFC_SLI_REV4)
1414 memcpy(new_ndlp->active_rrqs.xri_bitmap,
1415 &ndlp->active_rrqs.xri_bitmap,
1416 sizeof(ndlp->active_rrqs.xri_bitmap));
0ff10d46
JS
1417
1418 if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1419 new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1420 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1421
e47c9093 1422 /* Set state will put new_ndlp on to node list if not already done */
2e0fef85 1423 lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
488d1469 1424
2e0fef85 1425 /* Move this back to NPR state */
87af33fe
JS
1426 if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1427 /* The new_ndlp is replacing ndlp totally, so we need
1428 * to put ndlp on UNUSED list and try to free it.
1429 */
0ff10d46
JS
1430
1431 /* Fix up the rport accordingly */
1432 rport = ndlp->rport;
1433 if (rport) {
1434 rdata = rport->dd_data;
1435 if (rdata->pnode == ndlp) {
1436 lpfc_nlp_put(ndlp);
1437 ndlp->rport = NULL;
1438 rdata->pnode = lpfc_nlp_get(new_ndlp);
1439 new_ndlp->rport = rport;
1440 }
1441 new_ndlp->nlp_type = ndlp->nlp_type;
1442 }
58da1ffb
JS
1443 /* We shall actually free the ndlp with both nlp_DID and
1444 * nlp_portname fields equals 0 to avoid any ndlp on the
1445 * nodelist never to be used.
1446 */
1447 if (ndlp->nlp_DID == 0) {
1448 spin_lock_irq(&phba->ndlp_lock);
1449 NLP_SET_FREE_REQ(ndlp);
1450 spin_unlock_irq(&phba->ndlp_lock);
1451 }
0ff10d46 1452
58da1ffb
JS
1453 /* Two ndlps cannot have the same did on the nodelist */
1454 ndlp->nlp_DID = keepDID;
19ca7609
JS
1455 if (phba->sli_rev == LPFC_SLI_REV4)
1456 memcpy(&ndlp->active_rrqs.xri_bitmap,
1457 &rrq.xri_bitmap,
1458 sizeof(ndlp->active_rrqs.xri_bitmap));
2e0fef85 1459 lpfc_drop_node(vport, ndlp);
87af33fe 1460 }
92795650 1461 else {
2e0fef85 1462 lpfc_unreg_rpi(vport, ndlp);
58da1ffb
JS
1463 /* Two ndlps cannot have the same did */
1464 ndlp->nlp_DID = keepDID;
19ca7609
JS
1465 if (phba->sli_rev == LPFC_SLI_REV4)
1466 memcpy(&ndlp->active_rrqs.xri_bitmap,
1467 &rrq.xri_bitmap,
1468 sizeof(ndlp->active_rrqs.xri_bitmap));
2e0fef85 1469 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
38b92ef8
JS
1470 /* Since we are swapping the ndlp passed in with the new one
1471 * and the did has already been swapped, copy over the
1472 * state and names.
1473 */
1474 memcpy(&new_ndlp->nlp_portname, &ndlp->nlp_portname,
1475 sizeof(struct lpfc_name));
1476 memcpy(&new_ndlp->nlp_nodename, &ndlp->nlp_nodename,
1477 sizeof(struct lpfc_name));
1478 new_ndlp->nlp_state = ndlp->nlp_state;
1479 /* Fix up the rport accordingly */
1480 rport = ndlp->rport;
1481 if (rport) {
1482 rdata = rport->dd_data;
1483 put_node = rdata->pnode != NULL;
1484 put_rport = ndlp->rport != NULL;
1485 rdata->pnode = NULL;
1486 ndlp->rport = NULL;
1487 if (put_node)
1488 lpfc_nlp_put(ndlp);
1489 if (put_rport)
1490 put_device(&rport->dev);
1491 }
92795650 1492 }
488d1469
JS
1493 return new_ndlp;
1494}
1495
e59058c4 1496/**
3621a710 1497 * lpfc_end_rscn - Check and handle more rscn for a vport
e59058c4
JS
1498 * @vport: pointer to a host virtual N_Port data structure.
1499 *
1500 * This routine checks whether more Registration State Change
1501 * Notifications (RSCNs) came in while the discovery state machine was in
1502 * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1503 * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1504 * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1505 * handling the RSCNs.
1506 **/
87af33fe
JS
1507void
1508lpfc_end_rscn(struct lpfc_vport *vport)
1509{
1510 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1511
1512 if (vport->fc_flag & FC_RSCN_MODE) {
1513 /*
1514 * Check to see if more RSCNs came in while we were
1515 * processing this one.
1516 */
1517 if (vport->fc_rscn_id_cnt ||
1518 (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1519 lpfc_els_handle_rscn(vport);
1520 else {
1521 spin_lock_irq(shost->host_lock);
1522 vport->fc_flag &= ~FC_RSCN_MODE;
1523 spin_unlock_irq(shost->host_lock);
1524 }
1525 }
1526}
1527
19ca7609
JS
1528/**
1529 * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1530 * @phba: pointer to lpfc hba data structure.
1531 * @cmdiocb: pointer to lpfc command iocb data structure.
1532 * @rspiocb: pointer to lpfc response iocb data structure.
1533 *
1534 * This routine will call the clear rrq function to free the rrq and
1535 * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1536 * exist then the clear_rrq is still called because the rrq needs to
1537 * be freed.
1538 **/
1539
1540static void
1541lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1542 struct lpfc_iocbq *rspiocb)
1543{
1544 struct lpfc_vport *vport = cmdiocb->vport;
1545 IOCB_t *irsp;
1546 struct lpfc_nodelist *ndlp;
1547 struct lpfc_node_rrq *rrq;
1548
1549 /* we pass cmdiocb to state machine which needs rspiocb as well */
1550 rrq = cmdiocb->context_un.rrq;
1551 cmdiocb->context_un.rsp_iocb = rspiocb;
1552
1553 irsp = &rspiocb->iocb;
1554 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1555 "RRQ cmpl: status:x%x/x%x did:x%x",
1556 irsp->ulpStatus, irsp->un.ulpWord[4],
1557 irsp->un.elsreq64.remoteID);
1558
1559 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1560 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1561 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1562 "2882 RRQ completes to NPort x%x "
1563 "with no ndlp. Data: x%x x%x x%x\n",
1564 irsp->un.elsreq64.remoteID,
1565 irsp->ulpStatus, irsp->un.ulpWord[4],
1566 irsp->ulpIoTag);
1567 goto out;
1568 }
1569
1570 /* rrq completes to NPort <nlp_DID> */
1571 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1572 "2880 RRQ completes to NPort x%x "
1573 "Data: x%x x%x x%x x%x x%x\n",
1574 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1575 irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1576
1577 if (irsp->ulpStatus) {
1578 /* Check for retry */
1579 /* RRQ failed Don't print the vport to vport rjts */
1580 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1581 (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1582 ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1583 (phba)->pport->cfg_log_verbose & LOG_ELS)
1584 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1585 "2881 RRQ failure DID:%06X Status:x%x/x%x\n",
1586 ndlp->nlp_DID, irsp->ulpStatus,
1587 irsp->un.ulpWord[4]);
1588 }
1589out:
1590 if (rrq)
1591 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1592 lpfc_els_free_iocb(phba, cmdiocb);
1593 return;
1594}
e59058c4 1595/**
3621a710 1596 * lpfc_cmpl_els_plogi - Completion callback function for plogi
e59058c4
JS
1597 * @phba: pointer to lpfc hba data structure.
1598 * @cmdiocb: pointer to lpfc command iocb data structure.
1599 * @rspiocb: pointer to lpfc response iocb data structure.
1600 *
1601 * This routine is the completion callback function for issuing the Port
1602 * Login (PLOGI) command. For PLOGI completion, there must be an active
1603 * ndlp on the vport node list that matches the remote node ID from the
25985edc 1604 * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
e59058c4
JS
1605 * ignored and command IOCB released. The PLOGI response IOCB status is
1606 * checked for error conditons. If there is error status reported, PLOGI
1607 * retry shall be attempted by invoking the lpfc_els_retry() routine.
1608 * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1609 * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1610 * (DSM) is set for this PLOGI completion. Finally, it checks whether
1611 * there are additional N_Port nodes with the vport that need to perform
1612 * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1613 * PLOGIs.
1614 **/
dea3101e 1615static void
2e0fef85
JS
1616lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1617 struct lpfc_iocbq *rspiocb)
dea3101e 1618{
2e0fef85
JS
1619 struct lpfc_vport *vport = cmdiocb->vport;
1620 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 1621 IOCB_t *irsp;
dea3101e 1622 struct lpfc_nodelist *ndlp;
92795650 1623 struct lpfc_dmabuf *prsp;
dea3101e
JB
1624 int disc, rc, did, type;
1625
dea3101e
JB
1626 /* we pass cmdiocb to state machine which needs rspiocb as well */
1627 cmdiocb->context_un.rsp_iocb = rspiocb;
1628
1629 irsp = &rspiocb->iocb;
858c9f6c
JS
1630 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1631 "PLOGI cmpl: status:x%x/x%x did:x%x",
1632 irsp->ulpStatus, irsp->un.ulpWord[4],
1633 irsp->un.elsreq64.remoteID);
1634
2e0fef85 1635 ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
e47c9093 1636 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
e8b62011
JS
1637 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1638 "0136 PLOGI completes to NPort x%x "
1639 "with no ndlp. Data: x%x x%x x%x\n",
1640 irsp->un.elsreq64.remoteID,
1641 irsp->ulpStatus, irsp->un.ulpWord[4],
1642 irsp->ulpIoTag);
488d1469 1643 goto out;
ed957684 1644 }
dea3101e
JB
1645
1646 /* Since ndlp can be freed in the disc state machine, note if this node
1647 * is being used during discovery.
1648 */
2e0fef85 1649 spin_lock_irq(shost->host_lock);
dea3101e 1650 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
488d1469 1651 ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2e0fef85 1652 spin_unlock_irq(shost->host_lock);
dea3101e
JB
1653 rc = 0;
1654
1655 /* PLOGI completes to NPort <nlp_DID> */
e8b62011
JS
1656 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1657 "0102 PLOGI completes to NPort x%x "
1658 "Data: x%x x%x x%x x%x x%x\n",
1659 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1660 irsp->ulpTimeout, disc, vport->num_disc_nodes);
dea3101e 1661 /* Check to see if link went down during discovery */
2e0fef85
JS
1662 if (lpfc_els_chk_latt(vport)) {
1663 spin_lock_irq(shost->host_lock);
dea3101e 1664 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1665 spin_unlock_irq(shost->host_lock);
dea3101e
JB
1666 goto out;
1667 }
1668
1669 /* ndlp could be freed in DSM, save these values now */
1670 type = ndlp->nlp_type;
1671 did = ndlp->nlp_DID;
1672
1673 if (irsp->ulpStatus) {
1674 /* Check for retry */
1675 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1676 /* ELS command is being retried */
1677 if (disc) {
2e0fef85 1678 spin_lock_irq(shost->host_lock);
dea3101e 1679 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 1680 spin_unlock_irq(shost->host_lock);
dea3101e
JB
1681 }
1682 goto out;
1683 }
2a9bf3d0
JS
1684 /* PLOGI failed Don't print the vport to vport rjts */
1685 if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1686 (((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1687 ((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1688 (phba)->pport->cfg_log_verbose & LOG_ELS)
1689 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
e40a02c1
JS
1690 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
1691 ndlp->nlp_DID, irsp->ulpStatus,
1692 irsp->un.ulpWord[4]);
dea3101e 1693 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 1694 if (lpfc_error_lost_link(irsp))
c9f8735b 1695 rc = NLP_STE_FREED_NODE;
e47c9093 1696 else
2e0fef85 1697 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 1698 NLP_EVT_CMPL_PLOGI);
dea3101e
JB
1699 } else {
1700 /* Good status, call state machine */
92795650 1701 prsp = list_entry(((struct lpfc_dmabuf *)
92d7f7b0
JS
1702 cmdiocb->context2)->list.next,
1703 struct lpfc_dmabuf, list);
1704 ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2e0fef85 1705 rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 1706 NLP_EVT_CMPL_PLOGI);
dea3101e
JB
1707 }
1708
2e0fef85 1709 if (disc && vport->num_disc_nodes) {
dea3101e 1710 /* Check to see if there are more PLOGIs to be sent */
2e0fef85 1711 lpfc_more_plogi(vport);
dea3101e 1712
2e0fef85
JS
1713 if (vport->num_disc_nodes == 0) {
1714 spin_lock_irq(shost->host_lock);
1715 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1716 spin_unlock_irq(shost->host_lock);
dea3101e 1717
2e0fef85 1718 lpfc_can_disctmo(vport);
87af33fe 1719 lpfc_end_rscn(vport);
dea3101e
JB
1720 }
1721 }
1722
1723out:
1724 lpfc_els_free_iocb(phba, cmdiocb);
1725 return;
1726}
1727
e59058c4 1728/**
3621a710 1729 * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
e59058c4
JS
1730 * @vport: pointer to a host virtual N_Port data structure.
1731 * @did: destination port identifier.
1732 * @retry: number of retries to the command IOCB.
1733 *
1734 * This routine issues a Port Login (PLOGI) command to a remote N_Port
1735 * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1736 * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1737 * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1738 * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1739 *
1740 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1741 * will be incremented by 1 for holding the ndlp and the reference to ndlp
1742 * will be stored into the context1 field of the IOCB for the completion
1743 * callback function to the PLOGI ELS command.
1744 *
1745 * Return code
1746 * 0 - Successfully issued a plogi for @vport
1747 * 1 - failed to issue a plogi for @vport
1748 **/
dea3101e 1749int
2e0fef85 1750lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
dea3101e 1751{
2e0fef85 1752 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
1753 struct serv_parm *sp;
1754 IOCB_t *icmd;
98c9ea5c 1755 struct lpfc_nodelist *ndlp;
dea3101e 1756 struct lpfc_iocbq *elsiocb;
dea3101e
JB
1757 struct lpfc_sli *psli;
1758 uint8_t *pcmd;
1759 uint16_t cmdsize;
92d7f7b0 1760 int ret;
dea3101e
JB
1761
1762 psli = &phba->sli;
dea3101e 1763
98c9ea5c 1764 ndlp = lpfc_findnode_did(vport, did);
e47c9093
JS
1765 if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1766 ndlp = NULL;
98c9ea5c 1767
e47c9093 1768 /* If ndlp is not NULL, we will bump the reference count on it */
92d7f7b0 1769 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
98c9ea5c 1770 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2e0fef85 1771 ELS_CMD_PLOGI);
c9f8735b
JW
1772 if (!elsiocb)
1773 return 1;
dea3101e
JB
1774
1775 icmd = &elsiocb->iocb;
1776 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1777
1778 /* For PLOGI request, remainder of payload is service parameters */
1779 *((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
92d7f7b0
JS
1780 pcmd += sizeof(uint32_t);
1781 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
dea3101e
JB
1782 sp = (struct serv_parm *) pcmd;
1783
5ac6b303
JS
1784 /*
1785 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
1786 * to device on remote loops work.
1787 */
1788 if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
1789 sp->cmn.altBbCredit = 1;
1790
dea3101e
JB
1791 if (sp->cmn.fcphLow < FC_PH_4_3)
1792 sp->cmn.fcphLow = FC_PH_4_3;
1793
1794 if (sp->cmn.fcphHigh < FC_PH3)
1795 sp->cmn.fcphHigh = FC_PH3;
1796
858c9f6c
JS
1797 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1798 "Issue PLOGI: did:x%x",
1799 did, 0, 0);
1800
dea3101e
JB
1801 phba->fc_stat.elsXmitPLOGI++;
1802 elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
3772a991 1803 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
92d7f7b0
JS
1804
1805 if (ret == IOCB_ERROR) {
dea3101e 1806 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 1807 return 1;
dea3101e 1808 }
c9f8735b 1809 return 0;
dea3101e
JB
1810}
1811
e59058c4 1812/**
3621a710 1813 * lpfc_cmpl_els_prli - Completion callback function for prli
e59058c4
JS
1814 * @phba: pointer to lpfc hba data structure.
1815 * @cmdiocb: pointer to lpfc command iocb data structure.
1816 * @rspiocb: pointer to lpfc response iocb data structure.
1817 *
1818 * This routine is the completion callback function for a Process Login
1819 * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1820 * status. If there is error status reported, PRLI retry shall be attempted
1821 * by invoking the lpfc_els_retry() routine. Otherwise, the state
1822 * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1823 * ndlp to mark the PRLI completion.
1824 **/
dea3101e 1825static void
2e0fef85
JS
1826lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1827 struct lpfc_iocbq *rspiocb)
dea3101e 1828{
2e0fef85
JS
1829 struct lpfc_vport *vport = cmdiocb->vport;
1830 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
1831 IOCB_t *irsp;
1832 struct lpfc_sli *psli;
1833 struct lpfc_nodelist *ndlp;
1834
1835 psli = &phba->sli;
1836 /* we pass cmdiocb to state machine which needs rspiocb as well */
1837 cmdiocb->context_un.rsp_iocb = rspiocb;
1838
1839 irsp = &(rspiocb->iocb);
1840 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2e0fef85 1841 spin_lock_irq(shost->host_lock);
dea3101e 1842 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2e0fef85 1843 spin_unlock_irq(shost->host_lock);
dea3101e 1844
858c9f6c
JS
1845 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1846 "PRLI cmpl: status:x%x/x%x did:x%x",
1847 irsp->ulpStatus, irsp->un.ulpWord[4],
1848 ndlp->nlp_DID);
dea3101e 1849 /* PRLI completes to NPort <nlp_DID> */
e8b62011
JS
1850 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1851 "0103 PRLI completes to NPort x%x "
1852 "Data: x%x x%x x%x x%x\n",
1853 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1854 irsp->ulpTimeout, vport->num_disc_nodes);
dea3101e 1855
2e0fef85 1856 vport->fc_prli_sent--;
dea3101e 1857 /* Check to see if link went down during discovery */
2e0fef85 1858 if (lpfc_els_chk_latt(vport))
dea3101e
JB
1859 goto out;
1860
1861 if (irsp->ulpStatus) {
1862 /* Check for retry */
1863 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1864 /* ELS command is being retried */
1865 goto out;
1866 }
1867 /* PRLI failed */
e40a02c1
JS
1868 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1869 "2754 PRLI failure DID:%06X Status:x%x/x%x\n",
1870 ndlp->nlp_DID, irsp->ulpStatus,
1871 irsp->un.ulpWord[4]);
dea3101e 1872 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 1873 if (lpfc_error_lost_link(irsp))
dea3101e 1874 goto out;
e47c9093 1875 else
2e0fef85 1876 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 1877 NLP_EVT_CMPL_PRLI);
e47c9093 1878 } else
dea3101e 1879 /* Good status, call state machine */
2e0fef85 1880 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 1881 NLP_EVT_CMPL_PRLI);
dea3101e
JB
1882out:
1883 lpfc_els_free_iocb(phba, cmdiocb);
1884 return;
1885}
1886
e59058c4 1887/**
3621a710 1888 * lpfc_issue_els_prli - Issue a prli iocb command for a vport
e59058c4
JS
1889 * @vport: pointer to a host virtual N_Port data structure.
1890 * @ndlp: pointer to a node-list data structure.
1891 * @retry: number of retries to the command IOCB.
1892 *
1893 * This routine issues a Process Login (PRLI) ELS command for the
1894 * @vport. The PRLI service parameters are set up in the payload of the
1895 * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1896 * is put to the IOCB completion callback func field before invoking the
1897 * routine lpfc_sli_issue_iocb() to send out PRLI command.
1898 *
1899 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1900 * will be incremented by 1 for holding the ndlp and the reference to ndlp
1901 * will be stored into the context1 field of the IOCB for the completion
1902 * callback function to the PRLI ELS command.
1903 *
1904 * Return code
1905 * 0 - successfully issued prli iocb command for @vport
1906 * 1 - failed to issue prli iocb command for @vport
1907 **/
dea3101e 1908int
2e0fef85 1909lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
1910 uint8_t retry)
1911{
2e0fef85
JS
1912 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1913 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
1914 PRLI *npr;
1915 IOCB_t *icmd;
1916 struct lpfc_iocbq *elsiocb;
dea3101e
JB
1917 uint8_t *pcmd;
1918 uint16_t cmdsize;
1919
92d7f7b0 1920 cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2e0fef85
JS
1921 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1922 ndlp->nlp_DID, ELS_CMD_PRLI);
488d1469 1923 if (!elsiocb)
c9f8735b 1924 return 1;
dea3101e
JB
1925
1926 icmd = &elsiocb->iocb;
1927 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1928
1929 /* For PRLI request, remainder of payload is service parameters */
92d7f7b0 1930 memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
dea3101e 1931 *((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
92d7f7b0 1932 pcmd += sizeof(uint32_t);
dea3101e
JB
1933
1934 /* For PRLI, remainder of payload is PRLI parameter page */
1935 npr = (PRLI *) pcmd;
1936 /*
1937 * If our firmware version is 3.20 or later,
1938 * set the following bits for FC-TAPE support.
1939 */
1940 if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1941 npr->ConfmComplAllowed = 1;
1942 npr->Retry = 1;
1943 npr->TaskRetryIdReq = 1;
1944 }
1945 npr->estabImagePair = 1;
1946 npr->readXferRdyDis = 1;
1947
1948 /* For FCP support */
1949 npr->prliType = PRLI_FCP_TYPE;
1950 npr->initiatorFunc = 1;
1951
858c9f6c
JS
1952 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1953 "Issue PRLI: did:x%x",
1954 ndlp->nlp_DID, 0, 0);
1955
dea3101e
JB
1956 phba->fc_stat.elsXmitPRLI++;
1957 elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2e0fef85 1958 spin_lock_irq(shost->host_lock);
dea3101e 1959 ndlp->nlp_flag |= NLP_PRLI_SND;
2e0fef85 1960 spin_unlock_irq(shost->host_lock);
3772a991
JS
1961 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
1962 IOCB_ERROR) {
2e0fef85 1963 spin_lock_irq(shost->host_lock);
dea3101e 1964 ndlp->nlp_flag &= ~NLP_PRLI_SND;
2e0fef85 1965 spin_unlock_irq(shost->host_lock);
dea3101e 1966 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 1967 return 1;
dea3101e 1968 }
2e0fef85 1969 vport->fc_prli_sent++;
c9f8735b 1970 return 0;
dea3101e
JB
1971}
1972
90160e01 1973/**
3621a710 1974 * lpfc_rscn_disc - Perform rscn discovery for a vport
90160e01
JS
1975 * @vport: pointer to a host virtual N_Port data structure.
1976 *
1977 * This routine performs Registration State Change Notification (RSCN)
1978 * discovery for a @vport. If the @vport's node port recovery count is not
1979 * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1980 * the nodes that need recovery. If none of the PLOGI were needed through
1981 * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1982 * invoked to check and handle possible more RSCN came in during the period
1983 * of processing the current ones.
1984 **/
1985static void
1986lpfc_rscn_disc(struct lpfc_vport *vport)
1987{
1988 lpfc_can_disctmo(vport);
1989
1990 /* RSCN discovery */
1991 /* go thru NPR nodes and issue ELS PLOGIs */
1992 if (vport->fc_npr_cnt)
1993 if (lpfc_els_disc_plogi(vport))
1994 return;
1995
1996 lpfc_end_rscn(vport);
1997}
1998
1999/**
3621a710 2000 * lpfc_adisc_done - Complete the adisc phase of discovery
90160e01
JS
2001 * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2002 *
2003 * This function is called when the final ADISC is completed during discovery.
2004 * This function handles clearing link attention or issuing reg_vpi depending
2005 * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2006 * discovery.
2007 * This function is called with no locks held.
2008 **/
2009static void
2010lpfc_adisc_done(struct lpfc_vport *vport)
2011{
2012 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2013 struct lpfc_hba *phba = vport->phba;
2014
2015 /*
2016 * For NPIV, cmpl_reg_vpi will set port_state to READY,
2017 * and continue discovery.
2018 */
2019 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6fb120a7
JS
2020 !(vport->fc_flag & FC_RSCN_MODE) &&
2021 (phba->sli_rev < LPFC_SLI_REV4)) {
90160e01
JS
2022 lpfc_issue_reg_vpi(phba, vport);
2023 return;
2024 }
2025 /*
2026 * For SLI2, we need to set port_state to READY
2027 * and continue discovery.
2028 */
2029 if (vport->port_state < LPFC_VPORT_READY) {
2030 /* If we get here, there is nothing to ADISC */
2031 if (vport->port_type == LPFC_PHYSICAL_PORT)
2032 lpfc_issue_clear_la(phba, vport);
2033 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2034 vport->num_disc_nodes = 0;
2035 /* go thru NPR list, issue ELS PLOGIs */
2036 if (vport->fc_npr_cnt)
2037 lpfc_els_disc_plogi(vport);
2038 if (!vport->num_disc_nodes) {
2039 spin_lock_irq(shost->host_lock);
2040 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2041 spin_unlock_irq(shost->host_lock);
2042 lpfc_can_disctmo(vport);
2043 lpfc_end_rscn(vport);
2044 }
2045 }
2046 vport->port_state = LPFC_VPORT_READY;
2047 } else
2048 lpfc_rscn_disc(vport);
2049}
2050
e59058c4 2051/**
3621a710 2052 * lpfc_more_adisc - Issue more adisc as needed
e59058c4
JS
2053 * @vport: pointer to a host virtual N_Port data structure.
2054 *
2055 * This routine determines whether there are more ndlps on a @vport
2056 * node list need to have Address Discover (ADISC) issued. If so, it will
2057 * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2058 * remaining nodes which need to have ADISC sent.
2059 **/
0ff10d46 2060void
2e0fef85 2061lpfc_more_adisc(struct lpfc_vport *vport)
dea3101e
JB
2062{
2063 int sentadisc;
2064
2e0fef85
JS
2065 if (vport->num_disc_nodes)
2066 vport->num_disc_nodes--;
dea3101e 2067 /* Continue discovery with <num_disc_nodes> ADISCs to go */
e8b62011
JS
2068 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2069 "0210 Continue discovery with %d ADISCs to go "
2070 "Data: x%x x%x x%x\n",
2071 vport->num_disc_nodes, vport->fc_adisc_cnt,
2072 vport->fc_flag, vport->port_state);
dea3101e 2073 /* Check to see if there are more ADISCs to be sent */
2e0fef85
JS
2074 if (vport->fc_flag & FC_NLP_MORE) {
2075 lpfc_set_disctmo(vport);
2076 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2077 sentadisc = lpfc_els_disc_adisc(vport);
dea3101e 2078 }
90160e01
JS
2079 if (!vport->num_disc_nodes)
2080 lpfc_adisc_done(vport);
dea3101e
JB
2081 return;
2082}
2083
e59058c4 2084/**
3621a710 2085 * lpfc_cmpl_els_adisc - Completion callback function for adisc
e59058c4
JS
2086 * @phba: pointer to lpfc hba data structure.
2087 * @cmdiocb: pointer to lpfc command iocb data structure.
2088 * @rspiocb: pointer to lpfc response iocb data structure.
2089 *
2090 * This routine is the completion function for issuing the Address Discover
2091 * (ADISC) command. It first checks to see whether link went down during
2092 * the discovery process. If so, the node will be marked as node port
2093 * recovery for issuing discover IOCB by the link attention handler and
2094 * exit. Otherwise, the response status is checked. If error was reported
2095 * in the response status, the ADISC command shall be retried by invoking
2096 * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2097 * the response status, the state machine is invoked to set transition
2098 * with respect to NLP_EVT_CMPL_ADISC event.
2099 **/
dea3101e 2100static void
2e0fef85
JS
2101lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2102 struct lpfc_iocbq *rspiocb)
dea3101e 2103{
2e0fef85
JS
2104 struct lpfc_vport *vport = cmdiocb->vport;
2105 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2106 IOCB_t *irsp;
dea3101e 2107 struct lpfc_nodelist *ndlp;
2e0fef85 2108 int disc;
dea3101e
JB
2109
2110 /* we pass cmdiocb to state machine which needs rspiocb as well */
2111 cmdiocb->context_un.rsp_iocb = rspiocb;
2112
2113 irsp = &(rspiocb->iocb);
2114 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
dea3101e 2115
858c9f6c
JS
2116 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2117 "ADISC cmpl: status:x%x/x%x did:x%x",
2118 irsp->ulpStatus, irsp->un.ulpWord[4],
2119 ndlp->nlp_DID);
2120
dea3101e
JB
2121 /* Since ndlp can be freed in the disc state machine, note if this node
2122 * is being used during discovery.
2123 */
2e0fef85 2124 spin_lock_irq(shost->host_lock);
dea3101e 2125 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
c9f8735b 2126 ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2e0fef85 2127 spin_unlock_irq(shost->host_lock);
dea3101e 2128 /* ADISC completes to NPort <nlp_DID> */
e8b62011
JS
2129 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2130 "0104 ADISC completes to NPort x%x "
2131 "Data: x%x x%x x%x x%x x%x\n",
2132 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2133 irsp->ulpTimeout, disc, vport->num_disc_nodes);
dea3101e 2134 /* Check to see if link went down during discovery */
2e0fef85
JS
2135 if (lpfc_els_chk_latt(vport)) {
2136 spin_lock_irq(shost->host_lock);
dea3101e 2137 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 2138 spin_unlock_irq(shost->host_lock);
dea3101e
JB
2139 goto out;
2140 }
2141
2142 if (irsp->ulpStatus) {
2143 /* Check for retry */
2144 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2145 /* ELS command is being retried */
2146 if (disc) {
2e0fef85 2147 spin_lock_irq(shost->host_lock);
dea3101e 2148 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85
JS
2149 spin_unlock_irq(shost->host_lock);
2150 lpfc_set_disctmo(vport);
dea3101e
JB
2151 }
2152 goto out;
2153 }
2154 /* ADISC failed */
e40a02c1
JS
2155 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2156 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2157 ndlp->nlp_DID, irsp->ulpStatus,
2158 irsp->un.ulpWord[4]);
dea3101e 2159 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 2160 if (!lpfc_error_lost_link(irsp))
2e0fef85 2161 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
858c9f6c 2162 NLP_EVT_CMPL_ADISC);
e47c9093 2163 } else
dea3101e 2164 /* Good status, call state machine */
2e0fef85 2165 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
dea3101e 2166 NLP_EVT_CMPL_ADISC);
dea3101e 2167
90160e01
JS
2168 /* Check to see if there are more ADISCs to be sent */
2169 if (disc && vport->num_disc_nodes)
2e0fef85 2170 lpfc_more_adisc(vport);
dea3101e
JB
2171out:
2172 lpfc_els_free_iocb(phba, cmdiocb);
2173 return;
2174}
2175
e59058c4 2176/**
3621a710 2177 * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
e59058c4
JS
2178 * @vport: pointer to a virtual N_Port data structure.
2179 * @ndlp: pointer to a node-list data structure.
2180 * @retry: number of retries to the command IOCB.
2181 *
2182 * This routine issues an Address Discover (ADISC) for an @ndlp on a
2183 * @vport. It prepares the payload of the ADISC ELS command, updates the
2184 * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2185 * to issue the ADISC ELS command.
2186 *
2187 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2188 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2189 * will be stored into the context1 field of the IOCB for the completion
2190 * callback function to the ADISC ELS command.
2191 *
2192 * Return code
2193 * 0 - successfully issued adisc
2194 * 1 - failed to issue adisc
2195 **/
dea3101e 2196int
2e0fef85 2197lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
2198 uint8_t retry)
2199{
2e0fef85
JS
2200 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2201 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
2202 ADISC *ap;
2203 IOCB_t *icmd;
2204 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2205 uint8_t *pcmd;
2206 uint16_t cmdsize;
2207
92d7f7b0 2208 cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2e0fef85
JS
2209 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2210 ndlp->nlp_DID, ELS_CMD_ADISC);
488d1469 2211 if (!elsiocb)
c9f8735b 2212 return 1;
dea3101e
JB
2213
2214 icmd = &elsiocb->iocb;
2215 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2216
2217 /* For ADISC request, remainder of payload is service parameters */
2218 *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
92d7f7b0 2219 pcmd += sizeof(uint32_t);
dea3101e
JB
2220
2221 /* Fill in ADISC payload */
2222 ap = (ADISC *) pcmd;
2223 ap->hardAL_PA = phba->fc_pref_ALPA;
92d7f7b0
JS
2224 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2225 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 2226 ap->DID = be32_to_cpu(vport->fc_myDID);
dea3101e 2227
858c9f6c
JS
2228 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2229 "Issue ADISC: did:x%x",
2230 ndlp->nlp_DID, 0, 0);
2231
dea3101e
JB
2232 phba->fc_stat.elsXmitADISC++;
2233 elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2e0fef85 2234 spin_lock_irq(shost->host_lock);
dea3101e 2235 ndlp->nlp_flag |= NLP_ADISC_SND;
2e0fef85 2236 spin_unlock_irq(shost->host_lock);
3772a991
JS
2237 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2238 IOCB_ERROR) {
2e0fef85 2239 spin_lock_irq(shost->host_lock);
dea3101e 2240 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2e0fef85 2241 spin_unlock_irq(shost->host_lock);
dea3101e 2242 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2243 return 1;
dea3101e 2244 }
c9f8735b 2245 return 0;
dea3101e
JB
2246}
2247
e59058c4 2248/**
3621a710 2249 * lpfc_cmpl_els_logo - Completion callback function for logo
e59058c4
JS
2250 * @phba: pointer to lpfc hba data structure.
2251 * @cmdiocb: pointer to lpfc command iocb data structure.
2252 * @rspiocb: pointer to lpfc response iocb data structure.
2253 *
2254 * This routine is the completion function for issuing the ELS Logout (LOGO)
2255 * command. If no error status was reported from the LOGO response, the
2256 * state machine of the associated ndlp shall be invoked for transition with
2257 * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2258 * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2259 **/
dea3101e 2260static void
2e0fef85
JS
2261lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2262 struct lpfc_iocbq *rspiocb)
dea3101e 2263{
2e0fef85
JS
2264 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2265 struct lpfc_vport *vport = ndlp->vport;
2266 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
2267 IOCB_t *irsp;
2268 struct lpfc_sli *psli;
92494144 2269 struct lpfcMboxq *mbox;
dea3101e
JB
2270
2271 psli = &phba->sli;
2272 /* we pass cmdiocb to state machine which needs rspiocb as well */
2273 cmdiocb->context_un.rsp_iocb = rspiocb;
2274
2275 irsp = &(rspiocb->iocb);
2e0fef85 2276 spin_lock_irq(shost->host_lock);
dea3101e 2277 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2e0fef85 2278 spin_unlock_irq(shost->host_lock);
dea3101e 2279
858c9f6c
JS
2280 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2281 "LOGO cmpl: status:x%x/x%x did:x%x",
2282 irsp->ulpStatus, irsp->un.ulpWord[4],
2283 ndlp->nlp_DID);
dea3101e 2284 /* LOGO completes to NPort <nlp_DID> */
e8b62011
JS
2285 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2286 "0105 LOGO completes to NPort x%x "
2287 "Data: x%x x%x x%x x%x\n",
2288 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2289 irsp->ulpTimeout, vport->num_disc_nodes);
dea3101e 2290 /* Check to see if link went down during discovery */
2e0fef85 2291 if (lpfc_els_chk_latt(vport))
dea3101e
JB
2292 goto out;
2293
92d7f7b0
JS
2294 if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2295 /* NLP_EVT_DEVICE_RM should unregister the RPI
2296 * which should abort all outstanding IOs.
2297 */
2298 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2299 NLP_EVT_DEVICE_RM);
2300 goto out;
2301 }
2302
dea3101e
JB
2303 if (irsp->ulpStatus) {
2304 /* Check for retry */
2e0fef85 2305 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
dea3101e
JB
2306 /* ELS command is being retried */
2307 goto out;
dea3101e 2308 /* LOGO failed */
e40a02c1
JS
2309 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2310 "2756 LOGO failure DID:%06X Status:x%x/x%x\n",
2311 ndlp->nlp_DID, irsp->ulpStatus,
2312 irsp->un.ulpWord[4]);
dea3101e 2313 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
858c9f6c 2314 if (lpfc_error_lost_link(irsp))
dea3101e 2315 goto out;
858c9f6c 2316 else
2e0fef85 2317 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 2318 NLP_EVT_CMPL_LOGO);
e47c9093 2319 } else
5024ab17
JW
2320 /* Good status, call state machine.
2321 * This will unregister the rpi if needed.
2322 */
2e0fef85 2323 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
92d7f7b0 2324 NLP_EVT_CMPL_LOGO);
dea3101e
JB
2325out:
2326 lpfc_els_free_iocb(phba, cmdiocb);
92494144
JS
2327 /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2328 if ((vport->fc_flag & FC_PT2PT) &&
2329 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2330 phba->pport->fc_myDID = 0;
2331 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2332 if (mbox) {
2333 lpfc_config_link(phba, mbox);
2334 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2335 mbox->vport = vport;
2336 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2337 MBX_NOT_FINISHED) {
2338 mempool_free(mbox, phba->mbox_mem_pool);
2339 }
2340 }
2341 }
dea3101e
JB
2342 return;
2343}
2344
e59058c4 2345/**
3621a710 2346 * lpfc_issue_els_logo - Issue a logo to an node on a vport
e59058c4
JS
2347 * @vport: pointer to a virtual N_Port data structure.
2348 * @ndlp: pointer to a node-list data structure.
2349 * @retry: number of retries to the command IOCB.
2350 *
2351 * This routine constructs and issues an ELS Logout (LOGO) iocb command
2352 * to a remote node, referred by an @ndlp on a @vport. It constructs the
2353 * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2354 * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2355 *
2356 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2357 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2358 * will be stored into the context1 field of the IOCB for the completion
2359 * callback function to the LOGO ELS command.
2360 *
2361 * Return code
2362 * 0 - successfully issued logo
2363 * 1 - failed to issue logo
2364 **/
dea3101e 2365int
2e0fef85 2366lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e
JB
2367 uint8_t retry)
2368{
2e0fef85
JS
2369 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2370 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
2371 IOCB_t *icmd;
2372 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2373 uint8_t *pcmd;
2374 uint16_t cmdsize;
92d7f7b0 2375 int rc;
dea3101e 2376
98c9ea5c
JS
2377 spin_lock_irq(shost->host_lock);
2378 if (ndlp->nlp_flag & NLP_LOGO_SND) {
2379 spin_unlock_irq(shost->host_lock);
2380 return 0;
2381 }
2382 spin_unlock_irq(shost->host_lock);
2383
92d7f7b0 2384 cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2e0fef85
JS
2385 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2386 ndlp->nlp_DID, ELS_CMD_LOGO);
488d1469 2387 if (!elsiocb)
c9f8735b 2388 return 1;
dea3101e
JB
2389
2390 icmd = &elsiocb->iocb;
2391 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2392 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
92d7f7b0 2393 pcmd += sizeof(uint32_t);
dea3101e
JB
2394
2395 /* Fill in LOGO payload */
2e0fef85 2396 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
92d7f7b0
JS
2397 pcmd += sizeof(uint32_t);
2398 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
dea3101e 2399
858c9f6c
JS
2400 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2401 "Issue LOGO: did:x%x",
2402 ndlp->nlp_DID, 0, 0);
2403
dea3101e
JB
2404 phba->fc_stat.elsXmitLOGO++;
2405 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2e0fef85 2406 spin_lock_irq(shost->host_lock);
dea3101e 2407 ndlp->nlp_flag |= NLP_LOGO_SND;
2e0fef85 2408 spin_unlock_irq(shost->host_lock);
3772a991 2409 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
92d7f7b0
JS
2410
2411 if (rc == IOCB_ERROR) {
2e0fef85 2412 spin_lock_irq(shost->host_lock);
dea3101e 2413 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2e0fef85 2414 spin_unlock_irq(shost->host_lock);
dea3101e 2415 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2416 return 1;
dea3101e 2417 }
c9f8735b 2418 return 0;
dea3101e
JB
2419}
2420
e59058c4 2421/**
3621a710 2422 * lpfc_cmpl_els_cmd - Completion callback function for generic els command
e59058c4
JS
2423 * @phba: pointer to lpfc hba data structure.
2424 * @cmdiocb: pointer to lpfc command iocb data structure.
2425 * @rspiocb: pointer to lpfc response iocb data structure.
2426 *
2427 * This routine is a generic completion callback function for ELS commands.
2428 * Specifically, it is the callback function which does not need to perform
2429 * any command specific operations. It is currently used by the ELS command
2430 * issuing routines for the ELS State Change Request (SCR),
2431 * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2432 * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2433 * certain debug loggings, this callback function simply invokes the
2434 * lpfc_els_chk_latt() routine to check whether link went down during the
2435 * discovery process.
2436 **/
dea3101e 2437static void
2e0fef85
JS
2438lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2439 struct lpfc_iocbq *rspiocb)
dea3101e 2440{
2e0fef85 2441 struct lpfc_vport *vport = cmdiocb->vport;
dea3101e
JB
2442 IOCB_t *irsp;
2443
2444 irsp = &rspiocb->iocb;
2445
858c9f6c
JS
2446 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2447 "ELS cmd cmpl: status:x%x/x%x did:x%x",
2448 irsp->ulpStatus, irsp->un.ulpWord[4],
2449 irsp->un.elsreq64.remoteID);
dea3101e 2450 /* ELS cmd tag <ulpIoTag> completes */
e8b62011
JS
2451 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2452 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2453 irsp->ulpIoTag, irsp->ulpStatus,
2454 irsp->un.ulpWord[4], irsp->ulpTimeout);
dea3101e 2455 /* Check to see if link went down during discovery */
2e0fef85 2456 lpfc_els_chk_latt(vport);
dea3101e
JB
2457 lpfc_els_free_iocb(phba, cmdiocb);
2458 return;
2459}
2460
e59058c4 2461/**
3621a710 2462 * lpfc_issue_els_scr - Issue a scr to an node on a vport
e59058c4
JS
2463 * @vport: pointer to a host virtual N_Port data structure.
2464 * @nportid: N_Port identifier to the remote node.
2465 * @retry: number of retries to the command IOCB.
2466 *
2467 * This routine issues a State Change Request (SCR) to a fabric node
2468 * on a @vport. The remote node @nportid is passed into the function. It
2469 * first search the @vport node list to find the matching ndlp. If no such
2470 * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2471 * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2472 * routine is invoked to send the SCR IOCB.
2473 *
2474 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2475 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2476 * will be stored into the context1 field of the IOCB for the completion
2477 * callback function to the SCR ELS command.
2478 *
2479 * Return code
2480 * 0 - Successfully issued scr command
2481 * 1 - Failed to issue scr command
2482 **/
dea3101e 2483int
2e0fef85 2484lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
dea3101e 2485{
2e0fef85 2486 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
2487 IOCB_t *icmd;
2488 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2489 struct lpfc_sli *psli;
2490 uint8_t *pcmd;
2491 uint16_t cmdsize;
2492 struct lpfc_nodelist *ndlp;
2493
2494 psli = &phba->sli;
92d7f7b0 2495 cmdsize = (sizeof(uint32_t) + sizeof(SCR));
dea3101e 2496
e47c9093
JS
2497 ndlp = lpfc_findnode_did(vport, nportid);
2498 if (!ndlp) {
2499 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2500 if (!ndlp)
2501 return 1;
2502 lpfc_nlp_init(vport, ndlp, nportid);
2503 lpfc_enqueue_node(vport, ndlp);
2504 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2505 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2506 if (!ndlp)
2507 return 1;
2508 }
2e0fef85
JS
2509
2510 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2511 ndlp->nlp_DID, ELS_CMD_SCR);
dea3101e 2512
488d1469 2513 if (!elsiocb) {
fa4066b6
JS
2514 /* This will trigger the release of the node just
2515 * allocated
2516 */
329f9bc7 2517 lpfc_nlp_put(ndlp);
c9f8735b 2518 return 1;
dea3101e
JB
2519 }
2520
2521 icmd = &elsiocb->iocb;
2522 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2523
2524 *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
92d7f7b0 2525 pcmd += sizeof(uint32_t);
dea3101e
JB
2526
2527 /* For SCR, remainder of payload is SCR parameter page */
92d7f7b0 2528 memset(pcmd, 0, sizeof(SCR));
dea3101e
JB
2529 ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2530
858c9f6c
JS
2531 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2532 "Issue SCR: did:x%x",
2533 ndlp->nlp_DID, 0, 0);
2534
dea3101e
JB
2535 phba->fc_stat.elsXmitSCR++;
2536 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3772a991
JS
2537 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2538 IOCB_ERROR) {
fa4066b6
JS
2539 /* The additional lpfc_nlp_put will cause the following
2540 * lpfc_els_free_iocb routine to trigger the rlease of
2541 * the node.
2542 */
329f9bc7 2543 lpfc_nlp_put(ndlp);
dea3101e 2544 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2545 return 1;
dea3101e 2546 }
fa4066b6
JS
2547 /* This will cause the callback-function lpfc_cmpl_els_cmd to
2548 * trigger the release of node.
2549 */
329f9bc7 2550 lpfc_nlp_put(ndlp);
c9f8735b 2551 return 0;
dea3101e
JB
2552}
2553
e59058c4 2554/**
3621a710 2555 * lpfc_issue_els_farpr - Issue a farp to an node on a vport
e59058c4
JS
2556 * @vport: pointer to a host virtual N_Port data structure.
2557 * @nportid: N_Port identifier to the remote node.
2558 * @retry: number of retries to the command IOCB.
2559 *
2560 * This routine issues a Fibre Channel Address Resolution Response
2561 * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2562 * is passed into the function. It first search the @vport node list to find
2563 * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2564 * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2565 * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2566 *
2567 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2568 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2569 * will be stored into the context1 field of the IOCB for the completion
2570 * callback function to the PARPR ELS command.
2571 *
2572 * Return code
2573 * 0 - Successfully issued farpr command
2574 * 1 - Failed to issue farpr command
2575 **/
dea3101e 2576static int
2e0fef85 2577lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
dea3101e 2578{
2e0fef85 2579 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
2580 IOCB_t *icmd;
2581 struct lpfc_iocbq *elsiocb;
dea3101e
JB
2582 struct lpfc_sli *psli;
2583 FARP *fp;
2584 uint8_t *pcmd;
2585 uint32_t *lp;
2586 uint16_t cmdsize;
2587 struct lpfc_nodelist *ondlp;
2588 struct lpfc_nodelist *ndlp;
2589
2590 psli = &phba->sli;
92d7f7b0 2591 cmdsize = (sizeof(uint32_t) + sizeof(FARP));
dea3101e 2592
e47c9093
JS
2593 ndlp = lpfc_findnode_did(vport, nportid);
2594 if (!ndlp) {
2595 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2596 if (!ndlp)
2597 return 1;
2598 lpfc_nlp_init(vport, ndlp, nportid);
2599 lpfc_enqueue_node(vport, ndlp);
2600 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2601 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2602 if (!ndlp)
2603 return 1;
2604 }
2e0fef85
JS
2605
2606 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2607 ndlp->nlp_DID, ELS_CMD_RNID);
488d1469 2608 if (!elsiocb) {
fa4066b6
JS
2609 /* This will trigger the release of the node just
2610 * allocated
2611 */
329f9bc7 2612 lpfc_nlp_put(ndlp);
c9f8735b 2613 return 1;
dea3101e
JB
2614 }
2615
2616 icmd = &elsiocb->iocb;
2617 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2618
2619 *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
92d7f7b0 2620 pcmd += sizeof(uint32_t);
dea3101e
JB
2621
2622 /* Fill in FARPR payload */
2623 fp = (FARP *) (pcmd);
92d7f7b0 2624 memset(fp, 0, sizeof(FARP));
dea3101e
JB
2625 lp = (uint32_t *) pcmd;
2626 *lp++ = be32_to_cpu(nportid);
2e0fef85 2627 *lp++ = be32_to_cpu(vport->fc_myDID);
dea3101e
JB
2628 fp->Rflags = 0;
2629 fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2630
92d7f7b0
JS
2631 memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2632 memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 2633 ondlp = lpfc_findnode_did(vport, nportid);
e47c9093 2634 if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
dea3101e 2635 memcpy(&fp->OportName, &ondlp->nlp_portname,
92d7f7b0 2636 sizeof(struct lpfc_name));
dea3101e 2637 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
92d7f7b0 2638 sizeof(struct lpfc_name));
dea3101e
JB
2639 }
2640
858c9f6c
JS
2641 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2642 "Issue FARPR: did:x%x",
2643 ndlp->nlp_DID, 0, 0);
2644
dea3101e
JB
2645 phba->fc_stat.elsXmitFARPR++;
2646 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3772a991
JS
2647 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2648 IOCB_ERROR) {
fa4066b6
JS
2649 /* The additional lpfc_nlp_put will cause the following
2650 * lpfc_els_free_iocb routine to trigger the release of
2651 * the node.
2652 */
329f9bc7 2653 lpfc_nlp_put(ndlp);
dea3101e 2654 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2655 return 1;
dea3101e 2656 }
fa4066b6
JS
2657 /* This will cause the callback-function lpfc_cmpl_els_cmd to
2658 * trigger the release of the node.
2659 */
329f9bc7 2660 lpfc_nlp_put(ndlp);
c9f8735b 2661 return 0;
dea3101e
JB
2662}
2663
e59058c4 2664/**
3621a710 2665 * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
e59058c4
JS
2666 * @vport: pointer to a host virtual N_Port data structure.
2667 * @nlp: pointer to a node-list data structure.
2668 *
2669 * This routine cancels the timer with a delayed IOCB-command retry for
2670 * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2671 * removes the ELS retry event if it presents. In addition, if the
2672 * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2673 * commands are sent for the @vport's nodes that require issuing discovery
2674 * ADISC.
2675 **/
fdcebe28 2676void
2e0fef85 2677lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
fdcebe28 2678{
2e0fef85 2679 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
e47c9093 2680 struct lpfc_work_evt *evtp;
2e0fef85 2681
0d2b6b83
JS
2682 if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2683 return;
2e0fef85 2684 spin_lock_irq(shost->host_lock);
fdcebe28 2685 nlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 2686 spin_unlock_irq(shost->host_lock);
fdcebe28
JS
2687 del_timer_sync(&nlp->nlp_delayfunc);
2688 nlp->nlp_last_elscmd = 0;
e47c9093 2689 if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
fdcebe28 2690 list_del_init(&nlp->els_retry_evt.evt_listp);
e47c9093
JS
2691 /* Decrement nlp reference count held for the delayed retry */
2692 evtp = &nlp->els_retry_evt;
2693 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2694 }
fdcebe28 2695 if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2e0fef85 2696 spin_lock_irq(shost->host_lock);
fdcebe28 2697 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2e0fef85
JS
2698 spin_unlock_irq(shost->host_lock);
2699 if (vport->num_disc_nodes) {
0d2b6b83
JS
2700 if (vport->port_state < LPFC_VPORT_READY) {
2701 /* Check if there are more ADISCs to be sent */
2702 lpfc_more_adisc(vport);
0d2b6b83
JS
2703 } else {
2704 /* Check if there are more PLOGIs to be sent */
2705 lpfc_more_plogi(vport);
90160e01
JS
2706 if (vport->num_disc_nodes == 0) {
2707 spin_lock_irq(shost->host_lock);
2708 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2709 spin_unlock_irq(shost->host_lock);
2710 lpfc_can_disctmo(vport);
2711 lpfc_end_rscn(vport);
2712 }
fdcebe28
JS
2713 }
2714 }
2715 }
2716 return;
2717}
2718
e59058c4 2719/**
3621a710 2720 * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
e59058c4
JS
2721 * @ptr: holder for the pointer to the timer function associated data (ndlp).
2722 *
2723 * This routine is invoked by the ndlp delayed-function timer to check
2724 * whether there is any pending ELS retry event(s) with the node. If not, it
2725 * simply returns. Otherwise, if there is at least one ELS delayed event, it
2726 * adds the delayed events to the HBA work list and invokes the
2727 * lpfc_worker_wake_up() routine to wake up worker thread to process the
2728 * event. Note that lpfc_nlp_get() is called before posting the event to
2729 * the work list to hold reference count of ndlp so that it guarantees the
2730 * reference to ndlp will still be available when the worker thread gets
2731 * to the event associated with the ndlp.
2732 **/
dea3101e
JB
2733void
2734lpfc_els_retry_delay(unsigned long ptr)
2735{
2e0fef85
JS
2736 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2737 struct lpfc_vport *vport = ndlp->vport;
2e0fef85 2738 struct lpfc_hba *phba = vport->phba;
92d7f7b0 2739 unsigned long flags;
2e0fef85 2740 struct lpfc_work_evt *evtp = &ndlp->els_retry_evt;
dea3101e 2741
92d7f7b0 2742 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 2743 if (!list_empty(&evtp->evt_listp)) {
92d7f7b0 2744 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e
JB
2745 return;
2746 }
2747
fa4066b6
JS
2748 /* We need to hold the node by incrementing the reference
2749 * count until the queued work is done
2750 */
2751 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
5e9d9b82
JS
2752 if (evtp->evt_arg1) {
2753 evtp->evt = LPFC_EVT_ELS_RETRY;
2754 list_add_tail(&evtp->evt_listp, &phba->work_list);
92d7f7b0 2755 lpfc_worker_wake_up(phba);
5e9d9b82 2756 }
92d7f7b0 2757 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e
JB
2758 return;
2759}
2760
e59058c4 2761/**
3621a710 2762 * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
e59058c4
JS
2763 * @ndlp: pointer to a node-list data structure.
2764 *
2765 * This routine is the worker-thread handler for processing the @ndlp delayed
2766 * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2767 * the last ELS command from the associated ndlp and invokes the proper ELS
2768 * function according to the delayed ELS command to retry the command.
2769 **/
dea3101e
JB
2770void
2771lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2772{
2e0fef85
JS
2773 struct lpfc_vport *vport = ndlp->vport;
2774 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2775 uint32_t cmd, did, retry;
dea3101e 2776
2e0fef85 2777 spin_lock_irq(shost->host_lock);
5024ab17
JW
2778 did = ndlp->nlp_DID;
2779 cmd = ndlp->nlp_last_elscmd;
2780 ndlp->nlp_last_elscmd = 0;
dea3101e
JB
2781
2782 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2e0fef85 2783 spin_unlock_irq(shost->host_lock);
dea3101e
JB
2784 return;
2785 }
2786
2787 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 2788 spin_unlock_irq(shost->host_lock);
1a169689
JS
2789 /*
2790 * If a discovery event readded nlp_delayfunc after timer
2791 * firing and before processing the timer, cancel the
2792 * nlp_delayfunc.
2793 */
2794 del_timer_sync(&ndlp->nlp_delayfunc);
dea3101e 2795 retry = ndlp->nlp_retry;
4d9ab994 2796 ndlp->nlp_retry = 0;
dea3101e
JB
2797
2798 switch (cmd) {
2799 case ELS_CMD_FLOGI:
2e0fef85 2800 lpfc_issue_els_flogi(vport, ndlp, retry);
dea3101e
JB
2801 break;
2802 case ELS_CMD_PLOGI:
2e0fef85 2803 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
5024ab17 2804 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 2805 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6ad42535 2806 }
dea3101e
JB
2807 break;
2808 case ELS_CMD_ADISC:
2e0fef85 2809 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
5024ab17 2810 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 2811 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6ad42535 2812 }
dea3101e
JB
2813 break;
2814 case ELS_CMD_PRLI:
2e0fef85 2815 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
5024ab17 2816 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 2817 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
6ad42535 2818 }
dea3101e
JB
2819 break;
2820 case ELS_CMD_LOGO:
2e0fef85 2821 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
5024ab17 2822 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 2823 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6ad42535 2824 }
dea3101e 2825 break;
92d7f7b0 2826 case ELS_CMD_FDISC:
fedd3b7b
JS
2827 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
2828 lpfc_issue_els_fdisc(vport, ndlp, retry);
92d7f7b0 2829 break;
dea3101e
JB
2830 }
2831 return;
2832}
2833
e59058c4 2834/**
3621a710 2835 * lpfc_els_retry - Make retry decision on an els command iocb
e59058c4
JS
2836 * @phba: pointer to lpfc hba data structure.
2837 * @cmdiocb: pointer to lpfc command iocb data structure.
2838 * @rspiocb: pointer to lpfc response iocb data structure.
2839 *
2840 * This routine makes a retry decision on an ELS command IOCB, which has
2841 * failed. The following ELS IOCBs use this function for retrying the command
2842 * when previously issued command responsed with error status: FLOGI, PLOGI,
2843 * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2844 * returned error status, it makes the decision whether a retry shall be
2845 * issued for the command, and whether a retry shall be made immediately or
2846 * delayed. In the former case, the corresponding ELS command issuing-function
2847 * is called to retry the command. In the later case, the ELS command shall
2848 * be posted to the ndlp delayed event and delayed function timer set to the
2849 * ndlp for the delayed command issusing.
2850 *
2851 * Return code
2852 * 0 - No retry of els command is made
2853 * 1 - Immediate or delayed retry of els command is made
2854 **/
dea3101e 2855static int
2e0fef85
JS
2856lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2857 struct lpfc_iocbq *rspiocb)
dea3101e 2858{
2e0fef85
JS
2859 struct lpfc_vport *vport = cmdiocb->vport;
2860 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2861 IOCB_t *irsp = &rspiocb->iocb;
2862 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2863 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
dea3101e
JB
2864 uint32_t *elscmd;
2865 struct ls_rjt stat;
2e0fef85 2866 int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
98c9ea5c 2867 int logerr = 0;
2e0fef85 2868 uint32_t cmd = 0;
488d1469 2869 uint32_t did;
dea3101e 2870
488d1469 2871
dea3101e
JB
2872 /* Note: context2 may be 0 for internal driver abort
2873 * of delays ELS command.
2874 */
2875
2876 if (pcmd && pcmd->virt) {
2877 elscmd = (uint32_t *) (pcmd->virt);
2878 cmd = *elscmd++;
2879 }
2880
e47c9093 2881 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
488d1469
JS
2882 did = ndlp->nlp_DID;
2883 else {
2884 /* We should only hit this case for retrying PLOGI */
2885 did = irsp->un.elsreq64.remoteID;
2e0fef85 2886 ndlp = lpfc_findnode_did(vport, did);
e47c9093
JS
2887 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2888 && (cmd != ELS_CMD_PLOGI))
488d1469
JS
2889 return 1;
2890 }
2891
858c9f6c
JS
2892 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2893 "Retry ELS: wd7:x%x wd4:x%x did:x%x",
2894 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2895
dea3101e
JB
2896 switch (irsp->ulpStatus) {
2897 case IOSTAT_FCP_RSP_ERROR:
1151e3ec 2898 break;
dea3101e 2899 case IOSTAT_REMOTE_STOP:
1151e3ec
JS
2900 if (phba->sli_rev == LPFC_SLI_REV4) {
2901 /* This IO was aborted by the target, we don't
2902 * know the rxid and because we did not send the
2903 * ABTS we cannot generate and RRQ.
2904 */
2905 lpfc_set_rrq_active(phba, ndlp,
2906 cmdiocb->sli4_xritag, 0, 0);
2907 }
dea3101e 2908 break;
dea3101e
JB
2909 case IOSTAT_LOCAL_REJECT:
2910 switch ((irsp->un.ulpWord[4] & 0xff)) {
2911 case IOERR_LOOP_OPEN_FAILURE:
eaf15d5b
JS
2912 if (cmd == ELS_CMD_FLOGI) {
2913 if (PCI_DEVICE_ID_HORNET ==
2914 phba->pcidev->device) {
76a95d75 2915 phba->fc_topology = LPFC_TOPOLOGY_LOOP;
eaf15d5b
JS
2916 phba->pport->fc_myDID = 0;
2917 phba->alpa_map[0] = 0;
2918 phba->alpa_map[1] = 0;
2919 }
2920 }
2e0fef85 2921 if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
92d7f7b0 2922 delay = 1000;
dea3101e
JB
2923 retry = 1;
2924 break;
2925
92d7f7b0 2926 case IOERR_ILLEGAL_COMMAND:
7f5f3d0d
JS
2927 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2928 "0124 Retry illegal cmd x%x "
2929 "retry:x%x delay:x%x\n",
2930 cmd, cmdiocb->retry, delay);
2931 retry = 1;
2932 /* All command's retry policy */
2933 maxretry = 8;
2934 if (cmdiocb->retry > 2)
2935 delay = 1000;
92d7f7b0
JS
2936 break;
2937
dea3101e 2938 case IOERR_NO_RESOURCES:
98c9ea5c 2939 logerr = 1; /* HBA out of resources */
858c9f6c
JS
2940 retry = 1;
2941 if (cmdiocb->retry > 100)
2942 delay = 100;
2943 maxretry = 250;
2944 break;
2945
2946 case IOERR_ILLEGAL_FRAME:
92d7f7b0 2947 delay = 100;
dea3101e
JB
2948 retry = 1;
2949 break;
2950
858c9f6c 2951 case IOERR_SEQUENCE_TIMEOUT:
dea3101e
JB
2952 case IOERR_INVALID_RPI:
2953 retry = 1;
2954 break;
2955 }
2956 break;
2957
2958 case IOSTAT_NPORT_RJT:
2959 case IOSTAT_FABRIC_RJT:
2960 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2961 retry = 1;
2962 break;
2963 }
2964 break;
2965
2966 case IOSTAT_NPORT_BSY:
2967 case IOSTAT_FABRIC_BSY:
98c9ea5c 2968 logerr = 1; /* Fabric / Remote NPort out of resources */
dea3101e
JB
2969 retry = 1;
2970 break;
2971
2972 case IOSTAT_LS_RJT:
2973 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2974 /* Added for Vendor specifc support
2975 * Just keep retrying for these Rsn / Exp codes
2976 */
2977 switch (stat.un.b.lsRjtRsnCode) {
2978 case LSRJT_UNABLE_TPC:
2979 if (stat.un.b.lsRjtRsnCodeExp ==
2980 LSEXP_CMD_IN_PROGRESS) {
2981 if (cmd == ELS_CMD_PLOGI) {
92d7f7b0 2982 delay = 1000;
dea3101e
JB
2983 maxretry = 48;
2984 }
2985 retry = 1;
2986 break;
2987 }
ffc95493
JS
2988 if (stat.un.b.lsRjtRsnCodeExp ==
2989 LSEXP_CANT_GIVE_DATA) {
2990 if (cmd == ELS_CMD_PLOGI) {
2991 delay = 1000;
2992 maxretry = 48;
2993 }
2994 retry = 1;
2995 break;
2996 }
dea3101e 2997 if (cmd == ELS_CMD_PLOGI) {
92d7f7b0 2998 delay = 1000;
dea3101e
JB
2999 maxretry = lpfc_max_els_tries + 1;
3000 retry = 1;
3001 break;
3002 }
92d7f7b0
JS
3003 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3004 (cmd == ELS_CMD_FDISC) &&
3005 (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
e8b62011
JS
3006 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3007 "0125 FDISC Failed (x%x). "
3008 "Fabric out of resources\n",
3009 stat.un.lsRjtError);
92d7f7b0
JS
3010 lpfc_vport_set_state(vport,
3011 FC_VPORT_NO_FABRIC_RSCS);
3012 }
dea3101e
JB
3013 break;
3014
3015 case LSRJT_LOGICAL_BSY:
858c9f6c
JS
3016 if ((cmd == ELS_CMD_PLOGI) ||
3017 (cmd == ELS_CMD_PRLI)) {
92d7f7b0 3018 delay = 1000;
dea3101e 3019 maxretry = 48;
92d7f7b0 3020 } else if (cmd == ELS_CMD_FDISC) {
51ef4c26
JS
3021 /* FDISC retry policy */
3022 maxretry = 48;
3023 if (cmdiocb->retry >= 32)
3024 delay = 1000;
dea3101e
JB
3025 }
3026 retry = 1;
3027 break;
92d7f7b0
JS
3028
3029 case LSRJT_LOGICAL_ERR:
7f5f3d0d
JS
3030 /* There are some cases where switches return this
3031 * error when they are not ready and should be returning
3032 * Logical Busy. We should delay every time.
3033 */
3034 if (cmd == ELS_CMD_FDISC &&
3035 stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3036 maxretry = 3;
3037 delay = 1000;
3038 retry = 1;
3039 break;
3040 }
92d7f7b0
JS
3041 case LSRJT_PROTOCOL_ERR:
3042 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3043 (cmd == ELS_CMD_FDISC) &&
3044 ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3045 (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3046 ) {
e8b62011 3047 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
d7c255b2 3048 "0122 FDISC Failed (x%x). "
e8b62011
JS
3049 "Fabric Detected Bad WWN\n",
3050 stat.un.lsRjtError);
92d7f7b0
JS
3051 lpfc_vport_set_state(vport,
3052 FC_VPORT_FABRIC_REJ_WWN);
3053 }
3054 break;
dea3101e
JB
3055 }
3056 break;
3057
3058 case IOSTAT_INTERMED_RSP:
3059 case IOSTAT_BA_RJT:
3060 break;
3061
3062 default:
3063 break;
3064 }
3065
488d1469 3066 if (did == FDMI_DID)
dea3101e 3067 retry = 1;
dea3101e 3068
695a814e 3069 if (((cmd == ELS_CMD_FLOGI) || (cmd == ELS_CMD_FDISC)) &&
76a95d75 3070 (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
1b32f6aa 3071 !lpfc_error_lost_link(irsp)) {
98c9ea5c
JS
3072 /* FLOGI retry policy */
3073 retry = 1;
6669f9bb
JS
3074 /* retry forever */
3075 maxretry = 0;
3076 if (cmdiocb->retry >= 100)
3077 delay = 5000;
3078 else if (cmdiocb->retry >= 32)
98c9ea5c
JS
3079 delay = 1000;
3080 }
3081
6669f9bb
JS
3082 cmdiocb->retry++;
3083 if (maxretry && (cmdiocb->retry >= maxretry)) {
dea3101e
JB
3084 phba->fc_stat.elsRetryExceeded++;
3085 retry = 0;
3086 }
3087
ed957684
JS
3088 if ((vport->load_flag & FC_UNLOADING) != 0)
3089 retry = 0;
3090
dea3101e 3091 if (retry) {
38b92ef8
JS
3092 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3093 /* Stop retrying PLOGI and FDISC if in FCF discovery */
3094 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3095 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3096 "2849 Stop retry ELS command "
3097 "x%x to remote NPORT x%x, "
3098 "Data: x%x x%x\n", cmd, did,
3099 cmdiocb->retry, delay);
3100 return 0;
3101 }
3102 }
dea3101e
JB
3103
3104 /* Retry ELS command <elsCmd> to remote NPORT <did> */
e8b62011
JS
3105 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3106 "0107 Retry ELS command x%x to remote "
3107 "NPORT x%x Data: x%x x%x\n",
3108 cmd, did, cmdiocb->retry, delay);
dea3101e 3109
858c9f6c
JS
3110 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3111 ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
3112 ((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
3113 /* Don't reset timer for no resources */
3114
dea3101e 3115 /* If discovery / RSCN timer is running, reset it */
2e0fef85 3116 if (timer_pending(&vport->fc_disctmo) ||
92d7f7b0 3117 (vport->fc_flag & FC_RSCN_MODE))
2e0fef85 3118 lpfc_set_disctmo(vport);
dea3101e
JB
3119 }
3120
3121 phba->fc_stat.elsXmitRetry++;
58da1ffb 3122 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
dea3101e
JB
3123 phba->fc_stat.elsDelayRetry++;
3124 ndlp->nlp_retry = cmdiocb->retry;
3125
92d7f7b0
JS
3126 /* delay is specified in milliseconds */
3127 mod_timer(&ndlp->nlp_delayfunc,
3128 jiffies + msecs_to_jiffies(delay));
2e0fef85 3129 spin_lock_irq(shost->host_lock);
dea3101e 3130 ndlp->nlp_flag |= NLP_DELAY_TMO;
2e0fef85 3131 spin_unlock_irq(shost->host_lock);
dea3101e 3132
5024ab17 3133 ndlp->nlp_prev_state = ndlp->nlp_state;
858c9f6c
JS
3134 if (cmd == ELS_CMD_PRLI)
3135 lpfc_nlp_set_state(vport, ndlp,
3136 NLP_STE_REG_LOGIN_ISSUE);
3137 else
3138 lpfc_nlp_set_state(vport, ndlp,
3139 NLP_STE_NPR_NODE);
dea3101e
JB
3140 ndlp->nlp_last_elscmd = cmd;
3141
c9f8735b 3142 return 1;
dea3101e
JB
3143 }
3144 switch (cmd) {
3145 case ELS_CMD_FLOGI:
2e0fef85 3146 lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
c9f8735b 3147 return 1;
92d7f7b0
JS
3148 case ELS_CMD_FDISC:
3149 lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3150 return 1;
dea3101e 3151 case ELS_CMD_PLOGI:
58da1ffb 3152 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
488d1469 3153 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3154 lpfc_nlp_set_state(vport, ndlp,
de0c5b32 3155 NLP_STE_PLOGI_ISSUE);
488d1469 3156 }
2e0fef85 3157 lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
c9f8735b 3158 return 1;
dea3101e 3159 case ELS_CMD_ADISC:
5024ab17 3160 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3161 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3162 lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
c9f8735b 3163 return 1;
dea3101e 3164 case ELS_CMD_PRLI:
5024ab17 3165 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3166 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3167 lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
c9f8735b 3168 return 1;
dea3101e 3169 case ELS_CMD_LOGO:
5024ab17 3170 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3171 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
3172 lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
c9f8735b 3173 return 1;
dea3101e
JB
3174 }
3175 }
dea3101e 3176 /* No retry ELS command <elsCmd> to remote NPORT <did> */
98c9ea5c
JS
3177 if (logerr) {
3178 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3179 "0137 No retry ELS command x%x to remote "
3180 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
3181 cmd, did, irsp->ulpStatus,
3182 irsp->un.ulpWord[4]);
3183 }
3184 else {
3185 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
a58cbd52
JS
3186 "0108 No retry ELS command x%x to remote "
3187 "NPORT x%x Retried:%d Error:x%x/%x\n",
3188 cmd, did, cmdiocb->retry, irsp->ulpStatus,
3189 irsp->un.ulpWord[4]);
98c9ea5c 3190 }
c9f8735b 3191 return 0;
dea3101e
JB
3192}
3193
e59058c4 3194/**
3621a710 3195 * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
e59058c4
JS
3196 * @phba: pointer to lpfc hba data structure.
3197 * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3198 *
3199 * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3200 * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3201 * checks to see whether there is a lpfc DMA buffer associated with the
3202 * response of the command IOCB. If so, it will be released before releasing
3203 * the lpfc DMA buffer associated with the IOCB itself.
3204 *
3205 * Return code
3206 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3207 **/
09372820 3208static int
87af33fe
JS
3209lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3210{
3211 struct lpfc_dmabuf *buf_ptr;
3212
e59058c4 3213 /* Free the response before processing the command. */
87af33fe
JS
3214 if (!list_empty(&buf_ptr1->list)) {
3215 list_remove_head(&buf_ptr1->list, buf_ptr,
3216 struct lpfc_dmabuf,
3217 list);
3218 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3219 kfree(buf_ptr);
3220 }
3221 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3222 kfree(buf_ptr1);
3223 return 0;
3224}
3225
e59058c4 3226/**
3621a710 3227 * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
e59058c4
JS
3228 * @phba: pointer to lpfc hba data structure.
3229 * @buf_ptr: pointer to the lpfc dma buffer data structure.
3230 *
3231 * This routine releases the lpfc Direct Memory Access (DMA) buffer
3232 * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3233 * pool.
3234 *
3235 * Return code
3236 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3237 **/
09372820 3238static int
87af33fe
JS
3239lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3240{
3241 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3242 kfree(buf_ptr);
3243 return 0;
3244}
3245
e59058c4 3246/**
3621a710 3247 * lpfc_els_free_iocb - Free a command iocb and its associated resources
e59058c4
JS
3248 * @phba: pointer to lpfc hba data structure.
3249 * @elsiocb: pointer to lpfc els command iocb data structure.
3250 *
3251 * This routine frees a command IOCB and its associated resources. The
3252 * command IOCB data structure contains the reference to various associated
3253 * resources, these fields must be set to NULL if the associated reference
3254 * not present:
3255 * context1 - reference to ndlp
3256 * context2 - reference to cmd
3257 * context2->next - reference to rsp
3258 * context3 - reference to bpl
3259 *
3260 * It first properly decrements the reference count held on ndlp for the
3261 * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3262 * set, it invokes the lpfc_els_free_data() routine to release the Direct
3263 * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3264 * adds the DMA buffer the @phba data structure for the delayed release.
3265 * If reference to the Buffer Pointer List (BPL) is present, the
3266 * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3267 * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3268 * invoked to release the IOCB data structure back to @phba IOCBQ list.
3269 *
3270 * Return code
3271 * 0 - Success (currently, always return 0)
3272 **/
dea3101e 3273int
329f9bc7 3274lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
dea3101e
JB
3275{
3276 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
a8adb832
JS
3277 struct lpfc_nodelist *ndlp;
3278
3279 ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3280 if (ndlp) {
3281 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3282 lpfc_nlp_put(ndlp);
dea3101e 3283
a8adb832
JS
3284 /* If the ndlp is not being used by another discovery
3285 * thread, free it.
3286 */
3287 if (!lpfc_nlp_not_used(ndlp)) {
3288 /* If ndlp is being used by another discovery
3289 * thread, just clear NLP_DEFER_RM
3290 */
3291 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3292 }
3293 }
3294 else
3295 lpfc_nlp_put(ndlp);
329f9bc7
JS
3296 elsiocb->context1 = NULL;
3297 }
dea3101e
JB
3298 /* context2 = cmd, context2->next = rsp, context3 = bpl */
3299 if (elsiocb->context2) {
0ff10d46
JS
3300 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3301 /* Firmware could still be in progress of DMAing
3302 * payload, so don't free data buffer till after
3303 * a hbeat.
3304 */
3305 elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3306 buf_ptr = elsiocb->context2;
3307 elsiocb->context2 = NULL;
3308 if (buf_ptr) {
3309 buf_ptr1 = NULL;
3310 spin_lock_irq(&phba->hbalock);
3311 if (!list_empty(&buf_ptr->list)) {
3312 list_remove_head(&buf_ptr->list,
3313 buf_ptr1, struct lpfc_dmabuf,
3314 list);
3315 INIT_LIST_HEAD(&buf_ptr1->list);
3316 list_add_tail(&buf_ptr1->list,
3317 &phba->elsbuf);
3318 phba->elsbuf_cnt++;
3319 }
3320 INIT_LIST_HEAD(&buf_ptr->list);
3321 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3322 phba->elsbuf_cnt++;
3323 spin_unlock_irq(&phba->hbalock);
3324 }
3325 } else {
3326 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3327 lpfc_els_free_data(phba, buf_ptr1);
3328 }
dea3101e
JB
3329 }
3330
3331 if (elsiocb->context3) {
3332 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
87af33fe 3333 lpfc_els_free_bpl(phba, buf_ptr);
dea3101e 3334 }
604a3e30 3335 lpfc_sli_release_iocbq(phba, elsiocb);
dea3101e
JB
3336 return 0;
3337}
3338
e59058c4 3339/**
3621a710 3340 * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
e59058c4
JS
3341 * @phba: pointer to lpfc hba data structure.
3342 * @cmdiocb: pointer to lpfc command iocb data structure.
3343 * @rspiocb: pointer to lpfc response iocb data structure.
3344 *
3345 * This routine is the completion callback function to the Logout (LOGO)
3346 * Accept (ACC) Response ELS command. This routine is invoked to indicate
3347 * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3348 * release the ndlp if it has the last reference remaining (reference count
3349 * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3350 * field to NULL to inform the following lpfc_els_free_iocb() routine no
3351 * ndlp reference count needs to be decremented. Otherwise, the ndlp
3352 * reference use-count shall be decremented by the lpfc_els_free_iocb()
3353 * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3354 * IOCB data structure.
3355 **/
dea3101e 3356static void
2e0fef85
JS
3357lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3358 struct lpfc_iocbq *rspiocb)
dea3101e 3359{
2e0fef85
JS
3360 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3361 struct lpfc_vport *vport = cmdiocb->vport;
858c9f6c
JS
3362 IOCB_t *irsp;
3363
3364 irsp = &rspiocb->iocb;
3365 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3366 "ACC LOGO cmpl: status:x%x/x%x did:x%x",
3367 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
dea3101e 3368 /* ACC to LOGO completes to NPort <nlp_DID> */
e8b62011
JS
3369 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3370 "0109 ACC to LOGO completes to NPort x%x "
3371 "Data: x%x x%x x%x\n",
3372 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3373 ndlp->nlp_rpi);
87af33fe
JS
3374
3375 if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3376 /* NPort Recovery mode or node is just allocated */
3377 if (!lpfc_nlp_not_used(ndlp)) {
3378 /* If the ndlp is being used by another discovery
3379 * thread, just unregister the RPI.
3380 */
3381 lpfc_unreg_rpi(vport, ndlp);
fa4066b6
JS
3382 } else {
3383 /* Indicate the node has already released, should
3384 * not reference to it from within lpfc_els_free_iocb.
3385 */
3386 cmdiocb->context1 = NULL;
87af33fe 3387 }
dea3101e
JB
3388 }
3389 lpfc_els_free_iocb(phba, cmdiocb);
3390 return;
3391}
3392
e59058c4 3393/**
3621a710 3394 * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
e59058c4
JS
3395 * @phba: pointer to lpfc hba data structure.
3396 * @pmb: pointer to the driver internal queue element for mailbox command.
3397 *
3398 * This routine is the completion callback function for unregister default
3399 * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3400 * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3401 * decrements the ndlp reference count held for this completion callback
3402 * function. After that, it invokes the lpfc_nlp_not_used() to check
3403 * whether there is only one reference left on the ndlp. If so, it will
3404 * perform one more decrement and trigger the release of the ndlp.
3405 **/
858c9f6c
JS
3406void
3407lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3408{
3409 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3410 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3411
3412 pmb->context1 = NULL;
d439d286
JS
3413 pmb->context2 = NULL;
3414
858c9f6c
JS
3415 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3416 kfree(mp);
3417 mempool_free(pmb, phba->mbox_mem_pool);
58da1ffb 3418 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
a8adb832 3419 lpfc_nlp_put(ndlp);
a8adb832
JS
3420 /* This is the end of the default RPI cleanup logic for this
3421 * ndlp. If no other discovery threads are using this ndlp.
3422 * we should free all resources associated with it.
3423 */
3424 lpfc_nlp_not_used(ndlp);
3425 }
3772a991 3426
858c9f6c
JS
3427 return;
3428}
3429
e59058c4 3430/**
3621a710 3431 * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
e59058c4
JS
3432 * @phba: pointer to lpfc hba data structure.
3433 * @cmdiocb: pointer to lpfc command iocb data structure.
3434 * @rspiocb: pointer to lpfc response iocb data structure.
3435 *
3436 * This routine is the completion callback function for ELS Response IOCB
3437 * command. In normal case, this callback function just properly sets the
3438 * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3439 * field in the command IOCB is not NULL, the referred mailbox command will
3440 * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3441 * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3442 * link down event occurred during the discovery, the lpfc_nlp_not_used()
3443 * routine shall be invoked trying to release the ndlp if no other threads
3444 * are currently referring it.
3445 **/
dea3101e 3446static void
858c9f6c 3447lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
329f9bc7 3448 struct lpfc_iocbq *rspiocb)
dea3101e 3449{
2e0fef85
JS
3450 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3451 struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3452 struct Scsi_Host *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
87af33fe
JS
3453 IOCB_t *irsp;
3454 uint8_t *pcmd;
dea3101e 3455 LPFC_MBOXQ_t *mbox = NULL;
2e0fef85 3456 struct lpfc_dmabuf *mp = NULL;
87af33fe 3457 uint32_t ls_rjt = 0;
dea3101e 3458
33ccf8d1
JS
3459 irsp = &rspiocb->iocb;
3460
dea3101e
JB
3461 if (cmdiocb->context_un.mbox)
3462 mbox = cmdiocb->context_un.mbox;
3463
fa4066b6
JS
3464 /* First determine if this is a LS_RJT cmpl. Note, this callback
3465 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3466 */
87af33fe 3467 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
58da1ffb
JS
3468 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3469 (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
fa4066b6 3470 /* A LS_RJT associated with Default RPI cleanup has its own
3ad2f3fb 3471 * separate code path.
87af33fe
JS
3472 */
3473 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3474 ls_rjt = 1;
3475 }
3476
dea3101e 3477 /* Check to see if link went down during discovery */
58da1ffb 3478 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
dea3101e 3479 if (mbox) {
14691150
JS
3480 mp = (struct lpfc_dmabuf *) mbox->context1;
3481 if (mp) {
3482 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3483 kfree(mp);
3484 }
329f9bc7 3485 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 3486 }
58da1ffb
JS
3487 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3488 (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
fa4066b6 3489 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 3490 ndlp = NULL;
fa4066b6
JS
3491 /* Indicate the node has already released,
3492 * should not reference to it from within
3493 * the routine lpfc_els_free_iocb.
3494 */
3495 cmdiocb->context1 = NULL;
3496 }
dea3101e
JB
3497 goto out;
3498 }
3499
858c9f6c 3500 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
51ef4c26 3501 "ELS rsp cmpl: status:x%x/x%x did:x%x",
858c9f6c 3502 irsp->ulpStatus, irsp->un.ulpWord[4],
51ef4c26 3503 cmdiocb->iocb.un.elsreq64.remoteID);
dea3101e 3504 /* ELS response tag <ulpIoTag> completes */
e8b62011
JS
3505 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3506 "0110 ELS response tag x%x completes "
3507 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3508 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3509 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3510 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3511 ndlp->nlp_rpi);
dea3101e
JB
3512 if (mbox) {
3513 if ((rspiocb->iocb.ulpStatus == 0)
3514 && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
2e0fef85 3515 lpfc_unreg_rpi(vport, ndlp);
e47c9093
JS
3516 /* Increment reference count to ndlp to hold the
3517 * reference to ndlp for the callback function.
3518 */
329f9bc7 3519 mbox->context2 = lpfc_nlp_get(ndlp);
2e0fef85 3520 mbox->vport = vport;
858c9f6c
JS
3521 if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3522 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3523 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3524 }
3525 else {
3526 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3527 ndlp->nlp_prev_state = ndlp->nlp_state;
3528 lpfc_nlp_set_state(vport, ndlp,
2e0fef85 3529 NLP_STE_REG_LOGIN_ISSUE);
858c9f6c 3530 }
0b727fea 3531 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
e47c9093 3532 != MBX_NOT_FINISHED)
dea3101e 3533 goto out;
e47c9093
JS
3534 else
3535 /* Decrement the ndlp reference count we
3536 * set for this failed mailbox command.
3537 */
3538 lpfc_nlp_put(ndlp);
98c9ea5c
JS
3539
3540 /* ELS rsp: Cannot issue reg_login for <NPortid> */
3541 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3542 "0138 ELS rsp: Cannot issue reg_login for x%x "
3543 "Data: x%x x%x x%x\n",
3544 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3545 ndlp->nlp_rpi);
3546
fa4066b6 3547 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 3548 ndlp = NULL;
fa4066b6
JS
3549 /* Indicate node has already been released,
3550 * should not reference to it from within
3551 * the routine lpfc_els_free_iocb.
3552 */
3553 cmdiocb->context1 = NULL;
3554 }
dea3101e 3555 } else {
858c9f6c
JS
3556 /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3557 if (!lpfc_error_lost_link(irsp) &&
3558 ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
fa4066b6 3559 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 3560 ndlp = NULL;
fa4066b6
JS
3561 /* Indicate node has already been
3562 * released, should not reference
3563 * to it from within the routine
3564 * lpfc_els_free_iocb.
3565 */
3566 cmdiocb->context1 = NULL;
3567 }
dea3101e
JB
3568 }
3569 }
14691150
JS
3570 mp = (struct lpfc_dmabuf *) mbox->context1;
3571 if (mp) {
3572 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3573 kfree(mp);
3574 }
3575 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e
JB
3576 }
3577out:
58da1ffb 3578 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2e0fef85 3579 spin_lock_irq(shost->host_lock);
858c9f6c 3580 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
2e0fef85 3581 spin_unlock_irq(shost->host_lock);
87af33fe
JS
3582
3583 /* If the node is not being used by another discovery thread,
3584 * and we are sending a reject, we are done with it.
3585 * Release driver reference count here and free associated
3586 * resources.
3587 */
3588 if (ls_rjt)
fa4066b6
JS
3589 if (lpfc_nlp_not_used(ndlp))
3590 /* Indicate node has already been released,
3591 * should not reference to it from within
3592 * the routine lpfc_els_free_iocb.
3593 */
3594 cmdiocb->context1 = NULL;
dea3101e 3595 }
87af33fe 3596
dea3101e
JB
3597 lpfc_els_free_iocb(phba, cmdiocb);
3598 return;
3599}
3600
e59058c4 3601/**
3621a710 3602 * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
e59058c4
JS
3603 * @vport: pointer to a host virtual N_Port data structure.
3604 * @flag: the els command code to be accepted.
3605 * @oldiocb: pointer to the original lpfc command iocb data structure.
3606 * @ndlp: pointer to a node-list data structure.
3607 * @mbox: pointer to the driver internal queue element for mailbox command.
3608 *
3609 * This routine prepares and issues an Accept (ACC) response IOCB
3610 * command. It uses the @flag to properly set up the IOCB field for the
3611 * specific ACC response command to be issued and invokes the
3612 * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3613 * @mbox pointer is passed in, it will be put into the context_un.mbox
3614 * field of the IOCB for the completion callback function to issue the
3615 * mailbox command to the HBA later when callback is invoked.
3616 *
3617 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3618 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3619 * will be stored into the context1 field of the IOCB for the completion
3620 * callback function to the corresponding response ELS IOCB command.
3621 *
3622 * Return code
3623 * 0 - Successfully issued acc response
3624 * 1 - Failed to issue acc response
3625 **/
dea3101e 3626int
2e0fef85
JS
3627lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3628 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
51ef4c26 3629 LPFC_MBOXQ_t *mbox)
dea3101e 3630{
2e0fef85
JS
3631 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3632 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
3633 IOCB_t *icmd;
3634 IOCB_t *oldcmd;
3635 struct lpfc_iocbq *elsiocb;
dea3101e
JB
3636 struct lpfc_sli *psli;
3637 uint8_t *pcmd;
3638 uint16_t cmdsize;
3639 int rc;
82d9a2a2 3640 ELS_PKT *els_pkt_ptr;
dea3101e
JB
3641
3642 psli = &phba->sli;
dea3101e
JB
3643 oldcmd = &oldiocb->iocb;
3644
3645 switch (flag) {
3646 case ELS_CMD_ACC:
92d7f7b0 3647 cmdsize = sizeof(uint32_t);
2e0fef85
JS
3648 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3649 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 3650 if (!elsiocb) {
2e0fef85 3651 spin_lock_irq(shost->host_lock);
5024ab17 3652 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2e0fef85 3653 spin_unlock_irq(shost->host_lock);
c9f8735b 3654 return 1;
dea3101e 3655 }
2e0fef85 3656
dea3101e 3657 icmd = &elsiocb->iocb;
7851fe2c
JS
3658 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3659 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e
JB
3660 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3661 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 3662 pcmd += sizeof(uint32_t);
858c9f6c
JS
3663
3664 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3665 "Issue ACC: did:x%x flg:x%x",
3666 ndlp->nlp_DID, ndlp->nlp_flag, 0);
dea3101e
JB
3667 break;
3668 case ELS_CMD_PLOGI:
92d7f7b0 3669 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
2e0fef85
JS
3670 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3671 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 3672 if (!elsiocb)
c9f8735b 3673 return 1;
488d1469 3674
dea3101e 3675 icmd = &elsiocb->iocb;
7851fe2c
JS
3676 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3677 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e
JB
3678 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3679
3680 if (mbox)
3681 elsiocb->context_un.mbox = mbox;
3682
3683 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0
JS
3684 pcmd += sizeof(uint32_t);
3685 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
858c9f6c
JS
3686
3687 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3688 "Issue ACC PLOGI: did:x%x flg:x%x",
3689 ndlp->nlp_DID, ndlp->nlp_flag, 0);
dea3101e 3690 break;
82d9a2a2 3691 case ELS_CMD_PRLO:
92d7f7b0 3692 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
2e0fef85 3693 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
82d9a2a2
JS
3694 ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3695 if (!elsiocb)
3696 return 1;
3697
3698 icmd = &elsiocb->iocb;
7851fe2c
JS
3699 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3700 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
82d9a2a2
JS
3701 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3702
3703 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
92d7f7b0 3704 sizeof(uint32_t) + sizeof(PRLO));
82d9a2a2
JS
3705 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3706 els_pkt_ptr = (ELS_PKT *) pcmd;
3707 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
858c9f6c
JS
3708
3709 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3710 "Issue ACC PRLO: did:x%x flg:x%x",
3711 ndlp->nlp_DID, ndlp->nlp_flag, 0);
82d9a2a2 3712 break;
dea3101e 3713 default:
c9f8735b 3714 return 1;
dea3101e 3715 }
dea3101e 3716 /* Xmit ELS ACC response tag <ulpIoTag> */
e8b62011
JS
3717 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3718 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3719 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3720 elsiocb->iotag, elsiocb->iocb.ulpContext,
3721 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3722 ndlp->nlp_rpi);
dea3101e 3723 if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2e0fef85 3724 spin_lock_irq(shost->host_lock);
c9f8735b 3725 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2e0fef85 3726 spin_unlock_irq(shost->host_lock);
dea3101e
JB
3727 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3728 } else {
858c9f6c 3729 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e
JB
3730 }
3731
3732 phba->fc_stat.elsXmitACC++;
3772a991 3733 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
3734 if (rc == IOCB_ERROR) {
3735 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 3736 return 1;
dea3101e 3737 }
c9f8735b 3738 return 0;
dea3101e
JB
3739}
3740
e59058c4 3741/**
3621a710 3742 * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
e59058c4
JS
3743 * @vport: pointer to a virtual N_Port data structure.
3744 * @rejectError:
3745 * @oldiocb: pointer to the original lpfc command iocb data structure.
3746 * @ndlp: pointer to a node-list data structure.
3747 * @mbox: pointer to the driver internal queue element for mailbox command.
3748 *
3749 * This routine prepares and issue an Reject (RJT) response IOCB
3750 * command. If a @mbox pointer is passed in, it will be put into the
3751 * context_un.mbox field of the IOCB for the completion callback function
3752 * to issue to the HBA later.
3753 *
3754 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3755 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3756 * will be stored into the context1 field of the IOCB for the completion
3757 * callback function to the reject response ELS IOCB command.
3758 *
3759 * Return code
3760 * 0 - Successfully issued reject response
3761 * 1 - Failed to issue reject response
3762 **/
dea3101e 3763int
2e0fef85 3764lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
858c9f6c
JS
3765 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3766 LPFC_MBOXQ_t *mbox)
dea3101e 3767{
2e0fef85 3768 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
3769 IOCB_t *icmd;
3770 IOCB_t *oldcmd;
3771 struct lpfc_iocbq *elsiocb;
dea3101e
JB
3772 struct lpfc_sli *psli;
3773 uint8_t *pcmd;
3774 uint16_t cmdsize;
3775 int rc;
3776
3777 psli = &phba->sli;
92d7f7b0 3778 cmdsize = 2 * sizeof(uint32_t);
2e0fef85
JS
3779 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3780 ndlp->nlp_DID, ELS_CMD_LS_RJT);
488d1469 3781 if (!elsiocb)
c9f8735b 3782 return 1;
dea3101e
JB
3783
3784 icmd = &elsiocb->iocb;
3785 oldcmd = &oldiocb->iocb;
7851fe2c
JS
3786 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3787 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e
JB
3788 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3789
3790 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
92d7f7b0 3791 pcmd += sizeof(uint32_t);
dea3101e
JB
3792 *((uint32_t *) (pcmd)) = rejectError;
3793
51ef4c26 3794 if (mbox)
858c9f6c 3795 elsiocb->context_un.mbox = mbox;
858c9f6c 3796
dea3101e 3797 /* Xmit ELS RJT <err> response tag <ulpIoTag> */
e8b62011
JS
3798 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3799 "0129 Xmit ELS RJT x%x response tag x%x "
3800 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3801 "rpi x%x\n",
3802 rejectError, elsiocb->iotag,
3803 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3804 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
858c9f6c
JS
3805 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3806 "Issue LS_RJT: did:x%x flg:x%x err:x%x",
3807 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3808
dea3101e 3809 phba->fc_stat.elsXmitLSRJT++;
858c9f6c 3810 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3772a991 3811 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
51ef4c26 3812
dea3101e
JB
3813 if (rc == IOCB_ERROR) {
3814 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 3815 return 1;
dea3101e 3816 }
c9f8735b 3817 return 0;
dea3101e
JB
3818}
3819
e59058c4 3820/**
3621a710 3821 * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
e59058c4
JS
3822 * @vport: pointer to a virtual N_Port data structure.
3823 * @oldiocb: pointer to the original lpfc command iocb data structure.
3824 * @ndlp: pointer to a node-list data structure.
3825 *
3826 * This routine prepares and issues an Accept (ACC) response to Address
3827 * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3828 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3829 *
3830 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3831 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3832 * will be stored into the context1 field of the IOCB for the completion
3833 * callback function to the ADISC Accept response ELS IOCB command.
3834 *
3835 * Return code
3836 * 0 - Successfully issued acc adisc response
3837 * 1 - Failed to issue adisc acc response
3838 **/
dea3101e 3839int
2e0fef85
JS
3840lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3841 struct lpfc_nodelist *ndlp)
dea3101e 3842{
2e0fef85 3843 struct lpfc_hba *phba = vport->phba;
dea3101e 3844 ADISC *ap;
2e0fef85 3845 IOCB_t *icmd, *oldcmd;
dea3101e 3846 struct lpfc_iocbq *elsiocb;
dea3101e
JB
3847 uint8_t *pcmd;
3848 uint16_t cmdsize;
3849 int rc;
3850
92d7f7b0 3851 cmdsize = sizeof(uint32_t) + sizeof(ADISC);
2e0fef85
JS
3852 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3853 ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 3854 if (!elsiocb)
c9f8735b 3855 return 1;
dea3101e 3856
5b8bd0c9
JS
3857 icmd = &elsiocb->iocb;
3858 oldcmd = &oldiocb->iocb;
7851fe2c
JS
3859 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3860 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5b8bd0c9 3861
dea3101e 3862 /* Xmit ADISC ACC response tag <ulpIoTag> */
e8b62011
JS
3863 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3864 "0130 Xmit ADISC ACC response iotag x%x xri: "
3865 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3866 elsiocb->iotag, elsiocb->iocb.ulpContext,
3867 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3868 ndlp->nlp_rpi);
dea3101e
JB
3869 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3870
3871 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 3872 pcmd += sizeof(uint32_t);
dea3101e
JB
3873
3874 ap = (ADISC *) (pcmd);
3875 ap->hardAL_PA = phba->fc_pref_ALPA;
92d7f7b0
JS
3876 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3877 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 3878 ap->DID = be32_to_cpu(vport->fc_myDID);
dea3101e 3879
858c9f6c
JS
3880 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3881 "Issue ACC ADISC: did:x%x flg:x%x",
3882 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3883
dea3101e 3884 phba->fc_stat.elsXmitACC++;
858c9f6c 3885 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3772a991 3886 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
3887 if (rc == IOCB_ERROR) {
3888 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 3889 return 1;
dea3101e 3890 }
c9f8735b 3891 return 0;
dea3101e
JB
3892}
3893
e59058c4 3894/**
3621a710 3895 * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
e59058c4
JS
3896 * @vport: pointer to a virtual N_Port data structure.
3897 * @oldiocb: pointer to the original lpfc command iocb data structure.
3898 * @ndlp: pointer to a node-list data structure.
3899 *
3900 * This routine prepares and issues an Accept (ACC) response to Process
3901 * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3902 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3903 *
3904 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3905 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3906 * will be stored into the context1 field of the IOCB for the completion
3907 * callback function to the PRLI Accept response ELS IOCB command.
3908 *
3909 * Return code
3910 * 0 - Successfully issued acc prli response
3911 * 1 - Failed to issue acc prli response
3912 **/
dea3101e 3913int
2e0fef85 3914lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5b8bd0c9 3915 struct lpfc_nodelist *ndlp)
dea3101e 3916{
2e0fef85 3917 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
3918 PRLI *npr;
3919 lpfc_vpd_t *vpd;
3920 IOCB_t *icmd;
3921 IOCB_t *oldcmd;
3922 struct lpfc_iocbq *elsiocb;
dea3101e
JB
3923 struct lpfc_sli *psli;
3924 uint8_t *pcmd;
3925 uint16_t cmdsize;
3926 int rc;
3927
3928 psli = &phba->sli;
dea3101e 3929
92d7f7b0 3930 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
2e0fef85 3931 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
92d7f7b0 3932 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
c9f8735b
JW
3933 if (!elsiocb)
3934 return 1;
dea3101e 3935
5b8bd0c9
JS
3936 icmd = &elsiocb->iocb;
3937 oldcmd = &oldiocb->iocb;
7851fe2c
JS
3938 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3939 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
3940
dea3101e 3941 /* Xmit PRLI ACC response tag <ulpIoTag> */
e8b62011
JS
3942 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3943 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3944 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3945 elsiocb->iotag, elsiocb->iocb.ulpContext,
3946 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3947 ndlp->nlp_rpi);
dea3101e
JB
3948 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3949
3950 *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
92d7f7b0 3951 pcmd += sizeof(uint32_t);
dea3101e
JB
3952
3953 /* For PRLI, remainder of payload is PRLI parameter page */
92d7f7b0 3954 memset(pcmd, 0, sizeof(PRLI));
dea3101e
JB
3955
3956 npr = (PRLI *) pcmd;
3957 vpd = &phba->vpd;
3958 /*
0d2b6b83
JS
3959 * If the remote port is a target and our firmware version is 3.20 or
3960 * later, set the following bits for FC-TAPE support.
dea3101e 3961 */
0d2b6b83
JS
3962 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3963 (vpd->rev.feaLevelHigh >= 0x02)) {
dea3101e
JB
3964 npr->ConfmComplAllowed = 1;
3965 npr->Retry = 1;
3966 npr->TaskRetryIdReq = 1;
3967 }
3968
3969 npr->acceptRspCode = PRLI_REQ_EXECUTED;
3970 npr->estabImagePair = 1;
3971 npr->readXferRdyDis = 1;
3972 npr->ConfmComplAllowed = 1;
3973
3974 npr->prliType = PRLI_FCP_TYPE;
3975 npr->initiatorFunc = 1;
3976
858c9f6c
JS
3977 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3978 "Issue ACC PRLI: did:x%x flg:x%x",
3979 ndlp->nlp_DID, ndlp->nlp_flag, 0);
3980
dea3101e 3981 phba->fc_stat.elsXmitACC++;
858c9f6c 3982 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e 3983
3772a991 3984 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
3985 if (rc == IOCB_ERROR) {
3986 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 3987 return 1;
dea3101e 3988 }
c9f8735b 3989 return 0;
dea3101e
JB
3990}
3991
e59058c4 3992/**
3621a710 3993 * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
e59058c4
JS
3994 * @vport: pointer to a virtual N_Port data structure.
3995 * @format: rnid command format.
3996 * @oldiocb: pointer to the original lpfc command iocb data structure.
3997 * @ndlp: pointer to a node-list data structure.
3998 *
3999 * This routine issues a Request Node Identification Data (RNID) Accept
4000 * (ACC) response. It constructs the RNID ACC response command according to
4001 * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4002 * issue the response. Note that this command does not need to hold the ndlp
4003 * reference count for the callback. So, the ndlp reference count taken by
4004 * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4005 * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4006 * there is no ndlp reference available.
4007 *
4008 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4009 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4010 * will be stored into the context1 field of the IOCB for the completion
4011 * callback function. However, for the RNID Accept Response ELS command,
4012 * this is undone later by this routine after the IOCB is allocated.
4013 *
4014 * Return code
4015 * 0 - Successfully issued acc rnid response
4016 * 1 - Failed to issue acc rnid response
4017 **/
dea3101e 4018static int
2e0fef85 4019lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
329f9bc7 4020 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
dea3101e 4021{
2e0fef85 4022 struct lpfc_hba *phba = vport->phba;
dea3101e 4023 RNID *rn;
2e0fef85 4024 IOCB_t *icmd, *oldcmd;
dea3101e 4025 struct lpfc_iocbq *elsiocb;
dea3101e
JB
4026 struct lpfc_sli *psli;
4027 uint8_t *pcmd;
4028 uint16_t cmdsize;
4029 int rc;
4030
4031 psli = &phba->sli;
92d7f7b0
JS
4032 cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4033 + (2 * sizeof(struct lpfc_name));
dea3101e 4034 if (format)
92d7f7b0 4035 cmdsize += sizeof(RNID_TOP_DISC);
dea3101e 4036
2e0fef85
JS
4037 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4038 ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4039 if (!elsiocb)
c9f8735b 4040 return 1;
dea3101e 4041
5b8bd0c9
JS
4042 icmd = &elsiocb->iocb;
4043 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4044 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4045 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4046
dea3101e 4047 /* Xmit RNID ACC response tag <ulpIoTag> */
e8b62011
JS
4048 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4049 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
4050 elsiocb->iotag, elsiocb->iocb.ulpContext);
dea3101e 4051 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
dea3101e 4052 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4053 pcmd += sizeof(uint32_t);
dea3101e 4054
92d7f7b0 4055 memset(pcmd, 0, sizeof(RNID));
dea3101e
JB
4056 rn = (RNID *) (pcmd);
4057 rn->Format = format;
92d7f7b0
JS
4058 rn->CommonLen = (2 * sizeof(struct lpfc_name));
4059 memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4060 memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
dea3101e
JB
4061 switch (format) {
4062 case 0:
4063 rn->SpecificLen = 0;
4064 break;
4065 case RNID_TOPOLOGY_DISC:
92d7f7b0 4066 rn->SpecificLen = sizeof(RNID_TOP_DISC);
dea3101e 4067 memcpy(&rn->un.topologyDisc.portName,
92d7f7b0 4068 &vport->fc_portname, sizeof(struct lpfc_name));
dea3101e
JB
4069 rn->un.topologyDisc.unitType = RNID_HBA;
4070 rn->un.topologyDisc.physPort = 0;
4071 rn->un.topologyDisc.attachedNodes = 0;
4072 break;
4073 default:
4074 rn->CommonLen = 0;
4075 rn->SpecificLen = 0;
4076 break;
4077 }
4078
858c9f6c
JS
4079 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4080 "Issue ACC RNID: did:x%x flg:x%x",
4081 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4082
dea3101e 4083 phba->fc_stat.elsXmitACC++;
858c9f6c 4084 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
329f9bc7 4085 lpfc_nlp_put(ndlp);
dea3101e
JB
4086 elsiocb->context1 = NULL; /* Don't need ndlp for cmpl,
4087 * it could be freed */
4088
3772a991 4089 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e
JB
4090 if (rc == IOCB_ERROR) {
4091 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4092 return 1;
dea3101e 4093 }
c9f8735b 4094 return 0;
dea3101e
JB
4095}
4096
19ca7609
JS
4097/**
4098 * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
4099 * @vport: pointer to a virtual N_Port data structure.
4100 * @iocb: pointer to the lpfc command iocb data structure.
4101 * @ndlp: pointer to a node-list data structure.
4102 *
4103 * Return
4104 **/
4105static void
4106lpfc_els_clear_rrq(struct lpfc_vport *vport,
4107 struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
4108{
4109 struct lpfc_hba *phba = vport->phba;
4110 uint8_t *pcmd;
4111 struct RRQ *rrq;
4112 uint16_t rxid;
1151e3ec 4113 uint16_t xri;
19ca7609
JS
4114 struct lpfc_node_rrq *prrq;
4115
4116
4117 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
4118 pcmd += sizeof(uint32_t);
4119 rrq = (struct RRQ *)pcmd;
1151e3ec 4120 rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
9589b062 4121 rxid = bf_get(rrq_rxid, rrq);
19ca7609
JS
4122
4123 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4124 "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
4125 " x%x x%x\n",
1151e3ec 4126 be32_to_cpu(bf_get(rrq_did, rrq)),
9589b062 4127 bf_get(rrq_oxid, rrq),
19ca7609
JS
4128 rxid,
4129 iocb->iotag, iocb->iocb.ulpContext);
4130
4131 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4132 "Clear RRQ: did:x%x flg:x%x exchg:x%.08x",
4133 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
1151e3ec 4134 if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
9589b062 4135 xri = bf_get(rrq_oxid, rrq);
1151e3ec
JS
4136 else
4137 xri = rxid;
4138 prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
19ca7609 4139 if (prrq)
1151e3ec 4140 lpfc_clr_rrq_active(phba, xri, prrq);
19ca7609
JS
4141 return;
4142}
4143
12265f68
JS
4144/**
4145 * lpfc_els_rsp_echo_acc - Issue echo acc response
4146 * @vport: pointer to a virtual N_Port data structure.
4147 * @data: pointer to echo data to return in the accept.
4148 * @oldiocb: pointer to the original lpfc command iocb data structure.
4149 * @ndlp: pointer to a node-list data structure.
4150 *
4151 * Return code
4152 * 0 - Successfully issued acc echo response
4153 * 1 - Failed to issue acc echo response
4154 **/
4155static int
4156lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
4157 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4158{
4159 struct lpfc_hba *phba = vport->phba;
4160 struct lpfc_iocbq *elsiocb;
4161 struct lpfc_sli *psli;
4162 uint8_t *pcmd;
4163 uint16_t cmdsize;
4164 int rc;
4165
4166 psli = &phba->sli;
4167 cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
4168
4169 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4170 ndlp->nlp_DID, ELS_CMD_ACC);
4171 if (!elsiocb)
4172 return 1;
4173
7851fe2c
JS
4174 elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext; /* Xri / rx_id */
4175 elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
4176
12265f68
JS
4177 /* Xmit ECHO ACC response tag <ulpIoTag> */
4178 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4179 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
4180 elsiocb->iotag, elsiocb->iocb.ulpContext);
4181 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4182 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4183 pcmd += sizeof(uint32_t);
4184 memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
4185
4186 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4187 "Issue ACC ECHO: did:x%x flg:x%x",
4188 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4189
4190 phba->fc_stat.elsXmitACC++;
4191 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4192 lpfc_nlp_put(ndlp);
4193 elsiocb->context1 = NULL; /* Don't need ndlp for cmpl,
4194 * it could be freed */
4195
4196 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4197 if (rc == IOCB_ERROR) {
4198 lpfc_els_free_iocb(phba, elsiocb);
4199 return 1;
4200 }
4201 return 0;
4202}
4203
e59058c4 4204/**
3621a710 4205 * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
e59058c4
JS
4206 * @vport: pointer to a host virtual N_Port data structure.
4207 *
4208 * This routine issues Address Discover (ADISC) ELS commands to those
4209 * N_Ports which are in node port recovery state and ADISC has not been issued
4210 * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
4211 * lpfc_issue_els_adisc() routine, the per @vport number of discover count
4212 * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
4213 * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
4214 * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4215 * IOCBs quit for later pick up. On the other hand, after walking through
4216 * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4217 * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4218 * no more ADISC need to be sent.
4219 *
4220 * Return code
4221 * The number of N_Ports with adisc issued.
4222 **/
dea3101e 4223int
2e0fef85 4224lpfc_els_disc_adisc(struct lpfc_vport *vport)
dea3101e 4225{
2e0fef85 4226 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 4227 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 4228 int sentadisc = 0;
dea3101e 4229
685f0bf7 4230 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2e0fef85 4231 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
4232 if (!NLP_CHK_NODE_ACT(ndlp))
4233 continue;
685f0bf7
JS
4234 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4235 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4236 (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
2e0fef85 4237 spin_lock_irq(shost->host_lock);
685f0bf7 4238 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2e0fef85 4239 spin_unlock_irq(shost->host_lock);
685f0bf7 4240 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
4241 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4242 lpfc_issue_els_adisc(vport, ndlp, 0);
685f0bf7 4243 sentadisc++;
2e0fef85
JS
4244 vport->num_disc_nodes++;
4245 if (vport->num_disc_nodes >=
3de2a653 4246 vport->cfg_discovery_threads) {
2e0fef85
JS
4247 spin_lock_irq(shost->host_lock);
4248 vport->fc_flag |= FC_NLP_MORE;
4249 spin_unlock_irq(shost->host_lock);
685f0bf7 4250 break;
dea3101e
JB
4251 }
4252 }
4253 }
4254 if (sentadisc == 0) {
2e0fef85
JS
4255 spin_lock_irq(shost->host_lock);
4256 vport->fc_flag &= ~FC_NLP_MORE;
4257 spin_unlock_irq(shost->host_lock);
dea3101e 4258 }
2fe165b6 4259 return sentadisc;
dea3101e
JB
4260}
4261
e59058c4 4262/**
3621a710 4263 * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
e59058c4
JS
4264 * @vport: pointer to a host virtual N_Port data structure.
4265 *
4266 * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4267 * which are in node port recovery state, with a @vport. Each time an ELS
4268 * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4269 * the per @vport number of discover count (num_disc_nodes) shall be
4270 * incremented. If the num_disc_nodes reaches a pre-configured threshold
4271 * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4272 * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4273 * later pick up. On the other hand, after walking through all the ndlps with
4274 * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4275 * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4276 * PLOGI need to be sent.
4277 *
4278 * Return code
4279 * The number of N_Ports with plogi issued.
4280 **/
dea3101e 4281int
2e0fef85 4282lpfc_els_disc_plogi(struct lpfc_vport *vport)
dea3101e 4283{
2e0fef85 4284 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 4285 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 4286 int sentplogi = 0;
dea3101e 4287
2e0fef85
JS
4288 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
4289 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
4290 if (!NLP_CHK_NODE_ACT(ndlp))
4291 continue;
685f0bf7
JS
4292 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4293 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4294 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4295 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4296 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
4297 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4298 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
685f0bf7 4299 sentplogi++;
2e0fef85
JS
4300 vport->num_disc_nodes++;
4301 if (vport->num_disc_nodes >=
3de2a653 4302 vport->cfg_discovery_threads) {
2e0fef85
JS
4303 spin_lock_irq(shost->host_lock);
4304 vport->fc_flag |= FC_NLP_MORE;
4305 spin_unlock_irq(shost->host_lock);
685f0bf7 4306 break;
dea3101e
JB
4307 }
4308 }
4309 }
87af33fe
JS
4310 if (sentplogi) {
4311 lpfc_set_disctmo(vport);
4312 }
4313 else {
2e0fef85
JS
4314 spin_lock_irq(shost->host_lock);
4315 vport->fc_flag &= ~FC_NLP_MORE;
4316 spin_unlock_irq(shost->host_lock);
dea3101e 4317 }
2fe165b6 4318 return sentplogi;
dea3101e
JB
4319}
4320
e59058c4 4321/**
3621a710 4322 * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
e59058c4
JS
4323 * @vport: pointer to a host virtual N_Port data structure.
4324 *
4325 * This routine cleans up any Registration State Change Notification
4326 * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
4327 * @vport together with the host_lock is used to prevent multiple thread
4328 * trying to access the RSCN array on a same @vport at the same time.
4329 **/
92d7f7b0 4330void
2e0fef85 4331lpfc_els_flush_rscn(struct lpfc_vport *vport)
dea3101e 4332{
2e0fef85
JS
4333 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4334 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
4335 int i;
4336
7f5f3d0d
JS
4337 spin_lock_irq(shost->host_lock);
4338 if (vport->fc_rscn_flush) {
4339 /* Another thread is walking fc_rscn_id_list on this vport */
4340 spin_unlock_irq(shost->host_lock);
4341 return;
4342 }
4343 /* Indicate we are walking lpfc_els_flush_rscn on this vport */
4344 vport->fc_rscn_flush = 1;
4345 spin_unlock_irq(shost->host_lock);
4346
2e0fef85 4347 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
92d7f7b0 4348 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
2e0fef85 4349 vport->fc_rscn_id_list[i] = NULL;
dea3101e 4350 }
2e0fef85
JS
4351 spin_lock_irq(shost->host_lock);
4352 vport->fc_rscn_id_cnt = 0;
4353 vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
4354 spin_unlock_irq(shost->host_lock);
4355 lpfc_can_disctmo(vport);
7f5f3d0d
JS
4356 /* Indicate we are done walking this fc_rscn_id_list */
4357 vport->fc_rscn_flush = 0;
dea3101e
JB
4358}
4359
e59058c4 4360/**
3621a710 4361 * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
e59058c4
JS
4362 * @vport: pointer to a host virtual N_Port data structure.
4363 * @did: remote destination port identifier.
4364 *
4365 * This routine checks whether there is any pending Registration State
4366 * Configuration Notification (RSCN) to a @did on @vport.
4367 *
4368 * Return code
4369 * None zero - The @did matched with a pending rscn
4370 * 0 - not able to match @did with a pending rscn
4371 **/
dea3101e 4372int
2e0fef85 4373lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
dea3101e
JB
4374{
4375 D_ID ns_did;
4376 D_ID rscn_did;
dea3101e 4377 uint32_t *lp;
92d7f7b0 4378 uint32_t payload_len, i;
7f5f3d0d 4379 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e
JB
4380
4381 ns_did.un.word = did;
dea3101e
JB
4382
4383 /* Never match fabric nodes for RSCNs */
4384 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2e0fef85 4385 return 0;
dea3101e
JB
4386
4387 /* If we are doing a FULL RSCN rediscovery, match everything */
2e0fef85 4388 if (vport->fc_flag & FC_RSCN_DISCOVERY)
c9f8735b 4389 return did;
dea3101e 4390
7f5f3d0d
JS
4391 spin_lock_irq(shost->host_lock);
4392 if (vport->fc_rscn_flush) {
4393 /* Another thread is walking fc_rscn_id_list on this vport */
4394 spin_unlock_irq(shost->host_lock);
4395 return 0;
4396 }
4397 /* Indicate we are walking fc_rscn_id_list on this vport */
4398 vport->fc_rscn_flush = 1;
4399 spin_unlock_irq(shost->host_lock);
2e0fef85 4400 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
92d7f7b0
JS
4401 lp = vport->fc_rscn_id_list[i]->virt;
4402 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4403 payload_len -= sizeof(uint32_t); /* take off word 0 */
dea3101e 4404 while (payload_len) {
92d7f7b0
JS
4405 rscn_did.un.word = be32_to_cpu(*lp++);
4406 payload_len -= sizeof(uint32_t);
eaf15d5b
JS
4407 switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
4408 case RSCN_ADDRESS_FORMAT_PORT:
6fb120a7
JS
4409 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4410 && (ns_did.un.b.area == rscn_did.un.b.area)
4411 && (ns_did.un.b.id == rscn_did.un.b.id))
7f5f3d0d 4412 goto return_did_out;
dea3101e 4413 break;
eaf15d5b 4414 case RSCN_ADDRESS_FORMAT_AREA:
dea3101e
JB
4415 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4416 && (ns_did.un.b.area == rscn_did.un.b.area))
7f5f3d0d 4417 goto return_did_out;
dea3101e 4418 break;
eaf15d5b 4419 case RSCN_ADDRESS_FORMAT_DOMAIN:
dea3101e 4420 if (ns_did.un.b.domain == rscn_did.un.b.domain)
7f5f3d0d 4421 goto return_did_out;
dea3101e 4422 break;
eaf15d5b 4423 case RSCN_ADDRESS_FORMAT_FABRIC:
7f5f3d0d 4424 goto return_did_out;
dea3101e
JB
4425 }
4426 }
92d7f7b0 4427 }
7f5f3d0d
JS
4428 /* Indicate we are done with walking fc_rscn_id_list on this vport */
4429 vport->fc_rscn_flush = 0;
92d7f7b0 4430 return 0;
7f5f3d0d
JS
4431return_did_out:
4432 /* Indicate we are done with walking fc_rscn_id_list on this vport */
4433 vport->fc_rscn_flush = 0;
4434 return did;
dea3101e
JB
4435}
4436
e59058c4 4437/**
3621a710 4438 * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
e59058c4
JS
4439 * @vport: pointer to a host virtual N_Port data structure.
4440 *
4441 * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
4442 * state machine for a @vport's nodes that are with pending RSCN (Registration
4443 * State Change Notification).
4444 *
4445 * Return code
4446 * 0 - Successful (currently alway return 0)
4447 **/
dea3101e 4448static int
2e0fef85 4449lpfc_rscn_recovery_check(struct lpfc_vport *vport)
dea3101e 4450{
685f0bf7 4451 struct lpfc_nodelist *ndlp = NULL;
dea3101e 4452
0d2b6b83 4453 /* Move all affected nodes by pending RSCNs to NPR state. */
2e0fef85 4454 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093 4455 if (!NLP_CHK_NODE_ACT(ndlp) ||
0d2b6b83
JS
4456 (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
4457 !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
685f0bf7 4458 continue;
2e0fef85 4459 lpfc_disc_state_machine(vport, ndlp, NULL,
0d2b6b83
JS
4460 NLP_EVT_DEVICE_RECOVERY);
4461 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea3101e 4462 }
c9f8735b 4463 return 0;
dea3101e
JB
4464}
4465
ddcc50f0 4466/**
3621a710 4467 * lpfc_send_rscn_event - Send an RSCN event to management application
ddcc50f0
JS
4468 * @vport: pointer to a host virtual N_Port data structure.
4469 * @cmdiocb: pointer to lpfc command iocb data structure.
4470 *
4471 * lpfc_send_rscn_event sends an RSCN netlink event to management
4472 * applications.
4473 */
4474static void
4475lpfc_send_rscn_event(struct lpfc_vport *vport,
4476 struct lpfc_iocbq *cmdiocb)
4477{
4478 struct lpfc_dmabuf *pcmd;
4479 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4480 uint32_t *payload_ptr;
4481 uint32_t payload_len;
4482 struct lpfc_rscn_event_header *rscn_event_data;
4483
4484 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4485 payload_ptr = (uint32_t *) pcmd->virt;
4486 payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4487
4488 rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4489 payload_len, GFP_KERNEL);
4490 if (!rscn_event_data) {
4491 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4492 "0147 Failed to allocate memory for RSCN event\n");
4493 return;
4494 }
4495 rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4496 rscn_event_data->payload_length = payload_len;
4497 memcpy(rscn_event_data->rscn_payload, payload_ptr,
4498 payload_len);
4499
4500 fc_host_post_vendor_event(shost,
4501 fc_get_event_number(),
4502 sizeof(struct lpfc_els_event_header) + payload_len,
4503 (char *)rscn_event_data,
4504 LPFC_NL_VENDOR_ID);
4505
4506 kfree(rscn_event_data);
4507}
4508
e59058c4 4509/**
3621a710 4510 * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
e59058c4
JS
4511 * @vport: pointer to a host virtual N_Port data structure.
4512 * @cmdiocb: pointer to lpfc command iocb data structure.
4513 * @ndlp: pointer to a node-list data structure.
4514 *
4515 * This routine processes an unsolicited RSCN (Registration State Change
4516 * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4517 * to invoke fc_host_post_event() routine to the FC transport layer. If the
4518 * discover state machine is about to begin discovery, it just accepts the
4519 * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4520 * contains N_Port IDs for other vports on this HBA, it just accepts the
4521 * RSCN and ignore processing it. If the state machine is in the recovery
4522 * state, the fc_rscn_id_list of this @vport is walked and the
4523 * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4524 * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4525 * routine is invoked to handle the RSCN event.
4526 *
4527 * Return code
4528 * 0 - Just sent the acc response
4529 * 1 - Sent the acc response and waited for name server completion
4530 **/
dea3101e 4531static int
2e0fef85 4532lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
51ef4c26 4533 struct lpfc_nodelist *ndlp)
dea3101e 4534{
2e0fef85
JS
4535 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4536 struct lpfc_hba *phba = vport->phba;
dea3101e 4537 struct lpfc_dmabuf *pcmd;
92d7f7b0 4538 uint32_t *lp, *datap;
dea3101e 4539 IOCB_t *icmd;
92d7f7b0 4540 uint32_t payload_len, length, nportid, *cmd;
7f5f3d0d 4541 int rscn_cnt;
92d7f7b0 4542 int rscn_id = 0, hba_id = 0;
d2873e4c 4543 int i;
dea3101e
JB
4544
4545 icmd = &cmdiocb->iocb;
4546 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4547 lp = (uint32_t *) pcmd->virt;
4548
92d7f7b0
JS
4549 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4550 payload_len -= sizeof(uint32_t); /* take off word 0 */
dea3101e 4551 /* RSCN received */
e8b62011
JS
4552 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4553 "0214 RSCN received Data: x%x x%x x%x x%x\n",
7f5f3d0d
JS
4554 vport->fc_flag, payload_len, *lp,
4555 vport->fc_rscn_id_cnt);
ddcc50f0
JS
4556
4557 /* Send an RSCN event to the management application */
4558 lpfc_send_rscn_event(vport, cmdiocb);
4559
d2873e4c 4560 for (i = 0; i < payload_len/sizeof(uint32_t); i++)
2e0fef85 4561 fc_host_post_event(shost, fc_get_event_number(),
d2873e4c
JS
4562 FCH_EVT_RSCN, lp[i]);
4563
dea3101e
JB
4564 /* If we are about to begin discovery, just ACC the RSCN.
4565 * Discovery processing will satisfy it.
4566 */
2e0fef85 4567 if (vport->port_state <= LPFC_NS_QRY) {
858c9f6c
JS
4568 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4569 "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4570 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4571
51ef4c26 4572 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
c9f8735b 4573 return 0;
dea3101e
JB
4574 }
4575
92d7f7b0
JS
4576 /* If this RSCN just contains NPortIDs for other vports on this HBA,
4577 * just ACC and ignore it.
4578 */
4579 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3de2a653 4580 !(vport->cfg_peer_port_login)) {
92d7f7b0
JS
4581 i = payload_len;
4582 datap = lp;
4583 while (i > 0) {
4584 nportid = *datap++;
4585 nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4586 i -= sizeof(uint32_t);
4587 rscn_id++;
549e55cd
JS
4588 if (lpfc_find_vport_by_did(phba, nportid))
4589 hba_id++;
92d7f7b0
JS
4590 }
4591 if (rscn_id == hba_id) {
4592 /* ALL NPortIDs in RSCN are on HBA */
e8b62011 4593 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
d7c255b2 4594 "0219 Ignore RSCN "
e8b62011
JS
4595 "Data: x%x x%x x%x x%x\n",
4596 vport->fc_flag, payload_len,
7f5f3d0d 4597 *lp, vport->fc_rscn_id_cnt);
858c9f6c
JS
4598 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4599 "RCV RSCN vport: did:x%x/ste:x%x flg:x%x",
4600 ndlp->nlp_DID, vport->port_state,
4601 ndlp->nlp_flag);
4602
92d7f7b0 4603 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
51ef4c26 4604 ndlp, NULL);
92d7f7b0
JS
4605 return 0;
4606 }
4607 }
4608
7f5f3d0d
JS
4609 spin_lock_irq(shost->host_lock);
4610 if (vport->fc_rscn_flush) {
4611 /* Another thread is walking fc_rscn_id_list on this vport */
7f5f3d0d 4612 vport->fc_flag |= FC_RSCN_DISCOVERY;
97957244 4613 spin_unlock_irq(shost->host_lock);
58da1ffb
JS
4614 /* Send back ACC */
4615 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7f5f3d0d
JS
4616 return 0;
4617 }
4618 /* Indicate we are walking fc_rscn_id_list on this vport */
4619 vport->fc_rscn_flush = 1;
4620 spin_unlock_irq(shost->host_lock);
af901ca1 4621 /* Get the array count after successfully have the token */
7f5f3d0d 4622 rscn_cnt = vport->fc_rscn_id_cnt;
dea3101e
JB
4623 /* If we are already processing an RSCN, save the received
4624 * RSCN payload buffer, cmdiocb->context2 to process later.
4625 */
2e0fef85 4626 if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
858c9f6c
JS
4627 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4628 "RCV RSCN defer: did:x%x/ste:x%x flg:x%x",
4629 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4630
09372820 4631 spin_lock_irq(shost->host_lock);
92d7f7b0
JS
4632 vport->fc_flag |= FC_RSCN_DEFERRED;
4633 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
2e0fef85 4634 !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
2e0fef85
JS
4635 vport->fc_flag |= FC_RSCN_MODE;
4636 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
4637 if (rscn_cnt) {
4638 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4639 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4640 }
4641 if ((rscn_cnt) &&
4642 (payload_len + length <= LPFC_BPL_SIZE)) {
4643 *cmd &= ELS_CMD_MASK;
7f5f3d0d 4644 *cmd |= cpu_to_be32(payload_len + length);
92d7f7b0
JS
4645 memcpy(((uint8_t *)cmd) + length, lp,
4646 payload_len);
4647 } else {
4648 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4649 vport->fc_rscn_id_cnt++;
4650 /* If we zero, cmdiocb->context2, the calling
4651 * routine will not try to free it.
4652 */
4653 cmdiocb->context2 = NULL;
4654 }
dea3101e 4655 /* Deferred RSCN */
e8b62011
JS
4656 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4657 "0235 Deferred RSCN "
4658 "Data: x%x x%x x%x\n",
4659 vport->fc_rscn_id_cnt, vport->fc_flag,
4660 vport->port_state);
dea3101e 4661 } else {
2e0fef85
JS
4662 vport->fc_flag |= FC_RSCN_DISCOVERY;
4663 spin_unlock_irq(shost->host_lock);
dea3101e 4664 /* ReDiscovery RSCN */
e8b62011
JS
4665 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4666 "0234 ReDiscovery RSCN "
4667 "Data: x%x x%x x%x\n",
4668 vport->fc_rscn_id_cnt, vport->fc_flag,
4669 vport->port_state);
dea3101e 4670 }
7f5f3d0d
JS
4671 /* Indicate we are done walking fc_rscn_id_list on this vport */
4672 vport->fc_rscn_flush = 0;
dea3101e 4673 /* Send back ACC */
51ef4c26 4674 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 4675 /* send RECOVERY event for ALL nodes that match RSCN payload */
2e0fef85 4676 lpfc_rscn_recovery_check(vport);
09372820 4677 spin_lock_irq(shost->host_lock);
92d7f7b0 4678 vport->fc_flag &= ~FC_RSCN_DEFERRED;
09372820 4679 spin_unlock_irq(shost->host_lock);
c9f8735b 4680 return 0;
dea3101e 4681 }
858c9f6c
JS
4682 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4683 "RCV RSCN: did:x%x/ste:x%x flg:x%x",
4684 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4685
2e0fef85
JS
4686 spin_lock_irq(shost->host_lock);
4687 vport->fc_flag |= FC_RSCN_MODE;
4688 spin_unlock_irq(shost->host_lock);
4689 vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
7f5f3d0d
JS
4690 /* Indicate we are done walking fc_rscn_id_list on this vport */
4691 vport->fc_rscn_flush = 0;
dea3101e
JB
4692 /*
4693 * If we zero, cmdiocb->context2, the calling routine will
4694 * not try to free it.
4695 */
4696 cmdiocb->context2 = NULL;
2e0fef85 4697 lpfc_set_disctmo(vport);
dea3101e 4698 /* Send back ACC */
51ef4c26 4699 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 4700 /* send RECOVERY event for ALL nodes that match RSCN payload */
2e0fef85 4701 lpfc_rscn_recovery_check(vport);
2e0fef85 4702 return lpfc_els_handle_rscn(vport);
dea3101e
JB
4703}
4704
e59058c4 4705/**
3621a710 4706 * lpfc_els_handle_rscn - Handle rscn for a vport
e59058c4
JS
4707 * @vport: pointer to a host virtual N_Port data structure.
4708 *
4709 * This routine handles the Registration State Configuration Notification
4710 * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4711 * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4712 * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4713 * NameServer shall be issued. If CT command to the NameServer fails to be
4714 * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4715 * RSCN activities with the @vport.
4716 *
4717 * Return code
4718 * 0 - Cleaned up rscn on the @vport
4719 * 1 - Wait for plogi to name server before proceed
4720 **/
dea3101e 4721int
2e0fef85 4722lpfc_els_handle_rscn(struct lpfc_vport *vport)
dea3101e
JB
4723{
4724 struct lpfc_nodelist *ndlp;
2e0fef85 4725 struct lpfc_hba *phba = vport->phba;
dea3101e 4726
92d7f7b0
JS
4727 /* Ignore RSCN if the port is being torn down. */
4728 if (vport->load_flag & FC_UNLOADING) {
4729 lpfc_els_flush_rscn(vport);
4730 return 0;
4731 }
4732
dea3101e 4733 /* Start timer for RSCN processing */
2e0fef85 4734 lpfc_set_disctmo(vport);
dea3101e
JB
4735
4736 /* RSCN processed */
e8b62011
JS
4737 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4738 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4739 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4740 vport->port_state);
dea3101e
JB
4741
4742 /* To process RSCN, first compare RSCN data with NameServer */
2e0fef85 4743 vport->fc_ns_retry = 0;
0ff10d46
JS
4744 vport->num_disc_nodes = 0;
4745
2e0fef85 4746 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093
JS
4747 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4748 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
dea3101e 4749 /* Good ndlp, issue CT Request to NameServer */
92d7f7b0 4750 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
dea3101e
JB
4751 /* Wait for NameServer query cmpl before we can
4752 continue */
c9f8735b 4753 return 1;
dea3101e
JB
4754 } else {
4755 /* If login to NameServer does not exist, issue one */
4756 /* Good status, issue PLOGI to NameServer */
2e0fef85 4757 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093 4758 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
dea3101e
JB
4759 /* Wait for NameServer login cmpl before we can
4760 continue */
c9f8735b 4761 return 1;
2e0fef85 4762
e47c9093
JS
4763 if (ndlp) {
4764 ndlp = lpfc_enable_node(vport, ndlp,
4765 NLP_STE_PLOGI_ISSUE);
4766 if (!ndlp) {
4767 lpfc_els_flush_rscn(vport);
4768 return 0;
4769 }
4770 ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
dea3101e 4771 } else {
e47c9093
JS
4772 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4773 if (!ndlp) {
4774 lpfc_els_flush_rscn(vport);
4775 return 0;
4776 }
2e0fef85 4777 lpfc_nlp_init(vport, ndlp, NameServer_DID);
5024ab17 4778 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 4779 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
dea3101e 4780 }
e47c9093
JS
4781 ndlp->nlp_type |= NLP_FABRIC;
4782 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4783 /* Wait for NameServer login cmpl before we can
4784 * continue
4785 */
4786 return 1;
dea3101e
JB
4787 }
4788
2e0fef85 4789 lpfc_els_flush_rscn(vport);
c9f8735b 4790 return 0;
dea3101e
JB
4791}
4792
e59058c4 4793/**
3621a710 4794 * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
e59058c4
JS
4795 * @vport: pointer to a host virtual N_Port data structure.
4796 * @cmdiocb: pointer to lpfc command iocb data structure.
4797 * @ndlp: pointer to a node-list data structure.
4798 *
4799 * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4800 * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4801 * point topology. As an unsolicited FLOGI should not be received in a loop
4802 * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4803 * lpfc_check_sparm() routine is invoked to check the parameters in the
4804 * unsolicited FLOGI. If parameters validation failed, the routine
4805 * lpfc_els_rsp_reject() shall be called with reject reason code set to
4806 * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4807 * FLOGI shall be compared with the Port WWN of the @vport to determine who
4808 * will initiate PLOGI. The higher lexicographical value party shall has
4809 * higher priority (as the winning port) and will initiate PLOGI and
4810 * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4811 * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4812 * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4813 *
4814 * Return code
4815 * 0 - Successfully processed the unsolicited flogi
4816 * 1 - Failed to process the unsolicited flogi
4817 **/
dea3101e 4818static int
2e0fef85 4819lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
51ef4c26 4820 struct lpfc_nodelist *ndlp)
dea3101e 4821{
2e0fef85
JS
4822 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4823 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
4824 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4825 uint32_t *lp = (uint32_t *) pcmd->virt;
4826 IOCB_t *icmd = &cmdiocb->iocb;
4827 struct serv_parm *sp;
4828 LPFC_MBOXQ_t *mbox;
4829 struct ls_rjt stat;
4830 uint32_t cmd, did;
4831 int rc;
4832
4833 cmd = *lp++;
4834 sp = (struct serv_parm *) lp;
4835
4836 /* FLOGI received */
4837
2e0fef85 4838 lpfc_set_disctmo(vport);
dea3101e 4839
76a95d75 4840 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
dea3101e
JB
4841 /* We should never receive a FLOGI in loop mode, ignore it */
4842 did = icmd->un.elsreq64.remoteID;
4843
4844 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
4845 Loop Mode */
e8b62011
JS
4846 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4847 "0113 An FLOGI ELS command x%x was "
4848 "received from DID x%x in Loop Mode\n",
4849 cmd, did);
c9f8735b 4850 return 1;
dea3101e
JB
4851 }
4852
4853 did = Fabric_DID;
4854
341af102 4855 if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
dea3101e
JB
4856 /* For a FLOGI we accept, then if our portname is greater
4857 * then the remote portname we initiate Nport login.
4858 */
4859
2e0fef85 4860 rc = memcmp(&vport->fc_portname, &sp->portName,
92d7f7b0 4861 sizeof(struct lpfc_name));
dea3101e
JB
4862
4863 if (!rc) {
2e0fef85
JS
4864 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4865 if (!mbox)
c9f8735b 4866 return 1;
2e0fef85 4867
dea3101e
JB
4868 lpfc_linkdown(phba);
4869 lpfc_init_link(phba, mbox,
4870 phba->cfg_topology,
4871 phba->cfg_link_speed);
04c68496 4872 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
dea3101e 4873 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
ed957684 4874 mbox->vport = vport;
0b727fea 4875 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5b8bd0c9 4876 lpfc_set_loopback_flag(phba);
dea3101e 4877 if (rc == MBX_NOT_FINISHED) {
329f9bc7 4878 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 4879 }
c9f8735b 4880 return 1;
2fe165b6 4881 } else if (rc > 0) { /* greater than */
2e0fef85
JS
4882 spin_lock_irq(shost->host_lock);
4883 vport->fc_flag |= FC_PT2PT_PLOGI;
4884 spin_unlock_irq(shost->host_lock);
dea3101e 4885 }
2e0fef85
JS
4886 spin_lock_irq(shost->host_lock);
4887 vport->fc_flag |= FC_PT2PT;
4888 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4889 spin_unlock_irq(shost->host_lock);
dea3101e
JB
4890 } else {
4891 /* Reject this request because invalid parameters */
4892 stat.un.b.lsRjtRsvd0 = 0;
4893 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4894 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4895 stat.un.b.vendorUnique = 0;
858c9f6c
JS
4896 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4897 NULL);
c9f8735b 4898 return 1;
dea3101e
JB
4899 }
4900
4901 /* Send back ACC */
51ef4c26 4902 lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
dea3101e 4903
c9f8735b 4904 return 0;
dea3101e
JB
4905}
4906
e59058c4 4907/**
3621a710 4908 * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
e59058c4
JS
4909 * @vport: pointer to a host virtual N_Port data structure.
4910 * @cmdiocb: pointer to lpfc command iocb data structure.
4911 * @ndlp: pointer to a node-list data structure.
4912 *
4913 * This routine processes Request Node Identification Data (RNID) IOCB
4914 * received as an ELS unsolicited event. Only when the RNID specified format
4915 * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4916 * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4917 * Accept (ACC) the RNID ELS command. All the other RNID formats are
4918 * rejected by invoking the lpfc_els_rsp_reject() routine.
4919 *
4920 * Return code
4921 * 0 - Successfully processed rnid iocb (currently always return 0)
4922 **/
dea3101e 4923static int
2e0fef85
JS
4924lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4925 struct lpfc_nodelist *ndlp)
dea3101e
JB
4926{
4927 struct lpfc_dmabuf *pcmd;
4928 uint32_t *lp;
4929 IOCB_t *icmd;
4930 RNID *rn;
4931 struct ls_rjt stat;
4932 uint32_t cmd, did;
4933
4934 icmd = &cmdiocb->iocb;
4935 did = icmd->un.elsreq64.remoteID;
4936 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4937 lp = (uint32_t *) pcmd->virt;
4938
4939 cmd = *lp++;
4940 rn = (RNID *) lp;
4941
4942 /* RNID received */
4943
4944 switch (rn->Format) {
4945 case 0:
4946 case RNID_TOPOLOGY_DISC:
4947 /* Send back ACC */
2e0fef85 4948 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
dea3101e
JB
4949 break;
4950 default:
4951 /* Reject this request because format not supported */
4952 stat.un.b.lsRjtRsvd0 = 0;
4953 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4954 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4955 stat.un.b.vendorUnique = 0;
858c9f6c
JS
4956 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4957 NULL);
dea3101e 4958 }
c9f8735b 4959 return 0;
dea3101e
JB
4960}
4961
12265f68
JS
4962/**
4963 * lpfc_els_rcv_echo - Process an unsolicited echo iocb
4964 * @vport: pointer to a host virtual N_Port data structure.
4965 * @cmdiocb: pointer to lpfc command iocb data structure.
4966 * @ndlp: pointer to a node-list data structure.
4967 *
4968 * Return code
4969 * 0 - Successfully processed echo iocb (currently always return 0)
4970 **/
4971static int
4972lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4973 struct lpfc_nodelist *ndlp)
4974{
4975 uint8_t *pcmd;
4976
4977 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4978
4979 /* skip over first word of echo command to find echo data */
4980 pcmd += sizeof(uint32_t);
4981
4982 lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
4983 return 0;
4984}
4985
e59058c4 4986/**
3621a710 4987 * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
e59058c4
JS
4988 * @vport: pointer to a host virtual N_Port data structure.
4989 * @cmdiocb: pointer to lpfc command iocb data structure.
4990 * @ndlp: pointer to a node-list data structure.
4991 *
4992 * This routine processes a Link Incident Report Registration(LIRR) IOCB
4993 * received as an ELS unsolicited event. Currently, this function just invokes
4994 * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4995 *
4996 * Return code
4997 * 0 - Successfully processed lirr iocb (currently always return 0)
4998 **/
dea3101e 4999static int
2e0fef85
JS
5000lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5001 struct lpfc_nodelist *ndlp)
7bb3b137
JW
5002{
5003 struct ls_rjt stat;
5004
5005 /* For now, unconditionally reject this command */
5006 stat.un.b.lsRjtRsvd0 = 0;
5007 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5008 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5009 stat.un.b.vendorUnique = 0;
858c9f6c 5010 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7bb3b137
JW
5011 return 0;
5012}
5013
5ffc266e
JS
5014/**
5015 * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
5016 * @vport: pointer to a host virtual N_Port data structure.
5017 * @cmdiocb: pointer to lpfc command iocb data structure.
5018 * @ndlp: pointer to a node-list data structure.
5019 *
5020 * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
5021 * received as an ELS unsolicited event. A request to RRQ shall only
5022 * be accepted if the Originator Nx_Port N_Port_ID or the Responder
5023 * Nx_Port N_Port_ID of the target Exchange is the same as the
5024 * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
5025 * not accepted, an LS_RJT with reason code "Unable to perform
5026 * command request" and reason code explanation "Invalid Originator
5027 * S_ID" shall be returned. For now, we just unconditionally accept
5028 * RRQ from the target.
5029 **/
5030static void
5031lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5032 struct lpfc_nodelist *ndlp)
5033{
5034 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
19ca7609
JS
5035 if (vport->phba->sli_rev == LPFC_SLI_REV4)
5036 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
5ffc266e
JS
5037}
5038
12265f68
JS
5039/**
5040 * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
5041 * @phba: pointer to lpfc hba data structure.
5042 * @pmb: pointer to the driver internal queue element for mailbox command.
5043 *
5044 * This routine is the completion callback function for the MBX_READ_LNK_STAT
5045 * mailbox command. This callback function is to actually send the Accept
5046 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
5047 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
5048 * mailbox command, constructs the RPS response with the link statistics
5049 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
5050 * response to the RPS.
5051 *
5052 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5053 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5054 * will be stored into the context1 field of the IOCB for the completion
5055 * callback function to the RPS Accept Response ELS IOCB command.
5056 *
5057 **/
5058static void
5059lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5060{
5061 MAILBOX_t *mb;
5062 IOCB_t *icmd;
5063 struct RLS_RSP *rls_rsp;
5064 uint8_t *pcmd;
5065 struct lpfc_iocbq *elsiocb;
5066 struct lpfc_nodelist *ndlp;
7851fe2c
JS
5067 uint16_t oxid;
5068 uint16_t rxid;
12265f68
JS
5069 uint32_t cmdsize;
5070
5071 mb = &pmb->u.mb;
5072
5073 ndlp = (struct lpfc_nodelist *) pmb->context2;
7851fe2c
JS
5074 rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
5075 oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
12265f68
JS
5076 pmb->context1 = NULL;
5077 pmb->context2 = NULL;
5078
5079 if (mb->mbxStatus) {
5080 mempool_free(pmb, phba->mbox_mem_pool);
5081 return;
5082 }
5083
5084 cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
5085 mempool_free(pmb, phba->mbox_mem_pool);
5086 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5087 lpfc_max_els_tries, ndlp,
5088 ndlp->nlp_DID, ELS_CMD_ACC);
5089
5090 /* Decrement the ndlp reference count from previous mbox command */
5091 lpfc_nlp_put(ndlp);
5092
5093 if (!elsiocb)
5094 return;
5095
5096 icmd = &elsiocb->iocb;
7851fe2c
JS
5097 icmd->ulpContext = rxid;
5098 icmd->unsli3.rcvsli3.ox_id = oxid;
12265f68
JS
5099
5100 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5101 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5102 pcmd += sizeof(uint32_t); /* Skip past command */
5103 rls_rsp = (struct RLS_RSP *)pcmd;
5104
5105 rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
5106 rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
5107 rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
5108 rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
5109 rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
5110 rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
5111
5112 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5113 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5114 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
5115 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5116 elsiocb->iotag, elsiocb->iocb.ulpContext,
5117 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5118 ndlp->nlp_rpi);
5119 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5120 phba->fc_stat.elsXmitACC++;
5121 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5122 lpfc_els_free_iocb(phba, elsiocb);
5123}
5124
e59058c4 5125/**
3621a710 5126 * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
e59058c4
JS
5127 * @phba: pointer to lpfc hba data structure.
5128 * @pmb: pointer to the driver internal queue element for mailbox command.
5129 *
5130 * This routine is the completion callback function for the MBX_READ_LNK_STAT
5131 * mailbox command. This callback function is to actually send the Accept
5132 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
5133 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
5134 * mailbox command, constructs the RPS response with the link statistics
5135 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
5136 * response to the RPS.
5137 *
5138 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5139 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5140 * will be stored into the context1 field of the IOCB for the completion
5141 * callback function to the RPS Accept Response ELS IOCB command.
5142 *
5143 **/
082c0266 5144static void
329f9bc7 5145lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7bb3b137 5146{
7bb3b137
JW
5147 MAILBOX_t *mb;
5148 IOCB_t *icmd;
5149 RPS_RSP *rps_rsp;
5150 uint8_t *pcmd;
5151 struct lpfc_iocbq *elsiocb;
5152 struct lpfc_nodelist *ndlp;
7851fe2c
JS
5153 uint16_t status;
5154 uint16_t oxid;
5155 uint16_t rxid;
7bb3b137
JW
5156 uint32_t cmdsize;
5157
04c68496 5158 mb = &pmb->u.mb;
7bb3b137
JW
5159
5160 ndlp = (struct lpfc_nodelist *) pmb->context2;
7851fe2c
JS
5161 rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
5162 oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
041976fb
RD
5163 pmb->context1 = NULL;
5164 pmb->context2 = NULL;
7bb3b137
JW
5165
5166 if (mb->mbxStatus) {
329f9bc7 5167 mempool_free(pmb, phba->mbox_mem_pool);
7bb3b137
JW
5168 return;
5169 }
5170
5171 cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
329f9bc7 5172 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85
JS
5173 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5174 lpfc_max_els_tries, ndlp,
5175 ndlp->nlp_DID, ELS_CMD_ACC);
fa4066b6
JS
5176
5177 /* Decrement the ndlp reference count from previous mbox command */
329f9bc7 5178 lpfc_nlp_put(ndlp);
fa4066b6 5179
c9f8735b 5180 if (!elsiocb)
7bb3b137 5181 return;
7bb3b137
JW
5182
5183 icmd = &elsiocb->iocb;
7851fe2c
JS
5184 icmd->ulpContext = rxid;
5185 icmd->unsli3.rcvsli3.ox_id = oxid;
7bb3b137
JW
5186
5187 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5188 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 5189 pcmd += sizeof(uint32_t); /* Skip past command */
7bb3b137
JW
5190 rps_rsp = (RPS_RSP *)pcmd;
5191
76a95d75 5192 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
7bb3b137
JW
5193 status = 0x10;
5194 else
5195 status = 0x8;
2e0fef85 5196 if (phba->pport->fc_flag & FC_FABRIC)
7bb3b137
JW
5197 status |= 0x4;
5198
5199 rps_rsp->rsvd1 = 0;
09372820
JS
5200 rps_rsp->portStatus = cpu_to_be16(status);
5201 rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
5202 rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
5203 rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
5204 rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
5205 rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
5206 rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7bb3b137 5207 /* Xmit ELS RPS ACC response tag <ulpIoTag> */
e8b62011
JS
5208 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5209 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
5210 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5211 elsiocb->iotag, elsiocb->iocb.ulpContext,
5212 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5213 ndlp->nlp_rpi);
858c9f6c 5214 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7bb3b137 5215 phba->fc_stat.elsXmitACC++;
3772a991 5216 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7bb3b137 5217 lpfc_els_free_iocb(phba, elsiocb);
7bb3b137
JW
5218 return;
5219}
5220
e59058c4 5221/**
12265f68
JS
5222 * lpfc_els_rcv_rls - Process an unsolicited rls iocb
5223 * @vport: pointer to a host virtual N_Port data structure.
5224 * @cmdiocb: pointer to lpfc command iocb data structure.
5225 * @ndlp: pointer to a node-list data structure.
5226 *
5227 * This routine processes Read Port Status (RPL) IOCB received as an
5228 * ELS unsolicited event. It first checks the remote port state. If the
5229 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5230 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5231 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5232 * for reading the HBA link statistics. It is for the callback function,
5233 * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
5234 * to actually sending out RPL Accept (ACC) response.
5235 *
5236 * Return codes
5237 * 0 - Successfully processed rls iocb (currently always return 0)
5238 **/
5239static int
5240lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5241 struct lpfc_nodelist *ndlp)
5242{
5243 struct lpfc_hba *phba = vport->phba;
5244 LPFC_MBOXQ_t *mbox;
5245 struct lpfc_dmabuf *pcmd;
5246 struct ls_rjt stat;
5247
5248 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5249 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5250 /* reject the unsolicited RPS request and done with it */
5251 goto reject_out;
5252
5253 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5254
5255 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5256 if (mbox) {
5257 lpfc_read_lnk_stat(phba, mbox);
7851fe2c
JS
5258 mbox->context1 = (void *)((unsigned long)
5259 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
5260 cmdiocb->iocb.ulpContext)); /* rx_id */
12265f68
JS
5261 mbox->context2 = lpfc_nlp_get(ndlp);
5262 mbox->vport = vport;
5263 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
5264 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5265 != MBX_NOT_FINISHED)
5266 /* Mbox completion will send ELS Response */
5267 return 0;
5268 /* Decrement reference count used for the failed mbox
5269 * command.
5270 */
5271 lpfc_nlp_put(ndlp);
5272 mempool_free(mbox, phba->mbox_mem_pool);
5273 }
5274reject_out:
5275 /* issue rejection response */
5276 stat.un.b.lsRjtRsvd0 = 0;
5277 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5278 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5279 stat.un.b.vendorUnique = 0;
5280 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5281 return 0;
5282}
5283
5284/**
5285 * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
5286 * @vport: pointer to a host virtual N_Port data structure.
5287 * @cmdiocb: pointer to lpfc command iocb data structure.
5288 * @ndlp: pointer to a node-list data structure.
5289 *
5290 * This routine processes Read Timout Value (RTV) IOCB received as an
5291 * ELS unsolicited event. It first checks the remote port state. If the
5292 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5293 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5294 * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
5295 * Value (RTV) unsolicited IOCB event.
5296 *
5297 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5298 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5299 * will be stored into the context1 field of the IOCB for the completion
5300 * callback function to the RPS Accept Response ELS IOCB command.
5301 *
5302 * Return codes
5303 * 0 - Successfully processed rtv iocb (currently always return 0)
5304 **/
5305static int
5306lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5307 struct lpfc_nodelist *ndlp)
5308{
5309 struct lpfc_hba *phba = vport->phba;
5310 struct ls_rjt stat;
5311 struct RTV_RSP *rtv_rsp;
5312 uint8_t *pcmd;
5313 struct lpfc_iocbq *elsiocb;
5314 uint32_t cmdsize;
5315
5316
5317 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5318 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5319 /* reject the unsolicited RPS request and done with it */
5320 goto reject_out;
5321
5322 cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
5323 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5324 lpfc_max_els_tries, ndlp,
5325 ndlp->nlp_DID, ELS_CMD_ACC);
5326
5327 if (!elsiocb)
5328 return 1;
5329
5330 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5331 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5332 pcmd += sizeof(uint32_t); /* Skip past command */
5333
5334 /* use the command's xri in the response */
7851fe2c
JS
5335 elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext; /* Xri / rx_id */
5336 elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
12265f68
JS
5337
5338 rtv_rsp = (struct RTV_RSP *)pcmd;
5339
5340 /* populate RTV payload */
5341 rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
5342 rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
5343 bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
5344 bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
5345 rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
5346
5347 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5348 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5349 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
5350 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
5351 "Data: x%x x%x x%x\n",
5352 elsiocb->iotag, elsiocb->iocb.ulpContext,
5353 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5354 ndlp->nlp_rpi,
5355 rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
5356 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5357 phba->fc_stat.elsXmitACC++;
5358 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5359 lpfc_els_free_iocb(phba, elsiocb);
5360 return 0;
5361
5362reject_out:
5363 /* issue rejection response */
5364 stat.un.b.lsRjtRsvd0 = 0;
5365 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5366 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5367 stat.un.b.vendorUnique = 0;
5368 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5369 return 0;
5370}
5371
5372/* lpfc_els_rcv_rps - Process an unsolicited rps iocb
e59058c4
JS
5373 * @vport: pointer to a host virtual N_Port data structure.
5374 * @cmdiocb: pointer to lpfc command iocb data structure.
5375 * @ndlp: pointer to a node-list data structure.
5376 *
5377 * This routine processes Read Port Status (RPS) IOCB received as an
5378 * ELS unsolicited event. It first checks the remote port state. If the
5379 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5380 * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
5381 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5382 * for reading the HBA link statistics. It is for the callback function,
5383 * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
5384 * to actually sending out RPS Accept (ACC) response.
5385 *
5386 * Return codes
5387 * 0 - Successfully processed rps iocb (currently always return 0)
5388 **/
7bb3b137 5389static int
2e0fef85
JS
5390lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5391 struct lpfc_nodelist *ndlp)
dea3101e 5392{
2e0fef85 5393 struct lpfc_hba *phba = vport->phba;
dea3101e 5394 uint32_t *lp;
7bb3b137
JW
5395 uint8_t flag;
5396 LPFC_MBOXQ_t *mbox;
5397 struct lpfc_dmabuf *pcmd;
5398 RPS *rps;
5399 struct ls_rjt stat;
5400
2fe165b6 5401 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
90160e01
JS
5402 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5403 /* reject the unsolicited RPS request and done with it */
5404 goto reject_out;
7bb3b137
JW
5405
5406 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5407 lp = (uint32_t *) pcmd->virt;
5408 flag = (be32_to_cpu(*lp++) & 0xf);
5409 rps = (RPS *) lp;
5410
5411 if ((flag == 0) ||
5412 ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
2e0fef85 5413 ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
92d7f7b0 5414 sizeof(struct lpfc_name)) == 0))) {
2e0fef85 5415
92d7f7b0
JS
5416 printk("Fix me....\n");
5417 dump_stack();
2e0fef85
JS
5418 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5419 if (mbox) {
7bb3b137 5420 lpfc_read_lnk_stat(phba, mbox);
7851fe2c
JS
5421 mbox->context1 = (void *)((unsigned long)
5422 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
5423 cmdiocb->iocb.ulpContext)); /* rx_id */
329f9bc7 5424 mbox->context2 = lpfc_nlp_get(ndlp);
92d7f7b0 5425 mbox->vport = vport;
7bb3b137 5426 mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
fa4066b6 5427 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
0b727fea 5428 != MBX_NOT_FINISHED)
7bb3b137
JW
5429 /* Mbox completion will send ELS Response */
5430 return 0;
fa4066b6
JS
5431 /* Decrement reference count used for the failed mbox
5432 * command.
5433 */
329f9bc7 5434 lpfc_nlp_put(ndlp);
7bb3b137
JW
5435 mempool_free(mbox, phba->mbox_mem_pool);
5436 }
5437 }
90160e01
JS
5438
5439reject_out:
5440 /* issue rejection response */
7bb3b137
JW
5441 stat.un.b.lsRjtRsvd0 = 0;
5442 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5443 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5444 stat.un.b.vendorUnique = 0;
858c9f6c 5445 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7bb3b137
JW
5446 return 0;
5447}
5448
19ca7609
JS
5449/* lpfc_issue_els_rrq - Process an unsolicited rps iocb
5450 * @vport: pointer to a host virtual N_Port data structure.
5451 * @ndlp: pointer to a node-list data structure.
5452 * @did: DID of the target.
5453 * @rrq: Pointer to the rrq struct.
5454 *
5455 * Build a ELS RRQ command and send it to the target. If the issue_iocb is
5456 * Successful the the completion handler will clear the RRQ.
5457 *
5458 * Return codes
5459 * 0 - Successfully sent rrq els iocb.
5460 * 1 - Failed to send rrq els iocb.
5461 **/
5462static int
5463lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5464 uint32_t did, struct lpfc_node_rrq *rrq)
5465{
5466 struct lpfc_hba *phba = vport->phba;
5467 struct RRQ *els_rrq;
5468 IOCB_t *icmd;
5469 struct lpfc_iocbq *elsiocb;
5470 uint8_t *pcmd;
5471 uint16_t cmdsize;
5472 int ret;
5473
5474
5475 if (ndlp != rrq->ndlp)
5476 ndlp = rrq->ndlp;
5477 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5478 return 1;
5479
5480 /* If ndlp is not NULL, we will bump the reference count on it */
5481 cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
5482 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
5483 ELS_CMD_RRQ);
5484 if (!elsiocb)
5485 return 1;
5486
5487 icmd = &elsiocb->iocb;
5488 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5489
5490 /* For RRQ request, remainder of payload is Exchange IDs */
5491 *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
5492 pcmd += sizeof(uint32_t);
5493 els_rrq = (struct RRQ *) pcmd;
5494
5495 bf_set(rrq_oxid, els_rrq, rrq->xritag);
5496 bf_set(rrq_rxid, els_rrq, rrq->rxid);
5497 bf_set(rrq_did, els_rrq, vport->fc_myDID);
5498 els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
5499 els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
5500
5501
5502 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5503 "Issue RRQ: did:x%x",
5504 did, rrq->xritag, rrq->rxid);
5505 elsiocb->context_un.rrq = rrq;
5506 elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
5507 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5508
5509 if (ret == IOCB_ERROR) {
5510 lpfc_els_free_iocb(phba, elsiocb);
5511 return 1;
5512 }
5513 return 0;
5514}
5515
5516/**
5517 * lpfc_send_rrq - Sends ELS RRQ if needed.
5518 * @phba: pointer to lpfc hba data structure.
5519 * @rrq: pointer to the active rrq.
5520 *
5521 * This routine will call the lpfc_issue_els_rrq if the rrq is
5522 * still active for the xri. If this function returns a failure then
5523 * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
5524 *
5525 * Returns 0 Success.
5526 * 1 Failure.
5527 **/
5528int
5529lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
5530{
5531 struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
5532 rrq->nlp_DID);
5533 if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
5534 return lpfc_issue_els_rrq(rrq->vport, ndlp,
5535 rrq->nlp_DID, rrq);
5536 else
5537 return 1;
5538}
5539
e59058c4 5540/**
3621a710 5541 * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
e59058c4
JS
5542 * @vport: pointer to a host virtual N_Port data structure.
5543 * @cmdsize: size of the ELS command.
5544 * @oldiocb: pointer to the original lpfc command iocb data structure.
5545 * @ndlp: pointer to a node-list data structure.
5546 *
5547 * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
5548 * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
5549 *
5550 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5551 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5552 * will be stored into the context1 field of the IOCB for the completion
5553 * callback function to the RPL Accept Response ELS command.
5554 *
5555 * Return code
5556 * 0 - Successfully issued ACC RPL ELS command
5557 * 1 - Failed to issue ACC RPL ELS command
5558 **/
082c0266 5559static int
2e0fef85
JS
5560lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
5561 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7bb3b137 5562{
2e0fef85
JS
5563 struct lpfc_hba *phba = vport->phba;
5564 IOCB_t *icmd, *oldcmd;
7bb3b137
JW
5565 RPL_RSP rpl_rsp;
5566 struct lpfc_iocbq *elsiocb;
7bb3b137 5567 uint8_t *pcmd;
dea3101e 5568
2e0fef85
JS
5569 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5570 ndlp->nlp_DID, ELS_CMD_ACC);
7bb3b137 5571
488d1469 5572 if (!elsiocb)
7bb3b137 5573 return 1;
488d1469 5574
7bb3b137
JW
5575 icmd = &elsiocb->iocb;
5576 oldcmd = &oldiocb->iocb;
7851fe2c
JS
5577 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5578 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7bb3b137
JW
5579
5580 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5581 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 5582 pcmd += sizeof(uint16_t);
7bb3b137
JW
5583 *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
5584 pcmd += sizeof(uint16_t);
5585
5586 /* Setup the RPL ACC payload */
5587 rpl_rsp.listLen = be32_to_cpu(1);
5588 rpl_rsp.index = 0;
5589 rpl_rsp.port_num_blk.portNum = 0;
2e0fef85
JS
5590 rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
5591 memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7bb3b137 5592 sizeof(struct lpfc_name));
7bb3b137 5593 memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7bb3b137 5594 /* Xmit ELS RPL ACC response tag <ulpIoTag> */
e8b62011
JS
5595 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5596 "0120 Xmit ELS RPL ACC response tag x%x "
5597 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5598 "rpi x%x\n",
5599 elsiocb->iotag, elsiocb->iocb.ulpContext,
5600 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5601 ndlp->nlp_rpi);
858c9f6c 5602 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7bb3b137 5603 phba->fc_stat.elsXmitACC++;
3772a991
JS
5604 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
5605 IOCB_ERROR) {
7bb3b137
JW
5606 lpfc_els_free_iocb(phba, elsiocb);
5607 return 1;
5608 }
5609 return 0;
5610}
5611
e59058c4 5612/**
3621a710 5613 * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
e59058c4
JS
5614 * @vport: pointer to a host virtual N_Port data structure.
5615 * @cmdiocb: pointer to lpfc command iocb data structure.
5616 * @ndlp: pointer to a node-list data structure.
5617 *
5618 * This routine processes Read Port List (RPL) IOCB received as an ELS
5619 * unsolicited event. It first checks the remote port state. If the remote
5620 * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
5621 * invokes the lpfc_els_rsp_reject() routine to send reject response.
5622 * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
5623 * to accept the RPL.
5624 *
5625 * Return code
5626 * 0 - Successfully processed rpl iocb (currently always return 0)
5627 **/
7bb3b137 5628static int
2e0fef85
JS
5629lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5630 struct lpfc_nodelist *ndlp)
7bb3b137
JW
5631{
5632 struct lpfc_dmabuf *pcmd;
5633 uint32_t *lp;
5634 uint32_t maxsize;
5635 uint16_t cmdsize;
5636 RPL *rpl;
5637 struct ls_rjt stat;
5638
2fe165b6
JW
5639 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5640 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
90160e01 5641 /* issue rejection response */
7bb3b137
JW
5642 stat.un.b.lsRjtRsvd0 = 0;
5643 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5644 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5645 stat.un.b.vendorUnique = 0;
858c9f6c
JS
5646 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5647 NULL);
90160e01
JS
5648 /* rejected the unsolicited RPL request and done with it */
5649 return 0;
7bb3b137
JW
5650 }
5651
dea3101e
JB
5652 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5653 lp = (uint32_t *) pcmd->virt;
7bb3b137 5654 rpl = (RPL *) (lp + 1);
7bb3b137 5655 maxsize = be32_to_cpu(rpl->maxsize);
dea3101e 5656
7bb3b137
JW
5657 /* We support only one port */
5658 if ((rpl->index == 0) &&
5659 ((maxsize == 0) ||
5660 ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
5661 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
2fe165b6 5662 } else {
7bb3b137
JW
5663 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
5664 }
2e0fef85 5665 lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
dea3101e
JB
5666
5667 return 0;
5668}
5669
e59058c4 5670/**
3621a710 5671 * lpfc_els_rcv_farp - Process an unsolicited farp request els command
e59058c4
JS
5672 * @vport: pointer to a virtual N_Port data structure.
5673 * @cmdiocb: pointer to lpfc command iocb data structure.
5674 * @ndlp: pointer to a node-list data structure.
5675 *
5676 * This routine processes Fibre Channel Address Resolution Protocol
5677 * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
5678 * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
5679 * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
5680 * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
5681 * remote PortName is compared against the FC PortName stored in the @vport
5682 * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
5683 * compared against the FC NodeName stored in the @vport data structure.
5684 * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
5685 * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
5686 * invoked to send out FARP Response to the remote node. Before sending the
5687 * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
5688 * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
5689 * routine is invoked to log into the remote port first.
5690 *
5691 * Return code
5692 * 0 - Either the FARP Match Mode not supported or successfully processed
5693 **/
dea3101e 5694static int
2e0fef85
JS
5695lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5696 struct lpfc_nodelist *ndlp)
dea3101e
JB
5697{
5698 struct lpfc_dmabuf *pcmd;
5699 uint32_t *lp;
5700 IOCB_t *icmd;
5701 FARP *fp;
5702 uint32_t cmd, cnt, did;
5703
5704 icmd = &cmdiocb->iocb;
5705 did = icmd->un.elsreq64.remoteID;
5706 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5707 lp = (uint32_t *) pcmd->virt;
5708
5709 cmd = *lp++;
5710 fp = (FARP *) lp;
dea3101e 5711 /* FARP-REQ received from DID <did> */
e8b62011
JS
5712 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5713 "0601 FARP-REQ received from DID x%x\n", did);
dea3101e
JB
5714 /* We will only support match on WWPN or WWNN */
5715 if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
c9f8735b 5716 return 0;
dea3101e
JB
5717 }
5718
5719 cnt = 0;
5720 /* If this FARP command is searching for my portname */
5721 if (fp->Mflags & FARP_MATCH_PORT) {
2e0fef85 5722 if (memcmp(&fp->RportName, &vport->fc_portname,
92d7f7b0 5723 sizeof(struct lpfc_name)) == 0)
dea3101e
JB
5724 cnt = 1;
5725 }
5726
5727 /* If this FARP command is searching for my nodename */
5728 if (fp->Mflags & FARP_MATCH_NODE) {
2e0fef85 5729 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
92d7f7b0 5730 sizeof(struct lpfc_name)) == 0)
dea3101e
JB
5731 cnt = 1;
5732 }
5733
5734 if (cnt) {
5735 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
5736 (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
5737 /* Log back into the node before sending the FARP. */
5738 if (fp->Rflags & FARP_REQUEST_PLOGI) {
5024ab17 5739 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 5740 lpfc_nlp_set_state(vport, ndlp,
de0c5b32 5741 NLP_STE_PLOGI_ISSUE);
2e0fef85 5742 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
dea3101e
JB
5743 }
5744
5745 /* Send a FARP response to that node */
2e0fef85
JS
5746 if (fp->Rflags & FARP_REQUEST_FARPR)
5747 lpfc_issue_els_farpr(vport, did, 0);
dea3101e
JB
5748 }
5749 }
c9f8735b 5750 return 0;
dea3101e
JB
5751}
5752
e59058c4 5753/**
3621a710 5754 * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
e59058c4
JS
5755 * @vport: pointer to a host virtual N_Port data structure.
5756 * @cmdiocb: pointer to lpfc command iocb data structure.
5757 * @ndlp: pointer to a node-list data structure.
5758 *
5759 * This routine processes Fibre Channel Address Resolution Protocol
5760 * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
5761 * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
5762 * the FARP response request.
5763 *
5764 * Return code
5765 * 0 - Successfully processed FARPR IOCB (currently always return 0)
5766 **/
dea3101e 5767static int
2e0fef85
JS
5768lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5769 struct lpfc_nodelist *ndlp)
dea3101e
JB
5770{
5771 struct lpfc_dmabuf *pcmd;
5772 uint32_t *lp;
5773 IOCB_t *icmd;
5774 uint32_t cmd, did;
5775
5776 icmd = &cmdiocb->iocb;
5777 did = icmd->un.elsreq64.remoteID;
5778 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5779 lp = (uint32_t *) pcmd->virt;
5780
5781 cmd = *lp++;
5782 /* FARP-RSP received from DID <did> */
e8b62011
JS
5783 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5784 "0600 FARP-RSP received from DID x%x\n", did);
dea3101e 5785 /* ACCEPT the Farp resp request */
51ef4c26 5786 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e
JB
5787
5788 return 0;
5789}
5790
e59058c4 5791/**
3621a710 5792 * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
e59058c4
JS
5793 * @vport: pointer to a host virtual N_Port data structure.
5794 * @cmdiocb: pointer to lpfc command iocb data structure.
5795 * @fan_ndlp: pointer to a node-list data structure.
5796 *
5797 * This routine processes a Fabric Address Notification (FAN) IOCB
5798 * command received as an ELS unsolicited event. The FAN ELS command will
5799 * only be processed on a physical port (i.e., the @vport represents the
5800 * physical port). The fabric NodeName and PortName from the FAN IOCB are
5801 * compared against those in the phba data structure. If any of those is
5802 * different, the lpfc_initial_flogi() routine is invoked to initialize
5803 * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
5804 * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
5805 * is invoked to register login to the fabric.
5806 *
5807 * Return code
5808 * 0 - Successfully processed fan iocb (currently always return 0).
5809 **/
dea3101e 5810static int
2e0fef85
JS
5811lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5812 struct lpfc_nodelist *fan_ndlp)
dea3101e 5813{
0d2b6b83 5814 struct lpfc_hba *phba = vport->phba;
dea3101e 5815 uint32_t *lp;
5024ab17 5816 FAN *fp;
dea3101e 5817
0d2b6b83
JS
5818 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
5819 lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
5820 fp = (FAN *) ++lp;
5024ab17 5821 /* FAN received; Fan does not have a reply sequence */
0d2b6b83
JS
5822 if ((vport == phba->pport) &&
5823 (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5024ab17 5824 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
0d2b6b83 5825 sizeof(struct lpfc_name))) ||
5024ab17 5826 (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
0d2b6b83
JS
5827 sizeof(struct lpfc_name)))) {
5828 /* This port has switched fabrics. FLOGI is required */
76a95d75 5829 lpfc_issue_init_vfi(vport);
0d2b6b83
JS
5830 } else {
5831 /* FAN verified - skip FLOGI */
5832 vport->fc_myDID = vport->fc_prevDID;
6fb120a7
JS
5833 if (phba->sli_rev < LPFC_SLI_REV4)
5834 lpfc_issue_fabric_reglogin(vport);
5835 else
5836 lpfc_issue_reg_vfi(vport);
5024ab17 5837 }
dea3101e 5838 }
c9f8735b 5839 return 0;
dea3101e
JB
5840}
5841
e59058c4 5842/**
3621a710 5843 * lpfc_els_timeout - Handler funciton to the els timer
e59058c4
JS
5844 * @ptr: holder for the timer function associated data.
5845 *
5846 * This routine is invoked by the ELS timer after timeout. It posts the ELS
5847 * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
5848 * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
5849 * up the worker thread. It is for the worker thread to invoke the routine
5850 * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
5851 **/
dea3101e
JB
5852void
5853lpfc_els_timeout(unsigned long ptr)
5854{
2e0fef85
JS
5855 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5856 struct lpfc_hba *phba = vport->phba;
5e9d9b82 5857 uint32_t tmo_posted;
dea3101e
JB
5858 unsigned long iflag;
5859
2e0fef85 5860 spin_lock_irqsave(&vport->work_port_lock, iflag);
5e9d9b82
JS
5861 tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
5862 if (!tmo_posted)
2e0fef85 5863 vport->work_port_events |= WORKER_ELS_TMO;
5e9d9b82 5864 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
92d7f7b0 5865
5e9d9b82
JS
5866 if (!tmo_posted)
5867 lpfc_worker_wake_up(phba);
dea3101e
JB
5868 return;
5869}
5870
2a9bf3d0 5871
e59058c4 5872/**
3621a710 5873 * lpfc_els_timeout_handler - Process an els timeout event
e59058c4
JS
5874 * @vport: pointer to a virtual N_Port data structure.
5875 *
5876 * This routine is the actual handler function that processes an ELS timeout
5877 * event. It walks the ELS ring to get and abort all the IOCBs (except the
5878 * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
5879 * invoking the lpfc_sli_issue_abort_iotag() routine.
5880 **/
dea3101e 5881void
2e0fef85 5882lpfc_els_timeout_handler(struct lpfc_vport *vport)
dea3101e 5883{
2e0fef85 5884 struct lpfc_hba *phba = vport->phba;
dea3101e
JB
5885 struct lpfc_sli_ring *pring;
5886 struct lpfc_iocbq *tmp_iocb, *piocb;
5887 IOCB_t *cmd = NULL;
5888 struct lpfc_dmabuf *pcmd;
2e0fef85 5889 uint32_t els_command = 0;
dea3101e 5890 uint32_t timeout;
2e0fef85 5891 uint32_t remote_ID = 0xffffffff;
2a9bf3d0
JS
5892 LIST_HEAD(txcmplq_completions);
5893 LIST_HEAD(abort_list);
5894
dea3101e 5895
dea3101e
JB
5896 timeout = (uint32_t)(phba->fc_ratov << 1);
5897
5898 pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 5899
2a9bf3d0
JS
5900 spin_lock_irq(&phba->hbalock);
5901 list_splice_init(&pring->txcmplq, &txcmplq_completions);
5902 spin_unlock_irq(&phba->hbalock);
5903
5904 list_for_each_entry_safe(piocb, tmp_iocb, &txcmplq_completions, list) {
dea3101e
JB
5905 cmd = &piocb->iocb;
5906
2e0fef85
JS
5907 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
5908 piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
5909 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
dea3101e 5910 continue;
2e0fef85
JS
5911
5912 if (piocb->vport != vport)
5913 continue;
5914
dea3101e 5915 pcmd = (struct lpfc_dmabuf *) piocb->context2;
2e0fef85
JS
5916 if (pcmd)
5917 els_command = *(uint32_t *) (pcmd->virt);
dea3101e 5918
92d7f7b0
JS
5919 if (els_command == ELS_CMD_FARP ||
5920 els_command == ELS_CMD_FARPR ||
5921 els_command == ELS_CMD_FDISC)
5922 continue;
5923
dea3101e 5924 if (piocb->drvrTimeout > 0) {
92d7f7b0 5925 if (piocb->drvrTimeout >= timeout)
dea3101e 5926 piocb->drvrTimeout -= timeout;
92d7f7b0 5927 else
dea3101e 5928 piocb->drvrTimeout = 0;
dea3101e
JB
5929 continue;
5930 }
5931
2e0fef85
JS
5932 remote_ID = 0xffffffff;
5933 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
dea3101e 5934 remote_ID = cmd->un.elsreq64.remoteID;
2e0fef85
JS
5935 else {
5936 struct lpfc_nodelist *ndlp;
5937 ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
58da1ffb 5938 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2e0fef85 5939 remote_ID = ndlp->nlp_DID;
dea3101e 5940 }
2a9bf3d0
JS
5941 list_add_tail(&piocb->dlist, &abort_list);
5942 }
5943 spin_lock_irq(&phba->hbalock);
5944 list_splice(&txcmplq_completions, &pring->txcmplq);
5945 spin_unlock_irq(&phba->hbalock);
5946
5947 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
e8b62011 5948 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2a9bf3d0
JS
5949 "0127 ELS timeout Data: x%x x%x x%x "
5950 "x%x\n", els_command,
5951 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
5952 spin_lock_irq(&phba->hbalock);
5953 list_del_init(&piocb->dlist);
07951076 5954 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
2a9bf3d0 5955 spin_unlock_irq(&phba->hbalock);
dea3101e 5956 }
5a0e326d 5957
2e0fef85
JS
5958 if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
5959 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
dea3101e
JB
5960}
5961
e59058c4 5962/**
3621a710 5963 * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
e59058c4
JS
5964 * @vport: pointer to a host virtual N_Port data structure.
5965 *
5966 * This routine is used to clean up all the outstanding ELS commands on a
5967 * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
5968 * routine. After that, it walks the ELS transmit queue to remove all the
5969 * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
5970 * the IOCBs with a non-NULL completion callback function, the callback
5971 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5972 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
5973 * callback function, the IOCB will simply be released. Finally, it walks
5974 * the ELS transmit completion queue to issue an abort IOCB to any transmit
5975 * completion queue IOCB that is associated with the @vport and is not
5976 * an IOCB from libdfc (i.e., the management plane IOCBs that are not
5977 * part of the discovery state machine) out to HBA by invoking the
5978 * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
5979 * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
5980 * the IOCBs are aborted when this function returns.
5981 **/
dea3101e 5982void
2e0fef85 5983lpfc_els_flush_cmd(struct lpfc_vport *vport)
dea3101e 5984{
2534ba75 5985 LIST_HEAD(completions);
2e0fef85 5986 struct lpfc_hba *phba = vport->phba;
329f9bc7 5987 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e
JB
5988 struct lpfc_iocbq *tmp_iocb, *piocb;
5989 IOCB_t *cmd = NULL;
92d7f7b0
JS
5990
5991 lpfc_fabric_abort_vport(vport);
dea3101e 5992
2e0fef85 5993 spin_lock_irq(&phba->hbalock);
dea3101e
JB
5994 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5995 cmd = &piocb->iocb;
5996
5997 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5998 continue;
5999 }
6000
6001 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
329f9bc7
JS
6002 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
6003 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
6004 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
6005 cmd->ulpCommand == CMD_ABORT_XRI_CN)
dea3101e 6006 continue;
dea3101e 6007
2e0fef85
JS
6008 if (piocb->vport != vport)
6009 continue;
6010
2534ba75 6011 list_move_tail(&piocb->list, &completions);
1dcb58e5 6012 pring->txq_cnt--;
dea3101e
JB
6013 }
6014
6015 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
dea3101e
JB
6016 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
6017 continue;
6018 }
dea3101e 6019
2e0fef85
JS
6020 if (piocb->vport != vport)
6021 continue;
6022
07951076 6023 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
dea3101e 6024 }
2e0fef85 6025 spin_unlock_irq(&phba->hbalock);
2534ba75 6026
a257bf90
JS
6027 /* Cancell all the IOCBs from the completions list */
6028 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6029 IOERR_SLI_ABORTED);
2534ba75 6030
dea3101e
JB
6031 return;
6032}
6033
e59058c4 6034/**
3621a710 6035 * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
e59058c4
JS
6036 * @phba: pointer to lpfc hba data structure.
6037 *
6038 * This routine is used to clean up all the outstanding ELS commands on a
6039 * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
6040 * routine. After that, it walks the ELS transmit queue to remove all the
6041 * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
6042 * the IOCBs with the completion callback function associated, the callback
6043 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
6044 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
6045 * callback function associated, the IOCB will simply be released. Finally,
6046 * it walks the ELS transmit completion queue to issue an abort IOCB to any
6047 * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
6048 * management plane IOCBs that are not part of the discovery state machine)
6049 * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
6050 **/
549e55cd
JS
6051void
6052lpfc_els_flush_all_cmd(struct lpfc_hba *phba)
6053{
6054 LIST_HEAD(completions);
6055 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6056 struct lpfc_iocbq *tmp_iocb, *piocb;
6057 IOCB_t *cmd = NULL;
6058
6059 lpfc_fabric_abort_hba(phba);
6060 spin_lock_irq(&phba->hbalock);
6061 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
6062 cmd = &piocb->iocb;
6063 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
6064 continue;
6065 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
6066 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
6067 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
6068 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
6069 cmd->ulpCommand == CMD_ABORT_XRI_CN)
6070 continue;
6071 list_move_tail(&piocb->list, &completions);
6072 pring->txq_cnt--;
6073 }
6074 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6075 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
6076 continue;
6077 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6078 }
6079 spin_unlock_irq(&phba->hbalock);
a257bf90
JS
6080
6081 /* Cancel all the IOCBs from the completions list */
6082 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6083 IOERR_SLI_ABORTED);
6084
549e55cd
JS
6085 return;
6086}
6087
ea2151b4 6088/**
3621a710 6089 * lpfc_send_els_failure_event - Posts an ELS command failure event
ea2151b4
JS
6090 * @phba: Pointer to hba context object.
6091 * @cmdiocbp: Pointer to command iocb which reported error.
6092 * @rspiocbp: Pointer to response iocb which reported error.
6093 *
6094 * This function sends an event when there is an ELS command
6095 * failure.
6096 **/
6097void
6098lpfc_send_els_failure_event(struct lpfc_hba *phba,
6099 struct lpfc_iocbq *cmdiocbp,
6100 struct lpfc_iocbq *rspiocbp)
6101{
6102 struct lpfc_vport *vport = cmdiocbp->vport;
6103 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6104 struct lpfc_lsrjt_event lsrjt_event;
6105 struct lpfc_fabric_event_header fabric_event;
6106 struct ls_rjt stat;
6107 struct lpfc_nodelist *ndlp;
6108 uint32_t *pcmd;
6109
6110 ndlp = cmdiocbp->context1;
6111 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
6112 return;
6113
6114 if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
6115 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
6116 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
6117 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
6118 sizeof(struct lpfc_name));
6119 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
6120 sizeof(struct lpfc_name));
6121 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
6122 cmdiocbp->context2)->virt);
49198b37 6123 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
ea2151b4
JS
6124 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
6125 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
6126 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
6127 fc_host_post_vendor_event(shost,
6128 fc_get_event_number(),
6129 sizeof(lsrjt_event),
6130 (char *)&lsrjt_event,
ddcc50f0 6131 LPFC_NL_VENDOR_ID);
ea2151b4
JS
6132 return;
6133 }
6134 if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
6135 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
6136 fabric_event.event_type = FC_REG_FABRIC_EVENT;
6137 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
6138 fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
6139 else
6140 fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
6141 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
6142 sizeof(struct lpfc_name));
6143 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
6144 sizeof(struct lpfc_name));
6145 fc_host_post_vendor_event(shost,
6146 fc_get_event_number(),
6147 sizeof(fabric_event),
6148 (char *)&fabric_event,
ddcc50f0 6149 LPFC_NL_VENDOR_ID);
ea2151b4
JS
6150 return;
6151 }
6152
6153}
6154
6155/**
3621a710 6156 * lpfc_send_els_event - Posts unsolicited els event
ea2151b4
JS
6157 * @vport: Pointer to vport object.
6158 * @ndlp: Pointer FC node object.
6159 * @cmd: ELS command code.
6160 *
6161 * This function posts an event when there is an incoming
6162 * unsolicited ELS command.
6163 **/
6164static void
6165lpfc_send_els_event(struct lpfc_vport *vport,
6166 struct lpfc_nodelist *ndlp,
ddcc50f0 6167 uint32_t *payload)
ea2151b4 6168{
ddcc50f0
JS
6169 struct lpfc_els_event_header *els_data = NULL;
6170 struct lpfc_logo_event *logo_data = NULL;
ea2151b4
JS
6171 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6172
ddcc50f0
JS
6173 if (*payload == ELS_CMD_LOGO) {
6174 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
6175 if (!logo_data) {
6176 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6177 "0148 Failed to allocate memory "
6178 "for LOGO event\n");
6179 return;
6180 }
6181 els_data = &logo_data->header;
6182 } else {
6183 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
6184 GFP_KERNEL);
6185 if (!els_data) {
6186 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6187 "0149 Failed to allocate memory "
6188 "for ELS event\n");
6189 return;
6190 }
6191 }
6192 els_data->event_type = FC_REG_ELS_EVENT;
6193 switch (*payload) {
ea2151b4 6194 case ELS_CMD_PLOGI:
ddcc50f0 6195 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
ea2151b4
JS
6196 break;
6197 case ELS_CMD_PRLO:
ddcc50f0 6198 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
ea2151b4
JS
6199 break;
6200 case ELS_CMD_ADISC:
ddcc50f0
JS
6201 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
6202 break;
6203 case ELS_CMD_LOGO:
6204 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
6205 /* Copy the WWPN in the LOGO payload */
6206 memcpy(logo_data->logo_wwpn, &payload[2],
6207 sizeof(struct lpfc_name));
ea2151b4
JS
6208 break;
6209 default:
e916141c 6210 kfree(els_data);
ea2151b4
JS
6211 return;
6212 }
ddcc50f0
JS
6213 memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
6214 memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
6215 if (*payload == ELS_CMD_LOGO) {
6216 fc_host_post_vendor_event(shost,
6217 fc_get_event_number(),
6218 sizeof(struct lpfc_logo_event),
6219 (char *)logo_data,
6220 LPFC_NL_VENDOR_ID);
6221 kfree(logo_data);
6222 } else {
6223 fc_host_post_vendor_event(shost,
6224 fc_get_event_number(),
6225 sizeof(struct lpfc_els_event_header),
6226 (char *)els_data,
6227 LPFC_NL_VENDOR_ID);
6228 kfree(els_data);
6229 }
ea2151b4
JS
6230
6231 return;
6232}
6233
6234
e59058c4 6235/**
3621a710 6236 * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
e59058c4
JS
6237 * @phba: pointer to lpfc hba data structure.
6238 * @pring: pointer to a SLI ring.
6239 * @vport: pointer to a host virtual N_Port data structure.
6240 * @elsiocb: pointer to lpfc els command iocb data structure.
6241 *
6242 * This routine is used for processing the IOCB associated with a unsolicited
6243 * event. It first determines whether there is an existing ndlp that matches
6244 * the DID from the unsolicited IOCB. If not, it will create a new one with
6245 * the DID from the unsolicited IOCB. The ELS command from the unsolicited
6246 * IOCB is then used to invoke the proper routine and to set up proper state
6247 * of the discovery state machine.
6248 **/
ed957684
JS
6249static void
6250lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
92d7f7b0 6251 struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
dea3101e 6252{
87af33fe 6253 struct Scsi_Host *shost;
dea3101e 6254 struct lpfc_nodelist *ndlp;
dea3101e 6255 struct ls_rjt stat;
92d7f7b0 6256 uint32_t *payload;
2e0fef85 6257 uint32_t cmd, did, newnode, rjt_err = 0;
ed957684 6258 IOCB_t *icmd = &elsiocb->iocb;
dea3101e 6259
e47c9093 6260 if (!vport || !(elsiocb->context2))
dea3101e 6261 goto dropit;
2e0fef85 6262
dea3101e 6263 newnode = 0;
92d7f7b0
JS
6264 payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
6265 cmd = *payload;
ed957684 6266 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
495a714c 6267 lpfc_post_buffer(phba, pring, 1);
dea3101e 6268
858c9f6c
JS
6269 did = icmd->un.rcvels.remoteID;
6270 if (icmd->ulpStatus) {
6271 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6272 "RCV Unsol ELS: status:x%x/x%x did:x%x",
6273 icmd->ulpStatus, icmd->un.ulpWord[4], did);
dea3101e 6274 goto dropit;
858c9f6c 6275 }
dea3101e
JB
6276
6277 /* Check to see if link went down during discovery */
ed957684 6278 if (lpfc_els_chk_latt(vport))
dea3101e 6279 goto dropit;
dea3101e 6280
c868595d 6281 /* Ignore traffic received during vport shutdown. */
92d7f7b0
JS
6282 if (vport->load_flag & FC_UNLOADING)
6283 goto dropit;
6284
92494144
JS
6285 /* If NPort discovery is delayed drop incoming ELS */
6286 if ((vport->fc_flag & FC_DISC_DELAYED) &&
6287 (cmd != ELS_CMD_PLOGI))
6288 goto dropit;
6289
2e0fef85 6290 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 6291 if (!ndlp) {
dea3101e 6292 /* Cannot find existing Fabric ndlp, so allocate a new one */
c9f8735b 6293 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
ed957684 6294 if (!ndlp)
dea3101e 6295 goto dropit;
dea3101e 6296
2e0fef85 6297 lpfc_nlp_init(vport, ndlp, did);
98c9ea5c 6298 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
dea3101e 6299 newnode = 1;
e47c9093 6300 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
dea3101e 6301 ndlp->nlp_type |= NLP_FABRIC;
58da1ffb
JS
6302 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
6303 ndlp = lpfc_enable_node(vport, ndlp,
6304 NLP_STE_UNUSED_NODE);
6305 if (!ndlp)
6306 goto dropit;
6307 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6308 newnode = 1;
6309 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6310 ndlp->nlp_type |= NLP_FABRIC;
6311 } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
6312 /* This is similar to the new node path */
6313 ndlp = lpfc_nlp_get(ndlp);
6314 if (!ndlp)
6315 goto dropit;
6316 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6317 newnode = 1;
87af33fe 6318 }
dea3101e
JB
6319
6320 phba->fc_stat.elsRcvFrame++;
e47c9093 6321
329f9bc7 6322 elsiocb->context1 = lpfc_nlp_get(ndlp);
2e0fef85 6323 elsiocb->vport = vport;
dea3101e
JB
6324
6325 if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
6326 cmd &= ELS_CMD_MASK;
6327 }
6328 /* ELS command <elsCmd> received from NPORT <did> */
e8b62011
JS
6329 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6330 "0112 ELS command x%x received from NPORT x%x "
6331 "Data: x%x\n", cmd, did, vport->port_state);
dea3101e
JB
6332 switch (cmd) {
6333 case ELS_CMD_PLOGI:
858c9f6c
JS
6334 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6335 "RCV PLOGI: did:x%x/ste:x%x flg:x%x",
6336 did, vport->port_state, ndlp->nlp_flag);
6337
dea3101e 6338 phba->fc_stat.elsRcvPLOGI++;
858c9f6c
JS
6339 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
6340
ddcc50f0 6341 lpfc_send_els_event(vport, ndlp, payload);
92494144
JS
6342
6343 /* If Nport discovery is delayed, reject PLOGIs */
6344 if (vport->fc_flag & FC_DISC_DELAYED) {
6345 rjt_err = LSRJT_UNABLE_TPC;
6346 break;
6347 }
858c9f6c 6348 if (vport->port_state < LPFC_DISC_AUTH) {
1b32f6aa
JS
6349 if (!(phba->pport->fc_flag & FC_PT2PT) ||
6350 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
6351 rjt_err = LSRJT_UNABLE_TPC;
6352 break;
6353 }
6354 /* We get here, and drop thru, if we are PT2PT with
6355 * another NPort and the other side has initiated
6356 * the PLOGI before responding to our FLOGI.
6357 */
dea3101e 6358 }
87af33fe
JS
6359
6360 shost = lpfc_shost_from_vport(vport);
6361 spin_lock_irq(shost->host_lock);
6362 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
6363 spin_unlock_irq(shost->host_lock);
6364
2e0fef85
JS
6365 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6366 NLP_EVT_RCV_PLOGI);
858c9f6c 6367
dea3101e
JB
6368 break;
6369 case ELS_CMD_FLOGI:
858c9f6c
JS
6370 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6371 "RCV FLOGI: did:x%x/ste:x%x flg:x%x",
6372 did, vport->port_state, ndlp->nlp_flag);
6373
dea3101e 6374 phba->fc_stat.elsRcvFLOGI++;
51ef4c26 6375 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
87af33fe 6376 if (newnode)
98c9ea5c 6377 lpfc_nlp_put(ndlp);
dea3101e
JB
6378 break;
6379 case ELS_CMD_LOGO:
858c9f6c
JS
6380 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6381 "RCV LOGO: did:x%x/ste:x%x flg:x%x",
6382 did, vport->port_state, ndlp->nlp_flag);
6383
dea3101e 6384 phba->fc_stat.elsRcvLOGO++;
ddcc50f0 6385 lpfc_send_els_event(vport, ndlp, payload);
2e0fef85 6386 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6387 rjt_err = LSRJT_UNABLE_TPC;
dea3101e
JB
6388 break;
6389 }
2e0fef85 6390 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
dea3101e
JB
6391 break;
6392 case ELS_CMD_PRLO:
858c9f6c
JS
6393 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6394 "RCV PRLO: did:x%x/ste:x%x flg:x%x",
6395 did, vport->port_state, ndlp->nlp_flag);
6396
dea3101e 6397 phba->fc_stat.elsRcvPRLO++;
ddcc50f0 6398 lpfc_send_els_event(vport, ndlp, payload);
2e0fef85 6399 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6400 rjt_err = LSRJT_UNABLE_TPC;
dea3101e
JB
6401 break;
6402 }
2e0fef85 6403 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
dea3101e
JB
6404 break;
6405 case ELS_CMD_RSCN:
6406 phba->fc_stat.elsRcvRSCN++;
51ef4c26 6407 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
87af33fe 6408 if (newnode)
98c9ea5c 6409 lpfc_nlp_put(ndlp);
dea3101e
JB
6410 break;
6411 case ELS_CMD_ADISC:
858c9f6c
JS
6412 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6413 "RCV ADISC: did:x%x/ste:x%x flg:x%x",
6414 did, vport->port_state, ndlp->nlp_flag);
6415
ddcc50f0 6416 lpfc_send_els_event(vport, ndlp, payload);
dea3101e 6417 phba->fc_stat.elsRcvADISC++;
2e0fef85 6418 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6419 rjt_err = LSRJT_UNABLE_TPC;
dea3101e
JB
6420 break;
6421 }
2e0fef85
JS
6422 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6423 NLP_EVT_RCV_ADISC);
dea3101e
JB
6424 break;
6425 case ELS_CMD_PDISC:
858c9f6c
JS
6426 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6427 "RCV PDISC: did:x%x/ste:x%x flg:x%x",
6428 did, vport->port_state, ndlp->nlp_flag);
6429
dea3101e 6430 phba->fc_stat.elsRcvPDISC++;
2e0fef85 6431 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6432 rjt_err = LSRJT_UNABLE_TPC;
dea3101e
JB
6433 break;
6434 }
2e0fef85
JS
6435 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6436 NLP_EVT_RCV_PDISC);
dea3101e
JB
6437 break;
6438 case ELS_CMD_FARPR:
858c9f6c
JS
6439 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6440 "RCV FARPR: did:x%x/ste:x%x flg:x%x",
6441 did, vport->port_state, ndlp->nlp_flag);
6442
dea3101e 6443 phba->fc_stat.elsRcvFARPR++;
2e0fef85 6444 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
dea3101e
JB
6445 break;
6446 case ELS_CMD_FARP:
858c9f6c
JS
6447 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6448 "RCV FARP: did:x%x/ste:x%x flg:x%x",
6449 did, vport->port_state, ndlp->nlp_flag);
6450
dea3101e 6451 phba->fc_stat.elsRcvFARP++;
2e0fef85 6452 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
dea3101e
JB
6453 break;
6454 case ELS_CMD_FAN:
858c9f6c
JS
6455 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6456 "RCV FAN: did:x%x/ste:x%x flg:x%x",
6457 did, vport->port_state, ndlp->nlp_flag);
6458
dea3101e 6459 phba->fc_stat.elsRcvFAN++;
2e0fef85 6460 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
dea3101e 6461 break;
dea3101e 6462 case ELS_CMD_PRLI:
858c9f6c
JS
6463 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6464 "RCV PRLI: did:x%x/ste:x%x flg:x%x",
6465 did, vport->port_state, ndlp->nlp_flag);
6466
dea3101e 6467 phba->fc_stat.elsRcvPRLI++;
2e0fef85 6468 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6469 rjt_err = LSRJT_UNABLE_TPC;
dea3101e
JB
6470 break;
6471 }
2e0fef85 6472 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
dea3101e 6473 break;
7bb3b137 6474 case ELS_CMD_LIRR:
858c9f6c
JS
6475 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6476 "RCV LIRR: did:x%x/ste:x%x flg:x%x",
6477 did, vport->port_state, ndlp->nlp_flag);
6478
7bb3b137 6479 phba->fc_stat.elsRcvLIRR++;
2e0fef85 6480 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
87af33fe 6481 if (newnode)
98c9ea5c 6482 lpfc_nlp_put(ndlp);
7bb3b137 6483 break;
12265f68
JS
6484 case ELS_CMD_RLS:
6485 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6486 "RCV RLS: did:x%x/ste:x%x flg:x%x",
6487 did, vport->port_state, ndlp->nlp_flag);
6488
6489 phba->fc_stat.elsRcvRLS++;
6490 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
6491 if (newnode)
6492 lpfc_nlp_put(ndlp);
6493 break;
7bb3b137 6494 case ELS_CMD_RPS:
858c9f6c
JS
6495 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6496 "RCV RPS: did:x%x/ste:x%x flg:x%x",
6497 did, vport->port_state, ndlp->nlp_flag);
6498
7bb3b137 6499 phba->fc_stat.elsRcvRPS++;
2e0fef85 6500 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
87af33fe 6501 if (newnode)
98c9ea5c 6502 lpfc_nlp_put(ndlp);
7bb3b137
JW
6503 break;
6504 case ELS_CMD_RPL:
858c9f6c
JS
6505 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6506 "RCV RPL: did:x%x/ste:x%x flg:x%x",
6507 did, vport->port_state, ndlp->nlp_flag);
6508
7bb3b137 6509 phba->fc_stat.elsRcvRPL++;
2e0fef85 6510 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
87af33fe 6511 if (newnode)
98c9ea5c 6512 lpfc_nlp_put(ndlp);
7bb3b137 6513 break;
dea3101e 6514 case ELS_CMD_RNID:
858c9f6c
JS
6515 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6516 "RCV RNID: did:x%x/ste:x%x flg:x%x",
6517 did, vport->port_state, ndlp->nlp_flag);
6518
dea3101e 6519 phba->fc_stat.elsRcvRNID++;
2e0fef85 6520 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
87af33fe 6521 if (newnode)
98c9ea5c 6522 lpfc_nlp_put(ndlp);
dea3101e 6523 break;
12265f68
JS
6524 case ELS_CMD_RTV:
6525 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6526 "RCV RTV: did:x%x/ste:x%x flg:x%x",
6527 did, vport->port_state, ndlp->nlp_flag);
6528 phba->fc_stat.elsRcvRTV++;
6529 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
6530 if (newnode)
6531 lpfc_nlp_put(ndlp);
6532 break;
5ffc266e
JS
6533 case ELS_CMD_RRQ:
6534 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6535 "RCV RRQ: did:x%x/ste:x%x flg:x%x",
6536 did, vport->port_state, ndlp->nlp_flag);
6537
6538 phba->fc_stat.elsRcvRRQ++;
6539 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
6540 if (newnode)
6541 lpfc_nlp_put(ndlp);
6542 break;
12265f68
JS
6543 case ELS_CMD_ECHO:
6544 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6545 "RCV ECHO: did:x%x/ste:x%x flg:x%x",
6546 did, vport->port_state, ndlp->nlp_flag);
6547
6548 phba->fc_stat.elsRcvECHO++;
6549 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
6550 if (newnode)
6551 lpfc_nlp_put(ndlp);
6552 break;
dea3101e 6553 default:
858c9f6c
JS
6554 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6555 "RCV ELS cmd: cmd:x%x did:x%x/ste:x%x",
6556 cmd, did, vport->port_state);
6557
dea3101e 6558 /* Unsupported ELS command, reject */
63e801ce 6559 rjt_err = LSRJT_CMD_UNSUPPORTED;
dea3101e
JB
6560
6561 /* Unknown ELS command <elsCmd> received from NPORT <did> */
e8b62011
JS
6562 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6563 "0115 Unknown ELS command x%x "
6564 "received from NPORT x%x\n", cmd, did);
87af33fe 6565 if (newnode)
98c9ea5c 6566 lpfc_nlp_put(ndlp);
dea3101e
JB
6567 break;
6568 }
6569
6570 /* check if need to LS_RJT received ELS cmd */
6571 if (rjt_err) {
92d7f7b0 6572 memset(&stat, 0, sizeof(stat));
858c9f6c 6573 stat.un.b.lsRjtRsnCode = rjt_err;
1f679caf 6574 stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
858c9f6c
JS
6575 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
6576 NULL);
dea3101e
JB
6577 }
6578
d7c255b2
JS
6579 lpfc_nlp_put(elsiocb->context1);
6580 elsiocb->context1 = NULL;
ed957684
JS
6581 return;
6582
6583dropit:
98c9ea5c 6584 if (vport && !(vport->load_flag & FC_UNLOADING))
6fb120a7
JS
6585 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6586 "0111 Dropping received ELS cmd "
ed957684 6587 "Data: x%x x%x x%x\n",
6fb120a7 6588 icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
ed957684
JS
6589 phba->fc_stat.elsRcvDrop++;
6590}
6591
e59058c4 6592/**
3621a710 6593 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
e59058c4
JS
6594 * @phba: pointer to lpfc hba data structure.
6595 * @vpi: host virtual N_Port identifier.
6596 *
6597 * This routine finds a vport on a HBA (referred by @phba) through a
6598 * @vpi. The function walks the HBA's vport list and returns the address
6599 * of the vport with the matching @vpi.
6600 *
6601 * Return code
6602 * NULL - No vport with the matching @vpi found
6603 * Otherwise - Address to the vport with the matching @vpi.
6604 **/
6669f9bb 6605struct lpfc_vport *
92d7f7b0
JS
6606lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6607{
6608 struct lpfc_vport *vport;
549e55cd 6609 unsigned long flags;
5248a749 6610 int i = 0;
6d368e53
JS
6611
6612 /* The physical ports are always vpi 0 - translate is unnecessary. */
6613 if (vpi > 0) {
6614 /*
6615 * Translate the physical vpi to the logical vpi. The
6616 * vport stores the logical vpi.
6617 */
6618 for (i = 0; i < phba->max_vpi; i++) {
6619 if (vpi == phba->vpi_ids[i])
6620 break;
6621 }
6622
6623 if (i >= phba->max_vpi) {
6624 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
6625 "2936 Could not find Vport mapped "
6626 "to vpi %d\n", vpi);
6627 return NULL;
6628 }
6629 }
92d7f7b0 6630
549e55cd 6631 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0 6632 list_for_each_entry(vport, &phba->port_list, listentry) {
5248a749 6633 if (vport->vpi == i) {
549e55cd 6634 spin_unlock_irqrestore(&phba->hbalock, flags);
92d7f7b0 6635 return vport;
549e55cd 6636 }
92d7f7b0 6637 }
549e55cd 6638 spin_unlock_irqrestore(&phba->hbalock, flags);
92d7f7b0
JS
6639 return NULL;
6640}
ed957684 6641
e59058c4 6642/**
3621a710 6643 * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
e59058c4
JS
6644 * @phba: pointer to lpfc hba data structure.
6645 * @pring: pointer to a SLI ring.
6646 * @elsiocb: pointer to lpfc els iocb data structure.
6647 *
6648 * This routine is used to process an unsolicited event received from a SLI
6649 * (Service Level Interface) ring. The actual processing of the data buffer
6650 * associated with the unsolicited event is done by invoking the routine
6651 * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
6652 * SLI ring on which the unsolicited event was received.
6653 **/
ed957684
JS
6654void
6655lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6656 struct lpfc_iocbq *elsiocb)
6657{
6658 struct lpfc_vport *vport = phba->pport;
ed957684 6659 IOCB_t *icmd = &elsiocb->iocb;
ed957684 6660 dma_addr_t paddr;
92d7f7b0
JS
6661 struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
6662 struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
6663
d7c255b2 6664 elsiocb->context1 = NULL;
92d7f7b0
JS
6665 elsiocb->context2 = NULL;
6666 elsiocb->context3 = NULL;
ed957684 6667
92d7f7b0
JS
6668 if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
6669 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
6670 } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
6671 (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
ed957684
JS
6672 phba->fc_stat.NoRcvBuf++;
6673 /* Not enough posted buffers; Try posting more buffers */
92d7f7b0 6674 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
495a714c 6675 lpfc_post_buffer(phba, pring, 0);
ed957684
JS
6676 return;
6677 }
6678
92d7f7b0
JS
6679 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6680 (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
6681 icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6682 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
6683 vport = phba->pport;
6fb120a7
JS
6684 else
6685 vport = lpfc_find_vport_by_vpid(phba,
6d368e53 6686 icmd->unsli3.rcvsli3.vpi);
92d7f7b0 6687 }
6d368e53 6688
7f5f3d0d
JS
6689 /* If there are no BDEs associated
6690 * with this IOCB, there is nothing to do.
6691 */
ed957684
JS
6692 if (icmd->ulpBdeCount == 0)
6693 return;
6694
7f5f3d0d
JS
6695 /* type of ELS cmd is first 32bit word
6696 * in packet
6697 */
ed957684 6698 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
92d7f7b0 6699 elsiocb->context2 = bdeBuf1;
ed957684
JS
6700 } else {
6701 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
6702 icmd->un.cont64[0].addrLow);
92d7f7b0
JS
6703 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
6704 paddr);
ed957684
JS
6705 }
6706
92d7f7b0
JS
6707 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
6708 /*
6709 * The different unsolicited event handlers would tell us
6710 * if they are done with "mp" by setting context2 to NULL.
6711 */
dea3101e 6712 if (elsiocb->context2) {
92d7f7b0
JS
6713 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
6714 elsiocb->context2 = NULL;
dea3101e 6715 }
ed957684
JS
6716
6717 /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
92d7f7b0 6718 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
ed957684 6719 icmd->ulpBdeCount == 2) {
92d7f7b0
JS
6720 elsiocb->context2 = bdeBuf2;
6721 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
ed957684
JS
6722 /* free mp if we are done with it */
6723 if (elsiocb->context2) {
92d7f7b0
JS
6724 lpfc_in_buf_free(phba, elsiocb->context2);
6725 elsiocb->context2 = NULL;
6726 }
6727 }
6728}
6729
e59058c4 6730/**
3621a710 6731 * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
e59058c4
JS
6732 * @phba: pointer to lpfc hba data structure.
6733 * @vport: pointer to a virtual N_Port data structure.
6734 *
6735 * This routine issues a Port Login (PLOGI) to the Name Server with
6736 * State Change Request (SCR) for a @vport. This routine will create an
6737 * ndlp for the Name Server associated to the @vport if such node does
6738 * not already exist. The PLOGI to Name Server is issued by invoking the
6739 * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
6740 * (FDMI) is configured to the @vport, a FDMI node will be created and
6741 * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
6742 **/
92d7f7b0
JS
6743void
6744lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
6745{
6746 struct lpfc_nodelist *ndlp, *ndlp_fdmi;
92494144
JS
6747 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6748
6749 /*
6750 * If lpfc_delay_discovery parameter is set and the clean address
6751 * bit is cleared and fc fabric parameters chenged, delay FC NPort
6752 * discovery.
6753 */
6754 spin_lock_irq(shost->host_lock);
6755 if (vport->fc_flag & FC_DISC_DELAYED) {
6756 spin_unlock_irq(shost->host_lock);
6757 mod_timer(&vport->delayed_disc_tmo,
6758 jiffies + HZ * phba->fc_ratov);
6759 return;
6760 }
6761 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
6762
6763 ndlp = lpfc_findnode_did(vport, NameServer_DID);
6764 if (!ndlp) {
6765 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
6766 if (!ndlp) {
76a95d75 6767 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
92d7f7b0
JS
6768 lpfc_disc_start(vport);
6769 return;
6770 }
6771 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
6772 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6773 "0251 NameServer login: no memory\n");
92d7f7b0
JS
6774 return;
6775 }
6776 lpfc_nlp_init(vport, ndlp, NameServer_DID);
e47c9093
JS
6777 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
6778 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
6779 if (!ndlp) {
76a95d75 6780 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
e47c9093
JS
6781 lpfc_disc_start(vport);
6782 return;
6783 }
6784 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6785 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6786 "0348 NameServer login: node freed\n");
6787 return;
6788 }
92d7f7b0 6789 }
58da1ffb 6790 ndlp->nlp_type |= NLP_FABRIC;
92d7f7b0
JS
6791
6792 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6793
6794 if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
6795 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
6796 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6797 "0252 Cannot issue NameServer login\n");
92d7f7b0
JS
6798 return;
6799 }
6800
3de2a653 6801 if (vport->cfg_fdmi_on) {
63e801ce
JS
6802 /* If this is the first time, allocate an ndlp and initialize
6803 * it. Otherwise, make sure the node is enabled and then do the
6804 * login.
6805 */
6806 ndlp_fdmi = lpfc_findnode_did(vport, FDMI_DID);
6807 if (!ndlp_fdmi) {
6808 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
6809 GFP_KERNEL);
6810 if (ndlp_fdmi) {
6811 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
6812 ndlp_fdmi->nlp_type |= NLP_FABRIC;
6813 } else
6814 return;
6815 }
6816 if (!NLP_CHK_NODE_ACT(ndlp_fdmi))
6817 ndlp_fdmi = lpfc_enable_node(vport,
6818 ndlp_fdmi,
6819 NLP_STE_NPR_NODE);
6820
92d7f7b0 6821 if (ndlp_fdmi) {
58da1ffb 6822 lpfc_nlp_set_state(vport, ndlp_fdmi,
63e801ce
JS
6823 NLP_STE_PLOGI_ISSUE);
6824 lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID, 0);
92d7f7b0
JS
6825 }
6826 }
92d7f7b0
JS
6827}
6828
e59058c4 6829/**
3621a710 6830 * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
e59058c4
JS
6831 * @phba: pointer to lpfc hba data structure.
6832 * @pmb: pointer to the driver internal queue element for mailbox command.
6833 *
6834 * This routine is the completion callback function to register new vport
6835 * mailbox command. If the new vport mailbox command completes successfully,
6836 * the fabric registration login shall be performed on physical port (the
6837 * new vport created is actually a physical port, with VPI 0) or the port
6838 * login to Name Server for State Change Request (SCR) will be performed
6839 * on virtual port (real virtual port, with VPI greater than 0).
6840 **/
92d7f7b0
JS
6841static void
6842lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6843{
6844 struct lpfc_vport *vport = pmb->vport;
6845 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6846 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
04c68496 6847 MAILBOX_t *mb = &pmb->u.mb;
695a814e 6848 int rc;
92d7f7b0 6849
09372820 6850 spin_lock_irq(shost->host_lock);
92d7f7b0 6851 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
09372820 6852 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
6853
6854 if (mb->mbxStatus) {
e8b62011 6855 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
38b92ef8
JS
6856 "0915 Register VPI failed : Status: x%x"
6857 " upd bit: x%x \n", mb->mbxStatus,
6858 mb->un.varRegVpi.upd);
6859 if (phba->sli_rev == LPFC_SLI_REV4 &&
6860 mb->un.varRegVpi.upd)
6861 goto mbox_err_exit ;
92d7f7b0
JS
6862
6863 switch (mb->mbxStatus) {
6864 case 0x11: /* unsupported feature */
6865 case 0x9603: /* max_vpi exceeded */
7f5f3d0d 6866 case 0x9602: /* Link event since CLEAR_LA */
92d7f7b0
JS
6867 /* giving up on vport registration */
6868 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6869 spin_lock_irq(shost->host_lock);
6870 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
6871 spin_unlock_irq(shost->host_lock);
6872 lpfc_can_disctmo(vport);
6873 break;
695a814e
JS
6874 /* If reg_vpi fail with invalid VPI status, re-init VPI */
6875 case 0x20:
6876 spin_lock_irq(shost->host_lock);
6877 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
6878 spin_unlock_irq(shost->host_lock);
6879 lpfc_init_vpi(phba, pmb, vport->vpi);
6880 pmb->vport = vport;
6881 pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
6882 rc = lpfc_sli_issue_mbox(phba, pmb,
6883 MBX_NOWAIT);
6884 if (rc == MBX_NOT_FINISHED) {
6885 lpfc_printf_vlog(vport,
6886 KERN_ERR, LOG_MBOX,
6887 "2732 Failed to issue INIT_VPI"
6888 " mailbox command\n");
6889 } else {
6890 lpfc_nlp_put(ndlp);
6891 return;
6892 }
6893
92d7f7b0
JS
6894 default:
6895 /* Try to recover from this error */
5af5eee7
JS
6896 if (phba->sli_rev == LPFC_SLI_REV4)
6897 lpfc_sli4_unreg_all_rpis(vport);
92d7f7b0 6898 lpfc_mbx_unreg_vpi(vport);
09372820 6899 spin_lock_irq(shost->host_lock);
92d7f7b0 6900 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
09372820 6901 spin_unlock_irq(shost->host_lock);
4b40c59e
JS
6902 if (vport->port_type == LPFC_PHYSICAL_PORT
6903 && !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
76a95d75 6904 lpfc_issue_init_vfi(vport);
7f5f3d0d
JS
6905 else
6906 lpfc_initial_fdisc(vport);
92d7f7b0
JS
6907 break;
6908 }
92d7f7b0 6909 } else {
695a814e 6910 spin_lock_irq(shost->host_lock);
1987807d 6911 vport->vpi_state |= LPFC_VPI_REGISTERED;
695a814e
JS
6912 spin_unlock_irq(shost->host_lock);
6913 if (vport == phba->pport) {
6fb120a7
JS
6914 if (phba->sli_rev < LPFC_SLI_REV4)
6915 lpfc_issue_fabric_reglogin(vport);
695a814e 6916 else {
fc2b989b
JS
6917 /*
6918 * If the physical port is instantiated using
6919 * FDISC, do not start vport discovery.
6920 */
6921 if (vport->port_state != LPFC_FDISC)
6922 lpfc_start_fdiscs(phba);
695a814e
JS
6923 lpfc_do_scr_ns_plogi(phba, vport);
6924 }
6925 } else
92d7f7b0
JS
6926 lpfc_do_scr_ns_plogi(phba, vport);
6927 }
38b92ef8 6928mbox_err_exit:
fa4066b6
JS
6929 /* Now, we decrement the ndlp reference count held for this
6930 * callback function
6931 */
6932 lpfc_nlp_put(ndlp);
6933
92d7f7b0
JS
6934 mempool_free(pmb, phba->mbox_mem_pool);
6935 return;
6936}
6937
e59058c4 6938/**
3621a710 6939 * lpfc_register_new_vport - Register a new vport with a HBA
e59058c4
JS
6940 * @phba: pointer to lpfc hba data structure.
6941 * @vport: pointer to a host virtual N_Port data structure.
6942 * @ndlp: pointer to a node-list data structure.
6943 *
6944 * This routine registers the @vport as a new virtual port with a HBA.
6945 * It is done through a registering vpi mailbox command.
6946 **/
695a814e 6947void
92d7f7b0
JS
6948lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
6949 struct lpfc_nodelist *ndlp)
6950{
09372820 6951 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
6952 LPFC_MBOXQ_t *mbox;
6953
6954 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6955 if (mbox) {
6fb120a7 6956 lpfc_reg_vpi(vport, mbox);
92d7f7b0
JS
6957 mbox->vport = vport;
6958 mbox->context2 = lpfc_nlp_get(ndlp);
6959 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
0b727fea 6960 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
92d7f7b0 6961 == MBX_NOT_FINISHED) {
fa4066b6
JS
6962 /* mailbox command not success, decrement ndlp
6963 * reference count for this command
6964 */
6965 lpfc_nlp_put(ndlp);
92d7f7b0 6966 mempool_free(mbox, phba->mbox_mem_pool);
92d7f7b0 6967
e8b62011
JS
6968 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6969 "0253 Register VPI: Can't send mbox\n");
fa4066b6 6970 goto mbox_err_exit;
92d7f7b0
JS
6971 }
6972 } else {
e8b62011
JS
6973 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
6974 "0254 Register VPI: no memory\n");
fa4066b6 6975 goto mbox_err_exit;
92d7f7b0 6976 }
fa4066b6
JS
6977 return;
6978
6979mbox_err_exit:
6980 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6981 spin_lock_irq(shost->host_lock);
6982 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
6983 spin_unlock_irq(shost->host_lock);
6984 return;
92d7f7b0
JS
6985}
6986
695a814e 6987/**
0c9ab6f5 6988 * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
695a814e
JS
6989 * @phba: pointer to lpfc hba data structure.
6990 *
0c9ab6f5 6991 * This routine cancels the retry delay timers to all the vports.
695a814e
JS
6992 **/
6993void
0c9ab6f5 6994lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
695a814e
JS
6995{
6996 struct lpfc_vport **vports;
6997 struct lpfc_nodelist *ndlp;
695a814e 6998 uint32_t link_state;
0c9ab6f5 6999 int i;
695a814e
JS
7000
7001 /* Treat this failure as linkdown for all vports */
7002 link_state = phba->link_state;
7003 lpfc_linkdown(phba);
7004 phba->link_state = link_state;
7005
7006 vports = lpfc_create_vport_work_array(phba);
7007
7008 if (vports) {
7009 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
7010 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
7011 if (ndlp)
7012 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
7013 lpfc_els_flush_cmd(vports[i]);
7014 }
7015 lpfc_destroy_vport_work_array(phba, vports);
7016 }
0c9ab6f5
JS
7017}
7018
7019/**
7020 * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
7021 * @phba: pointer to lpfc hba data structure.
7022 *
7023 * This routine abort all pending discovery commands and
7024 * start a timer to retry FLOGI for the physical port
7025 * discovery.
7026 **/
7027void
7028lpfc_retry_pport_discovery(struct lpfc_hba *phba)
7029{
7030 struct lpfc_nodelist *ndlp;
7031 struct Scsi_Host *shost;
7032
7033 /* Cancel the all vports retry delay retry timers */
7034 lpfc_cancel_all_vport_retry_delay_timer(phba);
695a814e
JS
7035
7036 /* If fabric require FLOGI, then re-instantiate physical login */
7037 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
7038 if (!ndlp)
7039 return;
7040
695a814e
JS
7041 shost = lpfc_shost_from_vport(phba->pport);
7042 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
7043 spin_lock_irq(shost->host_lock);
7044 ndlp->nlp_flag |= NLP_DELAY_TMO;
7045 spin_unlock_irq(shost->host_lock);
7046 ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
7047 phba->pport->port_state = LPFC_FLOGI;
7048 return;
7049}
7050
7051/**
7052 * lpfc_fabric_login_reqd - Check if FLOGI required.
7053 * @phba: pointer to lpfc hba data structure.
7054 * @cmdiocb: pointer to FDISC command iocb.
7055 * @rspiocb: pointer to FDISC response iocb.
7056 *
7057 * This routine checks if a FLOGI is reguired for FDISC
7058 * to succeed.
7059 **/
7060static int
7061lpfc_fabric_login_reqd(struct lpfc_hba *phba,
7062 struct lpfc_iocbq *cmdiocb,
7063 struct lpfc_iocbq *rspiocb)
7064{
7065
7066 if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
7067 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
7068 return 0;
7069 else
7070 return 1;
7071}
7072
e59058c4 7073/**
3621a710 7074 * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
e59058c4
JS
7075 * @phba: pointer to lpfc hba data structure.
7076 * @cmdiocb: pointer to lpfc command iocb data structure.
7077 * @rspiocb: pointer to lpfc response iocb data structure.
7078 *
7079 * This routine is the completion callback function to a Fabric Discover
7080 * (FDISC) ELS command. Since all the FDISC ELS commands are issued
7081 * single threaded, each FDISC completion callback function will reset
7082 * the discovery timer for all vports such that the timers will not get
7083 * unnecessary timeout. The function checks the FDISC IOCB status. If error
7084 * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
7085 * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
7086 * assigned to the vport has been changed with the completion of the FDISC
7087 * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
7088 * are unregistered from the HBA, and then the lpfc_register_new_vport()
7089 * routine is invoked to register new vport with the HBA. Otherwise, the
7090 * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
7091 * Server for State Change Request (SCR).
7092 **/
92d7f7b0
JS
7093static void
7094lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7095 struct lpfc_iocbq *rspiocb)
7096{
7097 struct lpfc_vport *vport = cmdiocb->vport;
7098 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7099 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
7100 struct lpfc_nodelist *np;
7101 struct lpfc_nodelist *next_np;
7102 IOCB_t *irsp = &rspiocb->iocb;
7103 struct lpfc_iocbq *piocb;
92494144
JS
7104 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
7105 struct serv_parm *sp;
7106 uint8_t fabric_param_changed;
92d7f7b0 7107
e8b62011
JS
7108 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7109 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
7110 irsp->ulpStatus, irsp->un.ulpWord[4],
7111 vport->fc_prevDID);
92d7f7b0
JS
7112 /* Since all FDISCs are being single threaded, we
7113 * must reset the discovery timer for ALL vports
7114 * waiting to send FDISC when one completes.
7115 */
7116 list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
7117 lpfc_set_disctmo(piocb->vport);
7118 }
7119
858c9f6c
JS
7120 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7121 "FDISC cmpl: status:x%x/x%x prevdid:x%x",
7122 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
7123
92d7f7b0 7124 if (irsp->ulpStatus) {
695a814e
JS
7125
7126 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
7127 lpfc_retry_pport_discovery(phba);
7128 goto out;
7129 }
7130
92d7f7b0
JS
7131 /* Check for retry */
7132 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
7133 goto out;
92d7f7b0 7134 /* FDISC failed */
e8b62011 7135 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
d7c255b2 7136 "0126 FDISC failed. (%d/%d)\n",
e8b62011 7137 irsp->ulpStatus, irsp->un.ulpWord[4]);
d7c255b2
JS
7138 goto fdisc_failed;
7139 }
d7c255b2 7140 spin_lock_irq(shost->host_lock);
695a814e 7141 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
4b40c59e 7142 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
d7c255b2 7143 vport->fc_flag |= FC_FABRIC;
76a95d75 7144 if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
d7c255b2
JS
7145 vport->fc_flag |= FC_PUBLIC_LOOP;
7146 spin_unlock_irq(shost->host_lock);
92d7f7b0 7147
d7c255b2
JS
7148 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
7149 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
92494144
JS
7150 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
7151 sp = prsp->virt + sizeof(uint32_t);
7152 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
7153 memcpy(&vport->fabric_portname, &sp->portName,
7154 sizeof(struct lpfc_name));
7155 memcpy(&vport->fabric_nodename, &sp->nodeName,
7156 sizeof(struct lpfc_name));
7157 if (fabric_param_changed &&
d7c255b2
JS
7158 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
7159 /* If our NportID changed, we need to ensure all
7160 * remaining NPORTs get unreg_login'ed so we can
7161 * issue unreg_vpi.
7162 */
7163 list_for_each_entry_safe(np, next_np,
7164 &vport->fc_nodes, nlp_listp) {
7165 if (!NLP_CHK_NODE_ACT(ndlp) ||
7166 (np->nlp_state != NLP_STE_NPR_NODE) ||
7167 !(np->nlp_flag & NLP_NPR_ADISC))
7168 continue;
09372820 7169 spin_lock_irq(shost->host_lock);
d7c255b2 7170 np->nlp_flag &= ~NLP_NPR_ADISC;
09372820 7171 spin_unlock_irq(shost->host_lock);
d7c255b2 7172 lpfc_unreg_rpi(vport, np);
92d7f7b0 7173 }
78730cfe 7174 lpfc_cleanup_pending_mbox(vport);
5af5eee7
JS
7175
7176 if (phba->sli_rev == LPFC_SLI_REV4)
7177 lpfc_sli4_unreg_all_rpis(vport);
7178
d7c255b2
JS
7179 lpfc_mbx_unreg_vpi(vport);
7180 spin_lock_irq(shost->host_lock);
7181 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
0f65ff68
JS
7182 if (phba->sli_rev == LPFC_SLI_REV4)
7183 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
4b40c59e
JS
7184 else
7185 vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
d7c255b2 7186 spin_unlock_irq(shost->host_lock);
38b92ef8
JS
7187 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
7188 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
7189 /*
7190 * Driver needs to re-reg VPI in order for f/w
7191 * to update the MAC address.
7192 */
7193 lpfc_register_new_vport(phba, vport, ndlp);
5ac6b303 7194 goto out;
92d7f7b0
JS
7195 }
7196
ecfd03c6
JS
7197 if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
7198 lpfc_issue_init_vpi(vport);
7199 else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
d7c255b2
JS
7200 lpfc_register_new_vport(phba, vport, ndlp);
7201 else
7202 lpfc_do_scr_ns_plogi(phba, vport);
7203 goto out;
7204fdisc_failed:
7205 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7206 /* Cancel discovery timer */
7207 lpfc_can_disctmo(vport);
7208 lpfc_nlp_put(ndlp);
92d7f7b0
JS
7209out:
7210 lpfc_els_free_iocb(phba, cmdiocb);
7211}
7212
e59058c4 7213/**
3621a710 7214 * lpfc_issue_els_fdisc - Issue a fdisc iocb command
e59058c4
JS
7215 * @vport: pointer to a virtual N_Port data structure.
7216 * @ndlp: pointer to a node-list data structure.
7217 * @retry: number of retries to the command IOCB.
7218 *
7219 * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
7220 * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
7221 * routine to issue the IOCB, which makes sure only one outstanding fabric
7222 * IOCB will be sent off HBA at any given time.
7223 *
7224 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7225 * will be incremented by 1 for holding the ndlp and the reference to ndlp
7226 * will be stored into the context1 field of the IOCB for the completion
7227 * callback function to the FDISC ELS command.
7228 *
7229 * Return code
7230 * 0 - Successfully issued fdisc iocb command
7231 * 1 - Failed to issue fdisc iocb command
7232 **/
a6ababd2 7233static int
92d7f7b0
JS
7234lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7235 uint8_t retry)
7236{
7237 struct lpfc_hba *phba = vport->phba;
7238 IOCB_t *icmd;
7239 struct lpfc_iocbq *elsiocb;
7240 struct serv_parm *sp;
7241 uint8_t *pcmd;
7242 uint16_t cmdsize;
7243 int did = ndlp->nlp_DID;
7244 int rc;
92d7f7b0 7245
5ffc266e 7246 vport->port_state = LPFC_FDISC;
92d7f7b0
JS
7247 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
7248 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
7249 ELS_CMD_FDISC);
7250 if (!elsiocb) {
92d7f7b0 7251 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
7252 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7253 "0255 Issue FDISC: no IOCB\n");
92d7f7b0
JS
7254 return 1;
7255 }
7256
7257 icmd = &elsiocb->iocb;
7258 icmd->un.elsreq64.myID = 0;
7259 icmd->un.elsreq64.fl = 1;
7260
4042629e
JS
7261 if ((phba->sli_rev == LPFC_SLI_REV4) &&
7262 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7263 LPFC_SLI_INTF_IF_TYPE_0)) {
f1126688
JS
7264 /* FDISC needs to be 1 for WQE VPI */
7265 elsiocb->iocb.ulpCt_h = (SLI4_CT_VPI >> 1) & 1;
7266 elsiocb->iocb.ulpCt_l = SLI4_CT_VPI & 1 ;
7267 /* Set the ulpContext to the vpi */
6d368e53 7268 elsiocb->iocb.ulpContext = phba->vpi_ids[vport->vpi];
f1126688
JS
7269 } else {
7270 /* For FDISC, Let FDISC rsp set the NPortID for this VPI */
7271 icmd->ulpCt_h = 1;
7272 icmd->ulpCt_l = 0;
7273 }
92d7f7b0
JS
7274
7275 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7276 *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
7277 pcmd += sizeof(uint32_t); /* CSP Word 1 */
7278 memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
7279 sp = (struct serv_parm *) pcmd;
7280 /* Setup CSPs accordingly for Fabric */
7281 sp->cmn.e_d_tov = 0;
7282 sp->cmn.w2.r_a_tov = 0;
7283 sp->cls1.classValid = 0;
7284 sp->cls2.seqDelivery = 1;
7285 sp->cls3.seqDelivery = 1;
7286
7287 pcmd += sizeof(uint32_t); /* CSP Word 2 */
7288 pcmd += sizeof(uint32_t); /* CSP Word 3 */
7289 pcmd += sizeof(uint32_t); /* CSP Word 4 */
7290 pcmd += sizeof(uint32_t); /* Port Name */
7291 memcpy(pcmd, &vport->fc_portname, 8);
7292 pcmd += sizeof(uint32_t); /* Node Name */
7293 pcmd += sizeof(uint32_t); /* Node Name */
7294 memcpy(pcmd, &vport->fc_nodename, 8);
7295
7296 lpfc_set_disctmo(vport);
7297
7298 phba->fc_stat.elsXmitFDISC++;
7299 elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
7300
858c9f6c
JS
7301 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7302 "Issue FDISC: did:x%x",
7303 did, 0, 0);
7304
92d7f7b0
JS
7305 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
7306 if (rc == IOCB_ERROR) {
7307 lpfc_els_free_iocb(phba, elsiocb);
92d7f7b0 7308 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
7309 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7310 "0256 Issue FDISC: Cannot send IOCB\n");
92d7f7b0
JS
7311 return 1;
7312 }
7313 lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
92d7f7b0
JS
7314 return 0;
7315}
7316
e59058c4 7317/**
3621a710 7318 * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
e59058c4
JS
7319 * @phba: pointer to lpfc hba data structure.
7320 * @cmdiocb: pointer to lpfc command iocb data structure.
7321 * @rspiocb: pointer to lpfc response iocb data structure.
7322 *
7323 * This routine is the completion callback function to the issuing of a LOGO
7324 * ELS command off a vport. It frees the command IOCB and then decrement the
7325 * reference count held on ndlp for this completion function, indicating that
7326 * the reference to the ndlp is no long needed. Note that the
7327 * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
7328 * callback function and an additional explicit ndlp reference decrementation
7329 * will trigger the actual release of the ndlp.
7330 **/
92d7f7b0
JS
7331static void
7332lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7333 struct lpfc_iocbq *rspiocb)
7334{
7335 struct lpfc_vport *vport = cmdiocb->vport;
858c9f6c 7336 IOCB_t *irsp;
e47c9093 7337 struct lpfc_nodelist *ndlp;
9589b062 7338 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
858c9f6c 7339
9589b062 7340 ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
858c9f6c
JS
7341 irsp = &rspiocb->iocb;
7342 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7343 "LOGO npiv cmpl: status:x%x/x%x did:x%x",
7344 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
92d7f7b0
JS
7345
7346 lpfc_els_free_iocb(phba, cmdiocb);
7347 vport->unreg_vpi_cmpl = VPORT_ERROR;
e47c9093
JS
7348
7349 /* Trigger the release of the ndlp after logo */
7350 lpfc_nlp_put(ndlp);
9589b062
JS
7351
7352 /* NPIV LOGO completes to NPort <nlp_DID> */
7353 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7354 "2928 NPIV LOGO completes to NPort x%x "
7355 "Data: x%x x%x x%x x%x\n",
7356 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
7357 irsp->ulpTimeout, vport->num_disc_nodes);
7358
7359 if (irsp->ulpStatus == IOSTAT_SUCCESS) {
7360 spin_lock_irq(shost->host_lock);
7361 vport->fc_flag &= ~FC_FABRIC;
7362 spin_unlock_irq(shost->host_lock);
7363 }
92d7f7b0
JS
7364}
7365
e59058c4 7366/**
3621a710 7367 * lpfc_issue_els_npiv_logo - Issue a logo off a vport
e59058c4
JS
7368 * @vport: pointer to a virtual N_Port data structure.
7369 * @ndlp: pointer to a node-list data structure.
7370 *
7371 * This routine issues a LOGO ELS command to an @ndlp off a @vport.
7372 *
7373 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7374 * will be incremented by 1 for holding the ndlp and the reference to ndlp
7375 * will be stored into the context1 field of the IOCB for the completion
7376 * callback function to the LOGO ELS command.
7377 *
7378 * Return codes
7379 * 0 - Successfully issued logo off the @vport
7380 * 1 - Failed to issue logo off the @vport
7381 **/
92d7f7b0
JS
7382int
7383lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
7384{
7385 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7386 struct lpfc_hba *phba = vport->phba;
92d7f7b0
JS
7387 IOCB_t *icmd;
7388 struct lpfc_iocbq *elsiocb;
7389 uint8_t *pcmd;
7390 uint16_t cmdsize;
7391
7392 cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
7393 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
7394 ELS_CMD_LOGO);
7395 if (!elsiocb)
7396 return 1;
7397
7398 icmd = &elsiocb->iocb;
7399 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7400 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
7401 pcmd += sizeof(uint32_t);
7402
7403 /* Fill in LOGO payload */
7404 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
7405 pcmd += sizeof(uint32_t);
7406 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
7407
858c9f6c
JS
7408 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7409 "Issue LOGO npiv did:x%x flg:x%x",
7410 ndlp->nlp_DID, ndlp->nlp_flag, 0);
7411
92d7f7b0
JS
7412 elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
7413 spin_lock_irq(shost->host_lock);
7414 ndlp->nlp_flag |= NLP_LOGO_SND;
7415 spin_unlock_irq(shost->host_lock);
3772a991
JS
7416 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7417 IOCB_ERROR) {
92d7f7b0
JS
7418 spin_lock_irq(shost->host_lock);
7419 ndlp->nlp_flag &= ~NLP_LOGO_SND;
7420 spin_unlock_irq(shost->host_lock);
7421 lpfc_els_free_iocb(phba, elsiocb);
7422 return 1;
7423 }
7424 return 0;
7425}
7426
e59058c4 7427/**
3621a710 7428 * lpfc_fabric_block_timeout - Handler function to the fabric block timer
e59058c4
JS
7429 * @ptr: holder for the timer function associated data.
7430 *
7431 * This routine is invoked by the fabric iocb block timer after
7432 * timeout. It posts the fabric iocb block timeout event by setting the
7433 * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
7434 * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
7435 * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
7436 * posted event WORKER_FABRIC_BLOCK_TMO.
7437 **/
92d7f7b0
JS
7438void
7439lpfc_fabric_block_timeout(unsigned long ptr)
7440{
7441 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
7442 unsigned long iflags;
7443 uint32_t tmo_posted;
5e9d9b82 7444
92d7f7b0
JS
7445 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
7446 tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
7447 if (!tmo_posted)
7448 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
7449 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
7450
5e9d9b82
JS
7451 if (!tmo_posted)
7452 lpfc_worker_wake_up(phba);
7453 return;
92d7f7b0
JS
7454}
7455
e59058c4 7456/**
3621a710 7457 * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
e59058c4
JS
7458 * @phba: pointer to lpfc hba data structure.
7459 *
7460 * This routine issues one fabric iocb from the driver internal list to
7461 * the HBA. It first checks whether it's ready to issue one fabric iocb to
7462 * the HBA (whether there is no outstanding fabric iocb). If so, it shall
7463 * remove one pending fabric iocb from the driver internal list and invokes
7464 * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
7465 **/
92d7f7b0
JS
7466static void
7467lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
7468{
7469 struct lpfc_iocbq *iocb;
7470 unsigned long iflags;
7471 int ret;
92d7f7b0
JS
7472 IOCB_t *cmd;
7473
7474repeat:
7475 iocb = NULL;
7476 spin_lock_irqsave(&phba->hbalock, iflags);
7f5f3d0d 7477 /* Post any pending iocb to the SLI layer */
92d7f7b0
JS
7478 if (atomic_read(&phba->fabric_iocb_count) == 0) {
7479 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
7480 list);
7481 if (iocb)
7f5f3d0d 7482 /* Increment fabric iocb count to hold the position */
92d7f7b0
JS
7483 atomic_inc(&phba->fabric_iocb_count);
7484 }
7485 spin_unlock_irqrestore(&phba->hbalock, iflags);
7486 if (iocb) {
7487 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7488 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7489 iocb->iocb_flag |= LPFC_IO_FABRIC;
7490
858c9f6c
JS
7491 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7492 "Fabric sched1: ste:x%x",
7493 iocb->vport->port_state, 0, 0);
7494
3772a991 7495 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
92d7f7b0
JS
7496
7497 if (ret == IOCB_ERROR) {
7498 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7499 iocb->fabric_iocb_cmpl = NULL;
7500 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7501 cmd = &iocb->iocb;
7502 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
7503 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
7504 iocb->iocb_cmpl(phba, iocb, iocb);
7505
7506 atomic_dec(&phba->fabric_iocb_count);
7507 goto repeat;
7508 }
7509 }
7510
7511 return;
7512}
7513
e59058c4 7514/**
3621a710 7515 * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
e59058c4
JS
7516 * @phba: pointer to lpfc hba data structure.
7517 *
7518 * This routine unblocks the issuing fabric iocb command. The function
7519 * will clear the fabric iocb block bit and then invoke the routine
7520 * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
7521 * from the driver internal fabric iocb list.
7522 **/
92d7f7b0
JS
7523void
7524lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
7525{
7526 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7527
7528 lpfc_resume_fabric_iocbs(phba);
7529 return;
7530}
7531
e59058c4 7532/**
3621a710 7533 * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
e59058c4
JS
7534 * @phba: pointer to lpfc hba data structure.
7535 *
7536 * This routine blocks the issuing fabric iocb for a specified amount of
7537 * time (currently 100 ms). This is done by set the fabric iocb block bit
7538 * and set up a timeout timer for 100ms. When the block bit is set, no more
7539 * fabric iocb will be issued out of the HBA.
7540 **/
92d7f7b0
JS
7541static void
7542lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
7543{
7544 int blocked;
7545
7546 blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7f5f3d0d 7547 /* Start a timer to unblock fabric iocbs after 100ms */
92d7f7b0
JS
7548 if (!blocked)
7549 mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
7550
7551 return;
7552}
7553
e59058c4 7554/**
3621a710 7555 * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
e59058c4
JS
7556 * @phba: pointer to lpfc hba data structure.
7557 * @cmdiocb: pointer to lpfc command iocb data structure.
7558 * @rspiocb: pointer to lpfc response iocb data structure.
7559 *
7560 * This routine is the callback function that is put to the fabric iocb's
7561 * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
7562 * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
7563 * function first restores and invokes the original iocb's callback function
7564 * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
7565 * fabric bound iocb from the driver internal fabric iocb list onto the wire.
7566 **/
92d7f7b0
JS
7567static void
7568lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7569 struct lpfc_iocbq *rspiocb)
7570{
7571 struct ls_rjt stat;
7572
7573 if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
7574 BUG();
7575
7576 switch (rspiocb->iocb.ulpStatus) {
7577 case IOSTAT_NPORT_RJT:
7578 case IOSTAT_FABRIC_RJT:
7579 if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
7580 lpfc_block_fabric_iocbs(phba);
ed957684 7581 }
92d7f7b0
JS
7582 break;
7583
7584 case IOSTAT_NPORT_BSY:
7585 case IOSTAT_FABRIC_BSY:
7586 lpfc_block_fabric_iocbs(phba);
7587 break;
7588
7589 case IOSTAT_LS_RJT:
7590 stat.un.lsRjtError =
7591 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
7592 if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
7593 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
7594 lpfc_block_fabric_iocbs(phba);
7595 break;
7596 }
7597
7598 if (atomic_read(&phba->fabric_iocb_count) == 0)
7599 BUG();
7600
7601 cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
7602 cmdiocb->fabric_iocb_cmpl = NULL;
7603 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
7604 cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
7605
7606 atomic_dec(&phba->fabric_iocb_count);
7607 if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
7f5f3d0d
JS
7608 /* Post any pending iocbs to HBA */
7609 lpfc_resume_fabric_iocbs(phba);
92d7f7b0
JS
7610 }
7611}
7612
e59058c4 7613/**
3621a710 7614 * lpfc_issue_fabric_iocb - Issue a fabric iocb command
e59058c4
JS
7615 * @phba: pointer to lpfc hba data structure.
7616 * @iocb: pointer to lpfc command iocb data structure.
7617 *
7618 * This routine is used as the top-level API for issuing a fabric iocb command
7619 * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
7620 * function makes sure that only one fabric bound iocb will be outstanding at
7621 * any given time. As such, this function will first check to see whether there
7622 * is already an outstanding fabric iocb on the wire. If so, it will put the
7623 * newly issued iocb onto the driver internal fabric iocb list, waiting to be
7624 * issued later. Otherwise, it will issue the iocb on the wire and update the
7625 * fabric iocb count it indicate that there is one fabric iocb on the wire.
7626 *
7627 * Note, this implementation has a potential sending out fabric IOCBs out of
7628 * order. The problem is caused by the construction of the "ready" boolen does
7629 * not include the condition that the internal fabric IOCB list is empty. As
7630 * such, it is possible a fabric IOCB issued by this routine might be "jump"
7631 * ahead of the fabric IOCBs in the internal list.
7632 *
7633 * Return code
7634 * IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
7635 * IOCB_ERROR - failed to issue fabric iocb
7636 **/
a6ababd2 7637static int
92d7f7b0
JS
7638lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
7639{
7640 unsigned long iflags;
92d7f7b0
JS
7641 int ready;
7642 int ret;
7643
7644 if (atomic_read(&phba->fabric_iocb_count) > 1)
7645 BUG();
7646
7647 spin_lock_irqsave(&phba->hbalock, iflags);
7648 ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
7649 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7650
7f5f3d0d
JS
7651 if (ready)
7652 /* Increment fabric iocb count to hold the position */
7653 atomic_inc(&phba->fabric_iocb_count);
92d7f7b0
JS
7654 spin_unlock_irqrestore(&phba->hbalock, iflags);
7655 if (ready) {
7656 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7657 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7658 iocb->iocb_flag |= LPFC_IO_FABRIC;
7659
858c9f6c
JS
7660 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7661 "Fabric sched2: ste:x%x",
7662 iocb->vport->port_state, 0, 0);
7663
3772a991 7664 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
92d7f7b0
JS
7665
7666 if (ret == IOCB_ERROR) {
7667 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7668 iocb->fabric_iocb_cmpl = NULL;
7669 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7670 atomic_dec(&phba->fabric_iocb_count);
7671 }
7672 } else {
7673 spin_lock_irqsave(&phba->hbalock, iflags);
7674 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
7675 spin_unlock_irqrestore(&phba->hbalock, iflags);
7676 ret = IOCB_SUCCESS;
7677 }
7678 return ret;
7679}
7680
e59058c4 7681/**
3621a710 7682 * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
e59058c4
JS
7683 * @vport: pointer to a virtual N_Port data structure.
7684 *
7685 * This routine aborts all the IOCBs associated with a @vport from the
7686 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
7687 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
7688 * list, removes each IOCB associated with the @vport off the list, set the
7689 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
7690 * associated with the IOCB.
7691 **/
a6ababd2 7692static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
92d7f7b0
JS
7693{
7694 LIST_HEAD(completions);
7695 struct lpfc_hba *phba = vport->phba;
7696 struct lpfc_iocbq *tmp_iocb, *piocb;
92d7f7b0
JS
7697
7698 spin_lock_irq(&phba->hbalock);
7699 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
7700 list) {
7701
7702 if (piocb->vport != vport)
7703 continue;
7704
7705 list_move_tail(&piocb->list, &completions);
7706 }
7707 spin_unlock_irq(&phba->hbalock);
7708
a257bf90
JS
7709 /* Cancel all the IOCBs from the completions list */
7710 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7711 IOERR_SLI_ABORTED);
92d7f7b0
JS
7712}
7713
e59058c4 7714/**
3621a710 7715 * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
e59058c4
JS
7716 * @ndlp: pointer to a node-list data structure.
7717 *
7718 * This routine aborts all the IOCBs associated with an @ndlp from the
7719 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
7720 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
7721 * list, removes each IOCB associated with the @ndlp off the list, set the
7722 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
7723 * associated with the IOCB.
7724 **/
92d7f7b0
JS
7725void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
7726{
7727 LIST_HEAD(completions);
a257bf90 7728 struct lpfc_hba *phba = ndlp->phba;
92d7f7b0
JS
7729 struct lpfc_iocbq *tmp_iocb, *piocb;
7730 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
92d7f7b0
JS
7731
7732 spin_lock_irq(&phba->hbalock);
7733 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
7734 list) {
7735 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
7736
7737 list_move_tail(&piocb->list, &completions);
ed957684 7738 }
dea3101e 7739 }
92d7f7b0
JS
7740 spin_unlock_irq(&phba->hbalock);
7741
a257bf90
JS
7742 /* Cancel all the IOCBs from the completions list */
7743 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7744 IOERR_SLI_ABORTED);
92d7f7b0
JS
7745}
7746
e59058c4 7747/**
3621a710 7748 * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
e59058c4
JS
7749 * @phba: pointer to lpfc hba data structure.
7750 *
7751 * This routine aborts all the IOCBs currently on the driver internal
7752 * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
7753 * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
7754 * list, removes IOCBs off the list, set the status feild to
7755 * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
7756 * the IOCB.
7757 **/
92d7f7b0
JS
7758void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
7759{
7760 LIST_HEAD(completions);
92d7f7b0
JS
7761
7762 spin_lock_irq(&phba->hbalock);
7763 list_splice_init(&phba->fabric_iocb_list, &completions);
7764 spin_unlock_irq(&phba->hbalock);
7765
a257bf90
JS
7766 /* Cancel all the IOCBs from the completions list */
7767 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
7768 IOERR_SLI_ABORTED);
dea3101e 7769}
6fb120a7 7770
1151e3ec
JS
7771/**
7772 * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
7773 * @vport: pointer to lpfc vport data structure.
7774 *
7775 * This routine is invoked by the vport cleanup for deletions and the cleanup
7776 * for an ndlp on removal.
7777 **/
7778void
7779lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
7780{
7781 struct lpfc_hba *phba = vport->phba;
7782 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7783 unsigned long iflag = 0;
7784
7785 spin_lock_irqsave(&phba->hbalock, iflag);
7786 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
7787 list_for_each_entry_safe(sglq_entry, sglq_next,
7788 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
7789 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
7790 sglq_entry->ndlp = NULL;
7791 }
7792 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7793 spin_unlock_irqrestore(&phba->hbalock, iflag);
7794 return;
7795}
7796
6fb120a7
JS
7797/**
7798 * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
7799 * @phba: pointer to lpfc hba data structure.
7800 * @axri: pointer to the els xri abort wcqe structure.
7801 *
7802 * This routine is invoked by the worker thread to process a SLI4 slow-path
7803 * ELS aborted xri.
7804 **/
7805void
7806lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
7807 struct sli4_wcqe_xri_aborted *axri)
7808{
7809 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
19ca7609 7810 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
7851fe2c 7811 uint16_t lxri = 0;
19ca7609 7812
6fb120a7
JS
7813 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
7814 unsigned long iflag = 0;
19ca7609 7815 struct lpfc_nodelist *ndlp;
589a52d6 7816 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6fb120a7 7817
0f65ff68
JS
7818 spin_lock_irqsave(&phba->hbalock, iflag);
7819 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
6fb120a7
JS
7820 list_for_each_entry_safe(sglq_entry, sglq_next,
7821 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
7822 if (sglq_entry->sli4_xritag == xri) {
7823 list_del(&sglq_entry->list);
19ca7609
JS
7824 ndlp = sglq_entry->ndlp;
7825 sglq_entry->ndlp = NULL;
6fb120a7
JS
7826 list_add_tail(&sglq_entry->list,
7827 &phba->sli4_hba.lpfc_sgl_list);
0f65ff68
JS
7828 sglq_entry->state = SGL_FREED;
7829 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
6fb120a7 7830 spin_unlock_irqrestore(&phba->hbalock, iflag);
19ca7609 7831 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
589a52d6
JS
7832
7833 /* Check if TXQ queue needs to be serviced */
7834 if (pring->txq_cnt)
7835 lpfc_worker_wake_up(phba);
6fb120a7
JS
7836 return;
7837 }
7838 }
0f65ff68 7839 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7851fe2c
JS
7840 lxri = lpfc_sli4_xri_inrange(phba, xri);
7841 if (lxri == NO_XRI) {
7842 spin_unlock_irqrestore(&phba->hbalock, iflag);
7843 return;
7844 }
7845 sglq_entry = __lpfc_get_active_sglq(phba, lxri);
0f65ff68
JS
7846 if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
7847 spin_unlock_irqrestore(&phba->hbalock, iflag);
7848 return;
7849 }
7850 sglq_entry->state = SGL_XRI_ABORTED;
7851 spin_unlock_irqrestore(&phba->hbalock, iflag);
7852 return;
6fb120a7 7853}