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