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