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