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