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