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