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