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