Merge branch 'for-linus-v3.20' of git://git.infradead.org/linux-ubifs
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_els.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
16a59fb3 4 * Copyright (C) 2004-2014 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e 20 *******************************************************************/
09372820 21/* See Fibre Channel protocol T11 FC-LS for details */
dea3101e 22#include <linux/blkdev.h>
23#include <linux/pci.h>
5a0e3ad6 24#include <linux/slab.h>
dea3101e 25#include <linux/interrupt.h>
26
91886523 27#include <scsi/scsi.h>
dea3101e 28#include <scsi/scsi_device.h>
29#include <scsi/scsi_host.h>
30#include <scsi/scsi_transport_fc.h>
31
e74c03c8 32
da0436e9 33#include "lpfc_hw4.h"
dea3101e 34#include "lpfc_hw.h"
35#include "lpfc_sli.h"
da0436e9 36#include "lpfc_sli4.h"
ea2151b4 37#include "lpfc_nl.h"
dea3101e 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 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 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 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 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 104
105 /* CLEAR_LA should re-enable link attention events and
25985edc 106 * we should then immediately take a LATT event. The
dea3101e 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 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 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 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 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 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 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 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 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 252 icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
253 }
dea3101e 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 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 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 306 elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
307
308 if (prsp) {
309 list_add(&prsp->list, &pcmd->list);
310 }
dea3101e 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 937
938 /* Start discovery - this should just do CLEAR_LA */
2e0fef85 939 lpfc_disc_start(vport);
dea3101e 940 return 0;
92d7f7b0 941fail:
dea3101e 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 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 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 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 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 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 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 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 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 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 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 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 1202 uint8_t retry)
1203{
2e0fef85 1204 struct lpfc_hba *phba = vport->phba;
dea3101e 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 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 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 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 1272 tmo = phba->fc_ratov;
1273 phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
2e0fef85 1274 lpfc_set_disctmo(vport);
dea3101e 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 1285 if (rc == IOCB_ERROR) {
1286 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 1287 return 1;
dea3101e 1288 }
c9f8735b 1289 return 0;
dea3101e 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 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 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 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 1333 }
1334 }
2e0fef85 1335 spin_unlock_irq(&phba->hbalock);
dea3101e 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 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 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 1456{
1457 int sentplogi;
1458
2e0fef85
JS
1459 if (vport->num_disc_nodes)
1460 vport->num_disc_nodes--;
dea3101e 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 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 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 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 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 1874 goto out;
1875 }
1876
dea3101e 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 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 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 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 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 1957 struct serv_parm *sp;
1958 IOCB_t *icmd;
98c9ea5c 1959 struct lpfc_nodelist *ndlp;
dea3101e 1960 struct lpfc_iocbq *elsiocb;
dea3101e 1961 struct lpfc_sli *psli;
1962 uint8_t *pcmd;
1963 uint16_t cmdsize;
92d7f7b0 1964 int ret;
dea3101e 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 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 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 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 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 2014}
2015
e59058c4 2016/**
3621a710 2017 * lpfc_cmpl_els_prli - Completion callback function for prli
e59058c4
JS
2018 * @phba: pointer to lpfc hba data structure.
2019 * @cmdiocb: pointer to lpfc command iocb data structure.
2020 * @rspiocb: pointer to lpfc response iocb data structure.
2021 *
2022 * This routine is the completion callback function for a Process Login
2023 * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2024 * status. If there is error status reported, PRLI retry shall be attempted
2025 * by invoking the lpfc_els_retry() routine. Otherwise, the state
2026 * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2027 * ndlp to mark the PRLI completion.
2028 **/
dea3101e 2029static void
2e0fef85
JS
2030lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2031 struct lpfc_iocbq *rspiocb)
dea3101e 2032{
2e0fef85
JS
2033 struct lpfc_vport *vport = cmdiocb->vport;
2034 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2035 IOCB_t *irsp;
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 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 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 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 2118 PRLI *npr;
2119 IOCB_t *icmd;
2120 struct lpfc_iocbq *elsiocb;
dea3101e 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 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 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 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 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 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 */
2246 if (vport->port_type == LPFC_PHYSICAL_PORT)
2247 lpfc_issue_clear_la(phba, vport);
2248 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2249 vport->num_disc_nodes = 0;
2250 /* go thru NPR list, issue ELS PLOGIs */
2251 if (vport->fc_npr_cnt)
2252 lpfc_els_disc_plogi(vport);
2253 if (!vport->num_disc_nodes) {
2254 spin_lock_irq(shost->host_lock);
2255 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2256 spin_unlock_irq(shost->host_lock);
2257 lpfc_can_disctmo(vport);
2258 lpfc_end_rscn(vport);
2259 }
2260 }
2261 vport->port_state = LPFC_VPORT_READY;
2262 } else
2263 lpfc_rscn_disc(vport);
2264}
2265
e59058c4 2266/**
3621a710 2267 * lpfc_more_adisc - Issue more adisc as needed
e59058c4
JS
2268 * @vport: pointer to a host virtual N_Port data structure.
2269 *
2270 * This routine determines whether there are more ndlps on a @vport
2271 * node list need to have Address Discover (ADISC) issued. If so, it will
2272 * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2273 * remaining nodes which need to have ADISC sent.
2274 **/
0ff10d46 2275void
2e0fef85 2276lpfc_more_adisc(struct lpfc_vport *vport)
dea3101e 2277{
2e0fef85
JS
2278 if (vport->num_disc_nodes)
2279 vport->num_disc_nodes--;
dea3101e 2280 /* Continue discovery with <num_disc_nodes> ADISCs to go */
e8b62011
JS
2281 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2282 "0210 Continue discovery with %d ADISCs to go "
2283 "Data: x%x x%x x%x\n",
2284 vport->num_disc_nodes, vport->fc_adisc_cnt,
2285 vport->fc_flag, vport->port_state);
dea3101e 2286 /* Check to see if there are more ADISCs to be sent */
2e0fef85
JS
2287 if (vport->fc_flag & FC_NLP_MORE) {
2288 lpfc_set_disctmo(vport);
2289 /* go thru NPR nodes and issue any remaining ELS ADISCs */
eb016566 2290 lpfc_els_disc_adisc(vport);
dea3101e 2291 }
90160e01
JS
2292 if (!vport->num_disc_nodes)
2293 lpfc_adisc_done(vport);
dea3101e 2294 return;
2295}
2296
e59058c4 2297/**
3621a710 2298 * lpfc_cmpl_els_adisc - Completion callback function for adisc
e59058c4
JS
2299 * @phba: pointer to lpfc hba data structure.
2300 * @cmdiocb: pointer to lpfc command iocb data structure.
2301 * @rspiocb: pointer to lpfc response iocb data structure.
2302 *
2303 * This routine is the completion function for issuing the Address Discover
2304 * (ADISC) command. It first checks to see whether link went down during
2305 * the discovery process. If so, the node will be marked as node port
2306 * recovery for issuing discover IOCB by the link attention handler and
2307 * exit. Otherwise, the response status is checked. If error was reported
2308 * in the response status, the ADISC command shall be retried by invoking
2309 * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2310 * the response status, the state machine is invoked to set transition
2311 * with respect to NLP_EVT_CMPL_ADISC event.
2312 **/
dea3101e 2313static void
2e0fef85
JS
2314lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2315 struct lpfc_iocbq *rspiocb)
dea3101e 2316{
2e0fef85
JS
2317 struct lpfc_vport *vport = cmdiocb->vport;
2318 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2319 IOCB_t *irsp;
dea3101e 2320 struct lpfc_nodelist *ndlp;
2e0fef85 2321 int disc;
dea3101e 2322
2323 /* we pass cmdiocb to state machine which needs rspiocb as well */
2324 cmdiocb->context_un.rsp_iocb = rspiocb;
2325
2326 irsp = &(rspiocb->iocb);
2327 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
dea3101e 2328
858c9f6c
JS
2329 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2330 "ADISC cmpl: status:x%x/x%x did:x%x",
2331 irsp->ulpStatus, irsp->un.ulpWord[4],
2332 ndlp->nlp_DID);
2333
dea3101e 2334 /* Since ndlp can be freed in the disc state machine, note if this node
2335 * is being used during discovery.
2336 */
2e0fef85 2337 spin_lock_irq(shost->host_lock);
dea3101e 2338 disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
c9f8735b 2339 ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2e0fef85 2340 spin_unlock_irq(shost->host_lock);
dea3101e 2341 /* ADISC completes to NPort <nlp_DID> */
e8b62011
JS
2342 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2343 "0104 ADISC completes to NPort x%x "
2344 "Data: x%x x%x x%x x%x x%x\n",
2345 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2346 irsp->ulpTimeout, disc, vport->num_disc_nodes);
dea3101e 2347 /* Check to see if link went down during discovery */
2e0fef85
JS
2348 if (lpfc_els_chk_latt(vport)) {
2349 spin_lock_irq(shost->host_lock);
dea3101e 2350 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85 2351 spin_unlock_irq(shost->host_lock);
dea3101e 2352 goto out;
2353 }
2354
2355 if (irsp->ulpStatus) {
2356 /* Check for retry */
2357 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2358 /* ELS command is being retried */
2359 if (disc) {
2e0fef85 2360 spin_lock_irq(shost->host_lock);
dea3101e 2361 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2e0fef85
JS
2362 spin_unlock_irq(shost->host_lock);
2363 lpfc_set_disctmo(vport);
dea3101e 2364 }
2365 goto out;
2366 }
2367 /* ADISC failed */
e40a02c1
JS
2368 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2369 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2370 ndlp->nlp_DID, irsp->ulpStatus,
2371 irsp->un.ulpWord[4]);
dea3101e 2372 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
e47c9093 2373 if (!lpfc_error_lost_link(irsp))
2e0fef85 2374 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
858c9f6c 2375 NLP_EVT_CMPL_ADISC);
e47c9093 2376 } else
dea3101e 2377 /* Good status, call state machine */
2e0fef85 2378 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
dea3101e 2379 NLP_EVT_CMPL_ADISC);
dea3101e 2380
90160e01
JS
2381 /* Check to see if there are more ADISCs to be sent */
2382 if (disc && vport->num_disc_nodes)
2e0fef85 2383 lpfc_more_adisc(vport);
dea3101e 2384out:
2385 lpfc_els_free_iocb(phba, cmdiocb);
2386 return;
2387}
2388
e59058c4 2389/**
3621a710 2390 * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
e59058c4
JS
2391 * @vport: pointer to a virtual N_Port data structure.
2392 * @ndlp: pointer to a node-list data structure.
2393 * @retry: number of retries to the command IOCB.
2394 *
2395 * This routine issues an Address Discover (ADISC) for an @ndlp on a
2396 * @vport. It prepares the payload of the ADISC ELS command, updates the
2397 * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2398 * to issue the ADISC ELS command.
2399 *
2400 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2401 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2402 * will be stored into the context1 field of the IOCB for the completion
2403 * callback function to the ADISC ELS command.
2404 *
2405 * Return code
2406 * 0 - successfully issued adisc
2407 * 1 - failed to issue adisc
2408 **/
dea3101e 2409int
2e0fef85 2410lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e 2411 uint8_t retry)
2412{
2e0fef85
JS
2413 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2414 struct lpfc_hba *phba = vport->phba;
dea3101e 2415 ADISC *ap;
2416 IOCB_t *icmd;
2417 struct lpfc_iocbq *elsiocb;
dea3101e 2418 uint8_t *pcmd;
2419 uint16_t cmdsize;
2420
92d7f7b0 2421 cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2e0fef85
JS
2422 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2423 ndlp->nlp_DID, ELS_CMD_ADISC);
488d1469 2424 if (!elsiocb)
c9f8735b 2425 return 1;
dea3101e 2426
2427 icmd = &elsiocb->iocb;
2428 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2429
2430 /* For ADISC request, remainder of payload is service parameters */
2431 *((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
92d7f7b0 2432 pcmd += sizeof(uint32_t);
dea3101e 2433
2434 /* Fill in ADISC payload */
2435 ap = (ADISC *) pcmd;
2436 ap->hardAL_PA = phba->fc_pref_ALPA;
92d7f7b0
JS
2437 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2438 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 2439 ap->DID = be32_to_cpu(vport->fc_myDID);
dea3101e 2440
858c9f6c
JS
2441 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2442 "Issue ADISC: did:x%x",
2443 ndlp->nlp_DID, 0, 0);
2444
dea3101e 2445 phba->fc_stat.elsXmitADISC++;
2446 elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2e0fef85 2447 spin_lock_irq(shost->host_lock);
dea3101e 2448 ndlp->nlp_flag |= NLP_ADISC_SND;
2e0fef85 2449 spin_unlock_irq(shost->host_lock);
3772a991
JS
2450 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2451 IOCB_ERROR) {
2e0fef85 2452 spin_lock_irq(shost->host_lock);
dea3101e 2453 ndlp->nlp_flag &= ~NLP_ADISC_SND;
2e0fef85 2454 spin_unlock_irq(shost->host_lock);
dea3101e 2455 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2456 return 1;
dea3101e 2457 }
c9f8735b 2458 return 0;
dea3101e 2459}
2460
e59058c4 2461/**
3621a710 2462 * lpfc_cmpl_els_logo - Completion callback function for logo
e59058c4
JS
2463 * @phba: pointer to lpfc hba data structure.
2464 * @cmdiocb: pointer to lpfc command iocb data structure.
2465 * @rspiocb: pointer to lpfc response iocb data structure.
2466 *
2467 * This routine is the completion function for issuing the ELS Logout (LOGO)
2468 * command. If no error status was reported from the LOGO response, the
2469 * state machine of the associated ndlp shall be invoked for transition with
2470 * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2471 * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2472 **/
dea3101e 2473static void
2e0fef85
JS
2474lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2475 struct lpfc_iocbq *rspiocb)
dea3101e 2476{
2e0fef85
JS
2477 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2478 struct lpfc_vport *vport = ndlp->vport;
2479 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 2480 IOCB_t *irsp;
2481 struct lpfc_sli *psli;
92494144 2482 struct lpfcMboxq *mbox;
086a345f
JS
2483 unsigned long flags;
2484 uint32_t skip_recovery = 0;
dea3101e 2485
2486 psli = &phba->sli;
2487 /* we pass cmdiocb to state machine which needs rspiocb as well */
2488 cmdiocb->context_un.rsp_iocb = rspiocb;
2489
2490 irsp = &(rspiocb->iocb);
2e0fef85 2491 spin_lock_irq(shost->host_lock);
dea3101e 2492 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2e0fef85 2493 spin_unlock_irq(shost->host_lock);
dea3101e 2494
858c9f6c
JS
2495 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2496 "LOGO cmpl: status:x%x/x%x did:x%x",
2497 irsp->ulpStatus, irsp->un.ulpWord[4],
2498 ndlp->nlp_DID);
086a345f 2499
dea3101e 2500 /* LOGO completes to NPort <nlp_DID> */
e8b62011
JS
2501 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2502 "0105 LOGO completes to NPort x%x "
2503 "Data: x%x x%x x%x x%x\n",
2504 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2505 irsp->ulpTimeout, vport->num_disc_nodes);
086a345f
JS
2506
2507 if (lpfc_els_chk_latt(vport)) {
2508 skip_recovery = 1;
dea3101e 2509 goto out;
086a345f 2510 }
dea3101e 2511
086a345f 2512 /* Check to see if link went down during discovery */
92d7f7b0
JS
2513 if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2514 /* NLP_EVT_DEVICE_RM should unregister the RPI
2515 * which should abort all outstanding IOs.
2516 */
2517 lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2518 NLP_EVT_DEVICE_RM);
086a345f 2519 skip_recovery = 1;
92d7f7b0
JS
2520 goto out;
2521 }
2522
dea3101e 2523 if (irsp->ulpStatus) {
2524 /* Check for retry */
086a345f 2525 if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
dea3101e 2526 /* ELS command is being retried */
086a345f 2527 skip_recovery = 1;
dea3101e 2528 goto out;
086a345f 2529 }
dea3101e 2530 /* LOGO failed */
e40a02c1
JS
2531 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2532 "2756 LOGO failure DID:%06X Status:x%x/x%x\n",
2533 ndlp->nlp_DID, irsp->ulpStatus,
2534 irsp->un.ulpWord[4]);
dea3101e 2535 /* Do not call DSM for lpfc_els_abort'ed ELS cmds */
086a345f
JS
2536 if (lpfc_error_lost_link(irsp)) {
2537 skip_recovery = 1;
dea3101e 2538 goto out;
086a345f
JS
2539 }
2540 }
2541
2542 /* Call state machine. This will unregister the rpi if needed. */
2543 lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2544
dea3101e 2545out:
2546 lpfc_els_free_iocb(phba, cmdiocb);
92494144
JS
2547 /* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2548 if ((vport->fc_flag & FC_PT2PT) &&
2549 !(vport->fc_flag & FC_PT2PT_PLOGI)) {
2550 phba->pport->fc_myDID = 0;
2551 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2552 if (mbox) {
2553 lpfc_config_link(phba, mbox);
2554 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2555 mbox->vport = vport;
2556 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2557 MBX_NOT_FINISHED) {
2558 mempool_free(mbox, phba->mbox_mem_pool);
086a345f 2559 skip_recovery = 1;
92494144
JS
2560 }
2561 }
2562 }
086a345f
JS
2563
2564 /*
2565 * If the node is a target, the handling attempts to recover the port.
2566 * For any other port type, the rpi is unregistered as an implicit
2567 * LOGO.
2568 */
2569 if ((ndlp->nlp_type & NLP_FCP_TARGET) && (skip_recovery == 0)) {
2570 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2571 spin_lock_irqsave(shost->host_lock, flags);
2572 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2573 spin_unlock_irqrestore(shost->host_lock, flags);
2574
2575 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2576 "3187 LOGO completes to NPort x%x: Start "
2577 "Recovery Data: x%x x%x x%x x%x\n",
2578 ndlp->nlp_DID, irsp->ulpStatus,
2579 irsp->un.ulpWord[4], irsp->ulpTimeout,
2580 vport->num_disc_nodes);
2581 lpfc_disc_start(vport);
2582 }
dea3101e 2583 return;
2584}
2585
e59058c4 2586/**
3621a710 2587 * lpfc_issue_els_logo - Issue a logo to an node on a vport
e59058c4
JS
2588 * @vport: pointer to a virtual N_Port data structure.
2589 * @ndlp: pointer to a node-list data structure.
2590 * @retry: number of retries to the command IOCB.
2591 *
2592 * This routine constructs and issues an ELS Logout (LOGO) iocb command
2593 * to a remote node, referred by an @ndlp on a @vport. It constructs the
2594 * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2595 * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2596 *
2597 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2598 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2599 * will be stored into the context1 field of the IOCB for the completion
2600 * callback function to the LOGO ELS command.
2601 *
2602 * Return code
2603 * 0 - successfully issued logo
2604 * 1 - failed to issue logo
2605 **/
dea3101e 2606int
2e0fef85 2607lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
dea3101e 2608 uint8_t retry)
2609{
2e0fef85
JS
2610 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2611 struct lpfc_hba *phba = vport->phba;
dea3101e 2612 IOCB_t *icmd;
2613 struct lpfc_iocbq *elsiocb;
dea3101e 2614 uint8_t *pcmd;
2615 uint16_t cmdsize;
92d7f7b0 2616 int rc;
dea3101e 2617
98c9ea5c
JS
2618 spin_lock_irq(shost->host_lock);
2619 if (ndlp->nlp_flag & NLP_LOGO_SND) {
2620 spin_unlock_irq(shost->host_lock);
2621 return 0;
2622 }
2623 spin_unlock_irq(shost->host_lock);
2624
92d7f7b0 2625 cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2e0fef85
JS
2626 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2627 ndlp->nlp_DID, ELS_CMD_LOGO);
488d1469 2628 if (!elsiocb)
c9f8735b 2629 return 1;
dea3101e 2630
2631 icmd = &elsiocb->iocb;
2632 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2633 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
92d7f7b0 2634 pcmd += sizeof(uint32_t);
dea3101e 2635
2636 /* Fill in LOGO payload */
2e0fef85 2637 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
92d7f7b0
JS
2638 pcmd += sizeof(uint32_t);
2639 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
dea3101e 2640
858c9f6c
JS
2641 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2642 "Issue LOGO: did:x%x",
2643 ndlp->nlp_DID, 0, 0);
2644
086a345f
JS
2645 /*
2646 * If we are issuing a LOGO, we may try to recover the remote NPort
2647 * by issuing a PLOGI later. Even though we issue ELS cmds by the
2648 * VPI, if we have a valid RPI, and that RPI gets unreg'ed while
2649 * that ELS command is in-flight, the HBA returns a IOERR_INVALID_RPI
2650 * for that ELS cmd. To avoid this situation, lets get rid of the
2651 * RPI right now, before any ELS cmds are sent.
2652 */
2653 spin_lock_irq(shost->host_lock);
2654 ndlp->nlp_flag |= NLP_ISSUE_LOGO;
2655 spin_unlock_irq(shost->host_lock);
2656 if (lpfc_unreg_rpi(vport, ndlp)) {
2657 lpfc_els_free_iocb(phba, elsiocb);
2658 return 0;
2659 }
2660
dea3101e 2661 phba->fc_stat.elsXmitLOGO++;
2662 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2e0fef85 2663 spin_lock_irq(shost->host_lock);
dea3101e 2664 ndlp->nlp_flag |= NLP_LOGO_SND;
086a345f 2665 ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2e0fef85 2666 spin_unlock_irq(shost->host_lock);
3772a991 2667 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
92d7f7b0
JS
2668
2669 if (rc == IOCB_ERROR) {
2e0fef85 2670 spin_lock_irq(shost->host_lock);
dea3101e 2671 ndlp->nlp_flag &= ~NLP_LOGO_SND;
2e0fef85 2672 spin_unlock_irq(shost->host_lock);
dea3101e 2673 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2674 return 1;
dea3101e 2675 }
c9f8735b 2676 return 0;
dea3101e 2677}
2678
e59058c4 2679/**
3621a710 2680 * lpfc_cmpl_els_cmd - Completion callback function for generic els command
e59058c4
JS
2681 * @phba: pointer to lpfc hba data structure.
2682 * @cmdiocb: pointer to lpfc command iocb data structure.
2683 * @rspiocb: pointer to lpfc response iocb data structure.
2684 *
2685 * This routine is a generic completion callback function for ELS commands.
2686 * Specifically, it is the callback function which does not need to perform
2687 * any command specific operations. It is currently used by the ELS command
2688 * issuing routines for the ELS State Change Request (SCR),
2689 * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2690 * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2691 * certain debug loggings, this callback function simply invokes the
2692 * lpfc_els_chk_latt() routine to check whether link went down during the
2693 * discovery process.
2694 **/
dea3101e 2695static void
2e0fef85
JS
2696lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2697 struct lpfc_iocbq *rspiocb)
dea3101e 2698{
2e0fef85 2699 struct lpfc_vport *vport = cmdiocb->vport;
dea3101e 2700 IOCB_t *irsp;
2701
2702 irsp = &rspiocb->iocb;
2703
858c9f6c
JS
2704 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2705 "ELS cmd cmpl: status:x%x/x%x did:x%x",
2706 irsp->ulpStatus, irsp->un.ulpWord[4],
2707 irsp->un.elsreq64.remoteID);
dea3101e 2708 /* ELS cmd tag <ulpIoTag> completes */
e8b62011
JS
2709 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2710 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2711 irsp->ulpIoTag, irsp->ulpStatus,
2712 irsp->un.ulpWord[4], irsp->ulpTimeout);
dea3101e 2713 /* Check to see if link went down during discovery */
2e0fef85 2714 lpfc_els_chk_latt(vport);
dea3101e 2715 lpfc_els_free_iocb(phba, cmdiocb);
2716 return;
2717}
2718
e59058c4 2719/**
3621a710 2720 * lpfc_issue_els_scr - Issue a scr to an node on a vport
e59058c4
JS
2721 * @vport: pointer to a host virtual N_Port data structure.
2722 * @nportid: N_Port identifier to the remote node.
2723 * @retry: number of retries to the command IOCB.
2724 *
2725 * This routine issues a State Change Request (SCR) to a fabric node
2726 * on a @vport. The remote node @nportid is passed into the function. It
2727 * first search the @vport node list to find the matching ndlp. If no such
2728 * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2729 * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2730 * routine is invoked to send the SCR IOCB.
2731 *
2732 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2733 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2734 * will be stored into the context1 field of the IOCB for the completion
2735 * callback function to the SCR ELS command.
2736 *
2737 * Return code
2738 * 0 - Successfully issued scr command
2739 * 1 - Failed to issue scr command
2740 **/
dea3101e 2741int
2e0fef85 2742lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
dea3101e 2743{
2e0fef85 2744 struct lpfc_hba *phba = vport->phba;
dea3101e 2745 IOCB_t *icmd;
2746 struct lpfc_iocbq *elsiocb;
dea3101e 2747 struct lpfc_sli *psli;
2748 uint8_t *pcmd;
2749 uint16_t cmdsize;
2750 struct lpfc_nodelist *ndlp;
2751
2752 psli = &phba->sli;
92d7f7b0 2753 cmdsize = (sizeof(uint32_t) + sizeof(SCR));
dea3101e 2754
e47c9093
JS
2755 ndlp = lpfc_findnode_did(vport, nportid);
2756 if (!ndlp) {
2757 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2758 if (!ndlp)
2759 return 1;
2760 lpfc_nlp_init(vport, ndlp, nportid);
2761 lpfc_enqueue_node(vport, ndlp);
2762 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2763 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2764 if (!ndlp)
2765 return 1;
2766 }
2e0fef85
JS
2767
2768 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2769 ndlp->nlp_DID, ELS_CMD_SCR);
dea3101e 2770
488d1469 2771 if (!elsiocb) {
fa4066b6
JS
2772 /* This will trigger the release of the node just
2773 * allocated
2774 */
329f9bc7 2775 lpfc_nlp_put(ndlp);
c9f8735b 2776 return 1;
dea3101e 2777 }
2778
2779 icmd = &elsiocb->iocb;
2780 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2781
2782 *((uint32_t *) (pcmd)) = ELS_CMD_SCR;
92d7f7b0 2783 pcmd += sizeof(uint32_t);
dea3101e 2784
2785 /* For SCR, remainder of payload is SCR parameter page */
92d7f7b0 2786 memset(pcmd, 0, sizeof(SCR));
dea3101e 2787 ((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2788
858c9f6c
JS
2789 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2790 "Issue SCR: did:x%x",
2791 ndlp->nlp_DID, 0, 0);
2792
dea3101e 2793 phba->fc_stat.elsXmitSCR++;
2794 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3772a991
JS
2795 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2796 IOCB_ERROR) {
fa4066b6
JS
2797 /* The additional lpfc_nlp_put will cause the following
2798 * lpfc_els_free_iocb routine to trigger the rlease of
2799 * the node.
2800 */
329f9bc7 2801 lpfc_nlp_put(ndlp);
dea3101e 2802 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2803 return 1;
dea3101e 2804 }
fa4066b6
JS
2805 /* This will cause the callback-function lpfc_cmpl_els_cmd to
2806 * trigger the release of node.
2807 */
cff261f6 2808
329f9bc7 2809 lpfc_nlp_put(ndlp);
c9f8735b 2810 return 0;
dea3101e 2811}
2812
e59058c4 2813/**
3621a710 2814 * lpfc_issue_els_farpr - Issue a farp to an node on a vport
e59058c4
JS
2815 * @vport: pointer to a host virtual N_Port data structure.
2816 * @nportid: N_Port identifier to the remote node.
2817 * @retry: number of retries to the command IOCB.
2818 *
2819 * This routine issues a Fibre Channel Address Resolution Response
2820 * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2821 * is passed into the function. It first search the @vport node list to find
2822 * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2823 * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2824 * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2825 *
2826 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2827 * will be incremented by 1 for holding the ndlp and the reference to ndlp
2828 * will be stored into the context1 field of the IOCB for the completion
2829 * callback function to the PARPR ELS command.
2830 *
2831 * Return code
2832 * 0 - Successfully issued farpr command
2833 * 1 - Failed to issue farpr command
2834 **/
dea3101e 2835static int
2e0fef85 2836lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
dea3101e 2837{
2e0fef85 2838 struct lpfc_hba *phba = vport->phba;
dea3101e 2839 IOCB_t *icmd;
2840 struct lpfc_iocbq *elsiocb;
dea3101e 2841 struct lpfc_sli *psli;
2842 FARP *fp;
2843 uint8_t *pcmd;
2844 uint32_t *lp;
2845 uint16_t cmdsize;
2846 struct lpfc_nodelist *ondlp;
2847 struct lpfc_nodelist *ndlp;
2848
2849 psli = &phba->sli;
92d7f7b0 2850 cmdsize = (sizeof(uint32_t) + sizeof(FARP));
dea3101e 2851
e47c9093
JS
2852 ndlp = lpfc_findnode_did(vport, nportid);
2853 if (!ndlp) {
2854 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2855 if (!ndlp)
2856 return 1;
2857 lpfc_nlp_init(vport, ndlp, nportid);
2858 lpfc_enqueue_node(vport, ndlp);
2859 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2860 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2861 if (!ndlp)
2862 return 1;
2863 }
2e0fef85
JS
2864
2865 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2866 ndlp->nlp_DID, ELS_CMD_RNID);
488d1469 2867 if (!elsiocb) {
fa4066b6
JS
2868 /* This will trigger the release of the node just
2869 * allocated
2870 */
329f9bc7 2871 lpfc_nlp_put(ndlp);
c9f8735b 2872 return 1;
dea3101e 2873 }
2874
2875 icmd = &elsiocb->iocb;
2876 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2877
2878 *((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
92d7f7b0 2879 pcmd += sizeof(uint32_t);
dea3101e 2880
2881 /* Fill in FARPR payload */
2882 fp = (FARP *) (pcmd);
92d7f7b0 2883 memset(fp, 0, sizeof(FARP));
dea3101e 2884 lp = (uint32_t *) pcmd;
2885 *lp++ = be32_to_cpu(nportid);
2e0fef85 2886 *lp++ = be32_to_cpu(vport->fc_myDID);
dea3101e 2887 fp->Rflags = 0;
2888 fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2889
92d7f7b0
JS
2890 memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2891 memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 2892 ondlp = lpfc_findnode_did(vport, nportid);
e47c9093 2893 if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
dea3101e 2894 memcpy(&fp->OportName, &ondlp->nlp_portname,
92d7f7b0 2895 sizeof(struct lpfc_name));
dea3101e 2896 memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
92d7f7b0 2897 sizeof(struct lpfc_name));
dea3101e 2898 }
2899
858c9f6c
JS
2900 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2901 "Issue FARPR: did:x%x",
2902 ndlp->nlp_DID, 0, 0);
2903
dea3101e 2904 phba->fc_stat.elsXmitFARPR++;
2905 elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3772a991
JS
2906 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2907 IOCB_ERROR) {
fa4066b6
JS
2908 /* The additional lpfc_nlp_put will cause the following
2909 * lpfc_els_free_iocb routine to trigger the release of
2910 * the node.
2911 */
329f9bc7 2912 lpfc_nlp_put(ndlp);
dea3101e 2913 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 2914 return 1;
dea3101e 2915 }
fa4066b6
JS
2916 /* This will cause the callback-function lpfc_cmpl_els_cmd to
2917 * trigger the release of the node.
2918 */
329f9bc7 2919 lpfc_nlp_put(ndlp);
c9f8735b 2920 return 0;
dea3101e 2921}
2922
e59058c4 2923/**
3621a710 2924 * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
e59058c4
JS
2925 * @vport: pointer to a host virtual N_Port data structure.
2926 * @nlp: pointer to a node-list data structure.
2927 *
2928 * This routine cancels the timer with a delayed IOCB-command retry for
2929 * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2930 * removes the ELS retry event if it presents. In addition, if the
2931 * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2932 * commands are sent for the @vport's nodes that require issuing discovery
2933 * ADISC.
2934 **/
fdcebe28 2935void
2e0fef85 2936lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
fdcebe28 2937{
2e0fef85 2938 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
e47c9093 2939 struct lpfc_work_evt *evtp;
2e0fef85 2940
0d2b6b83
JS
2941 if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2942 return;
2e0fef85 2943 spin_lock_irq(shost->host_lock);
fdcebe28 2944 nlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 2945 spin_unlock_irq(shost->host_lock);
fdcebe28
JS
2946 del_timer_sync(&nlp->nlp_delayfunc);
2947 nlp->nlp_last_elscmd = 0;
e47c9093 2948 if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
fdcebe28 2949 list_del_init(&nlp->els_retry_evt.evt_listp);
e47c9093
JS
2950 /* Decrement nlp reference count held for the delayed retry */
2951 evtp = &nlp->els_retry_evt;
2952 lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2953 }
fdcebe28 2954 if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2e0fef85 2955 spin_lock_irq(shost->host_lock);
fdcebe28 2956 nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2e0fef85
JS
2957 spin_unlock_irq(shost->host_lock);
2958 if (vport->num_disc_nodes) {
0d2b6b83
JS
2959 if (vport->port_state < LPFC_VPORT_READY) {
2960 /* Check if there are more ADISCs to be sent */
2961 lpfc_more_adisc(vport);
0d2b6b83
JS
2962 } else {
2963 /* Check if there are more PLOGIs to be sent */
2964 lpfc_more_plogi(vport);
90160e01
JS
2965 if (vport->num_disc_nodes == 0) {
2966 spin_lock_irq(shost->host_lock);
2967 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2968 spin_unlock_irq(shost->host_lock);
2969 lpfc_can_disctmo(vport);
2970 lpfc_end_rscn(vport);
2971 }
fdcebe28
JS
2972 }
2973 }
2974 }
2975 return;
2976}
2977
e59058c4 2978/**
3621a710 2979 * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
e59058c4
JS
2980 * @ptr: holder for the pointer to the timer function associated data (ndlp).
2981 *
2982 * This routine is invoked by the ndlp delayed-function timer to check
2983 * whether there is any pending ELS retry event(s) with the node. If not, it
2984 * simply returns. Otherwise, if there is at least one ELS delayed event, it
2985 * adds the delayed events to the HBA work list and invokes the
2986 * lpfc_worker_wake_up() routine to wake up worker thread to process the
2987 * event. Note that lpfc_nlp_get() is called before posting the event to
2988 * the work list to hold reference count of ndlp so that it guarantees the
2989 * reference to ndlp will still be available when the worker thread gets
2990 * to the event associated with the ndlp.
2991 **/
dea3101e 2992void
2993lpfc_els_retry_delay(unsigned long ptr)
2994{
2e0fef85
JS
2995 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2996 struct lpfc_vport *vport = ndlp->vport;
2e0fef85 2997 struct lpfc_hba *phba = vport->phba;
92d7f7b0 2998 unsigned long flags;
2e0fef85 2999 struct lpfc_work_evt *evtp = &ndlp->els_retry_evt;
dea3101e 3000
92d7f7b0 3001 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 3002 if (!list_empty(&evtp->evt_listp)) {
92d7f7b0 3003 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 3004 return;
3005 }
3006
fa4066b6
JS
3007 /* We need to hold the node by incrementing the reference
3008 * count until the queued work is done
3009 */
3010 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
5e9d9b82
JS
3011 if (evtp->evt_arg1) {
3012 evtp->evt = LPFC_EVT_ELS_RETRY;
3013 list_add_tail(&evtp->evt_listp, &phba->work_list);
92d7f7b0 3014 lpfc_worker_wake_up(phba);
5e9d9b82 3015 }
92d7f7b0 3016 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 3017 return;
3018}
3019
e59058c4 3020/**
3621a710 3021 * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
e59058c4
JS
3022 * @ndlp: pointer to a node-list data structure.
3023 *
3024 * This routine is the worker-thread handler for processing the @ndlp delayed
3025 * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3026 * the last ELS command from the associated ndlp and invokes the proper ELS
3027 * function according to the delayed ELS command to retry the command.
3028 **/
dea3101e 3029void
3030lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3031{
2e0fef85
JS
3032 struct lpfc_vport *vport = ndlp->vport;
3033 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
eb016566 3034 uint32_t cmd, retry;
dea3101e 3035
2e0fef85 3036 spin_lock_irq(shost->host_lock);
5024ab17
JW
3037 cmd = ndlp->nlp_last_elscmd;
3038 ndlp->nlp_last_elscmd = 0;
dea3101e 3039
3040 if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2e0fef85 3041 spin_unlock_irq(shost->host_lock);
dea3101e 3042 return;
3043 }
3044
3045 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2e0fef85 3046 spin_unlock_irq(shost->host_lock);
1a169689
JS
3047 /*
3048 * If a discovery event readded nlp_delayfunc after timer
3049 * firing and before processing the timer, cancel the
3050 * nlp_delayfunc.
3051 */
3052 del_timer_sync(&ndlp->nlp_delayfunc);
dea3101e 3053 retry = ndlp->nlp_retry;
4d9ab994 3054 ndlp->nlp_retry = 0;
dea3101e 3055
3056 switch (cmd) {
3057 case ELS_CMD_FLOGI:
2e0fef85 3058 lpfc_issue_els_flogi(vport, ndlp, retry);
dea3101e 3059 break;
3060 case ELS_CMD_PLOGI:
2e0fef85 3061 if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
5024ab17 3062 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3063 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6ad42535 3064 }
dea3101e 3065 break;
3066 case ELS_CMD_ADISC:
2e0fef85 3067 if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
5024ab17 3068 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3069 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
6ad42535 3070 }
dea3101e 3071 break;
3072 case ELS_CMD_PRLI:
2e0fef85 3073 if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
5024ab17 3074 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3075 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
6ad42535 3076 }
dea3101e 3077 break;
3078 case ELS_CMD_LOGO:
2e0fef85 3079 if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
5024ab17 3080 ndlp->nlp_prev_state = ndlp->nlp_state;
086a345f 3081 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
6ad42535 3082 }
dea3101e 3083 break;
92d7f7b0 3084 case ELS_CMD_FDISC:
fedd3b7b
JS
3085 if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3086 lpfc_issue_els_fdisc(vport, ndlp, retry);
92d7f7b0 3087 break;
dea3101e 3088 }
3089 return;
3090}
3091
e59058c4 3092/**
3621a710 3093 * lpfc_els_retry - Make retry decision on an els command iocb
e59058c4
JS
3094 * @phba: pointer to lpfc hba data structure.
3095 * @cmdiocb: pointer to lpfc command iocb data structure.
3096 * @rspiocb: pointer to lpfc response iocb data structure.
3097 *
3098 * This routine makes a retry decision on an ELS command IOCB, which has
3099 * failed. The following ELS IOCBs use this function for retrying the command
3100 * when previously issued command responsed with error status: FLOGI, PLOGI,
3101 * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3102 * returned error status, it makes the decision whether a retry shall be
3103 * issued for the command, and whether a retry shall be made immediately or
3104 * delayed. In the former case, the corresponding ELS command issuing-function
3105 * is called to retry the command. In the later case, the ELS command shall
3106 * be posted to the ndlp delayed event and delayed function timer set to the
3107 * ndlp for the delayed command issusing.
3108 *
3109 * Return code
3110 * 0 - No retry of els command is made
3111 * 1 - Immediate or delayed retry of els command is made
3112 **/
dea3101e 3113static int
2e0fef85
JS
3114lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3115 struct lpfc_iocbq *rspiocb)
dea3101e 3116{
2e0fef85
JS
3117 struct lpfc_vport *vport = cmdiocb->vport;
3118 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3119 IOCB_t *irsp = &rspiocb->iocb;
3120 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3121 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
dea3101e 3122 uint32_t *elscmd;
3123 struct ls_rjt stat;
2e0fef85 3124 int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
98c9ea5c 3125 int logerr = 0;
2e0fef85 3126 uint32_t cmd = 0;
488d1469 3127 uint32_t did;
dea3101e 3128
488d1469 3129
dea3101e 3130 /* Note: context2 may be 0 for internal driver abort
3131 * of delays ELS command.
3132 */
3133
3134 if (pcmd && pcmd->virt) {
3135 elscmd = (uint32_t *) (pcmd->virt);
3136 cmd = *elscmd++;
3137 }
3138
e47c9093 3139 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
488d1469
JS
3140 did = ndlp->nlp_DID;
3141 else {
3142 /* We should only hit this case for retrying PLOGI */
3143 did = irsp->un.elsreq64.remoteID;
2e0fef85 3144 ndlp = lpfc_findnode_did(vport, did);
e47c9093
JS
3145 if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3146 && (cmd != ELS_CMD_PLOGI))
488d1469
JS
3147 return 1;
3148 }
3149
858c9f6c
JS
3150 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3151 "Retry ELS: wd7:x%x wd4:x%x did:x%x",
3152 *(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3153
dea3101e 3154 switch (irsp->ulpStatus) {
3155 case IOSTAT_FCP_RSP_ERROR:
1151e3ec 3156 break;
dea3101e 3157 case IOSTAT_REMOTE_STOP:
1151e3ec
JS
3158 if (phba->sli_rev == LPFC_SLI_REV4) {
3159 /* This IO was aborted by the target, we don't
3160 * know the rxid and because we did not send the
3161 * ABTS we cannot generate and RRQ.
3162 */
3163 lpfc_set_rrq_active(phba, ndlp,
ee0f4fe1 3164 cmdiocb->sli4_lxritag, 0, 0);
1151e3ec 3165 }
dea3101e 3166 break;
dea3101e 3167 case IOSTAT_LOCAL_REJECT:
e3d2b802 3168 switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
dea3101e 3169 case IOERR_LOOP_OPEN_FAILURE:
eaf15d5b
JS
3170 if (cmd == ELS_CMD_FLOGI) {
3171 if (PCI_DEVICE_ID_HORNET ==
3172 phba->pcidev->device) {
76a95d75 3173 phba->fc_topology = LPFC_TOPOLOGY_LOOP;
eaf15d5b
JS
3174 phba->pport->fc_myDID = 0;
3175 phba->alpa_map[0] = 0;
3176 phba->alpa_map[1] = 0;
3177 }
3178 }
2e0fef85 3179 if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
92d7f7b0 3180 delay = 1000;
dea3101e 3181 retry = 1;
3182 break;
3183
92d7f7b0 3184 case IOERR_ILLEGAL_COMMAND:
7f5f3d0d
JS
3185 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3186 "0124 Retry illegal cmd x%x "
3187 "retry:x%x delay:x%x\n",
3188 cmd, cmdiocb->retry, delay);
3189 retry = 1;
3190 /* All command's retry policy */
3191 maxretry = 8;
3192 if (cmdiocb->retry > 2)
3193 delay = 1000;
92d7f7b0
JS
3194 break;
3195
dea3101e 3196 case IOERR_NO_RESOURCES:
98c9ea5c 3197 logerr = 1; /* HBA out of resources */
858c9f6c
JS
3198 retry = 1;
3199 if (cmdiocb->retry > 100)
3200 delay = 100;
3201 maxretry = 250;
3202 break;
3203
3204 case IOERR_ILLEGAL_FRAME:
92d7f7b0 3205 delay = 100;
dea3101e 3206 retry = 1;
3207 break;
3208
858c9f6c 3209 case IOERR_SEQUENCE_TIMEOUT:
dea3101e 3210 case IOERR_INVALID_RPI:
5b5b36a9
JS
3211 if (cmd == ELS_CMD_PLOGI &&
3212 did == NameServer_DID) {
3213 /* Continue forever if plogi to */
3214 /* the nameserver fails */
3215 maxretry = 0;
3216 delay = 100;
3217 }
dea3101e 3218 retry = 1;
3219 break;
3220 }
3221 break;
3222
3223 case IOSTAT_NPORT_RJT:
3224 case IOSTAT_FABRIC_RJT:
3225 if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3226 retry = 1;
3227 break;
3228 }
3229 break;
3230
3231 case IOSTAT_NPORT_BSY:
3232 case IOSTAT_FABRIC_BSY:
98c9ea5c 3233 logerr = 1; /* Fabric / Remote NPort out of resources */
dea3101e 3234 retry = 1;
3235 break;
3236
3237 case IOSTAT_LS_RJT:
3238 stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3239 /* Added for Vendor specifc support
3240 * Just keep retrying for these Rsn / Exp codes
3241 */
3242 switch (stat.un.b.lsRjtRsnCode) {
3243 case LSRJT_UNABLE_TPC:
3244 if (stat.un.b.lsRjtRsnCodeExp ==
3245 LSEXP_CMD_IN_PROGRESS) {
3246 if (cmd == ELS_CMD_PLOGI) {
92d7f7b0 3247 delay = 1000;
dea3101e 3248 maxretry = 48;
3249 }
3250 retry = 1;
3251 break;
3252 }
ffc95493
JS
3253 if (stat.un.b.lsRjtRsnCodeExp ==
3254 LSEXP_CANT_GIVE_DATA) {
3255 if (cmd == ELS_CMD_PLOGI) {
3256 delay = 1000;
3257 maxretry = 48;
3258 }
3259 retry = 1;
3260 break;
3261 }
4c1b64ba
JS
3262 if ((cmd == ELS_CMD_PLOGI) ||
3263 (cmd == ELS_CMD_PRLI)) {
92d7f7b0 3264 delay = 1000;
dea3101e 3265 maxretry = lpfc_max_els_tries + 1;
3266 retry = 1;
3267 break;
3268 }
92d7f7b0
JS
3269 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3270 (cmd == ELS_CMD_FDISC) &&
3271 (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
e8b62011
JS
3272 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3273 "0125 FDISC Failed (x%x). "
3274 "Fabric out of resources\n",
3275 stat.un.lsRjtError);
92d7f7b0
JS
3276 lpfc_vport_set_state(vport,
3277 FC_VPORT_NO_FABRIC_RSCS);
3278 }
dea3101e 3279 break;
3280
3281 case LSRJT_LOGICAL_BSY:
858c9f6c
JS
3282 if ((cmd == ELS_CMD_PLOGI) ||
3283 (cmd == ELS_CMD_PRLI)) {
92d7f7b0 3284 delay = 1000;
dea3101e 3285 maxretry = 48;
92d7f7b0 3286 } else if (cmd == ELS_CMD_FDISC) {
51ef4c26
JS
3287 /* FDISC retry policy */
3288 maxretry = 48;
3289 if (cmdiocb->retry >= 32)
3290 delay = 1000;
dea3101e 3291 }
3292 retry = 1;
3293 break;
92d7f7b0
JS
3294
3295 case LSRJT_LOGICAL_ERR:
7f5f3d0d
JS
3296 /* There are some cases where switches return this
3297 * error when they are not ready and should be returning
3298 * Logical Busy. We should delay every time.
3299 */
3300 if (cmd == ELS_CMD_FDISC &&
3301 stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
3302 maxretry = 3;
3303 delay = 1000;
3304 retry = 1;
3305 break;
3306 }
92d7f7b0
JS
3307 case LSRJT_PROTOCOL_ERR:
3308 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3309 (cmd == ELS_CMD_FDISC) &&
3310 ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
3311 (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
3312 ) {
e8b62011 3313 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
d7c255b2 3314 "0122 FDISC Failed (x%x). "
e8b62011
JS
3315 "Fabric Detected Bad WWN\n",
3316 stat.un.lsRjtError);
92d7f7b0
JS
3317 lpfc_vport_set_state(vport,
3318 FC_VPORT_FABRIC_REJ_WWN);
3319 }
3320 break;
dea3101e 3321 }
3322 break;
3323
3324 case IOSTAT_INTERMED_RSP:
3325 case IOSTAT_BA_RJT:
3326 break;
3327
3328 default:
3329 break;
3330 }
3331
488d1469 3332 if (did == FDMI_DID)
dea3101e 3333 retry = 1;
dea3101e 3334
df9e1b59 3335 if ((cmd == ELS_CMD_FLOGI) &&
76a95d75 3336 (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
1b32f6aa 3337 !lpfc_error_lost_link(irsp)) {
98c9ea5c
JS
3338 /* FLOGI retry policy */
3339 retry = 1;
df9e1b59 3340 /* retry FLOGI forever */
6669f9bb
JS
3341 maxretry = 0;
3342 if (cmdiocb->retry >= 100)
3343 delay = 5000;
3344 else if (cmdiocb->retry >= 32)
98c9ea5c 3345 delay = 1000;
df9e1b59
JS
3346 } else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
3347 /* retry FDISCs every second up to devloss */
3348 retry = 1;
3349 maxretry = vport->cfg_devloss_tmo;
3350 delay = 1000;
98c9ea5c
JS
3351 }
3352
6669f9bb
JS
3353 cmdiocb->retry++;
3354 if (maxretry && (cmdiocb->retry >= maxretry)) {
dea3101e 3355 phba->fc_stat.elsRetryExceeded++;
3356 retry = 0;
3357 }
3358
ed957684
JS
3359 if ((vport->load_flag & FC_UNLOADING) != 0)
3360 retry = 0;
3361
dea3101e 3362 if (retry) {
38b92ef8
JS
3363 if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
3364 /* Stop retrying PLOGI and FDISC if in FCF discovery */
3365 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3366 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3367 "2849 Stop retry ELS command "
3368 "x%x to remote NPORT x%x, "
3369 "Data: x%x x%x\n", cmd, did,
3370 cmdiocb->retry, delay);
3371 return 0;
3372 }
3373 }
dea3101e 3374
3375 /* Retry ELS command <elsCmd> to remote NPORT <did> */
e8b62011
JS
3376 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3377 "0107 Retry ELS command x%x to remote "
3378 "NPORT x%x Data: x%x x%x\n",
3379 cmd, did, cmdiocb->retry, delay);
dea3101e 3380
858c9f6c
JS
3381 if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
3382 ((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
e3d2b802
JS
3383 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
3384 IOERR_NO_RESOURCES))) {
858c9f6c
JS
3385 /* Don't reset timer for no resources */
3386
dea3101e 3387 /* If discovery / RSCN timer is running, reset it */
2e0fef85 3388 if (timer_pending(&vport->fc_disctmo) ||
92d7f7b0 3389 (vport->fc_flag & FC_RSCN_MODE))
2e0fef85 3390 lpfc_set_disctmo(vport);
dea3101e 3391 }
3392
3393 phba->fc_stat.elsXmitRetry++;
58da1ffb 3394 if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
dea3101e 3395 phba->fc_stat.elsDelayRetry++;
3396 ndlp->nlp_retry = cmdiocb->retry;
3397
92d7f7b0
JS
3398 /* delay is specified in milliseconds */
3399 mod_timer(&ndlp->nlp_delayfunc,
3400 jiffies + msecs_to_jiffies(delay));
2e0fef85 3401 spin_lock_irq(shost->host_lock);
dea3101e 3402 ndlp->nlp_flag |= NLP_DELAY_TMO;
2e0fef85 3403 spin_unlock_irq(shost->host_lock);
dea3101e 3404
5024ab17 3405 ndlp->nlp_prev_state = ndlp->nlp_state;
858c9f6c
JS
3406 if (cmd == ELS_CMD_PRLI)
3407 lpfc_nlp_set_state(vport, ndlp,
4c1b64ba 3408 NLP_STE_PRLI_ISSUE);
858c9f6c
JS
3409 else
3410 lpfc_nlp_set_state(vport, ndlp,
3411 NLP_STE_NPR_NODE);
dea3101e 3412 ndlp->nlp_last_elscmd = cmd;
3413
c9f8735b 3414 return 1;
dea3101e 3415 }
3416 switch (cmd) {
3417 case ELS_CMD_FLOGI:
2e0fef85 3418 lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
c9f8735b 3419 return 1;
92d7f7b0
JS
3420 case ELS_CMD_FDISC:
3421 lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
3422 return 1;
dea3101e 3423 case ELS_CMD_PLOGI:
58da1ffb 3424 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
488d1469 3425 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 3426 lpfc_nlp_set_state(vport, ndlp,
de0c5b32 3427 NLP_STE_PLOGI_ISSUE);
488d1469 3428 }
2e0fef85 3429 lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
c9f8735b 3430 return 1;
dea3101e 3431 case ELS_CMD_ADISC:
5024ab17 3432 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3433 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3434 lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
c9f8735b 3435 return 1;
dea3101e 3436 case ELS_CMD_PRLI:
5024ab17 3437 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
3438 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3439 lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
c9f8735b 3440 return 1;
dea3101e 3441 case ELS_CMD_LOGO:
5024ab17 3442 ndlp->nlp_prev_state = ndlp->nlp_state;
086a345f 3443 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
2e0fef85 3444 lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
c9f8735b 3445 return 1;
dea3101e 3446 }
3447 }
dea3101e 3448 /* No retry ELS command <elsCmd> to remote NPORT <did> */
98c9ea5c
JS
3449 if (logerr) {
3450 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3451 "0137 No retry ELS command x%x to remote "
3452 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
3453 cmd, did, irsp->ulpStatus,
3454 irsp->un.ulpWord[4]);
3455 }
3456 else {
3457 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
a58cbd52
JS
3458 "0108 No retry ELS command x%x to remote "
3459 "NPORT x%x Retried:%d Error:x%x/%x\n",
3460 cmd, did, cmdiocb->retry, irsp->ulpStatus,
3461 irsp->un.ulpWord[4]);
98c9ea5c 3462 }
c9f8735b 3463 return 0;
dea3101e 3464}
3465
e59058c4 3466/**
3621a710 3467 * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
e59058c4
JS
3468 * @phba: pointer to lpfc hba data structure.
3469 * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
3470 *
3471 * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
3472 * associated with a command IOCB back to the lpfc DMA buffer pool. It first
3473 * checks to see whether there is a lpfc DMA buffer associated with the
3474 * response of the command IOCB. If so, it will be released before releasing
3475 * the lpfc DMA buffer associated with the IOCB itself.
3476 *
3477 * Return code
3478 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3479 **/
09372820 3480static int
87af33fe
JS
3481lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
3482{
3483 struct lpfc_dmabuf *buf_ptr;
3484
e59058c4 3485 /* Free the response before processing the command. */
87af33fe
JS
3486 if (!list_empty(&buf_ptr1->list)) {
3487 list_remove_head(&buf_ptr1->list, buf_ptr,
3488 struct lpfc_dmabuf,
3489 list);
3490 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3491 kfree(buf_ptr);
3492 }
3493 lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
3494 kfree(buf_ptr1);
3495 return 0;
3496}
3497
e59058c4 3498/**
3621a710 3499 * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
e59058c4
JS
3500 * @phba: pointer to lpfc hba data structure.
3501 * @buf_ptr: pointer to the lpfc dma buffer data structure.
3502 *
3503 * This routine releases the lpfc Direct Memory Access (DMA) buffer
3504 * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
3505 * pool.
3506 *
3507 * Return code
3508 * 0 - Successfully released lpfc DMA buffer (currently, always return 0)
3509 **/
09372820 3510static int
87af33fe
JS
3511lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
3512{
3513 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
3514 kfree(buf_ptr);
3515 return 0;
3516}
3517
e59058c4 3518/**
3621a710 3519 * lpfc_els_free_iocb - Free a command iocb and its associated resources
e59058c4
JS
3520 * @phba: pointer to lpfc hba data structure.
3521 * @elsiocb: pointer to lpfc els command iocb data structure.
3522 *
3523 * This routine frees a command IOCB and its associated resources. The
3524 * command IOCB data structure contains the reference to various associated
3525 * resources, these fields must be set to NULL if the associated reference
3526 * not present:
3527 * context1 - reference to ndlp
3528 * context2 - reference to cmd
3529 * context2->next - reference to rsp
3530 * context3 - reference to bpl
3531 *
3532 * It first properly decrements the reference count held on ndlp for the
3533 * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
3534 * set, it invokes the lpfc_els_free_data() routine to release the Direct
3535 * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
3536 * adds the DMA buffer the @phba data structure for the delayed release.
3537 * If reference to the Buffer Pointer List (BPL) is present, the
3538 * lpfc_els_free_bpl() routine is invoked to release the DMA memory
3539 * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
3540 * invoked to release the IOCB data structure back to @phba IOCBQ list.
3541 *
3542 * Return code
3543 * 0 - Success (currently, always return 0)
3544 **/
dea3101e 3545int
329f9bc7 3546lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
dea3101e 3547{
3548 struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
a8adb832
JS
3549 struct lpfc_nodelist *ndlp;
3550
3551 ndlp = (struct lpfc_nodelist *)elsiocb->context1;
3552 if (ndlp) {
3553 if (ndlp->nlp_flag & NLP_DEFER_RM) {
3554 lpfc_nlp_put(ndlp);
dea3101e 3555
a8adb832
JS
3556 /* If the ndlp is not being used by another discovery
3557 * thread, free it.
3558 */
3559 if (!lpfc_nlp_not_used(ndlp)) {
3560 /* If ndlp is being used by another discovery
3561 * thread, just clear NLP_DEFER_RM
3562 */
3563 ndlp->nlp_flag &= ~NLP_DEFER_RM;
3564 }
3565 }
3566 else
3567 lpfc_nlp_put(ndlp);
329f9bc7
JS
3568 elsiocb->context1 = NULL;
3569 }
dea3101e 3570 /* context2 = cmd, context2->next = rsp, context3 = bpl */
3571 if (elsiocb->context2) {
0ff10d46
JS
3572 if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
3573 /* Firmware could still be in progress of DMAing
3574 * payload, so don't free data buffer till after
3575 * a hbeat.
3576 */
3577 elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
3578 buf_ptr = elsiocb->context2;
3579 elsiocb->context2 = NULL;
3580 if (buf_ptr) {
3581 buf_ptr1 = NULL;
3582 spin_lock_irq(&phba->hbalock);
3583 if (!list_empty(&buf_ptr->list)) {
3584 list_remove_head(&buf_ptr->list,
3585 buf_ptr1, struct lpfc_dmabuf,
3586 list);
3587 INIT_LIST_HEAD(&buf_ptr1->list);
3588 list_add_tail(&buf_ptr1->list,
3589 &phba->elsbuf);
3590 phba->elsbuf_cnt++;
3591 }
3592 INIT_LIST_HEAD(&buf_ptr->list);
3593 list_add_tail(&buf_ptr->list, &phba->elsbuf);
3594 phba->elsbuf_cnt++;
3595 spin_unlock_irq(&phba->hbalock);
3596 }
3597 } else {
3598 buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
3599 lpfc_els_free_data(phba, buf_ptr1);
3600 }
dea3101e 3601 }
3602
3603 if (elsiocb->context3) {
3604 buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
87af33fe 3605 lpfc_els_free_bpl(phba, buf_ptr);
dea3101e 3606 }
604a3e30 3607 lpfc_sli_release_iocbq(phba, elsiocb);
dea3101e 3608 return 0;
3609}
3610
e59058c4 3611/**
3621a710 3612 * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
e59058c4
JS
3613 * @phba: pointer to lpfc hba data structure.
3614 * @cmdiocb: pointer to lpfc command iocb data structure.
3615 * @rspiocb: pointer to lpfc response iocb data structure.
3616 *
3617 * This routine is the completion callback function to the Logout (LOGO)
3618 * Accept (ACC) Response ELS command. This routine is invoked to indicate
3619 * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
3620 * release the ndlp if it has the last reference remaining (reference count
3621 * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
3622 * field to NULL to inform the following lpfc_els_free_iocb() routine no
3623 * ndlp reference count needs to be decremented. Otherwise, the ndlp
3624 * reference use-count shall be decremented by the lpfc_els_free_iocb()
3625 * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
3626 * IOCB data structure.
3627 **/
dea3101e 3628static void
2e0fef85
JS
3629lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3630 struct lpfc_iocbq *rspiocb)
dea3101e 3631{
2e0fef85
JS
3632 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3633 struct lpfc_vport *vport = cmdiocb->vport;
858c9f6c
JS
3634 IOCB_t *irsp;
3635
3636 irsp = &rspiocb->iocb;
3637 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3638 "ACC LOGO cmpl: status:x%x/x%x did:x%x",
3639 irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
dea3101e 3640 /* ACC to LOGO completes to NPort <nlp_DID> */
e8b62011
JS
3641 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3642 "0109 ACC to LOGO completes to NPort x%x "
3643 "Data: x%x x%x x%x\n",
3644 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3645 ndlp->nlp_rpi);
87af33fe
JS
3646
3647 if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
3648 /* NPort Recovery mode or node is just allocated */
3649 if (!lpfc_nlp_not_used(ndlp)) {
3650 /* If the ndlp is being used by another discovery
3651 * thread, just unregister the RPI.
3652 */
3653 lpfc_unreg_rpi(vport, ndlp);
fa4066b6
JS
3654 } else {
3655 /* Indicate the node has already released, should
3656 * not reference to it from within lpfc_els_free_iocb.
3657 */
3658 cmdiocb->context1 = NULL;
87af33fe 3659 }
dea3101e 3660 }
73d91e50
JS
3661
3662 /*
3663 * The driver received a LOGO from the rport and has ACK'd it.
df9e1b59 3664 * At this point, the driver is done so release the IOCB
73d91e50 3665 */
dea3101e 3666 lpfc_els_free_iocb(phba, cmdiocb);
df9e1b59
JS
3667
3668 /*
3669 * Remove the ndlp reference if it's a fabric node that has
3670 * sent us an unsolicted LOGO.
3671 */
3672 if (ndlp->nlp_type & NLP_FABRIC)
3673 lpfc_nlp_put(ndlp);
3674
dea3101e 3675 return;
3676}
3677
e59058c4 3678/**
3621a710 3679 * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
e59058c4
JS
3680 * @phba: pointer to lpfc hba data structure.
3681 * @pmb: pointer to the driver internal queue element for mailbox command.
3682 *
3683 * This routine is the completion callback function for unregister default
3684 * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
3685 * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
3686 * decrements the ndlp reference count held for this completion callback
3687 * function. After that, it invokes the lpfc_nlp_not_used() to check
3688 * whether there is only one reference left on the ndlp. If so, it will
3689 * perform one more decrement and trigger the release of the ndlp.
3690 **/
858c9f6c
JS
3691void
3692lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3693{
3694 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3695 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3696
3697 pmb->context1 = NULL;
d439d286
JS
3698 pmb->context2 = NULL;
3699
858c9f6c
JS
3700 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3701 kfree(mp);
3702 mempool_free(pmb, phba->mbox_mem_pool);
086a345f
JS
3703 if (ndlp) {
3704 if (NLP_CHK_NODE_ACT(ndlp)) {
3705 lpfc_nlp_put(ndlp);
3706 /* This is the end of the default RPI cleanup logic for
3707 * this ndlp. If no other discovery threads are using
3708 * this ndlp, free all resources associated with it.
3709 */
3710 lpfc_nlp_not_used(ndlp);
3711 } else {
3712 lpfc_drop_node(ndlp->vport, ndlp);
3713 }
a8adb832 3714 }
3772a991 3715
858c9f6c
JS
3716 return;
3717}
3718
e59058c4 3719/**
3621a710 3720 * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
e59058c4
JS
3721 * @phba: pointer to lpfc hba data structure.
3722 * @cmdiocb: pointer to lpfc command iocb data structure.
3723 * @rspiocb: pointer to lpfc response iocb data structure.
3724 *
3725 * This routine is the completion callback function for ELS Response IOCB
3726 * command. In normal case, this callback function just properly sets the
3727 * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
3728 * field in the command IOCB is not NULL, the referred mailbox command will
3729 * be send out, and then invokes the lpfc_els_free_iocb() routine to release
3730 * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
3731 * link down event occurred during the discovery, the lpfc_nlp_not_used()
3732 * routine shall be invoked trying to release the ndlp if no other threads
3733 * are currently referring it.
3734 **/
dea3101e 3735static void
858c9f6c 3736lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
329f9bc7 3737 struct lpfc_iocbq *rspiocb)
dea3101e 3738{
2e0fef85
JS
3739 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3740 struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
3741 struct Scsi_Host *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
87af33fe
JS
3742 IOCB_t *irsp;
3743 uint8_t *pcmd;
dea3101e 3744 LPFC_MBOXQ_t *mbox = NULL;
2e0fef85 3745 struct lpfc_dmabuf *mp = NULL;
87af33fe 3746 uint32_t ls_rjt = 0;
dea3101e 3747
33ccf8d1
JS
3748 irsp = &rspiocb->iocb;
3749
dea3101e 3750 if (cmdiocb->context_un.mbox)
3751 mbox = cmdiocb->context_un.mbox;
3752
fa4066b6
JS
3753 /* First determine if this is a LS_RJT cmpl. Note, this callback
3754 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3755 */
87af33fe 3756 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
58da1ffb
JS
3757 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3758 (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
fa4066b6 3759 /* A LS_RJT associated with Default RPI cleanup has its own
3ad2f3fb 3760 * separate code path.
87af33fe
JS
3761 */
3762 if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3763 ls_rjt = 1;
3764 }
3765
dea3101e 3766 /* Check to see if link went down during discovery */
58da1ffb 3767 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
dea3101e 3768 if (mbox) {
14691150
JS
3769 mp = (struct lpfc_dmabuf *) mbox->context1;
3770 if (mp) {
3771 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3772 kfree(mp);
3773 }
329f9bc7 3774 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 3775 }
58da1ffb
JS
3776 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3777 (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
fa4066b6 3778 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 3779 ndlp = NULL;
fa4066b6
JS
3780 /* Indicate the node has already released,
3781 * should not reference to it from within
3782 * the routine lpfc_els_free_iocb.
3783 */
3784 cmdiocb->context1 = NULL;
3785 }
dea3101e 3786 goto out;
3787 }
3788
858c9f6c 3789 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
51ef4c26 3790 "ELS rsp cmpl: status:x%x/x%x did:x%x",
858c9f6c 3791 irsp->ulpStatus, irsp->un.ulpWord[4],
51ef4c26 3792 cmdiocb->iocb.un.elsreq64.remoteID);
dea3101e 3793 /* ELS response tag <ulpIoTag> completes */
e8b62011
JS
3794 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3795 "0110 ELS response tag x%x completes "
3796 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3797 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3798 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3799 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3800 ndlp->nlp_rpi);
dea3101e 3801 if (mbox) {
3802 if ((rspiocb->iocb.ulpStatus == 0)
3803 && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
2e0fef85 3804 lpfc_unreg_rpi(vport, ndlp);
e47c9093
JS
3805 /* Increment reference count to ndlp to hold the
3806 * reference to ndlp for the callback function.
3807 */
329f9bc7 3808 mbox->context2 = lpfc_nlp_get(ndlp);
2e0fef85 3809 mbox->vport = vport;
858c9f6c
JS
3810 if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3811 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3812 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3813 }
3814 else {
3815 mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3816 ndlp->nlp_prev_state = ndlp->nlp_state;
3817 lpfc_nlp_set_state(vport, ndlp,
2e0fef85 3818 NLP_STE_REG_LOGIN_ISSUE);
858c9f6c 3819 }
0b727fea 3820 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
e47c9093 3821 != MBX_NOT_FINISHED)
dea3101e 3822 goto out;
e47c9093
JS
3823 else
3824 /* Decrement the ndlp reference count we
3825 * set for this failed mailbox command.
3826 */
3827 lpfc_nlp_put(ndlp);
98c9ea5c
JS
3828
3829 /* ELS rsp: Cannot issue reg_login for <NPortid> */
3830 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3831 "0138 ELS rsp: Cannot issue reg_login for x%x "
3832 "Data: x%x x%x x%x\n",
3833 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3834 ndlp->nlp_rpi);
3835
fa4066b6 3836 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 3837 ndlp = NULL;
fa4066b6
JS
3838 /* Indicate node has already been released,
3839 * should not reference to it from within
3840 * the routine lpfc_els_free_iocb.
3841 */
3842 cmdiocb->context1 = NULL;
3843 }
dea3101e 3844 } else {
858c9f6c
JS
3845 /* Do not drop node for lpfc_els_abort'ed ELS cmds */
3846 if (!lpfc_error_lost_link(irsp) &&
3847 ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
fa4066b6 3848 if (lpfc_nlp_not_used(ndlp)) {
98c9ea5c 3849 ndlp = NULL;
fa4066b6
JS
3850 /* Indicate node has already been
3851 * released, should not reference
3852 * to it from within the routine
3853 * lpfc_els_free_iocb.
3854 */
3855 cmdiocb->context1 = NULL;
3856 }
dea3101e 3857 }
3858 }
14691150
JS
3859 mp = (struct lpfc_dmabuf *) mbox->context1;
3860 if (mp) {
3861 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3862 kfree(mp);
3863 }
3864 mempool_free(mbox, phba->mbox_mem_pool);
dea3101e 3865 }
3866out:
58da1ffb 3867 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2e0fef85 3868 spin_lock_irq(shost->host_lock);
858c9f6c 3869 ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
2e0fef85 3870 spin_unlock_irq(shost->host_lock);
87af33fe
JS
3871
3872 /* If the node is not being used by another discovery thread,
3873 * and we are sending a reject, we are done with it.
3874 * Release driver reference count here and free associated
3875 * resources.
3876 */
3877 if (ls_rjt)
fa4066b6
JS
3878 if (lpfc_nlp_not_used(ndlp))
3879 /* Indicate node has already been released,
3880 * should not reference to it from within
3881 * the routine lpfc_els_free_iocb.
3882 */
3883 cmdiocb->context1 = NULL;
dea3101e 3884 }
87af33fe 3885
dea3101e 3886 lpfc_els_free_iocb(phba, cmdiocb);
3887 return;
3888}
3889
e59058c4 3890/**
3621a710 3891 * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
e59058c4
JS
3892 * @vport: pointer to a host virtual N_Port data structure.
3893 * @flag: the els command code to be accepted.
3894 * @oldiocb: pointer to the original lpfc command iocb data structure.
3895 * @ndlp: pointer to a node-list data structure.
3896 * @mbox: pointer to the driver internal queue element for mailbox command.
3897 *
3898 * This routine prepares and issues an Accept (ACC) response IOCB
3899 * command. It uses the @flag to properly set up the IOCB field for the
3900 * specific ACC response command to be issued and invokes the
3901 * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3902 * @mbox pointer is passed in, it will be put into the context_un.mbox
3903 * field of the IOCB for the completion callback function to issue the
3904 * mailbox command to the HBA later when callback is invoked.
3905 *
3906 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3907 * will be incremented by 1 for holding the ndlp and the reference to ndlp
3908 * will be stored into the context1 field of the IOCB for the completion
3909 * callback function to the corresponding response ELS IOCB command.
3910 *
3911 * Return code
3912 * 0 - Successfully issued acc response
3913 * 1 - Failed to issue acc response
3914 **/
dea3101e 3915int
2e0fef85
JS
3916lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3917 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
51ef4c26 3918 LPFC_MBOXQ_t *mbox)
dea3101e 3919{
2e0fef85
JS
3920 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3921 struct lpfc_hba *phba = vport->phba;
dea3101e 3922 IOCB_t *icmd;
3923 IOCB_t *oldcmd;
3924 struct lpfc_iocbq *elsiocb;
dea3101e 3925 struct lpfc_sli *psli;
3926 uint8_t *pcmd;
3927 uint16_t cmdsize;
3928 int rc;
82d9a2a2 3929 ELS_PKT *els_pkt_ptr;
dea3101e 3930
3931 psli = &phba->sli;
dea3101e 3932 oldcmd = &oldiocb->iocb;
3933
3934 switch (flag) {
3935 case ELS_CMD_ACC:
92d7f7b0 3936 cmdsize = sizeof(uint32_t);
2e0fef85
JS
3937 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3938 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 3939 if (!elsiocb) {
2e0fef85 3940 spin_lock_irq(shost->host_lock);
5024ab17 3941 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2e0fef85 3942 spin_unlock_irq(shost->host_lock);
c9f8735b 3943 return 1;
dea3101e 3944 }
2e0fef85 3945
dea3101e 3946 icmd = &elsiocb->iocb;
7851fe2c
JS
3947 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3948 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e 3949 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3950 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 3951 pcmd += sizeof(uint32_t);
858c9f6c
JS
3952
3953 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3954 "Issue ACC: did:x%x flg:x%x",
3955 ndlp->nlp_DID, ndlp->nlp_flag, 0);
dea3101e 3956 break;
3957 case ELS_CMD_PLOGI:
92d7f7b0 3958 cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
2e0fef85
JS
3959 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3960 ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 3961 if (!elsiocb)
c9f8735b 3962 return 1;
488d1469 3963
dea3101e 3964 icmd = &elsiocb->iocb;
7851fe2c
JS
3965 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3966 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e 3967 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3968
3969 if (mbox)
3970 elsiocb->context_un.mbox = mbox;
3971
3972 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0
JS
3973 pcmd += sizeof(uint32_t);
3974 memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
858c9f6c
JS
3975
3976 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3977 "Issue ACC PLOGI: did:x%x flg:x%x",
3978 ndlp->nlp_DID, ndlp->nlp_flag, 0);
dea3101e 3979 break;
82d9a2a2 3980 case ELS_CMD_PRLO:
92d7f7b0 3981 cmdsize = sizeof(uint32_t) + sizeof(PRLO);
2e0fef85 3982 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
82d9a2a2
JS
3983 ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3984 if (!elsiocb)
3985 return 1;
3986
3987 icmd = &elsiocb->iocb;
7851fe2c
JS
3988 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
3989 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
82d9a2a2
JS
3990 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3991
3992 memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
92d7f7b0 3993 sizeof(uint32_t) + sizeof(PRLO));
82d9a2a2
JS
3994 *((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3995 els_pkt_ptr = (ELS_PKT *) pcmd;
3996 els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
858c9f6c
JS
3997
3998 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3999 "Issue ACC PRLO: did:x%x flg:x%x",
4000 ndlp->nlp_DID, ndlp->nlp_flag, 0);
82d9a2a2 4001 break;
dea3101e 4002 default:
c9f8735b 4003 return 1;
dea3101e 4004 }
dea3101e 4005 /* Xmit ELS ACC response tag <ulpIoTag> */
e8b62011
JS
4006 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4007 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
e6446439
JS
4008 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x "
4009 "fc_flag x%x\n",
e8b62011
JS
4010 elsiocb->iotag, elsiocb->iocb.ulpContext,
4011 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
e6446439 4012 ndlp->nlp_rpi, vport->fc_flag);
dea3101e 4013 if (ndlp->nlp_flag & NLP_LOGO_ACC) {
2e0fef85 4014 spin_lock_irq(shost->host_lock);
c9f8735b 4015 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2e0fef85 4016 spin_unlock_irq(shost->host_lock);
dea3101e 4017 elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4018 } else {
858c9f6c 4019 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e 4020 }
4021
4022 phba->fc_stat.elsXmitACC++;
3772a991 4023 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e 4024 if (rc == IOCB_ERROR) {
4025 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4026 return 1;
dea3101e 4027 }
c9f8735b 4028 return 0;
dea3101e 4029}
4030
e59058c4 4031/**
3621a710 4032 * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
e59058c4
JS
4033 * @vport: pointer to a virtual N_Port data structure.
4034 * @rejectError:
4035 * @oldiocb: pointer to the original lpfc command iocb data structure.
4036 * @ndlp: pointer to a node-list data structure.
4037 * @mbox: pointer to the driver internal queue element for mailbox command.
4038 *
4039 * This routine prepares and issue an Reject (RJT) response IOCB
4040 * command. If a @mbox pointer is passed in, it will be put into the
4041 * context_un.mbox field of the IOCB for the completion callback function
4042 * to issue to the HBA later.
4043 *
4044 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4045 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4046 * will be stored into the context1 field of the IOCB for the completion
4047 * callback function to the reject response ELS IOCB command.
4048 *
4049 * Return code
4050 * 0 - Successfully issued reject response
4051 * 1 - Failed to issue reject response
4052 **/
dea3101e 4053int
2e0fef85 4054lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
858c9f6c
JS
4055 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4056 LPFC_MBOXQ_t *mbox)
dea3101e 4057{
2e0fef85 4058 struct lpfc_hba *phba = vport->phba;
dea3101e 4059 IOCB_t *icmd;
4060 IOCB_t *oldcmd;
4061 struct lpfc_iocbq *elsiocb;
dea3101e 4062 struct lpfc_sli *psli;
4063 uint8_t *pcmd;
4064 uint16_t cmdsize;
4065 int rc;
4066
4067 psli = &phba->sli;
92d7f7b0 4068 cmdsize = 2 * sizeof(uint32_t);
2e0fef85
JS
4069 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4070 ndlp->nlp_DID, ELS_CMD_LS_RJT);
488d1469 4071 if (!elsiocb)
c9f8735b 4072 return 1;
dea3101e 4073
4074 icmd = &elsiocb->iocb;
4075 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4076 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4077 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
dea3101e 4078 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4079
4080 *((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
92d7f7b0 4081 pcmd += sizeof(uint32_t);
dea3101e 4082 *((uint32_t *) (pcmd)) = rejectError;
4083
51ef4c26 4084 if (mbox)
858c9f6c 4085 elsiocb->context_un.mbox = mbox;
858c9f6c 4086
dea3101e 4087 /* Xmit ELS RJT <err> response tag <ulpIoTag> */
e8b62011
JS
4088 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4089 "0129 Xmit ELS RJT x%x response tag x%x "
4090 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4091 "rpi x%x\n",
4092 rejectError, elsiocb->iotag,
4093 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4094 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
858c9f6c
JS
4095 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4096 "Issue LS_RJT: did:x%x flg:x%x err:x%x",
4097 ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4098
dea3101e 4099 phba->fc_stat.elsXmitLSRJT++;
858c9f6c 4100 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3772a991 4101 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
51ef4c26 4102
dea3101e 4103 if (rc == IOCB_ERROR) {
4104 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4105 return 1;
dea3101e 4106 }
c9f8735b 4107 return 0;
dea3101e 4108}
4109
e59058c4 4110/**
3621a710 4111 * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
e59058c4
JS
4112 * @vport: pointer to a virtual N_Port data structure.
4113 * @oldiocb: pointer to the original lpfc command iocb data structure.
4114 * @ndlp: pointer to a node-list data structure.
4115 *
4116 * This routine prepares and issues an Accept (ACC) response to Address
4117 * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4118 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4119 *
4120 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4121 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4122 * will be stored into the context1 field of the IOCB for the completion
4123 * callback function to the ADISC Accept response ELS IOCB command.
4124 *
4125 * Return code
4126 * 0 - Successfully issued acc adisc response
4127 * 1 - Failed to issue adisc acc response
4128 **/
dea3101e 4129int
2e0fef85
JS
4130lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4131 struct lpfc_nodelist *ndlp)
dea3101e 4132{
2e0fef85 4133 struct lpfc_hba *phba = vport->phba;
dea3101e 4134 ADISC *ap;
2e0fef85 4135 IOCB_t *icmd, *oldcmd;
dea3101e 4136 struct lpfc_iocbq *elsiocb;
dea3101e 4137 uint8_t *pcmd;
4138 uint16_t cmdsize;
4139 int rc;
4140
92d7f7b0 4141 cmdsize = sizeof(uint32_t) + sizeof(ADISC);
2e0fef85
JS
4142 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4143 ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4144 if (!elsiocb)
c9f8735b 4145 return 1;
dea3101e 4146
5b8bd0c9
JS
4147 icmd = &elsiocb->iocb;
4148 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4149 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4150 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5b8bd0c9 4151
dea3101e 4152 /* Xmit ADISC ACC response tag <ulpIoTag> */
e8b62011
JS
4153 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4154 "0130 Xmit ADISC ACC response iotag x%x xri: "
4155 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4156 elsiocb->iotag, elsiocb->iocb.ulpContext,
4157 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4158 ndlp->nlp_rpi);
dea3101e 4159 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4160
4161 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4162 pcmd += sizeof(uint32_t);
dea3101e 4163
4164 ap = (ADISC *) (pcmd);
4165 ap->hardAL_PA = phba->fc_pref_ALPA;
92d7f7b0
JS
4166 memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4167 memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2e0fef85 4168 ap->DID = be32_to_cpu(vport->fc_myDID);
dea3101e 4169
858c9f6c
JS
4170 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4171 "Issue ACC ADISC: did:x%x flg:x%x",
4172 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4173
dea3101e 4174 phba->fc_stat.elsXmitACC++;
858c9f6c 4175 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3772a991 4176 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e 4177 if (rc == IOCB_ERROR) {
4178 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4179 return 1;
dea3101e 4180 }
c9f8735b 4181 return 0;
dea3101e 4182}
4183
e59058c4 4184/**
3621a710 4185 * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
e59058c4
JS
4186 * @vport: pointer to a virtual N_Port data structure.
4187 * @oldiocb: pointer to the original lpfc command iocb data structure.
4188 * @ndlp: pointer to a node-list data structure.
4189 *
4190 * This routine prepares and issues an Accept (ACC) response to Process
4191 * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
4192 * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4193 *
4194 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4195 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4196 * will be stored into the context1 field of the IOCB for the completion
4197 * callback function to the PRLI Accept response ELS IOCB command.
4198 *
4199 * Return code
4200 * 0 - Successfully issued acc prli response
4201 * 1 - Failed to issue acc prli response
4202 **/
dea3101e 4203int
2e0fef85 4204lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5b8bd0c9 4205 struct lpfc_nodelist *ndlp)
dea3101e 4206{
2e0fef85 4207 struct lpfc_hba *phba = vport->phba;
dea3101e 4208 PRLI *npr;
4209 lpfc_vpd_t *vpd;
4210 IOCB_t *icmd;
4211 IOCB_t *oldcmd;
4212 struct lpfc_iocbq *elsiocb;
dea3101e 4213 struct lpfc_sli *psli;
4214 uint8_t *pcmd;
4215 uint16_t cmdsize;
4216 int rc;
4217
4218 psli = &phba->sli;
dea3101e 4219
92d7f7b0 4220 cmdsize = sizeof(uint32_t) + sizeof(PRLI);
2e0fef85 4221 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
92d7f7b0 4222 ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
c9f8735b
JW
4223 if (!elsiocb)
4224 return 1;
dea3101e 4225
5b8bd0c9
JS
4226 icmd = &elsiocb->iocb;
4227 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4228 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4229 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4230
dea3101e 4231 /* Xmit PRLI ACC response tag <ulpIoTag> */
e8b62011
JS
4232 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4233 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
4234 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4235 elsiocb->iotag, elsiocb->iocb.ulpContext,
4236 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4237 ndlp->nlp_rpi);
dea3101e 4238 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4239
4240 *((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
92d7f7b0 4241 pcmd += sizeof(uint32_t);
dea3101e 4242
4243 /* For PRLI, remainder of payload is PRLI parameter page */
92d7f7b0 4244 memset(pcmd, 0, sizeof(PRLI));
dea3101e 4245
4246 npr = (PRLI *) pcmd;
4247 vpd = &phba->vpd;
4248 /*
0d2b6b83
JS
4249 * If the remote port is a target and our firmware version is 3.20 or
4250 * later, set the following bits for FC-TAPE support.
dea3101e 4251 */
0d2b6b83
JS
4252 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
4253 (vpd->rev.feaLevelHigh >= 0x02)) {
dea3101e 4254 npr->ConfmComplAllowed = 1;
4255 npr->Retry = 1;
4256 npr->TaskRetryIdReq = 1;
4257 }
4258
4259 npr->acceptRspCode = PRLI_REQ_EXECUTED;
4260 npr->estabImagePair = 1;
4261 npr->readXferRdyDis = 1;
4262 npr->ConfmComplAllowed = 1;
4263
4264 npr->prliType = PRLI_FCP_TYPE;
4265 npr->initiatorFunc = 1;
4266
858c9f6c
JS
4267 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4268 "Issue ACC PRLI: did:x%x flg:x%x",
4269 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4270
dea3101e 4271 phba->fc_stat.elsXmitACC++;
858c9f6c 4272 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e 4273
3772a991 4274 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e 4275 if (rc == IOCB_ERROR) {
4276 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4277 return 1;
dea3101e 4278 }
c9f8735b 4279 return 0;
dea3101e 4280}
4281
e59058c4 4282/**
3621a710 4283 * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
e59058c4
JS
4284 * @vport: pointer to a virtual N_Port data structure.
4285 * @format: rnid command format.
4286 * @oldiocb: pointer to the original lpfc command iocb data structure.
4287 * @ndlp: pointer to a node-list data structure.
4288 *
4289 * This routine issues a Request Node Identification Data (RNID) Accept
4290 * (ACC) response. It constructs the RNID ACC response command according to
4291 * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
4292 * issue the response. Note that this command does not need to hold the ndlp
4293 * reference count for the callback. So, the ndlp reference count taken by
4294 * the lpfc_prep_els_iocb() routine is put back and the context1 field of
4295 * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
4296 * there is no ndlp reference available.
4297 *
4298 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4299 * will be incremented by 1 for holding the ndlp and the reference to ndlp
4300 * will be stored into the context1 field of the IOCB for the completion
4301 * callback function. However, for the RNID Accept Response ELS command,
4302 * this is undone later by this routine after the IOCB is allocated.
4303 *
4304 * Return code
4305 * 0 - Successfully issued acc rnid response
4306 * 1 - Failed to issue acc rnid response
4307 **/
dea3101e 4308static int
2e0fef85 4309lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
329f9bc7 4310 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
dea3101e 4311{
2e0fef85 4312 struct lpfc_hba *phba = vport->phba;
dea3101e 4313 RNID *rn;
2e0fef85 4314 IOCB_t *icmd, *oldcmd;
dea3101e 4315 struct lpfc_iocbq *elsiocb;
dea3101e 4316 struct lpfc_sli *psli;
4317 uint8_t *pcmd;
4318 uint16_t cmdsize;
4319 int rc;
4320
4321 psli = &phba->sli;
92d7f7b0
JS
4322 cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
4323 + (2 * sizeof(struct lpfc_name));
dea3101e 4324 if (format)
92d7f7b0 4325 cmdsize += sizeof(RNID_TOP_DISC);
dea3101e 4326
2e0fef85
JS
4327 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4328 ndlp->nlp_DID, ELS_CMD_ACC);
488d1469 4329 if (!elsiocb)
c9f8735b 4330 return 1;
dea3101e 4331
5b8bd0c9
JS
4332 icmd = &elsiocb->iocb;
4333 oldcmd = &oldiocb->iocb;
7851fe2c
JS
4334 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
4335 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4336
dea3101e 4337 /* Xmit RNID ACC response tag <ulpIoTag> */
e8b62011
JS
4338 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4339 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
4340 elsiocb->iotag, elsiocb->iocb.ulpContext);
dea3101e 4341 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
dea3101e 4342 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 4343 pcmd += sizeof(uint32_t);
dea3101e 4344
92d7f7b0 4345 memset(pcmd, 0, sizeof(RNID));
dea3101e 4346 rn = (RNID *) (pcmd);
4347 rn->Format = format;
92d7f7b0
JS
4348 rn->CommonLen = (2 * sizeof(struct lpfc_name));
4349 memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
4350 memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
dea3101e 4351 switch (format) {
4352 case 0:
4353 rn->SpecificLen = 0;
4354 break;
4355 case RNID_TOPOLOGY_DISC:
92d7f7b0 4356 rn->SpecificLen = sizeof(RNID_TOP_DISC);
dea3101e 4357 memcpy(&rn->un.topologyDisc.portName,
92d7f7b0 4358 &vport->fc_portname, sizeof(struct lpfc_name));
dea3101e 4359 rn->un.topologyDisc.unitType = RNID_HBA;
4360 rn->un.topologyDisc.physPort = 0;
4361 rn->un.topologyDisc.attachedNodes = 0;
4362 break;
4363 default:
4364 rn->CommonLen = 0;
4365 rn->SpecificLen = 0;
4366 break;
4367 }
4368
858c9f6c
JS
4369 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4370 "Issue ACC RNID: did:x%x flg:x%x",
4371 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4372
dea3101e 4373 phba->fc_stat.elsXmitACC++;
858c9f6c 4374 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
dea3101e 4375
3772a991 4376 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
dea3101e 4377 if (rc == IOCB_ERROR) {
4378 lpfc_els_free_iocb(phba, elsiocb);
c9f8735b 4379 return 1;
dea3101e 4380 }
c9f8735b 4381 return 0;
dea3101e 4382}
4383
19ca7609
JS
4384/**
4385 * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
4386 * @vport: pointer to a virtual N_Port data structure.
4387 * @iocb: pointer to the lpfc command iocb data structure.
4388 * @ndlp: pointer to a node-list data structure.
4389 *
4390 * Return
4391 **/
4392static void
4393lpfc_els_clear_rrq(struct lpfc_vport *vport,
4394 struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
4395{
4396 struct lpfc_hba *phba = vport->phba;
4397 uint8_t *pcmd;
4398 struct RRQ *rrq;
4399 uint16_t rxid;
1151e3ec 4400 uint16_t xri;
19ca7609
JS
4401 struct lpfc_node_rrq *prrq;
4402
4403
4404 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
4405 pcmd += sizeof(uint32_t);
4406 rrq = (struct RRQ *)pcmd;
1151e3ec 4407 rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
9589b062 4408 rxid = bf_get(rrq_rxid, rrq);
19ca7609
JS
4409
4410 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4411 "2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
4412 " x%x x%x\n",
1151e3ec 4413 be32_to_cpu(bf_get(rrq_did, rrq)),
9589b062 4414 bf_get(rrq_oxid, rrq),
19ca7609
JS
4415 rxid,
4416 iocb->iotag, iocb->iocb.ulpContext);
4417
4418 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4419 "Clear RRQ: did:x%x flg:x%x exchg:x%.08x",
4420 ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
1151e3ec 4421 if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
9589b062 4422 xri = bf_get(rrq_oxid, rrq);
1151e3ec
JS
4423 else
4424 xri = rxid;
4425 prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
19ca7609 4426 if (prrq)
1151e3ec 4427 lpfc_clr_rrq_active(phba, xri, prrq);
19ca7609
JS
4428 return;
4429}
4430
12265f68
JS
4431/**
4432 * lpfc_els_rsp_echo_acc - Issue echo acc response
4433 * @vport: pointer to a virtual N_Port data structure.
4434 * @data: pointer to echo data to return in the accept.
4435 * @oldiocb: pointer to the original lpfc command iocb data structure.
4436 * @ndlp: pointer to a node-list data structure.
4437 *
4438 * Return code
4439 * 0 - Successfully issued acc echo response
4440 * 1 - Failed to issue acc echo response
4441 **/
4442static int
4443lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
4444 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4445{
4446 struct lpfc_hba *phba = vport->phba;
4447 struct lpfc_iocbq *elsiocb;
4448 struct lpfc_sli *psli;
4449 uint8_t *pcmd;
4450 uint16_t cmdsize;
4451 int rc;
4452
4453 psli = &phba->sli;
4454 cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
4455
bf08611b
JS
4456 /* The accumulated length can exceed the BPL_SIZE. For
4457 * now, use this as the limit
4458 */
4459 if (cmdsize > LPFC_BPL_SIZE)
4460 cmdsize = LPFC_BPL_SIZE;
12265f68
JS
4461 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4462 ndlp->nlp_DID, ELS_CMD_ACC);
4463 if (!elsiocb)
4464 return 1;
4465
7851fe2c
JS
4466 elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext; /* Xri / rx_id */
4467 elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
4468
12265f68
JS
4469 /* Xmit ECHO ACC response tag <ulpIoTag> */
4470 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4471 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
4472 elsiocb->iotag, elsiocb->iocb.ulpContext);
4473 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4474 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4475 pcmd += sizeof(uint32_t);
4476 memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
4477
4478 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4479 "Issue ACC ECHO: did:x%x flg:x%x",
4480 ndlp->nlp_DID, ndlp->nlp_flag, 0);
4481
4482 phba->fc_stat.elsXmitACC++;
4483 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
12265f68
JS
4484
4485 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4486 if (rc == IOCB_ERROR) {
4487 lpfc_els_free_iocb(phba, elsiocb);
4488 return 1;
4489 }
4490 return 0;
4491}
4492
e59058c4 4493/**
3621a710 4494 * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
e59058c4
JS
4495 * @vport: pointer to a host virtual N_Port data structure.
4496 *
4497 * This routine issues Address Discover (ADISC) ELS commands to those
4498 * N_Ports which are in node port recovery state and ADISC has not been issued
4499 * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
4500 * lpfc_issue_els_adisc() routine, the per @vport number of discover count
4501 * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
4502 * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
4503 * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
4504 * IOCBs quit for later pick up. On the other hand, after walking through
4505 * all the ndlps with the @vport and there is none ADISC IOCB issued, the
4506 * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
4507 * no more ADISC need to be sent.
4508 *
4509 * Return code
4510 * The number of N_Ports with adisc issued.
4511 **/
dea3101e 4512int
2e0fef85 4513lpfc_els_disc_adisc(struct lpfc_vport *vport)
dea3101e 4514{
2e0fef85 4515 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 4516 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 4517 int sentadisc = 0;
dea3101e 4518
685f0bf7 4519 /* go thru NPR nodes and issue any remaining ELS ADISCs */
2e0fef85 4520 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
4521 if (!NLP_CHK_NODE_ACT(ndlp))
4522 continue;
685f0bf7
JS
4523 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4524 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4525 (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
2e0fef85 4526 spin_lock_irq(shost->host_lock);
685f0bf7 4527 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2e0fef85 4528 spin_unlock_irq(shost->host_lock);
685f0bf7 4529 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
4530 lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4531 lpfc_issue_els_adisc(vport, ndlp, 0);
685f0bf7 4532 sentadisc++;
2e0fef85
JS
4533 vport->num_disc_nodes++;
4534 if (vport->num_disc_nodes >=
3de2a653 4535 vport->cfg_discovery_threads) {
2e0fef85
JS
4536 spin_lock_irq(shost->host_lock);
4537 vport->fc_flag |= FC_NLP_MORE;
4538 spin_unlock_irq(shost->host_lock);
685f0bf7 4539 break;
dea3101e 4540 }
4541 }
4542 }
4543 if (sentadisc == 0) {
2e0fef85
JS
4544 spin_lock_irq(shost->host_lock);
4545 vport->fc_flag &= ~FC_NLP_MORE;
4546 spin_unlock_irq(shost->host_lock);
dea3101e 4547 }
2fe165b6 4548 return sentadisc;
dea3101e 4549}
4550
e59058c4 4551/**
3621a710 4552 * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
e59058c4
JS
4553 * @vport: pointer to a host virtual N_Port data structure.
4554 *
4555 * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
4556 * which are in node port recovery state, with a @vport. Each time an ELS
4557 * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
4558 * the per @vport number of discover count (num_disc_nodes) shall be
4559 * incremented. If the num_disc_nodes reaches a pre-configured threshold
4560 * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
4561 * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
4562 * later pick up. On the other hand, after walking through all the ndlps with
4563 * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
4564 * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
4565 * PLOGI need to be sent.
4566 *
4567 * Return code
4568 * The number of N_Ports with plogi issued.
4569 **/
dea3101e 4570int
2e0fef85 4571lpfc_els_disc_plogi(struct lpfc_vport *vport)
dea3101e 4572{
2e0fef85 4573 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 4574 struct lpfc_nodelist *ndlp, *next_ndlp;
2e0fef85 4575 int sentplogi = 0;
dea3101e 4576
2e0fef85
JS
4577 /* go thru NPR nodes and issue any remaining ELS PLOGIs */
4578 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
4579 if (!NLP_CHK_NODE_ACT(ndlp))
4580 continue;
685f0bf7
JS
4581 if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
4582 (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
4583 (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
4584 (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
4585 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85
JS
4586 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4587 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
685f0bf7 4588 sentplogi++;
2e0fef85
JS
4589 vport->num_disc_nodes++;
4590 if (vport->num_disc_nodes >=
3de2a653 4591 vport->cfg_discovery_threads) {
2e0fef85
JS
4592 spin_lock_irq(shost->host_lock);
4593 vport->fc_flag |= FC_NLP_MORE;
4594 spin_unlock_irq(shost->host_lock);
685f0bf7 4595 break;
dea3101e 4596 }
4597 }
4598 }
87af33fe
JS
4599 if (sentplogi) {
4600 lpfc_set_disctmo(vport);
4601 }
4602 else {
2e0fef85
JS
4603 spin_lock_irq(shost->host_lock);
4604 vport->fc_flag &= ~FC_NLP_MORE;
4605 spin_unlock_irq(shost->host_lock);
dea3101e 4606 }
2fe165b6 4607 return sentplogi;
dea3101e 4608}
4609
e59058c4 4610/**
3621a710 4611 * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
e59058c4
JS
4612 * @vport: pointer to a host virtual N_Port data structure.
4613 *
4614 * This routine cleans up any Registration State Change Notification
4615 * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
4616 * @vport together with the host_lock is used to prevent multiple thread
4617 * trying to access the RSCN array on a same @vport at the same time.
4618 **/
92d7f7b0 4619void
2e0fef85 4620lpfc_els_flush_rscn(struct lpfc_vport *vport)
dea3101e 4621{
2e0fef85
JS
4622 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4623 struct lpfc_hba *phba = vport->phba;
dea3101e 4624 int i;
4625
7f5f3d0d
JS
4626 spin_lock_irq(shost->host_lock);
4627 if (vport->fc_rscn_flush) {
4628 /* Another thread is walking fc_rscn_id_list on this vport */
4629 spin_unlock_irq(shost->host_lock);
4630 return;
4631 }
4632 /* Indicate we are walking lpfc_els_flush_rscn on this vport */
4633 vport->fc_rscn_flush = 1;
4634 spin_unlock_irq(shost->host_lock);
4635
2e0fef85 4636 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
92d7f7b0 4637 lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
2e0fef85 4638 vport->fc_rscn_id_list[i] = NULL;
dea3101e 4639 }
2e0fef85
JS
4640 spin_lock_irq(shost->host_lock);
4641 vport->fc_rscn_id_cnt = 0;
4642 vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
4643 spin_unlock_irq(shost->host_lock);
4644 lpfc_can_disctmo(vport);
7f5f3d0d
JS
4645 /* Indicate we are done walking this fc_rscn_id_list */
4646 vport->fc_rscn_flush = 0;
dea3101e 4647}
4648
e59058c4 4649/**
3621a710 4650 * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
e59058c4
JS
4651 * @vport: pointer to a host virtual N_Port data structure.
4652 * @did: remote destination port identifier.
4653 *
4654 * This routine checks whether there is any pending Registration State
4655 * Configuration Notification (RSCN) to a @did on @vport.
4656 *
4657 * Return code
4658 * None zero - The @did matched with a pending rscn
4659 * 0 - not able to match @did with a pending rscn
4660 **/
dea3101e 4661int
2e0fef85 4662lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
dea3101e 4663{
4664 D_ID ns_did;
4665 D_ID rscn_did;
dea3101e 4666 uint32_t *lp;
92d7f7b0 4667 uint32_t payload_len, i;
7f5f3d0d 4668 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea3101e 4669
4670 ns_did.un.word = did;
dea3101e 4671
4672 /* Never match fabric nodes for RSCNs */
4673 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
2e0fef85 4674 return 0;
dea3101e 4675
4676 /* If we are doing a FULL RSCN rediscovery, match everything */
2e0fef85 4677 if (vport->fc_flag & FC_RSCN_DISCOVERY)
c9f8735b 4678 return did;
dea3101e 4679
7f5f3d0d
JS
4680 spin_lock_irq(shost->host_lock);
4681 if (vport->fc_rscn_flush) {
4682 /* Another thread is walking fc_rscn_id_list on this vport */
4683 spin_unlock_irq(shost->host_lock);
4684 return 0;
4685 }
4686 /* Indicate we are walking fc_rscn_id_list on this vport */
4687 vport->fc_rscn_flush = 1;
4688 spin_unlock_irq(shost->host_lock);
2e0fef85 4689 for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
92d7f7b0
JS
4690 lp = vport->fc_rscn_id_list[i]->virt;
4691 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4692 payload_len -= sizeof(uint32_t); /* take off word 0 */
dea3101e 4693 while (payload_len) {
92d7f7b0
JS
4694 rscn_did.un.word = be32_to_cpu(*lp++);
4695 payload_len -= sizeof(uint32_t);
eaf15d5b
JS
4696 switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
4697 case RSCN_ADDRESS_FORMAT_PORT:
6fb120a7
JS
4698 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4699 && (ns_did.un.b.area == rscn_did.un.b.area)
4700 && (ns_did.un.b.id == rscn_did.un.b.id))
7f5f3d0d 4701 goto return_did_out;
dea3101e 4702 break;
eaf15d5b 4703 case RSCN_ADDRESS_FORMAT_AREA:
dea3101e 4704 if ((ns_did.un.b.domain == rscn_did.un.b.domain)
4705 && (ns_did.un.b.area == rscn_did.un.b.area))
7f5f3d0d 4706 goto return_did_out;
dea3101e 4707 break;
eaf15d5b 4708 case RSCN_ADDRESS_FORMAT_DOMAIN:
dea3101e 4709 if (ns_did.un.b.domain == rscn_did.un.b.domain)
7f5f3d0d 4710 goto return_did_out;
dea3101e 4711 break;
eaf15d5b 4712 case RSCN_ADDRESS_FORMAT_FABRIC:
7f5f3d0d 4713 goto return_did_out;
dea3101e 4714 }
4715 }
92d7f7b0 4716 }
7f5f3d0d
JS
4717 /* Indicate we are done with walking fc_rscn_id_list on this vport */
4718 vport->fc_rscn_flush = 0;
92d7f7b0 4719 return 0;
7f5f3d0d
JS
4720return_did_out:
4721 /* Indicate we are done with walking fc_rscn_id_list on this vport */
4722 vport->fc_rscn_flush = 0;
4723 return did;
dea3101e 4724}
4725
e59058c4 4726/**
3621a710 4727 * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
e59058c4
JS
4728 * @vport: pointer to a host virtual N_Port data structure.
4729 *
4730 * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
4731 * state machine for a @vport's nodes that are with pending RSCN (Registration
4732 * State Change Notification).
4733 *
4734 * Return code
4735 * 0 - Successful (currently alway return 0)
4736 **/
dea3101e 4737static int
2e0fef85 4738lpfc_rscn_recovery_check(struct lpfc_vport *vport)
dea3101e 4739{
685f0bf7 4740 struct lpfc_nodelist *ndlp = NULL;
dea3101e 4741
0d2b6b83 4742 /* Move all affected nodes by pending RSCNs to NPR state. */
2e0fef85 4743 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093 4744 if (!NLP_CHK_NODE_ACT(ndlp) ||
0d2b6b83
JS
4745 (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
4746 !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
685f0bf7 4747 continue;
2e0fef85 4748 lpfc_disc_state_machine(vport, ndlp, NULL,
0d2b6b83
JS
4749 NLP_EVT_DEVICE_RECOVERY);
4750 lpfc_cancel_retry_delay_tmo(vport, ndlp);
dea3101e 4751 }
c9f8735b 4752 return 0;
dea3101e 4753}
4754
ddcc50f0 4755/**
3621a710 4756 * lpfc_send_rscn_event - Send an RSCN event to management application
ddcc50f0
JS
4757 * @vport: pointer to a host virtual N_Port data structure.
4758 * @cmdiocb: pointer to lpfc command iocb data structure.
4759 *
4760 * lpfc_send_rscn_event sends an RSCN netlink event to management
4761 * applications.
4762 */
4763static void
4764lpfc_send_rscn_event(struct lpfc_vport *vport,
4765 struct lpfc_iocbq *cmdiocb)
4766{
4767 struct lpfc_dmabuf *pcmd;
4768 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4769 uint32_t *payload_ptr;
4770 uint32_t payload_len;
4771 struct lpfc_rscn_event_header *rscn_event_data;
4772
4773 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4774 payload_ptr = (uint32_t *) pcmd->virt;
4775 payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
4776
4777 rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
4778 payload_len, GFP_KERNEL);
4779 if (!rscn_event_data) {
4780 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4781 "0147 Failed to allocate memory for RSCN event\n");
4782 return;
4783 }
4784 rscn_event_data->event_type = FC_REG_RSCN_EVENT;
4785 rscn_event_data->payload_length = payload_len;
4786 memcpy(rscn_event_data->rscn_payload, payload_ptr,
4787 payload_len);
4788
4789 fc_host_post_vendor_event(shost,
4790 fc_get_event_number(),
4791 sizeof(struct lpfc_els_event_header) + payload_len,
4792 (char *)rscn_event_data,
4793 LPFC_NL_VENDOR_ID);
4794
4795 kfree(rscn_event_data);
4796}
4797
e59058c4 4798/**
3621a710 4799 * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
e59058c4
JS
4800 * @vport: pointer to a host virtual N_Port data structure.
4801 * @cmdiocb: pointer to lpfc command iocb data structure.
4802 * @ndlp: pointer to a node-list data structure.
4803 *
4804 * This routine processes an unsolicited RSCN (Registration State Change
4805 * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
4806 * to invoke fc_host_post_event() routine to the FC transport layer. If the
4807 * discover state machine is about to begin discovery, it just accepts the
4808 * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
4809 * contains N_Port IDs for other vports on this HBA, it just accepts the
4810 * RSCN and ignore processing it. If the state machine is in the recovery
4811 * state, the fc_rscn_id_list of this @vport is walked and the
4812 * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
4813 * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
4814 * routine is invoked to handle the RSCN event.
4815 *
4816 * Return code
4817 * 0 - Just sent the acc response
4818 * 1 - Sent the acc response and waited for name server completion
4819 **/
dea3101e 4820static int
2e0fef85 4821lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
51ef4c26 4822 struct lpfc_nodelist *ndlp)
dea3101e 4823{
2e0fef85
JS
4824 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4825 struct lpfc_hba *phba = vport->phba;
dea3101e 4826 struct lpfc_dmabuf *pcmd;
92d7f7b0 4827 uint32_t *lp, *datap;
dea3101e 4828 IOCB_t *icmd;
92d7f7b0 4829 uint32_t payload_len, length, nportid, *cmd;
7f5f3d0d 4830 int rscn_cnt;
92d7f7b0 4831 int rscn_id = 0, hba_id = 0;
d2873e4c 4832 int i;
dea3101e 4833
4834 icmd = &cmdiocb->iocb;
4835 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4836 lp = (uint32_t *) pcmd->virt;
4837
92d7f7b0
JS
4838 payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
4839 payload_len -= sizeof(uint32_t); /* take off word 0 */
dea3101e 4840 /* RSCN received */
e8b62011
JS
4841 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4842 "0214 RSCN received Data: x%x x%x x%x x%x\n",
7f5f3d0d
JS
4843 vport->fc_flag, payload_len, *lp,
4844 vport->fc_rscn_id_cnt);
ddcc50f0
JS
4845
4846 /* Send an RSCN event to the management application */
4847 lpfc_send_rscn_event(vport, cmdiocb);
4848
d2873e4c 4849 for (i = 0; i < payload_len/sizeof(uint32_t); i++)
2e0fef85 4850 fc_host_post_event(shost, fc_get_event_number(),
d2873e4c
JS
4851 FCH_EVT_RSCN, lp[i]);
4852
dea3101e 4853 /* If we are about to begin discovery, just ACC the RSCN.
4854 * Discovery processing will satisfy it.
4855 */
2e0fef85 4856 if (vport->port_state <= LPFC_NS_QRY) {
858c9f6c
JS
4857 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4858 "RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4859 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4860
51ef4c26 4861 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
c9f8735b 4862 return 0;
dea3101e 4863 }
4864
92d7f7b0
JS
4865 /* If this RSCN just contains NPortIDs for other vports on this HBA,
4866 * just ACC and ignore it.
4867 */
4868 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3de2a653 4869 !(vport->cfg_peer_port_login)) {
92d7f7b0
JS
4870 i = payload_len;
4871 datap = lp;
4872 while (i > 0) {
4873 nportid = *datap++;
4874 nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4875 i -= sizeof(uint32_t);
4876 rscn_id++;
549e55cd
JS
4877 if (lpfc_find_vport_by_did(phba, nportid))
4878 hba_id++;
92d7f7b0
JS
4879 }
4880 if (rscn_id == hba_id) {
4881 /* ALL NPortIDs in RSCN are on HBA */
e8b62011 4882 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
d7c255b2 4883 "0219 Ignore RSCN "
e8b62011
JS
4884 "Data: x%x x%x x%x x%x\n",
4885 vport->fc_flag, payload_len,
7f5f3d0d 4886 *lp, vport->fc_rscn_id_cnt);
858c9f6c
JS
4887 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4888 "RCV RSCN vport: did:x%x/ste:x%x flg:x%x",
4889 ndlp->nlp_DID, vport->port_state,
4890 ndlp->nlp_flag);
4891
92d7f7b0 4892 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
51ef4c26 4893 ndlp, NULL);
92d7f7b0
JS
4894 return 0;
4895 }
4896 }
4897
7f5f3d0d
JS
4898 spin_lock_irq(shost->host_lock);
4899 if (vport->fc_rscn_flush) {
4900 /* Another thread is walking fc_rscn_id_list on this vport */
7f5f3d0d 4901 vport->fc_flag |= FC_RSCN_DISCOVERY;
97957244 4902 spin_unlock_irq(shost->host_lock);
58da1ffb
JS
4903 /* Send back ACC */
4904 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7f5f3d0d
JS
4905 return 0;
4906 }
4907 /* Indicate we are walking fc_rscn_id_list on this vport */
4908 vport->fc_rscn_flush = 1;
4909 spin_unlock_irq(shost->host_lock);
af901ca1 4910 /* Get the array count after successfully have the token */
7f5f3d0d 4911 rscn_cnt = vport->fc_rscn_id_cnt;
dea3101e 4912 /* If we are already processing an RSCN, save the received
4913 * RSCN payload buffer, cmdiocb->context2 to process later.
4914 */
2e0fef85 4915 if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
858c9f6c
JS
4916 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4917 "RCV RSCN defer: did:x%x/ste:x%x flg:x%x",
4918 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4919
09372820 4920 spin_lock_irq(shost->host_lock);
92d7f7b0
JS
4921 vport->fc_flag |= FC_RSCN_DEFERRED;
4922 if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
2e0fef85 4923 !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
2e0fef85
JS
4924 vport->fc_flag |= FC_RSCN_MODE;
4925 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
4926 if (rscn_cnt) {
4927 cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4928 length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4929 }
4930 if ((rscn_cnt) &&
4931 (payload_len + length <= LPFC_BPL_SIZE)) {
4932 *cmd &= ELS_CMD_MASK;
7f5f3d0d 4933 *cmd |= cpu_to_be32(payload_len + length);
92d7f7b0
JS
4934 memcpy(((uint8_t *)cmd) + length, lp,
4935 payload_len);
4936 } else {
4937 vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4938 vport->fc_rscn_id_cnt++;
4939 /* If we zero, cmdiocb->context2, the calling
4940 * routine will not try to free it.
4941 */
4942 cmdiocb->context2 = NULL;
4943 }
dea3101e 4944 /* Deferred RSCN */
e8b62011
JS
4945 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4946 "0235 Deferred RSCN "
4947 "Data: x%x x%x x%x\n",
4948 vport->fc_rscn_id_cnt, vport->fc_flag,
4949 vport->port_state);
dea3101e 4950 } else {
2e0fef85
JS
4951 vport->fc_flag |= FC_RSCN_DISCOVERY;
4952 spin_unlock_irq(shost->host_lock);
dea3101e 4953 /* ReDiscovery RSCN */
e8b62011
JS
4954 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4955 "0234 ReDiscovery RSCN "
4956 "Data: x%x x%x x%x\n",
4957 vport->fc_rscn_id_cnt, vport->fc_flag,
4958 vport->port_state);
dea3101e 4959 }
7f5f3d0d
JS
4960 /* Indicate we are done walking fc_rscn_id_list on this vport */
4961 vport->fc_rscn_flush = 0;
dea3101e 4962 /* Send back ACC */
51ef4c26 4963 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 4964 /* send RECOVERY event for ALL nodes that match RSCN payload */
2e0fef85 4965 lpfc_rscn_recovery_check(vport);
09372820 4966 spin_lock_irq(shost->host_lock);
92d7f7b0 4967 vport->fc_flag &= ~FC_RSCN_DEFERRED;
09372820 4968 spin_unlock_irq(shost->host_lock);
c9f8735b 4969 return 0;
dea3101e 4970 }
858c9f6c
JS
4971 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4972 "RCV RSCN: did:x%x/ste:x%x flg:x%x",
4973 ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4974
2e0fef85
JS
4975 spin_lock_irq(shost->host_lock);
4976 vport->fc_flag |= FC_RSCN_MODE;
4977 spin_unlock_irq(shost->host_lock);
4978 vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
7f5f3d0d
JS
4979 /* Indicate we are done walking fc_rscn_id_list on this vport */
4980 vport->fc_rscn_flush = 0;
dea3101e 4981 /*
4982 * If we zero, cmdiocb->context2, the calling routine will
4983 * not try to free it.
4984 */
4985 cmdiocb->context2 = NULL;
2e0fef85 4986 lpfc_set_disctmo(vport);
dea3101e 4987 /* Send back ACC */
51ef4c26 4988 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 4989 /* send RECOVERY event for ALL nodes that match RSCN payload */
2e0fef85 4990 lpfc_rscn_recovery_check(vport);
2e0fef85 4991 return lpfc_els_handle_rscn(vport);
dea3101e 4992}
4993
e59058c4 4994/**
3621a710 4995 * lpfc_els_handle_rscn - Handle rscn for a vport
e59058c4
JS
4996 * @vport: pointer to a host virtual N_Port data structure.
4997 *
4998 * This routine handles the Registration State Configuration Notification
4999 * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
5000 * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
5001 * if the ndlp to NameServer exists, a Common Transport (CT) command to the
5002 * NameServer shall be issued. If CT command to the NameServer fails to be
5003 * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
5004 * RSCN activities with the @vport.
5005 *
5006 * Return code
5007 * 0 - Cleaned up rscn on the @vport
5008 * 1 - Wait for plogi to name server before proceed
5009 **/
dea3101e 5010int
2e0fef85 5011lpfc_els_handle_rscn(struct lpfc_vport *vport)
dea3101e 5012{
5013 struct lpfc_nodelist *ndlp;
2e0fef85 5014 struct lpfc_hba *phba = vport->phba;
dea3101e 5015
92d7f7b0
JS
5016 /* Ignore RSCN if the port is being torn down. */
5017 if (vport->load_flag & FC_UNLOADING) {
5018 lpfc_els_flush_rscn(vport);
5019 return 0;
5020 }
5021
dea3101e 5022 /* Start timer for RSCN processing */
2e0fef85 5023 lpfc_set_disctmo(vport);
dea3101e 5024
5025 /* RSCN processed */
e8b62011
JS
5026 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5027 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
5028 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
5029 vport->port_state);
dea3101e 5030
5031 /* To process RSCN, first compare RSCN data with NameServer */
2e0fef85 5032 vport->fc_ns_retry = 0;
0ff10d46
JS
5033 vport->num_disc_nodes = 0;
5034
2e0fef85 5035 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093
JS
5036 if (ndlp && NLP_CHK_NODE_ACT(ndlp)
5037 && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
dea3101e 5038 /* Good ndlp, issue CT Request to NameServer */
92d7f7b0 5039 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
dea3101e 5040 /* Wait for NameServer query cmpl before we can
5041 continue */
c9f8735b 5042 return 1;
dea3101e 5043 } else {
5044 /* If login to NameServer does not exist, issue one */
5045 /* Good status, issue PLOGI to NameServer */
2e0fef85 5046 ndlp = lpfc_findnode_did(vport, NameServer_DID);
e47c9093 5047 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
dea3101e 5048 /* Wait for NameServer login cmpl before we can
5049 continue */
c9f8735b 5050 return 1;
2e0fef85 5051
e47c9093
JS
5052 if (ndlp) {
5053 ndlp = lpfc_enable_node(vport, ndlp,
5054 NLP_STE_PLOGI_ISSUE);
5055 if (!ndlp) {
5056 lpfc_els_flush_rscn(vport);
5057 return 0;
5058 }
5059 ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
dea3101e 5060 } else {
e47c9093
JS
5061 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5062 if (!ndlp) {
5063 lpfc_els_flush_rscn(vport);
5064 return 0;
5065 }
2e0fef85 5066 lpfc_nlp_init(vport, ndlp, NameServer_DID);
5024ab17 5067 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 5068 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
dea3101e 5069 }
e47c9093
JS
5070 ndlp->nlp_type |= NLP_FABRIC;
5071 lpfc_issue_els_plogi(vport, NameServer_DID, 0);
5072 /* Wait for NameServer login cmpl before we can
5073 * continue
5074 */
5075 return 1;
dea3101e 5076 }
5077
2e0fef85 5078 lpfc_els_flush_rscn(vport);
c9f8735b 5079 return 0;
dea3101e 5080}
5081
e59058c4 5082/**
3621a710 5083 * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
e59058c4
JS
5084 * @vport: pointer to a host virtual N_Port data structure.
5085 * @cmdiocb: pointer to lpfc command iocb data structure.
5086 * @ndlp: pointer to a node-list data structure.
5087 *
5088 * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
5089 * unsolicited event. An unsolicited FLOGI can be received in a point-to-
5090 * point topology. As an unsolicited FLOGI should not be received in a loop
5091 * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
5092 * lpfc_check_sparm() routine is invoked to check the parameters in the
5093 * unsolicited FLOGI. If parameters validation failed, the routine
5094 * lpfc_els_rsp_reject() shall be called with reject reason code set to
5095 * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
5096 * FLOGI shall be compared with the Port WWN of the @vport to determine who
5097 * will initiate PLOGI. The higher lexicographical value party shall has
5098 * higher priority (as the winning port) and will initiate PLOGI and
5099 * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
5100 * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
5101 * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
5102 *
5103 * Return code
5104 * 0 - Successfully processed the unsolicited flogi
5105 * 1 - Failed to process the unsolicited flogi
5106 **/
dea3101e 5107static int
2e0fef85 5108lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
51ef4c26 5109 struct lpfc_nodelist *ndlp)
dea3101e 5110{
2e0fef85
JS
5111 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5112 struct lpfc_hba *phba = vport->phba;
dea3101e 5113 struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5114 uint32_t *lp = (uint32_t *) pcmd->virt;
5115 IOCB_t *icmd = &cmdiocb->iocb;
5116 struct serv_parm *sp;
5117 LPFC_MBOXQ_t *mbox;
5118 struct ls_rjt stat;
5119 uint32_t cmd, did;
5120 int rc;
e74c03c8
JS
5121 uint32_t fc_flag = 0;
5122 uint32_t port_state = 0;
dea3101e 5123
5124 cmd = *lp++;
5125 sp = (struct serv_parm *) lp;
5126
5127 /* FLOGI received */
5128
2e0fef85 5129 lpfc_set_disctmo(vport);
dea3101e 5130
76a95d75 5131 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
dea3101e 5132 /* We should never receive a FLOGI in loop mode, ignore it */
5133 did = icmd->un.elsreq64.remoteID;
5134
5135 /* An FLOGI ELS command <elsCmd> was received from DID <did> in
5136 Loop Mode */
e8b62011
JS
5137 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5138 "0113 An FLOGI ELS command x%x was "
5139 "received from DID x%x in Loop Mode\n",
5140 cmd, did);
c9f8735b 5141 return 1;
dea3101e 5142 }
5143
341af102 5144 if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1))) {
dea3101e 5145 /* For a FLOGI we accept, then if our portname is greater
5146 * then the remote portname we initiate Nport login.
5147 */
5148
2e0fef85 5149 rc = memcmp(&vport->fc_portname, &sp->portName,
92d7f7b0 5150 sizeof(struct lpfc_name));
dea3101e 5151
5152 if (!rc) {
1b51197d
JS
5153 if (phba->sli_rev < LPFC_SLI_REV4) {
5154 mbox = mempool_alloc(phba->mbox_mem_pool,
5155 GFP_KERNEL);
5156 if (!mbox)
5157 return 1;
5158 lpfc_linkdown(phba);
5159 lpfc_init_link(phba, mbox,
5160 phba->cfg_topology,
5161 phba->cfg_link_speed);
5162 mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
5163 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5164 mbox->vport = vport;
5165 rc = lpfc_sli_issue_mbox(phba, mbox,
5166 MBX_NOWAIT);
5167 lpfc_set_loopback_flag(phba);
5168 if (rc == MBX_NOT_FINISHED)
5169 mempool_free(mbox, phba->mbox_mem_pool);
c9f8735b 5170 return 1;
1b51197d
JS
5171 } else {
5172 /* abort the flogi coming back to ourselves
5173 * due to external loopback on the port.
5174 */
5175 lpfc_els_abort_flogi(phba);
5176 return 0;
dea3101e 5177 }
2fe165b6 5178 } else if (rc > 0) { /* greater than */
2e0fef85
JS
5179 spin_lock_irq(shost->host_lock);
5180 vport->fc_flag |= FC_PT2PT_PLOGI;
5181 spin_unlock_irq(shost->host_lock);
939723a4
JS
5182
5183 /* If we have the high WWPN we can assign our own
5184 * myDID; otherwise, we have to WAIT for a PLOGI
5185 * from the remote NPort to find out what it
5186 * will be.
5187 */
e6446439 5188 vport->fc_myDID = PT2PT_LocalID;
e74c03c8
JS
5189 } else
5190 vport->fc_myDID = PT2PT_RemoteID;
939723a4
JS
5191
5192 /*
5193 * The vport state should go to LPFC_FLOGI only
5194 * AFTER we issue a FLOGI, not receive one.
5195 */
2e0fef85 5196 spin_lock_irq(shost->host_lock);
e74c03c8
JS
5197 fc_flag = vport->fc_flag;
5198 port_state = vport->port_state;
2e0fef85
JS
5199 vport->fc_flag |= FC_PT2PT;
5200 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
e74c03c8 5201 vport->port_state = LPFC_FLOGI;
2e0fef85 5202 spin_unlock_irq(shost->host_lock);
e74c03c8
JS
5203 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5204 "3311 Rcv Flogi PS x%x new PS x%x "
5205 "fc_flag x%x new fc_flag x%x\n",
5206 port_state, vport->port_state,
5207 fc_flag, vport->fc_flag);
939723a4
JS
5208
5209 /*
5210 * We temporarily set fc_myDID to make it look like we are
5211 * a Fabric. This is done just so we end up with the right
5212 * did / sid on the FLOGI ACC rsp.
5213 */
5214 did = vport->fc_myDID;
5215 vport->fc_myDID = Fabric_DID;
5216
dea3101e 5217 } else {
5218 /* Reject this request because invalid parameters */
5219 stat.un.b.lsRjtRsvd0 = 0;
5220 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5221 stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
5222 stat.un.b.vendorUnique = 0;
939723a4
JS
5223
5224 /*
5225 * We temporarily set fc_myDID to make it look like we are
5226 * a Fabric. This is done just so we end up with the right
5227 * did / sid on the FLOGI LS_RJT rsp.
5228 */
5229 did = vport->fc_myDID;
5230 vport->fc_myDID = Fabric_DID;
5231
858c9f6c
JS
5232 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5233 NULL);
939723a4
JS
5234
5235 /* Now lets put fc_myDID back to what its supposed to be */
5236 vport->fc_myDID = did;
5237
c9f8735b 5238 return 1;
dea3101e 5239 }
5240
5241 /* Send back ACC */
51ef4c26 5242 lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
dea3101e 5243
939723a4
JS
5244 /* Now lets put fc_myDID back to what its supposed to be */
5245 vport->fc_myDID = did;
5246
e6446439 5247 if (!(vport->fc_flag & FC_PT2PT_PLOGI)) {
939723a4 5248
e6446439
JS
5249 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5250 if (!mbox)
5251 goto fail;
5252
5253 lpfc_config_link(phba, mbox);
5254
5255 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5256 mbox->vport = vport;
5257 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5258 if (rc == MBX_NOT_FINISHED) {
5259 mempool_free(mbox, phba->mbox_mem_pool);
5260 goto fail;
5261 }
5262 }
5263
c9f8735b 5264 return 0;
e6446439
JS
5265fail:
5266 return 1;
dea3101e 5267}
5268
e59058c4 5269/**
3621a710 5270 * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
e59058c4
JS
5271 * @vport: pointer to a host virtual N_Port data structure.
5272 * @cmdiocb: pointer to lpfc command iocb data structure.
5273 * @ndlp: pointer to a node-list data structure.
5274 *
5275 * This routine processes Request Node Identification Data (RNID) IOCB
5276 * received as an ELS unsolicited event. Only when the RNID specified format
5277 * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
5278 * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
5279 * Accept (ACC) the RNID ELS command. All the other RNID formats are
5280 * rejected by invoking the lpfc_els_rsp_reject() routine.
5281 *
5282 * Return code
5283 * 0 - Successfully processed rnid iocb (currently always return 0)
5284 **/
dea3101e 5285static int
2e0fef85
JS
5286lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5287 struct lpfc_nodelist *ndlp)
dea3101e 5288{
5289 struct lpfc_dmabuf *pcmd;
5290 uint32_t *lp;
5291 IOCB_t *icmd;
5292 RNID *rn;
5293 struct ls_rjt stat;
eb016566 5294 uint32_t cmd;
dea3101e 5295
5296 icmd = &cmdiocb->iocb;
dea3101e 5297 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5298 lp = (uint32_t *) pcmd->virt;
5299
5300 cmd = *lp++;
5301 rn = (RNID *) lp;
5302
5303 /* RNID received */
5304
5305 switch (rn->Format) {
5306 case 0:
5307 case RNID_TOPOLOGY_DISC:
5308 /* Send back ACC */
2e0fef85 5309 lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
dea3101e 5310 break;
5311 default:
5312 /* Reject this request because format not supported */
5313 stat.un.b.lsRjtRsvd0 = 0;
5314 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5315 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5316 stat.un.b.vendorUnique = 0;
858c9f6c
JS
5317 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
5318 NULL);
dea3101e 5319 }
c9f8735b 5320 return 0;
dea3101e 5321}
5322
12265f68
JS
5323/**
5324 * lpfc_els_rcv_echo - Process an unsolicited echo iocb
5325 * @vport: pointer to a host virtual N_Port data structure.
5326 * @cmdiocb: pointer to lpfc command iocb data structure.
5327 * @ndlp: pointer to a node-list data structure.
5328 *
5329 * Return code
5330 * 0 - Successfully processed echo iocb (currently always return 0)
5331 **/
5332static int
5333lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5334 struct lpfc_nodelist *ndlp)
5335{
5336 uint8_t *pcmd;
5337
5338 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
5339
5340 /* skip over first word of echo command to find echo data */
5341 pcmd += sizeof(uint32_t);
5342
5343 lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
5344 return 0;
5345}
5346
e59058c4 5347/**
3621a710 5348 * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
e59058c4
JS
5349 * @vport: pointer to a host virtual N_Port data structure.
5350 * @cmdiocb: pointer to lpfc command iocb data structure.
5351 * @ndlp: pointer to a node-list data structure.
5352 *
5353 * This routine processes a Link Incident Report Registration(LIRR) IOCB
5354 * received as an ELS unsolicited event. Currently, this function just invokes
5355 * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
5356 *
5357 * Return code
5358 * 0 - Successfully processed lirr iocb (currently always return 0)
5359 **/
dea3101e 5360static int
2e0fef85
JS
5361lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5362 struct lpfc_nodelist *ndlp)
7bb3b137
JW
5363{
5364 struct ls_rjt stat;
5365
5366 /* For now, unconditionally reject this command */
5367 stat.un.b.lsRjtRsvd0 = 0;
5368 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5369 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5370 stat.un.b.vendorUnique = 0;
858c9f6c 5371 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7bb3b137
JW
5372 return 0;
5373}
5374
5ffc266e
JS
5375/**
5376 * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
5377 * @vport: pointer to a host virtual N_Port data structure.
5378 * @cmdiocb: pointer to lpfc command iocb data structure.
5379 * @ndlp: pointer to a node-list data structure.
5380 *
5381 * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
5382 * received as an ELS unsolicited event. A request to RRQ shall only
5383 * be accepted if the Originator Nx_Port N_Port_ID or the Responder
5384 * Nx_Port N_Port_ID of the target Exchange is the same as the
5385 * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
5386 * not accepted, an LS_RJT with reason code "Unable to perform
5387 * command request" and reason code explanation "Invalid Originator
5388 * S_ID" shall be returned. For now, we just unconditionally accept
5389 * RRQ from the target.
5390 **/
5391static void
5392lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5393 struct lpfc_nodelist *ndlp)
5394{
5395 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
19ca7609
JS
5396 if (vport->phba->sli_rev == LPFC_SLI_REV4)
5397 lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
5ffc266e
JS
5398}
5399
12265f68
JS
5400/**
5401 * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
5402 * @phba: pointer to lpfc hba data structure.
5403 * @pmb: pointer to the driver internal queue element for mailbox command.
5404 *
5405 * This routine is the completion callback function for the MBX_READ_LNK_STAT
5406 * mailbox command. This callback function is to actually send the Accept
5407 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
5408 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
5409 * mailbox command, constructs the RPS response with the link statistics
5410 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
5411 * response to the RPS.
5412 *
5413 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5414 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5415 * will be stored into the context1 field of the IOCB for the completion
5416 * callback function to the RPS Accept Response ELS IOCB command.
5417 *
5418 **/
5419static void
5420lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5421{
5422 MAILBOX_t *mb;
5423 IOCB_t *icmd;
5424 struct RLS_RSP *rls_rsp;
5425 uint8_t *pcmd;
5426 struct lpfc_iocbq *elsiocb;
5427 struct lpfc_nodelist *ndlp;
7851fe2c
JS
5428 uint16_t oxid;
5429 uint16_t rxid;
12265f68
JS
5430 uint32_t cmdsize;
5431
5432 mb = &pmb->u.mb;
5433
5434 ndlp = (struct lpfc_nodelist *) pmb->context2;
7851fe2c
JS
5435 rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
5436 oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
12265f68
JS
5437 pmb->context1 = NULL;
5438 pmb->context2 = NULL;
5439
5440 if (mb->mbxStatus) {
5441 mempool_free(pmb, phba->mbox_mem_pool);
5442 return;
5443 }
5444
5445 cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
12265f68
JS
5446 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5447 lpfc_max_els_tries, ndlp,
5448 ndlp->nlp_DID, ELS_CMD_ACC);
5449
5450 /* Decrement the ndlp reference count from previous mbox command */
5451 lpfc_nlp_put(ndlp);
5452
37db57e3
JS
5453 if (!elsiocb) {
5454 mempool_free(pmb, phba->mbox_mem_pool);
12265f68 5455 return;
37db57e3 5456 }
12265f68
JS
5457
5458 icmd = &elsiocb->iocb;
7851fe2c
JS
5459 icmd->ulpContext = rxid;
5460 icmd->unsli3.rcvsli3.ox_id = oxid;
12265f68
JS
5461
5462 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5463 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5464 pcmd += sizeof(uint32_t); /* Skip past command */
5465 rls_rsp = (struct RLS_RSP *)pcmd;
5466
5467 rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
5468 rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
5469 rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
5470 rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
5471 rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
5472 rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
37db57e3 5473 mempool_free(pmb, phba->mbox_mem_pool);
12265f68
JS
5474 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5475 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5476 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
5477 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5478 elsiocb->iotag, elsiocb->iocb.ulpContext,
5479 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5480 ndlp->nlp_rpi);
5481 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5482 phba->fc_stat.elsXmitACC++;
5483 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5484 lpfc_els_free_iocb(phba, elsiocb);
5485}
5486
e59058c4 5487/**
3621a710 5488 * lpfc_els_rsp_rps_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
e59058c4
JS
5489 * @phba: pointer to lpfc hba data structure.
5490 * @pmb: pointer to the driver internal queue element for mailbox command.
5491 *
5492 * This routine is the completion callback function for the MBX_READ_LNK_STAT
5493 * mailbox command. This callback function is to actually send the Accept
5494 * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
5495 * collects the link statistics from the completion of the MBX_READ_LNK_STAT
5496 * mailbox command, constructs the RPS response with the link statistics
5497 * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
5498 * response to the RPS.
5499 *
5500 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5501 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5502 * will be stored into the context1 field of the IOCB for the completion
5503 * callback function to the RPS Accept Response ELS IOCB command.
5504 *
5505 **/
082c0266 5506static void
329f9bc7 5507lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7bb3b137 5508{
7bb3b137
JW
5509 MAILBOX_t *mb;
5510 IOCB_t *icmd;
5511 RPS_RSP *rps_rsp;
5512 uint8_t *pcmd;
5513 struct lpfc_iocbq *elsiocb;
5514 struct lpfc_nodelist *ndlp;
7851fe2c
JS
5515 uint16_t status;
5516 uint16_t oxid;
5517 uint16_t rxid;
7bb3b137
JW
5518 uint32_t cmdsize;
5519
04c68496 5520 mb = &pmb->u.mb;
7bb3b137
JW
5521
5522 ndlp = (struct lpfc_nodelist *) pmb->context2;
7851fe2c
JS
5523 rxid = (uint16_t) ((unsigned long)(pmb->context1) & 0xffff);
5524 oxid = (uint16_t) (((unsigned long)(pmb->context1) >> 16) & 0xffff);
041976fb
RD
5525 pmb->context1 = NULL;
5526 pmb->context2 = NULL;
7bb3b137
JW
5527
5528 if (mb->mbxStatus) {
329f9bc7 5529 mempool_free(pmb, phba->mbox_mem_pool);
7bb3b137
JW
5530 return;
5531 }
5532
5533 cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
329f9bc7 5534 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85
JS
5535 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5536 lpfc_max_els_tries, ndlp,
5537 ndlp->nlp_DID, ELS_CMD_ACC);
fa4066b6
JS
5538
5539 /* Decrement the ndlp reference count from previous mbox command */
329f9bc7 5540 lpfc_nlp_put(ndlp);
fa4066b6 5541
c9f8735b 5542 if (!elsiocb)
7bb3b137 5543 return;
7bb3b137
JW
5544
5545 icmd = &elsiocb->iocb;
7851fe2c
JS
5546 icmd->ulpContext = rxid;
5547 icmd->unsli3.rcvsli3.ox_id = oxid;
7bb3b137
JW
5548
5549 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5550 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 5551 pcmd += sizeof(uint32_t); /* Skip past command */
7bb3b137
JW
5552 rps_rsp = (RPS_RSP *)pcmd;
5553
76a95d75 5554 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
7bb3b137
JW
5555 status = 0x10;
5556 else
5557 status = 0x8;
2e0fef85 5558 if (phba->pport->fc_flag & FC_FABRIC)
7bb3b137
JW
5559 status |= 0x4;
5560
5561 rps_rsp->rsvd1 = 0;
09372820
JS
5562 rps_rsp->portStatus = cpu_to_be16(status);
5563 rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
5564 rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
5565 rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
5566 rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
5567 rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
5568 rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7bb3b137 5569 /* Xmit ELS RPS ACC response tag <ulpIoTag> */
e8b62011
JS
5570 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5571 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
5572 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5573 elsiocb->iotag, elsiocb->iocb.ulpContext,
5574 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5575 ndlp->nlp_rpi);
858c9f6c 5576 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7bb3b137 5577 phba->fc_stat.elsXmitACC++;
3772a991 5578 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7bb3b137 5579 lpfc_els_free_iocb(phba, elsiocb);
7bb3b137
JW
5580 return;
5581}
5582
e59058c4 5583/**
12265f68
JS
5584 * lpfc_els_rcv_rls - Process an unsolicited rls iocb
5585 * @vport: pointer to a host virtual N_Port data structure.
5586 * @cmdiocb: pointer to lpfc command iocb data structure.
5587 * @ndlp: pointer to a node-list data structure.
5588 *
5589 * This routine processes Read Port Status (RPL) IOCB received as an
5590 * ELS unsolicited event. It first checks the remote port state. If the
5591 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5592 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5593 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5594 * for reading the HBA link statistics. It is for the callback function,
5595 * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
5596 * to actually sending out RPL Accept (ACC) response.
5597 *
5598 * Return codes
5599 * 0 - Successfully processed rls iocb (currently always return 0)
5600 **/
5601static int
5602lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5603 struct lpfc_nodelist *ndlp)
5604{
5605 struct lpfc_hba *phba = vport->phba;
5606 LPFC_MBOXQ_t *mbox;
5607 struct lpfc_dmabuf *pcmd;
5608 struct ls_rjt stat;
5609
5610 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5611 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5612 /* reject the unsolicited RPS request and done with it */
5613 goto reject_out;
5614
5615 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5616
5617 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5618 if (mbox) {
5619 lpfc_read_lnk_stat(phba, mbox);
7851fe2c
JS
5620 mbox->context1 = (void *)((unsigned long)
5621 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
5622 cmdiocb->iocb.ulpContext)); /* rx_id */
12265f68
JS
5623 mbox->context2 = lpfc_nlp_get(ndlp);
5624 mbox->vport = vport;
5625 mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
5626 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5627 != MBX_NOT_FINISHED)
5628 /* Mbox completion will send ELS Response */
5629 return 0;
5630 /* Decrement reference count used for the failed mbox
5631 * command.
5632 */
5633 lpfc_nlp_put(ndlp);
5634 mempool_free(mbox, phba->mbox_mem_pool);
5635 }
5636reject_out:
5637 /* issue rejection response */
5638 stat.un.b.lsRjtRsvd0 = 0;
5639 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5640 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5641 stat.un.b.vendorUnique = 0;
5642 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5643 return 0;
5644}
5645
5646/**
5647 * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
5648 * @vport: pointer to a host virtual N_Port data structure.
5649 * @cmdiocb: pointer to lpfc command iocb data structure.
5650 * @ndlp: pointer to a node-list data structure.
5651 *
5652 * This routine processes Read Timout Value (RTV) IOCB received as an
5653 * ELS unsolicited event. It first checks the remote port state. If the
5654 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5655 * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
5656 * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
5657 * Value (RTV) unsolicited IOCB event.
5658 *
5659 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5660 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5661 * will be stored into the context1 field of the IOCB for the completion
5662 * callback function to the RPS Accept Response ELS IOCB command.
5663 *
5664 * Return codes
5665 * 0 - Successfully processed rtv iocb (currently always return 0)
5666 **/
5667static int
5668lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5669 struct lpfc_nodelist *ndlp)
5670{
5671 struct lpfc_hba *phba = vport->phba;
5672 struct ls_rjt stat;
5673 struct RTV_RSP *rtv_rsp;
5674 uint8_t *pcmd;
5675 struct lpfc_iocbq *elsiocb;
5676 uint32_t cmdsize;
5677
5678
5679 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
5680 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5681 /* reject the unsolicited RPS request and done with it */
5682 goto reject_out;
5683
5684 cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
5685 elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
5686 lpfc_max_els_tries, ndlp,
5687 ndlp->nlp_DID, ELS_CMD_ACC);
5688
5689 if (!elsiocb)
5690 return 1;
5691
5692 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5693 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5694 pcmd += sizeof(uint32_t); /* Skip past command */
5695
5696 /* use the command's xri in the response */
7851fe2c
JS
5697 elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext; /* Xri / rx_id */
5698 elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
12265f68
JS
5699
5700 rtv_rsp = (struct RTV_RSP *)pcmd;
5701
5702 /* populate RTV payload */
5703 rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
5704 rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
5705 bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
5706 bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
5707 rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
5708
5709 /* Xmit ELS RLS ACC response tag <ulpIoTag> */
5710 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
5711 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
5712 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
5713 "Data: x%x x%x x%x\n",
5714 elsiocb->iotag, elsiocb->iocb.ulpContext,
5715 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5716 ndlp->nlp_rpi,
5717 rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
5718 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5719 phba->fc_stat.elsXmitACC++;
5720 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
5721 lpfc_els_free_iocb(phba, elsiocb);
5722 return 0;
5723
5724reject_out:
5725 /* issue rejection response */
5726 stat.un.b.lsRjtRsvd0 = 0;
5727 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5728 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5729 stat.un.b.vendorUnique = 0;
5730 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
5731 return 0;
5732}
5733
5734/* lpfc_els_rcv_rps - Process an unsolicited rps iocb
e59058c4
JS
5735 * @vport: pointer to a host virtual N_Port data structure.
5736 * @cmdiocb: pointer to lpfc command iocb data structure.
5737 * @ndlp: pointer to a node-list data structure.
5738 *
5739 * This routine processes Read Port Status (RPS) IOCB received as an
5740 * ELS unsolicited event. It first checks the remote port state. If the
5741 * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
5742 * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
5743 * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
5744 * for reading the HBA link statistics. It is for the callback function,
5745 * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
5746 * to actually sending out RPS Accept (ACC) response.
5747 *
5748 * Return codes
5749 * 0 - Successfully processed rps iocb (currently always return 0)
5750 **/
7bb3b137 5751static int
2e0fef85
JS
5752lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5753 struct lpfc_nodelist *ndlp)
dea3101e 5754{
2e0fef85 5755 struct lpfc_hba *phba = vport->phba;
dea3101e 5756 uint32_t *lp;
7bb3b137
JW
5757 uint8_t flag;
5758 LPFC_MBOXQ_t *mbox;
5759 struct lpfc_dmabuf *pcmd;
5760 RPS *rps;
5761 struct ls_rjt stat;
5762
2fe165b6 5763 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
90160e01
JS
5764 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
5765 /* reject the unsolicited RPS request and done with it */
5766 goto reject_out;
7bb3b137
JW
5767
5768 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
5769 lp = (uint32_t *) pcmd->virt;
5770 flag = (be32_to_cpu(*lp++) & 0xf);
5771 rps = (RPS *) lp;
5772
5773 if ((flag == 0) ||
5774 ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
2e0fef85 5775 ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
92d7f7b0 5776 sizeof(struct lpfc_name)) == 0))) {
2e0fef85 5777
92d7f7b0
JS
5778 printk("Fix me....\n");
5779 dump_stack();
2e0fef85
JS
5780 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
5781 if (mbox) {
7bb3b137 5782 lpfc_read_lnk_stat(phba, mbox);
7851fe2c
JS
5783 mbox->context1 = (void *)((unsigned long)
5784 ((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
5785 cmdiocb->iocb.ulpContext)); /* rx_id */
329f9bc7 5786 mbox->context2 = lpfc_nlp_get(ndlp);
92d7f7b0 5787 mbox->vport = vport;
7bb3b137 5788 mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
fa4066b6 5789 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
0b727fea 5790 != MBX_NOT_FINISHED)
7bb3b137
JW
5791 /* Mbox completion will send ELS Response */
5792 return 0;
fa4066b6
JS
5793 /* Decrement reference count used for the failed mbox
5794 * command.
5795 */
329f9bc7 5796 lpfc_nlp_put(ndlp);
7bb3b137
JW
5797 mempool_free(mbox, phba->mbox_mem_pool);
5798 }
5799 }
90160e01
JS
5800
5801reject_out:
5802 /* issue rejection response */
7bb3b137
JW
5803 stat.un.b.lsRjtRsvd0 = 0;
5804 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
5805 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
5806 stat.un.b.vendorUnique = 0;
858c9f6c 5807 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7bb3b137
JW
5808 return 0;
5809}
5810
19ca7609
JS
5811/* lpfc_issue_els_rrq - Process an unsolicited rps iocb
5812 * @vport: pointer to a host virtual N_Port data structure.
5813 * @ndlp: pointer to a node-list data structure.
5814 * @did: DID of the target.
5815 * @rrq: Pointer to the rrq struct.
5816 *
5817 * Build a ELS RRQ command and send it to the target. If the issue_iocb is
5818 * Successful the the completion handler will clear the RRQ.
5819 *
5820 * Return codes
5821 * 0 - Successfully sent rrq els iocb.
5822 * 1 - Failed to send rrq els iocb.
5823 **/
5824static int
5825lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5826 uint32_t did, struct lpfc_node_rrq *rrq)
5827{
5828 struct lpfc_hba *phba = vport->phba;
5829 struct RRQ *els_rrq;
5830 IOCB_t *icmd;
5831 struct lpfc_iocbq *elsiocb;
5832 uint8_t *pcmd;
5833 uint16_t cmdsize;
5834 int ret;
5835
5836
5837 if (ndlp != rrq->ndlp)
5838 ndlp = rrq->ndlp;
5839 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5840 return 1;
5841
5842 /* If ndlp is not NULL, we will bump the reference count on it */
5843 cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
5844 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
5845 ELS_CMD_RRQ);
5846 if (!elsiocb)
5847 return 1;
5848
5849 icmd = &elsiocb->iocb;
5850 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5851
5852 /* For RRQ request, remainder of payload is Exchange IDs */
5853 *((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
5854 pcmd += sizeof(uint32_t);
5855 els_rrq = (struct RRQ *) pcmd;
5856
ee0f4fe1 5857 bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
19ca7609
JS
5858 bf_set(rrq_rxid, els_rrq, rrq->rxid);
5859 bf_set(rrq_did, els_rrq, vport->fc_myDID);
5860 els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
5861 els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
5862
5863
5864 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5865 "Issue RRQ: did:x%x",
5866 did, rrq->xritag, rrq->rxid);
5867 elsiocb->context_un.rrq = rrq;
5868 elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
5869 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5870
5871 if (ret == IOCB_ERROR) {
5872 lpfc_els_free_iocb(phba, elsiocb);
5873 return 1;
5874 }
5875 return 0;
5876}
5877
5878/**
5879 * lpfc_send_rrq - Sends ELS RRQ if needed.
5880 * @phba: pointer to lpfc hba data structure.
5881 * @rrq: pointer to the active rrq.
5882 *
5883 * This routine will call the lpfc_issue_els_rrq if the rrq is
5884 * still active for the xri. If this function returns a failure then
5885 * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
5886 *
5887 * Returns 0 Success.
5888 * 1 Failure.
5889 **/
5890int
5891lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
5892{
5893 struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
5894 rrq->nlp_DID);
5895 if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
5896 return lpfc_issue_els_rrq(rrq->vport, ndlp,
5897 rrq->nlp_DID, rrq);
5898 else
5899 return 1;
5900}
5901
e59058c4 5902/**
3621a710 5903 * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
e59058c4
JS
5904 * @vport: pointer to a host virtual N_Port data structure.
5905 * @cmdsize: size of the ELS command.
5906 * @oldiocb: pointer to the original lpfc command iocb data structure.
5907 * @ndlp: pointer to a node-list data structure.
5908 *
5909 * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
5910 * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
5911 *
5912 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5913 * will be incremented by 1 for holding the ndlp and the reference to ndlp
5914 * will be stored into the context1 field of the IOCB for the completion
5915 * callback function to the RPL Accept Response ELS command.
5916 *
5917 * Return code
5918 * 0 - Successfully issued ACC RPL ELS command
5919 * 1 - Failed to issue ACC RPL ELS command
5920 **/
082c0266 5921static int
2e0fef85
JS
5922lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
5923 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7bb3b137 5924{
2e0fef85
JS
5925 struct lpfc_hba *phba = vport->phba;
5926 IOCB_t *icmd, *oldcmd;
7bb3b137
JW
5927 RPL_RSP rpl_rsp;
5928 struct lpfc_iocbq *elsiocb;
7bb3b137 5929 uint8_t *pcmd;
dea3101e 5930
2e0fef85
JS
5931 elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5932 ndlp->nlp_DID, ELS_CMD_ACC);
7bb3b137 5933
488d1469 5934 if (!elsiocb)
7bb3b137 5935 return 1;
488d1469 5936
7bb3b137
JW
5937 icmd = &elsiocb->iocb;
5938 oldcmd = &oldiocb->iocb;
7851fe2c
JS
5939 icmd->ulpContext = oldcmd->ulpContext; /* Xri / rx_id */
5940 icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7bb3b137
JW
5941
5942 pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5943 *((uint32_t *) (pcmd)) = ELS_CMD_ACC;
92d7f7b0 5944 pcmd += sizeof(uint16_t);
7bb3b137
JW
5945 *((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
5946 pcmd += sizeof(uint16_t);
5947
5948 /* Setup the RPL ACC payload */
5949 rpl_rsp.listLen = be32_to_cpu(1);
5950 rpl_rsp.index = 0;
5951 rpl_rsp.port_num_blk.portNum = 0;
2e0fef85
JS
5952 rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
5953 memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7bb3b137 5954 sizeof(struct lpfc_name));
7bb3b137 5955 memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7bb3b137 5956 /* Xmit ELS RPL ACC response tag <ulpIoTag> */
e8b62011
JS
5957 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5958 "0120 Xmit ELS RPL ACC response tag x%x "
5959 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
5960 "rpi x%x\n",
5961 elsiocb->iotag, elsiocb->iocb.ulpContext,
5962 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5963 ndlp->nlp_rpi);
858c9f6c 5964 elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7bb3b137 5965 phba->fc_stat.elsXmitACC++;
3772a991
JS
5966 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
5967 IOCB_ERROR) {
7bb3b137
JW
5968 lpfc_els_free_iocb(phba, elsiocb);
5969 return 1;
5970 }
5971 return 0;
5972}
5973
e59058c4 5974/**
3621a710 5975 * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
e59058c4
JS
5976 * @vport: pointer to a host virtual N_Port data structure.
5977 * @cmdiocb: pointer to lpfc command iocb data structure.
5978 * @ndlp: pointer to a node-list data structure.
5979 *
5980 * This routine processes Read Port List (RPL) IOCB received as an ELS
5981 * unsolicited event. It first checks the remote port state. If the remote
5982 * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
5983 * invokes the lpfc_els_rsp_reject() routine to send reject response.
5984 * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
5985 * to accept the RPL.
5986 *
5987 * Return code
5988 * 0 - Successfully processed rpl iocb (currently always return 0)
5989 **/
7bb3b137 5990static int
2e0fef85
JS
5991lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
5992 struct lpfc_nodelist *ndlp)
7bb3b137
JW
5993{
5994 struct lpfc_dmabuf *pcmd;
5995 uint32_t *lp;
5996 uint32_t maxsize;
5997 uint16_t cmdsize;
5998 RPL *rpl;
5999 struct ls_rjt stat;
6000
2fe165b6
JW
6001 if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
6002 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
90160e01 6003 /* issue rejection response */
7bb3b137
JW
6004 stat.un.b.lsRjtRsvd0 = 0;
6005 stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6006 stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
6007 stat.un.b.vendorUnique = 0;
858c9f6c
JS
6008 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
6009 NULL);
90160e01
JS
6010 /* rejected the unsolicited RPL request and done with it */
6011 return 0;
7bb3b137
JW
6012 }
6013
dea3101e 6014 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6015 lp = (uint32_t *) pcmd->virt;
7bb3b137 6016 rpl = (RPL *) (lp + 1);
7bb3b137 6017 maxsize = be32_to_cpu(rpl->maxsize);
dea3101e 6018
7bb3b137
JW
6019 /* We support only one port */
6020 if ((rpl->index == 0) &&
6021 ((maxsize == 0) ||
6022 ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
6023 cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
2fe165b6 6024 } else {
7bb3b137
JW
6025 cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
6026 }
2e0fef85 6027 lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
dea3101e 6028
6029 return 0;
6030}
6031
e59058c4 6032/**
3621a710 6033 * lpfc_els_rcv_farp - Process an unsolicited farp request els command
e59058c4
JS
6034 * @vport: pointer to a virtual N_Port data structure.
6035 * @cmdiocb: pointer to lpfc command iocb data structure.
6036 * @ndlp: pointer to a node-list data structure.
6037 *
6038 * This routine processes Fibre Channel Address Resolution Protocol
6039 * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
6040 * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
6041 * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
6042 * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
6043 * remote PortName is compared against the FC PortName stored in the @vport
6044 * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
6045 * compared against the FC NodeName stored in the @vport data structure.
6046 * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
6047 * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
6048 * invoked to send out FARP Response to the remote node. Before sending the
6049 * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
6050 * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
6051 * routine is invoked to log into the remote port first.
6052 *
6053 * Return code
6054 * 0 - Either the FARP Match Mode not supported or successfully processed
6055 **/
dea3101e 6056static int
2e0fef85
JS
6057lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6058 struct lpfc_nodelist *ndlp)
dea3101e 6059{
6060 struct lpfc_dmabuf *pcmd;
6061 uint32_t *lp;
6062 IOCB_t *icmd;
6063 FARP *fp;
6064 uint32_t cmd, cnt, did;
6065
6066 icmd = &cmdiocb->iocb;
6067 did = icmd->un.elsreq64.remoteID;
6068 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6069 lp = (uint32_t *) pcmd->virt;
6070
6071 cmd = *lp++;
6072 fp = (FARP *) lp;
dea3101e 6073 /* FARP-REQ received from DID <did> */
e8b62011
JS
6074 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6075 "0601 FARP-REQ received from DID x%x\n", did);
dea3101e 6076 /* We will only support match on WWPN or WWNN */
6077 if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
c9f8735b 6078 return 0;
dea3101e 6079 }
6080
6081 cnt = 0;
6082 /* If this FARP command is searching for my portname */
6083 if (fp->Mflags & FARP_MATCH_PORT) {
2e0fef85 6084 if (memcmp(&fp->RportName, &vport->fc_portname,
92d7f7b0 6085 sizeof(struct lpfc_name)) == 0)
dea3101e 6086 cnt = 1;
6087 }
6088
6089 /* If this FARP command is searching for my nodename */
6090 if (fp->Mflags & FARP_MATCH_NODE) {
2e0fef85 6091 if (memcmp(&fp->RnodeName, &vport->fc_nodename,
92d7f7b0 6092 sizeof(struct lpfc_name)) == 0)
dea3101e 6093 cnt = 1;
6094 }
6095
6096 if (cnt) {
6097 if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
6098 (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
6099 /* Log back into the node before sending the FARP. */
6100 if (fp->Rflags & FARP_REQUEST_PLOGI) {
5024ab17 6101 ndlp->nlp_prev_state = ndlp->nlp_state;
2e0fef85 6102 lpfc_nlp_set_state(vport, ndlp,
de0c5b32 6103 NLP_STE_PLOGI_ISSUE);
2e0fef85 6104 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
dea3101e 6105 }
6106
6107 /* Send a FARP response to that node */
2e0fef85
JS
6108 if (fp->Rflags & FARP_REQUEST_FARPR)
6109 lpfc_issue_els_farpr(vport, did, 0);
dea3101e 6110 }
6111 }
c9f8735b 6112 return 0;
dea3101e 6113}
6114
e59058c4 6115/**
3621a710 6116 * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
e59058c4
JS
6117 * @vport: pointer to a host virtual N_Port data structure.
6118 * @cmdiocb: pointer to lpfc command iocb data structure.
6119 * @ndlp: pointer to a node-list data structure.
6120 *
6121 * This routine processes Fibre Channel Address Resolution Protocol
6122 * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
6123 * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
6124 * the FARP response request.
6125 *
6126 * Return code
6127 * 0 - Successfully processed FARPR IOCB (currently always return 0)
6128 **/
dea3101e 6129static int
2e0fef85
JS
6130lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6131 struct lpfc_nodelist *ndlp)
dea3101e 6132{
6133 struct lpfc_dmabuf *pcmd;
6134 uint32_t *lp;
6135 IOCB_t *icmd;
6136 uint32_t cmd, did;
6137
6138 icmd = &cmdiocb->iocb;
6139 did = icmd->un.elsreq64.remoteID;
6140 pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6141 lp = (uint32_t *) pcmd->virt;
6142
6143 cmd = *lp++;
6144 /* FARP-RSP received from DID <did> */
e8b62011
JS
6145 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6146 "0600 FARP-RSP received from DID x%x\n", did);
dea3101e 6147 /* ACCEPT the Farp resp request */
51ef4c26 6148 lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
dea3101e 6149
6150 return 0;
6151}
6152
e59058c4 6153/**
3621a710 6154 * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
e59058c4
JS
6155 * @vport: pointer to a host virtual N_Port data structure.
6156 * @cmdiocb: pointer to lpfc command iocb data structure.
6157 * @fan_ndlp: pointer to a node-list data structure.
6158 *
6159 * This routine processes a Fabric Address Notification (FAN) IOCB
6160 * command received as an ELS unsolicited event. The FAN ELS command will
6161 * only be processed on a physical port (i.e., the @vport represents the
6162 * physical port). The fabric NodeName and PortName from the FAN IOCB are
6163 * compared against those in the phba data structure. If any of those is
6164 * different, the lpfc_initial_flogi() routine is invoked to initialize
6165 * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
6166 * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
6167 * is invoked to register login to the fabric.
6168 *
6169 * Return code
6170 * 0 - Successfully processed fan iocb (currently always return 0).
6171 **/
dea3101e 6172static int
2e0fef85
JS
6173lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6174 struct lpfc_nodelist *fan_ndlp)
dea3101e 6175{
0d2b6b83 6176 struct lpfc_hba *phba = vport->phba;
dea3101e 6177 uint32_t *lp;
5024ab17 6178 FAN *fp;
dea3101e 6179
0d2b6b83
JS
6180 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
6181 lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
6182 fp = (FAN *) ++lp;
5024ab17 6183 /* FAN received; Fan does not have a reply sequence */
0d2b6b83
JS
6184 if ((vport == phba->pport) &&
6185 (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
5024ab17 6186 if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
0d2b6b83 6187 sizeof(struct lpfc_name))) ||
5024ab17 6188 (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
0d2b6b83
JS
6189 sizeof(struct lpfc_name)))) {
6190 /* This port has switched fabrics. FLOGI is required */
76a95d75 6191 lpfc_issue_init_vfi(vport);
0d2b6b83
JS
6192 } else {
6193 /* FAN verified - skip FLOGI */
6194 vport->fc_myDID = vport->fc_prevDID;
6fb120a7
JS
6195 if (phba->sli_rev < LPFC_SLI_REV4)
6196 lpfc_issue_fabric_reglogin(vport);
1b51197d
JS
6197 else {
6198 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6199 "3138 Need register VFI: (x%x/%x)\n",
6200 vport->fc_prevDID, vport->fc_myDID);
6fb120a7 6201 lpfc_issue_reg_vfi(vport);
1b51197d 6202 }
5024ab17 6203 }
dea3101e 6204 }
c9f8735b 6205 return 0;
dea3101e 6206}
6207
e59058c4 6208/**
3621a710 6209 * lpfc_els_timeout - Handler funciton to the els timer
e59058c4
JS
6210 * @ptr: holder for the timer function associated data.
6211 *
6212 * This routine is invoked by the ELS timer after timeout. It posts the ELS
6213 * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
6214 * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
6215 * up the worker thread. It is for the worker thread to invoke the routine
6216 * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
6217 **/
dea3101e 6218void
6219lpfc_els_timeout(unsigned long ptr)
6220{
2e0fef85
JS
6221 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
6222 struct lpfc_hba *phba = vport->phba;
5e9d9b82 6223 uint32_t tmo_posted;
dea3101e 6224 unsigned long iflag;
6225
2e0fef85 6226 spin_lock_irqsave(&vport->work_port_lock, iflag);
5e9d9b82 6227 tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
06918ac5 6228 if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
2e0fef85 6229 vport->work_port_events |= WORKER_ELS_TMO;
5e9d9b82 6230 spin_unlock_irqrestore(&vport->work_port_lock, iflag);
92d7f7b0 6231
06918ac5 6232 if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
5e9d9b82 6233 lpfc_worker_wake_up(phba);
dea3101e 6234 return;
6235}
6236
2a9bf3d0 6237
e59058c4 6238/**
3621a710 6239 * lpfc_els_timeout_handler - Process an els timeout event
e59058c4
JS
6240 * @vport: pointer to a virtual N_Port data structure.
6241 *
6242 * This routine is the actual handler function that processes an ELS timeout
6243 * event. It walks the ELS ring to get and abort all the IOCBs (except the
6244 * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
6245 * invoking the lpfc_sli_issue_abort_iotag() routine.
6246 **/
dea3101e 6247void
2e0fef85 6248lpfc_els_timeout_handler(struct lpfc_vport *vport)
dea3101e 6249{
2e0fef85 6250 struct lpfc_hba *phba = vport->phba;
dea3101e 6251 struct lpfc_sli_ring *pring;
6252 struct lpfc_iocbq *tmp_iocb, *piocb;
6253 IOCB_t *cmd = NULL;
6254 struct lpfc_dmabuf *pcmd;
2e0fef85 6255 uint32_t els_command = 0;
dea3101e 6256 uint32_t timeout;
2e0fef85 6257 uint32_t remote_ID = 0xffffffff;
2a9bf3d0
JS
6258 LIST_HEAD(abort_list);
6259
dea3101e 6260
dea3101e 6261 timeout = (uint32_t)(phba->fc_ratov << 1);
6262
6263 pring = &phba->sli.ring[LPFC_ELS_RING];
06918ac5
JS
6264 if ((phba->pport->load_flag & FC_UNLOADING))
6265 return;
2a9bf3d0 6266 spin_lock_irq(&phba->hbalock);
0976e1a6
JS
6267 if (phba->sli_rev == LPFC_SLI_REV4)
6268 spin_lock(&pring->ring_lock);
2a9bf3d0 6269
06918ac5
JS
6270 if ((phba->pport->load_flag & FC_UNLOADING)) {
6271 if (phba->sli_rev == LPFC_SLI_REV4)
6272 spin_unlock(&pring->ring_lock);
6273 spin_unlock_irq(&phba->hbalock);
6274 return;
6275 }
6276
0976e1a6 6277 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
dea3101e 6278 cmd = &piocb->iocb;
6279
2e0fef85
JS
6280 if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
6281 piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6282 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
dea3101e 6283 continue;
2e0fef85
JS
6284
6285 if (piocb->vport != vport)
6286 continue;
6287
dea3101e 6288 pcmd = (struct lpfc_dmabuf *) piocb->context2;
2e0fef85
JS
6289 if (pcmd)
6290 els_command = *(uint32_t *) (pcmd->virt);
dea3101e 6291
92d7f7b0
JS
6292 if (els_command == ELS_CMD_FARP ||
6293 els_command == ELS_CMD_FARPR ||
6294 els_command == ELS_CMD_FDISC)
6295 continue;
6296
dea3101e 6297 if (piocb->drvrTimeout > 0) {
92d7f7b0 6298 if (piocb->drvrTimeout >= timeout)
dea3101e 6299 piocb->drvrTimeout -= timeout;
92d7f7b0 6300 else
dea3101e 6301 piocb->drvrTimeout = 0;
dea3101e 6302 continue;
6303 }
6304
2e0fef85
JS
6305 remote_ID = 0xffffffff;
6306 if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
dea3101e 6307 remote_ID = cmd->un.elsreq64.remoteID;
2e0fef85
JS
6308 else {
6309 struct lpfc_nodelist *ndlp;
6310 ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
58da1ffb 6311 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2e0fef85 6312 remote_ID = ndlp->nlp_DID;
dea3101e 6313 }
2a9bf3d0
JS
6314 list_add_tail(&piocb->dlist, &abort_list);
6315 }
0976e1a6
JS
6316 if (phba->sli_rev == LPFC_SLI_REV4)
6317 spin_unlock(&pring->ring_lock);
2a9bf3d0
JS
6318 spin_unlock_irq(&phba->hbalock);
6319
6320 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
15026c9e 6321 cmd = &piocb->iocb;
e8b62011 6322 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2a9bf3d0
JS
6323 "0127 ELS timeout Data: x%x x%x x%x "
6324 "x%x\n", els_command,
6325 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
6326 spin_lock_irq(&phba->hbalock);
6327 list_del_init(&piocb->dlist);
07951076 6328 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
2a9bf3d0 6329 spin_unlock_irq(&phba->hbalock);
dea3101e 6330 }
5a0e326d 6331
0e9bb8d7 6332 if (!list_empty(&phba->sli.ring[LPFC_ELS_RING].txcmplq))
06918ac5
JS
6333 if (!(phba->pport->load_flag & FC_UNLOADING))
6334 mod_timer(&vport->els_tmofunc,
6335 jiffies + msecs_to_jiffies(1000 * timeout));
dea3101e 6336}
6337
e59058c4 6338/**
3621a710 6339 * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
e59058c4
JS
6340 * @vport: pointer to a host virtual N_Port data structure.
6341 *
6342 * This routine is used to clean up all the outstanding ELS commands on a
6343 * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
6344 * routine. After that, it walks the ELS transmit queue to remove all the
6345 * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
6346 * the IOCBs with a non-NULL completion callback function, the callback
6347 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
6348 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
6349 * callback function, the IOCB will simply be released. Finally, it walks
6350 * the ELS transmit completion queue to issue an abort IOCB to any transmit
6351 * completion queue IOCB that is associated with the @vport and is not
6352 * an IOCB from libdfc (i.e., the management plane IOCBs that are not
6353 * part of the discovery state machine) out to HBA by invoking the
6354 * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
6355 * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
6356 * the IOCBs are aborted when this function returns.
6357 **/
dea3101e 6358void
2e0fef85 6359lpfc_els_flush_cmd(struct lpfc_vport *vport)
dea3101e 6360{
0976e1a6 6361 LIST_HEAD(abort_list);
2e0fef85 6362 struct lpfc_hba *phba = vport->phba;
329f9bc7 6363 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 6364 struct lpfc_iocbq *tmp_iocb, *piocb;
6365 IOCB_t *cmd = NULL;
92d7f7b0
JS
6366
6367 lpfc_fabric_abort_vport(vport);
0976e1a6
JS
6368 /*
6369 * For SLI3, only the hbalock is required. But SLI4 needs to coordinate
6370 * with the ring insert operation. Because lpfc_sli_issue_abort_iotag
6371 * ultimately grabs the ring_lock, the driver must splice the list into
6372 * a working list and release the locks before calling the abort.
6373 */
6374 spin_lock_irq(&phba->hbalock);
6375 if (phba->sli_rev == LPFC_SLI_REV4)
6376 spin_lock(&pring->ring_lock);
6377
6378 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
6379 if (piocb->iocb_flag & LPFC_IO_LIBDFC)
6380 continue;
6381
6382 if (piocb->vport != vport)
6383 continue;
6384 list_add_tail(&piocb->dlist, &abort_list);
6385 }
6386 if (phba->sli_rev == LPFC_SLI_REV4)
6387 spin_unlock(&pring->ring_lock);
6388 spin_unlock_irq(&phba->hbalock);
6389 /* Abort each iocb on the aborted list and remove the dlist links. */
6390 list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
6391 spin_lock_irq(&phba->hbalock);
6392 list_del_init(&piocb->dlist);
6393 lpfc_sli_issue_abort_iotag(phba, pring, piocb);
6394 spin_unlock_irq(&phba->hbalock);
6395 }
6396 if (!list_empty(&abort_list))
6397 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6398 "3387 abort list for txq not empty\n");
6399 INIT_LIST_HEAD(&abort_list);
dea3101e 6400
2e0fef85 6401 spin_lock_irq(&phba->hbalock);
0976e1a6
JS
6402 if (phba->sli_rev == LPFC_SLI_REV4)
6403 spin_lock(&pring->ring_lock);
6404
dea3101e 6405 list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
6406 cmd = &piocb->iocb;
6407
6408 if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
6409 continue;
6410 }
6411
6412 /* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
329f9bc7
JS
6413 if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
6414 cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
6415 cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
6416 cmd->ulpCommand == CMD_ABORT_XRI_CN)
dea3101e 6417 continue;
dea3101e 6418
2e0fef85
JS
6419 if (piocb->vport != vport)
6420 continue;
6421
0976e1a6
JS
6422 list_del_init(&piocb->list);
6423 list_add_tail(&piocb->list, &abort_list);
dea3101e 6424 }
0976e1a6
JS
6425 if (phba->sli_rev == LPFC_SLI_REV4)
6426 spin_unlock(&pring->ring_lock);
2e0fef85 6427 spin_unlock_irq(&phba->hbalock);
2534ba75 6428
a257bf90 6429 /* Cancell all the IOCBs from the completions list */
0976e1a6
JS
6430 lpfc_sli_cancel_iocbs(phba, &abort_list,
6431 IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
2534ba75 6432
dea3101e 6433 return;
6434}
6435
e59058c4 6436/**
3621a710 6437 * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
e59058c4
JS
6438 * @phba: pointer to lpfc hba data structure.
6439 *
6440 * This routine is used to clean up all the outstanding ELS commands on a
6441 * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
6442 * routine. After that, it walks the ELS transmit queue to remove all the
6443 * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
6444 * the IOCBs with the completion callback function associated, the callback
6445 * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
6446 * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
6447 * callback function associated, the IOCB will simply be released. Finally,
6448 * it walks the ELS transmit completion queue to issue an abort IOCB to any
6449 * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
6450 * management plane IOCBs that are not part of the discovery state machine)
6451 * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
6452 **/
549e55cd
JS
6453void
6454lpfc_els_flush_all_cmd(struct lpfc_hba *phba)
6455{
0976e1a6
JS
6456 struct lpfc_vport *vport;
6457 list_for_each_entry(vport, &phba->port_list, listentry)
6458 lpfc_els_flush_cmd(vport);
a257bf90 6459
549e55cd
JS
6460 return;
6461}
6462
ea2151b4 6463/**
3621a710 6464 * lpfc_send_els_failure_event - Posts an ELS command failure event
ea2151b4
JS
6465 * @phba: Pointer to hba context object.
6466 * @cmdiocbp: Pointer to command iocb which reported error.
6467 * @rspiocbp: Pointer to response iocb which reported error.
6468 *
6469 * This function sends an event when there is an ELS command
6470 * failure.
6471 **/
6472void
6473lpfc_send_els_failure_event(struct lpfc_hba *phba,
6474 struct lpfc_iocbq *cmdiocbp,
6475 struct lpfc_iocbq *rspiocbp)
6476{
6477 struct lpfc_vport *vport = cmdiocbp->vport;
6478 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6479 struct lpfc_lsrjt_event lsrjt_event;
6480 struct lpfc_fabric_event_header fabric_event;
6481 struct ls_rjt stat;
6482 struct lpfc_nodelist *ndlp;
6483 uint32_t *pcmd;
6484
6485 ndlp = cmdiocbp->context1;
6486 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
6487 return;
6488
6489 if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
6490 lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
6491 lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
6492 memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
6493 sizeof(struct lpfc_name));
6494 memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
6495 sizeof(struct lpfc_name));
6496 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
6497 cmdiocbp->context2)->virt);
49198b37 6498 lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
ea2151b4
JS
6499 stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
6500 lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
6501 lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
6502 fc_host_post_vendor_event(shost,
6503 fc_get_event_number(),
6504 sizeof(lsrjt_event),
6505 (char *)&lsrjt_event,
ddcc50f0 6506 LPFC_NL_VENDOR_ID);
ea2151b4
JS
6507 return;
6508 }
6509 if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
6510 (rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
6511 fabric_event.event_type = FC_REG_FABRIC_EVENT;
6512 if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
6513 fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
6514 else
6515 fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
6516 memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
6517 sizeof(struct lpfc_name));
6518 memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
6519 sizeof(struct lpfc_name));
6520 fc_host_post_vendor_event(shost,
6521 fc_get_event_number(),
6522 sizeof(fabric_event),
6523 (char *)&fabric_event,
ddcc50f0 6524 LPFC_NL_VENDOR_ID);
ea2151b4
JS
6525 return;
6526 }
6527
6528}
6529
6530/**
3621a710 6531 * lpfc_send_els_event - Posts unsolicited els event
ea2151b4
JS
6532 * @vport: Pointer to vport object.
6533 * @ndlp: Pointer FC node object.
6534 * @cmd: ELS command code.
6535 *
6536 * This function posts an event when there is an incoming
6537 * unsolicited ELS command.
6538 **/
6539static void
6540lpfc_send_els_event(struct lpfc_vport *vport,
6541 struct lpfc_nodelist *ndlp,
ddcc50f0 6542 uint32_t *payload)
ea2151b4 6543{
ddcc50f0
JS
6544 struct lpfc_els_event_header *els_data = NULL;
6545 struct lpfc_logo_event *logo_data = NULL;
ea2151b4
JS
6546 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6547
ddcc50f0
JS
6548 if (*payload == ELS_CMD_LOGO) {
6549 logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
6550 if (!logo_data) {
6551 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6552 "0148 Failed to allocate memory "
6553 "for LOGO event\n");
6554 return;
6555 }
6556 els_data = &logo_data->header;
6557 } else {
6558 els_data = kmalloc(sizeof(struct lpfc_els_event_header),
6559 GFP_KERNEL);
6560 if (!els_data) {
6561 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6562 "0149 Failed to allocate memory "
6563 "for ELS event\n");
6564 return;
6565 }
6566 }
6567 els_data->event_type = FC_REG_ELS_EVENT;
6568 switch (*payload) {
ea2151b4 6569 case ELS_CMD_PLOGI:
ddcc50f0 6570 els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
ea2151b4
JS
6571 break;
6572 case ELS_CMD_PRLO:
ddcc50f0 6573 els_data->subcategory = LPFC_EVENT_PRLO_RCV;
ea2151b4
JS
6574 break;
6575 case ELS_CMD_ADISC:
ddcc50f0
JS
6576 els_data->subcategory = LPFC_EVENT_ADISC_RCV;
6577 break;
6578 case ELS_CMD_LOGO:
6579 els_data->subcategory = LPFC_EVENT_LOGO_RCV;
6580 /* Copy the WWPN in the LOGO payload */
6581 memcpy(logo_data->logo_wwpn, &payload[2],
6582 sizeof(struct lpfc_name));
ea2151b4
JS
6583 break;
6584 default:
e916141c 6585 kfree(els_data);
ea2151b4
JS
6586 return;
6587 }
ddcc50f0
JS
6588 memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
6589 memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
6590 if (*payload == ELS_CMD_LOGO) {
6591 fc_host_post_vendor_event(shost,
6592 fc_get_event_number(),
6593 sizeof(struct lpfc_logo_event),
6594 (char *)logo_data,
6595 LPFC_NL_VENDOR_ID);
6596 kfree(logo_data);
6597 } else {
6598 fc_host_post_vendor_event(shost,
6599 fc_get_event_number(),
6600 sizeof(struct lpfc_els_event_header),
6601 (char *)els_data,
6602 LPFC_NL_VENDOR_ID);
6603 kfree(els_data);
6604 }
ea2151b4
JS
6605
6606 return;
6607}
6608
6609
e59058c4 6610/**
3621a710 6611 * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
e59058c4
JS
6612 * @phba: pointer to lpfc hba data structure.
6613 * @pring: pointer to a SLI ring.
6614 * @vport: pointer to a host virtual N_Port data structure.
6615 * @elsiocb: pointer to lpfc els command iocb data structure.
6616 *
6617 * This routine is used for processing the IOCB associated with a unsolicited
6618 * event. It first determines whether there is an existing ndlp that matches
6619 * the DID from the unsolicited IOCB. If not, it will create a new one with
6620 * the DID from the unsolicited IOCB. The ELS command from the unsolicited
6621 * IOCB is then used to invoke the proper routine and to set up proper state
6622 * of the discovery state machine.
6623 **/
ed957684
JS
6624static void
6625lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
92d7f7b0 6626 struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
dea3101e 6627{
87af33fe 6628 struct Scsi_Host *shost;
dea3101e 6629 struct lpfc_nodelist *ndlp;
dea3101e 6630 struct ls_rjt stat;
92d7f7b0 6631 uint32_t *payload;
303f2f9c
JS
6632 uint32_t cmd, did, newnode;
6633 uint8_t rjt_exp, rjt_err = 0;
ed957684 6634 IOCB_t *icmd = &elsiocb->iocb;
dea3101e 6635
e47c9093 6636 if (!vport || !(elsiocb->context2))
dea3101e 6637 goto dropit;
2e0fef85 6638
dea3101e 6639 newnode = 0;
92d7f7b0
JS
6640 payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
6641 cmd = *payload;
ed957684 6642 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
495a714c 6643 lpfc_post_buffer(phba, pring, 1);
dea3101e 6644
858c9f6c
JS
6645 did = icmd->un.rcvels.remoteID;
6646 if (icmd->ulpStatus) {
6647 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6648 "RCV Unsol ELS: status:x%x/x%x did:x%x",
6649 icmd->ulpStatus, icmd->un.ulpWord[4], did);
dea3101e 6650 goto dropit;
858c9f6c 6651 }
dea3101e 6652
6653 /* Check to see if link went down during discovery */
ed957684 6654 if (lpfc_els_chk_latt(vport))
dea3101e 6655 goto dropit;
dea3101e 6656
c868595d 6657 /* Ignore traffic received during vport shutdown. */
92d7f7b0
JS
6658 if (vport->load_flag & FC_UNLOADING)
6659 goto dropit;
6660
92494144
JS
6661 /* If NPort discovery is delayed drop incoming ELS */
6662 if ((vport->fc_flag & FC_DISC_DELAYED) &&
6663 (cmd != ELS_CMD_PLOGI))
6664 goto dropit;
6665
2e0fef85 6666 ndlp = lpfc_findnode_did(vport, did);
c9f8735b 6667 if (!ndlp) {
dea3101e 6668 /* Cannot find existing Fabric ndlp, so allocate a new one */
c9f8735b 6669 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
ed957684 6670 if (!ndlp)
dea3101e 6671 goto dropit;
dea3101e 6672
2e0fef85 6673 lpfc_nlp_init(vport, ndlp, did);
98c9ea5c 6674 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
dea3101e 6675 newnode = 1;
e47c9093 6676 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
dea3101e 6677 ndlp->nlp_type |= NLP_FABRIC;
58da1ffb
JS
6678 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
6679 ndlp = lpfc_enable_node(vport, ndlp,
6680 NLP_STE_UNUSED_NODE);
6681 if (!ndlp)
6682 goto dropit;
6683 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6684 newnode = 1;
6685 if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6686 ndlp->nlp_type |= NLP_FABRIC;
6687 } else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
6688 /* This is similar to the new node path */
6689 ndlp = lpfc_nlp_get(ndlp);
6690 if (!ndlp)
6691 goto dropit;
6692 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6693 newnode = 1;
87af33fe 6694 }
dea3101e 6695
6696 phba->fc_stat.elsRcvFrame++;
e47c9093 6697
12838e74
JS
6698 /*
6699 * Do not process any unsolicited ELS commands
6700 * if the ndlp is in DEV_LOSS
6701 */
6702 if (ndlp->nlp_add_flag & NLP_IN_DEV_LOSS)
6703 goto dropit;
6704
329f9bc7 6705 elsiocb->context1 = lpfc_nlp_get(ndlp);
2e0fef85 6706 elsiocb->vport = vport;
dea3101e 6707
6708 if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
6709 cmd &= ELS_CMD_MASK;
6710 }
6711 /* ELS command <elsCmd> received from NPORT <did> */
e8b62011
JS
6712 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6713 "0112 ELS command x%x received from NPORT x%x "
e74c03c8
JS
6714 "Data: x%x x%x x%x x%x\n",
6715 cmd, did, vport->port_state, vport->fc_flag,
6716 vport->fc_myDID, vport->fc_prevDID);
dea3101e 6717 switch (cmd) {
6718 case ELS_CMD_PLOGI:
858c9f6c
JS
6719 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6720 "RCV PLOGI: did:x%x/ste:x%x flg:x%x",
6721 did, vport->port_state, ndlp->nlp_flag);
6722
dea3101e 6723 phba->fc_stat.elsRcvPLOGI++;
858c9f6c 6724 ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
e74c03c8
JS
6725 if (phba->sli_rev == LPFC_SLI_REV4 &&
6726 (phba->pport->fc_flag & FC_PT2PT)) {
6727 vport->fc_prevDID = vport->fc_myDID;
6728 /* Our DID needs to be updated before registering
6729 * the vfi. This is done in lpfc_rcv_plogi but
6730 * that is called after the reg_vfi.
6731 */
6732 vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
6733 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6734 "3312 Remote port assigned DID x%x "
6735 "%x\n", vport->fc_myDID,
6736 vport->fc_prevDID);
6737 }
858c9f6c 6738
ddcc50f0 6739 lpfc_send_els_event(vport, ndlp, payload);
92494144
JS
6740
6741 /* If Nport discovery is delayed, reject PLOGIs */
6742 if (vport->fc_flag & FC_DISC_DELAYED) {
6743 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6744 rjt_exp = LSEXP_NOTHING_MORE;
92494144
JS
6745 break;
6746 }
e74c03c8 6747 shost = lpfc_shost_from_vport(vport);
858c9f6c 6748 if (vport->port_state < LPFC_DISC_AUTH) {
1b32f6aa
JS
6749 if (!(phba->pport->fc_flag & FC_PT2PT) ||
6750 (phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
6751 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6752 rjt_exp = LSEXP_NOTHING_MORE;
1b32f6aa
JS
6753 break;
6754 }
6755 /* We get here, and drop thru, if we are PT2PT with
6756 * another NPort and the other side has initiated
6757 * the PLOGI before responding to our FLOGI.
6758 */
e74c03c8
JS
6759 if (phba->sli_rev == LPFC_SLI_REV4 &&
6760 (phba->fc_topology_changed ||
6761 vport->fc_myDID != vport->fc_prevDID)) {
6762 lpfc_unregister_fcf_prep(phba);
6763 spin_lock_irq(shost->host_lock);
6764 vport->fc_flag &= ~FC_VFI_REGISTERED;
6765 spin_unlock_irq(shost->host_lock);
6766 phba->fc_topology_changed = 0;
6767 lpfc_issue_reg_vfi(vport);
6768 }
dea3101e 6769 }
87af33fe 6770
87af33fe
JS
6771 spin_lock_irq(shost->host_lock);
6772 ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
6773 spin_unlock_irq(shost->host_lock);
6774
2e0fef85
JS
6775 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6776 NLP_EVT_RCV_PLOGI);
858c9f6c 6777
dea3101e 6778 break;
6779 case ELS_CMD_FLOGI:
858c9f6c
JS
6780 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6781 "RCV FLOGI: did:x%x/ste:x%x flg:x%x",
6782 did, vport->port_state, ndlp->nlp_flag);
6783
dea3101e 6784 phba->fc_stat.elsRcvFLOGI++;
51ef4c26 6785 lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
87af33fe 6786 if (newnode)
98c9ea5c 6787 lpfc_nlp_put(ndlp);
dea3101e 6788 break;
6789 case ELS_CMD_LOGO:
858c9f6c
JS
6790 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6791 "RCV LOGO: did:x%x/ste:x%x flg:x%x",
6792 did, vport->port_state, ndlp->nlp_flag);
6793
dea3101e 6794 phba->fc_stat.elsRcvLOGO++;
ddcc50f0 6795 lpfc_send_els_event(vport, ndlp, payload);
2e0fef85 6796 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6797 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6798 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e 6799 break;
6800 }
2e0fef85 6801 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
dea3101e 6802 break;
6803 case ELS_CMD_PRLO:
858c9f6c
JS
6804 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6805 "RCV PRLO: did:x%x/ste:x%x flg:x%x",
6806 did, vport->port_state, ndlp->nlp_flag);
6807
dea3101e 6808 phba->fc_stat.elsRcvPRLO++;
ddcc50f0 6809 lpfc_send_els_event(vport, ndlp, payload);
2e0fef85 6810 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6811 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6812 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e 6813 break;
6814 }
2e0fef85 6815 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
dea3101e 6816 break;
6817 case ELS_CMD_RSCN:
6818 phba->fc_stat.elsRcvRSCN++;
51ef4c26 6819 lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
87af33fe 6820 if (newnode)
98c9ea5c 6821 lpfc_nlp_put(ndlp);
dea3101e 6822 break;
6823 case ELS_CMD_ADISC:
858c9f6c
JS
6824 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6825 "RCV ADISC: did:x%x/ste:x%x flg:x%x",
6826 did, vport->port_state, ndlp->nlp_flag);
6827
ddcc50f0 6828 lpfc_send_els_event(vport, ndlp, payload);
dea3101e 6829 phba->fc_stat.elsRcvADISC++;
2e0fef85 6830 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6831 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6832 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e 6833 break;
6834 }
2e0fef85
JS
6835 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6836 NLP_EVT_RCV_ADISC);
dea3101e 6837 break;
6838 case ELS_CMD_PDISC:
858c9f6c
JS
6839 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6840 "RCV PDISC: did:x%x/ste:x%x flg:x%x",
6841 did, vport->port_state, ndlp->nlp_flag);
6842
dea3101e 6843 phba->fc_stat.elsRcvPDISC++;
2e0fef85 6844 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6845 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6846 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e 6847 break;
6848 }
2e0fef85
JS
6849 lpfc_disc_state_machine(vport, ndlp, elsiocb,
6850 NLP_EVT_RCV_PDISC);
dea3101e 6851 break;
6852 case ELS_CMD_FARPR:
858c9f6c
JS
6853 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6854 "RCV FARPR: did:x%x/ste:x%x flg:x%x",
6855 did, vport->port_state, ndlp->nlp_flag);
6856
dea3101e 6857 phba->fc_stat.elsRcvFARPR++;
2e0fef85 6858 lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
dea3101e 6859 break;
6860 case ELS_CMD_FARP:
858c9f6c
JS
6861 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6862 "RCV FARP: did:x%x/ste:x%x flg:x%x",
6863 did, vport->port_state, ndlp->nlp_flag);
6864
dea3101e 6865 phba->fc_stat.elsRcvFARP++;
2e0fef85 6866 lpfc_els_rcv_farp(vport, elsiocb, ndlp);
dea3101e 6867 break;
6868 case ELS_CMD_FAN:
858c9f6c
JS
6869 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6870 "RCV FAN: did:x%x/ste:x%x flg:x%x",
6871 did, vport->port_state, ndlp->nlp_flag);
6872
dea3101e 6873 phba->fc_stat.elsRcvFAN++;
2e0fef85 6874 lpfc_els_rcv_fan(vport, elsiocb, ndlp);
dea3101e 6875 break;
dea3101e 6876 case ELS_CMD_PRLI:
858c9f6c
JS
6877 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6878 "RCV PRLI: did:x%x/ste:x%x flg:x%x",
6879 did, vport->port_state, ndlp->nlp_flag);
6880
dea3101e 6881 phba->fc_stat.elsRcvPRLI++;
2e0fef85 6882 if (vport->port_state < LPFC_DISC_AUTH) {
858c9f6c 6883 rjt_err = LSRJT_UNABLE_TPC;
303f2f9c 6884 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e 6885 break;
6886 }
2e0fef85 6887 lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
dea3101e 6888 break;
7bb3b137 6889 case ELS_CMD_LIRR:
858c9f6c
JS
6890 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6891 "RCV LIRR: did:x%x/ste:x%x flg:x%x",
6892 did, vport->port_state, ndlp->nlp_flag);
6893
7bb3b137 6894 phba->fc_stat.elsRcvLIRR++;
2e0fef85 6895 lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
87af33fe 6896 if (newnode)
98c9ea5c 6897 lpfc_nlp_put(ndlp);
7bb3b137 6898 break;
12265f68
JS
6899 case ELS_CMD_RLS:
6900 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6901 "RCV RLS: did:x%x/ste:x%x flg:x%x",
6902 did, vport->port_state, ndlp->nlp_flag);
6903
6904 phba->fc_stat.elsRcvRLS++;
6905 lpfc_els_rcv_rls(vport, elsiocb, ndlp);
6906 if (newnode)
6907 lpfc_nlp_put(ndlp);
6908 break;
7bb3b137 6909 case ELS_CMD_RPS:
858c9f6c
JS
6910 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6911 "RCV RPS: did:x%x/ste:x%x flg:x%x",
6912 did, vport->port_state, ndlp->nlp_flag);
6913
7bb3b137 6914 phba->fc_stat.elsRcvRPS++;
2e0fef85 6915 lpfc_els_rcv_rps(vport, elsiocb, ndlp);
87af33fe 6916 if (newnode)
98c9ea5c 6917 lpfc_nlp_put(ndlp);
7bb3b137
JW
6918 break;
6919 case ELS_CMD_RPL:
858c9f6c
JS
6920 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6921 "RCV RPL: did:x%x/ste:x%x flg:x%x",
6922 did, vport->port_state, ndlp->nlp_flag);
6923
7bb3b137 6924 phba->fc_stat.elsRcvRPL++;
2e0fef85 6925 lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
87af33fe 6926 if (newnode)
98c9ea5c 6927 lpfc_nlp_put(ndlp);
7bb3b137 6928 break;
dea3101e 6929 case ELS_CMD_RNID:
858c9f6c
JS
6930 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6931 "RCV RNID: did:x%x/ste:x%x flg:x%x",
6932 did, vport->port_state, ndlp->nlp_flag);
6933
dea3101e 6934 phba->fc_stat.elsRcvRNID++;
2e0fef85 6935 lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
87af33fe 6936 if (newnode)
98c9ea5c 6937 lpfc_nlp_put(ndlp);
dea3101e 6938 break;
12265f68
JS
6939 case ELS_CMD_RTV:
6940 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6941 "RCV RTV: did:x%x/ste:x%x flg:x%x",
6942 did, vport->port_state, ndlp->nlp_flag);
6943 phba->fc_stat.elsRcvRTV++;
6944 lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
6945 if (newnode)
6946 lpfc_nlp_put(ndlp);
6947 break;
5ffc266e
JS
6948 case ELS_CMD_RRQ:
6949 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6950 "RCV RRQ: did:x%x/ste:x%x flg:x%x",
6951 did, vport->port_state, ndlp->nlp_flag);
6952
6953 phba->fc_stat.elsRcvRRQ++;
6954 lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
6955 if (newnode)
6956 lpfc_nlp_put(ndlp);
6957 break;
12265f68
JS
6958 case ELS_CMD_ECHO:
6959 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6960 "RCV ECHO: did:x%x/ste:x%x flg:x%x",
6961 did, vport->port_state, ndlp->nlp_flag);
6962
6963 phba->fc_stat.elsRcvECHO++;
6964 lpfc_els_rcv_echo(vport, elsiocb, ndlp);
6965 if (newnode)
6966 lpfc_nlp_put(ndlp);
6967 break;
303f2f9c
JS
6968 case ELS_CMD_REC:
6969 /* receive this due to exchange closed */
6970 rjt_err = LSRJT_UNABLE_TPC;
6971 rjt_exp = LSEXP_INVALID_OX_RX;
6972 break;
dea3101e 6973 default:
858c9f6c
JS
6974 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6975 "RCV ELS cmd: cmd:x%x did:x%x/ste:x%x",
6976 cmd, did, vport->port_state);
6977
dea3101e 6978 /* Unsupported ELS command, reject */
63e801ce 6979 rjt_err = LSRJT_CMD_UNSUPPORTED;
303f2f9c 6980 rjt_exp = LSEXP_NOTHING_MORE;
dea3101e 6981
6982 /* Unknown ELS command <elsCmd> received from NPORT <did> */
e8b62011
JS
6983 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6984 "0115 Unknown ELS command x%x "
6985 "received from NPORT x%x\n", cmd, did);
87af33fe 6986 if (newnode)
98c9ea5c 6987 lpfc_nlp_put(ndlp);
dea3101e 6988 break;
6989 }
6990
6991 /* check if need to LS_RJT received ELS cmd */
6992 if (rjt_err) {
92d7f7b0 6993 memset(&stat, 0, sizeof(stat));
858c9f6c 6994 stat.un.b.lsRjtRsnCode = rjt_err;
303f2f9c 6995 stat.un.b.lsRjtRsnCodeExp = rjt_exp;
858c9f6c
JS
6996 lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
6997 NULL);
dea3101e 6998 }
6999
d7c255b2
JS
7000 lpfc_nlp_put(elsiocb->context1);
7001 elsiocb->context1 = NULL;
ed957684
JS
7002 return;
7003
7004dropit:
98c9ea5c 7005 if (vport && !(vport->load_flag & FC_UNLOADING))
6fb120a7
JS
7006 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7007 "0111 Dropping received ELS cmd "
ed957684 7008 "Data: x%x x%x x%x\n",
6fb120a7 7009 icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
ed957684
JS
7010 phba->fc_stat.elsRcvDrop++;
7011}
7012
e59058c4 7013/**
3621a710 7014 * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
e59058c4
JS
7015 * @phba: pointer to lpfc hba data structure.
7016 * @pring: pointer to a SLI ring.
7017 * @elsiocb: pointer to lpfc els iocb data structure.
7018 *
7019 * This routine is used to process an unsolicited event received from a SLI
7020 * (Service Level Interface) ring. The actual processing of the data buffer
7021 * associated with the unsolicited event is done by invoking the routine
7022 * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
7023 * SLI ring on which the unsolicited event was received.
7024 **/
ed957684
JS
7025void
7026lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7027 struct lpfc_iocbq *elsiocb)
7028{
7029 struct lpfc_vport *vport = phba->pport;
ed957684 7030 IOCB_t *icmd = &elsiocb->iocb;
ed957684 7031 dma_addr_t paddr;
92d7f7b0
JS
7032 struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
7033 struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
7034
d7c255b2 7035 elsiocb->context1 = NULL;
92d7f7b0
JS
7036 elsiocb->context2 = NULL;
7037 elsiocb->context3 = NULL;
ed957684 7038
92d7f7b0
JS
7039 if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
7040 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
7041 } else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
e3d2b802
JS
7042 (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
7043 IOERR_RCV_BUFFER_WAITING) {
ed957684
JS
7044 phba->fc_stat.NoRcvBuf++;
7045 /* Not enough posted buffers; Try posting more buffers */
92d7f7b0 7046 if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
495a714c 7047 lpfc_post_buffer(phba, pring, 0);
ed957684
JS
7048 return;
7049 }
7050
92d7f7b0
JS
7051 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
7052 (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
7053 icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
7054 if (icmd->unsli3.rcvsli3.vpi == 0xffff)
7055 vport = phba->pport;
6fb120a7
JS
7056 else
7057 vport = lpfc_find_vport_by_vpid(phba,
6d368e53 7058 icmd->unsli3.rcvsli3.vpi);
92d7f7b0 7059 }
6d368e53 7060
7f5f3d0d
JS
7061 /* If there are no BDEs associated
7062 * with this IOCB, there is nothing to do.
7063 */
ed957684
JS
7064 if (icmd->ulpBdeCount == 0)
7065 return;
7066
7f5f3d0d
JS
7067 /* type of ELS cmd is first 32bit word
7068 * in packet
7069 */
ed957684 7070 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
92d7f7b0 7071 elsiocb->context2 = bdeBuf1;
ed957684
JS
7072 } else {
7073 paddr = getPaddr(icmd->un.cont64[0].addrHigh,
7074 icmd->un.cont64[0].addrLow);
92d7f7b0
JS
7075 elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
7076 paddr);
ed957684
JS
7077 }
7078
92d7f7b0
JS
7079 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
7080 /*
7081 * The different unsolicited event handlers would tell us
7082 * if they are done with "mp" by setting context2 to NULL.
7083 */
dea3101e 7084 if (elsiocb->context2) {
92d7f7b0
JS
7085 lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
7086 elsiocb->context2 = NULL;
dea3101e 7087 }
ed957684
JS
7088
7089 /* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
92d7f7b0 7090 if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
ed957684 7091 icmd->ulpBdeCount == 2) {
92d7f7b0
JS
7092 elsiocb->context2 = bdeBuf2;
7093 lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
ed957684
JS
7094 /* free mp if we are done with it */
7095 if (elsiocb->context2) {
92d7f7b0
JS
7096 lpfc_in_buf_free(phba, elsiocb->context2);
7097 elsiocb->context2 = NULL;
7098 }
7099 }
7100}
7101
e59058c4 7102/**
3621a710 7103 * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
e59058c4
JS
7104 * @phba: pointer to lpfc hba data structure.
7105 * @vport: pointer to a virtual N_Port data structure.
7106 *
7107 * This routine issues a Port Login (PLOGI) to the Name Server with
7108 * State Change Request (SCR) for a @vport. This routine will create an
7109 * ndlp for the Name Server associated to the @vport if such node does
7110 * not already exist. The PLOGI to Name Server is issued by invoking the
7111 * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
7112 * (FDMI) is configured to the @vport, a FDMI node will be created and
7113 * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
7114 **/
92d7f7b0
JS
7115void
7116lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
7117{
7118 struct lpfc_nodelist *ndlp, *ndlp_fdmi;
92494144
JS
7119 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7120
7121 /*
7122 * If lpfc_delay_discovery parameter is set and the clean address
7123 * bit is cleared and fc fabric parameters chenged, delay FC NPort
7124 * discovery.
7125 */
7126 spin_lock_irq(shost->host_lock);
7127 if (vport->fc_flag & FC_DISC_DELAYED) {
7128 spin_unlock_irq(shost->host_lock);
18775708
JS
7129 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
7130 "3334 Delay fc port discovery for %d seconds\n",
7131 phba->fc_ratov);
92494144 7132 mod_timer(&vport->delayed_disc_tmo,
256ec0d0 7133 jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
92494144
JS
7134 return;
7135 }
7136 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
7137
7138 ndlp = lpfc_findnode_did(vport, NameServer_DID);
7139 if (!ndlp) {
7140 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
7141 if (!ndlp) {
76a95d75 7142 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
92d7f7b0
JS
7143 lpfc_disc_start(vport);
7144 return;
7145 }
7146 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
7147 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7148 "0251 NameServer login: no memory\n");
92d7f7b0
JS
7149 return;
7150 }
7151 lpfc_nlp_init(vport, ndlp, NameServer_DID);
e47c9093
JS
7152 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
7153 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
7154 if (!ndlp) {
76a95d75 7155 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
e47c9093
JS
7156 lpfc_disc_start(vport);
7157 return;
7158 }
7159 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7160 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7161 "0348 NameServer login: node freed\n");
7162 return;
7163 }
92d7f7b0 7164 }
58da1ffb 7165 ndlp->nlp_type |= NLP_FABRIC;
92d7f7b0
JS
7166
7167 lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
7168
7169 if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
7170 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
7171 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7172 "0252 Cannot issue NameServer login\n");
92d7f7b0
JS
7173 return;
7174 }
7175
3de2a653 7176 if (vport->cfg_fdmi_on) {
63e801ce
JS
7177 /* If this is the first time, allocate an ndlp and initialize
7178 * it. Otherwise, make sure the node is enabled and then do the
7179 * login.
7180 */
7181 ndlp_fdmi = lpfc_findnode_did(vport, FDMI_DID);
7182 if (!ndlp_fdmi) {
7183 ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
7184 GFP_KERNEL);
7185 if (ndlp_fdmi) {
7186 lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
7187 ndlp_fdmi->nlp_type |= NLP_FABRIC;
7188 } else
7189 return;
7190 }
7191 if (!NLP_CHK_NODE_ACT(ndlp_fdmi))
7192 ndlp_fdmi = lpfc_enable_node(vport,
7193 ndlp_fdmi,
7194 NLP_STE_NPR_NODE);
7195
92d7f7b0 7196 if (ndlp_fdmi) {
58da1ffb 7197 lpfc_nlp_set_state(vport, ndlp_fdmi,
63e801ce
JS
7198 NLP_STE_PLOGI_ISSUE);
7199 lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID, 0);
92d7f7b0
JS
7200 }
7201 }
92d7f7b0
JS
7202}
7203
e59058c4 7204/**
3621a710 7205 * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
e59058c4
JS
7206 * @phba: pointer to lpfc hba data structure.
7207 * @pmb: pointer to the driver internal queue element for mailbox command.
7208 *
7209 * This routine is the completion callback function to register new vport
7210 * mailbox command. If the new vport mailbox command completes successfully,
7211 * the fabric registration login shall be performed on physical port (the
7212 * new vport created is actually a physical port, with VPI 0) or the port
7213 * login to Name Server for State Change Request (SCR) will be performed
7214 * on virtual port (real virtual port, with VPI greater than 0).
7215 **/
92d7f7b0
JS
7216static void
7217lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7218{
7219 struct lpfc_vport *vport = pmb->vport;
7220 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7221 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
04c68496 7222 MAILBOX_t *mb = &pmb->u.mb;
695a814e 7223 int rc;
92d7f7b0 7224
09372820 7225 spin_lock_irq(shost->host_lock);
92d7f7b0 7226 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
09372820 7227 spin_unlock_irq(shost->host_lock);
92d7f7b0
JS
7228
7229 if (mb->mbxStatus) {
e8b62011 7230 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
38b92ef8
JS
7231 "0915 Register VPI failed : Status: x%x"
7232 " upd bit: x%x \n", mb->mbxStatus,
7233 mb->un.varRegVpi.upd);
7234 if (phba->sli_rev == LPFC_SLI_REV4 &&
7235 mb->un.varRegVpi.upd)
7236 goto mbox_err_exit ;
92d7f7b0
JS
7237
7238 switch (mb->mbxStatus) {
7239 case 0x11: /* unsupported feature */
7240 case 0x9603: /* max_vpi exceeded */
7f5f3d0d 7241 case 0x9602: /* Link event since CLEAR_LA */
92d7f7b0
JS
7242 /* giving up on vport registration */
7243 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7244 spin_lock_irq(shost->host_lock);
7245 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
7246 spin_unlock_irq(shost->host_lock);
7247 lpfc_can_disctmo(vport);
7248 break;
695a814e
JS
7249 /* If reg_vpi fail with invalid VPI status, re-init VPI */
7250 case 0x20:
7251 spin_lock_irq(shost->host_lock);
7252 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
7253 spin_unlock_irq(shost->host_lock);
7254 lpfc_init_vpi(phba, pmb, vport->vpi);
7255 pmb->vport = vport;
7256 pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
7257 rc = lpfc_sli_issue_mbox(phba, pmb,
7258 MBX_NOWAIT);
7259 if (rc == MBX_NOT_FINISHED) {
7260 lpfc_printf_vlog(vport,
7261 KERN_ERR, LOG_MBOX,
7262 "2732 Failed to issue INIT_VPI"
7263 " mailbox command\n");
7264 } else {
7265 lpfc_nlp_put(ndlp);
7266 return;
7267 }
7268
92d7f7b0
JS
7269 default:
7270 /* Try to recover from this error */
5af5eee7
JS
7271 if (phba->sli_rev == LPFC_SLI_REV4)
7272 lpfc_sli4_unreg_all_rpis(vport);
92d7f7b0 7273 lpfc_mbx_unreg_vpi(vport);
09372820 7274 spin_lock_irq(shost->host_lock);
92d7f7b0 7275 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
09372820 7276 spin_unlock_irq(shost->host_lock);
4b40c59e
JS
7277 if (vport->port_type == LPFC_PHYSICAL_PORT
7278 && !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
76a95d75 7279 lpfc_issue_init_vfi(vport);
7f5f3d0d
JS
7280 else
7281 lpfc_initial_fdisc(vport);
92d7f7b0
JS
7282 break;
7283 }
92d7f7b0 7284 } else {
695a814e 7285 spin_lock_irq(shost->host_lock);
1987807d 7286 vport->vpi_state |= LPFC_VPI_REGISTERED;
695a814e
JS
7287 spin_unlock_irq(shost->host_lock);
7288 if (vport == phba->pport) {
6fb120a7
JS
7289 if (phba->sli_rev < LPFC_SLI_REV4)
7290 lpfc_issue_fabric_reglogin(vport);
695a814e 7291 else {
fc2b989b
JS
7292 /*
7293 * If the physical port is instantiated using
7294 * FDISC, do not start vport discovery.
7295 */
7296 if (vport->port_state != LPFC_FDISC)
7297 lpfc_start_fdiscs(phba);
695a814e
JS
7298 lpfc_do_scr_ns_plogi(phba, vport);
7299 }
7300 } else
92d7f7b0
JS
7301 lpfc_do_scr_ns_plogi(phba, vport);
7302 }
38b92ef8 7303mbox_err_exit:
fa4066b6
JS
7304 /* Now, we decrement the ndlp reference count held for this
7305 * callback function
7306 */
7307 lpfc_nlp_put(ndlp);
7308
92d7f7b0
JS
7309 mempool_free(pmb, phba->mbox_mem_pool);
7310 return;
7311}
7312
e59058c4 7313/**
3621a710 7314 * lpfc_register_new_vport - Register a new vport with a HBA
e59058c4
JS
7315 * @phba: pointer to lpfc hba data structure.
7316 * @vport: pointer to a host virtual N_Port data structure.
7317 * @ndlp: pointer to a node-list data structure.
7318 *
7319 * This routine registers the @vport as a new virtual port with a HBA.
7320 * It is done through a registering vpi mailbox command.
7321 **/
695a814e 7322void
92d7f7b0
JS
7323lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
7324 struct lpfc_nodelist *ndlp)
7325{
09372820 7326 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
92d7f7b0
JS
7327 LPFC_MBOXQ_t *mbox;
7328
7329 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7330 if (mbox) {
6fb120a7 7331 lpfc_reg_vpi(vport, mbox);
92d7f7b0
JS
7332 mbox->vport = vport;
7333 mbox->context2 = lpfc_nlp_get(ndlp);
7334 mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
0b727fea 7335 if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
92d7f7b0 7336 == MBX_NOT_FINISHED) {
fa4066b6
JS
7337 /* mailbox command not success, decrement ndlp
7338 * reference count for this command
7339 */
7340 lpfc_nlp_put(ndlp);
92d7f7b0 7341 mempool_free(mbox, phba->mbox_mem_pool);
92d7f7b0 7342
e8b62011
JS
7343 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7344 "0253 Register VPI: Can't send mbox\n");
fa4066b6 7345 goto mbox_err_exit;
92d7f7b0
JS
7346 }
7347 } else {
e8b62011
JS
7348 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
7349 "0254 Register VPI: no memory\n");
fa4066b6 7350 goto mbox_err_exit;
92d7f7b0 7351 }
fa4066b6
JS
7352 return;
7353
7354mbox_err_exit:
7355 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7356 spin_lock_irq(shost->host_lock);
7357 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
7358 spin_unlock_irq(shost->host_lock);
7359 return;
92d7f7b0
JS
7360}
7361
695a814e 7362/**
0c9ab6f5 7363 * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
695a814e
JS
7364 * @phba: pointer to lpfc hba data structure.
7365 *
0c9ab6f5 7366 * This routine cancels the retry delay timers to all the vports.
695a814e
JS
7367 **/
7368void
0c9ab6f5 7369lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
695a814e
JS
7370{
7371 struct lpfc_vport **vports;
7372 struct lpfc_nodelist *ndlp;
695a814e 7373 uint32_t link_state;
0c9ab6f5 7374 int i;
695a814e
JS
7375
7376 /* Treat this failure as linkdown for all vports */
7377 link_state = phba->link_state;
7378 lpfc_linkdown(phba);
7379 phba->link_state = link_state;
7380
7381 vports = lpfc_create_vport_work_array(phba);
7382
7383 if (vports) {
7384 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
7385 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
7386 if (ndlp)
7387 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
7388 lpfc_els_flush_cmd(vports[i]);
7389 }
7390 lpfc_destroy_vport_work_array(phba, vports);
7391 }
0c9ab6f5
JS
7392}
7393
7394/**
7395 * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
7396 * @phba: pointer to lpfc hba data structure.
7397 *
7398 * This routine abort all pending discovery commands and
7399 * start a timer to retry FLOGI for the physical port
7400 * discovery.
7401 **/
7402void
7403lpfc_retry_pport_discovery(struct lpfc_hba *phba)
7404{
7405 struct lpfc_nodelist *ndlp;
7406 struct Scsi_Host *shost;
7407
7408 /* Cancel the all vports retry delay retry timers */
7409 lpfc_cancel_all_vport_retry_delay_timer(phba);
695a814e
JS
7410
7411 /* If fabric require FLOGI, then re-instantiate physical login */
7412 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
7413 if (!ndlp)
7414 return;
7415
695a814e 7416 shost = lpfc_shost_from_vport(phba->pport);
256ec0d0 7417 mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
695a814e
JS
7418 spin_lock_irq(shost->host_lock);
7419 ndlp->nlp_flag |= NLP_DELAY_TMO;
7420 spin_unlock_irq(shost->host_lock);
7421 ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
7422 phba->pport->port_state = LPFC_FLOGI;
7423 return;
7424}
7425
7426/**
7427 * lpfc_fabric_login_reqd - Check if FLOGI required.
7428 * @phba: pointer to lpfc hba data structure.
7429 * @cmdiocb: pointer to FDISC command iocb.
7430 * @rspiocb: pointer to FDISC response iocb.
7431 *
7432 * This routine checks if a FLOGI is reguired for FDISC
7433 * to succeed.
7434 **/
7435static int
7436lpfc_fabric_login_reqd(struct lpfc_hba *phba,
7437 struct lpfc_iocbq *cmdiocb,
7438 struct lpfc_iocbq *rspiocb)
7439{
7440
7441 if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
7442 (rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
7443 return 0;
7444 else
7445 return 1;
7446}
7447
e59058c4 7448/**
3621a710 7449 * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
e59058c4
JS
7450 * @phba: pointer to lpfc hba data structure.
7451 * @cmdiocb: pointer to lpfc command iocb data structure.
7452 * @rspiocb: pointer to lpfc response iocb data structure.
7453 *
7454 * This routine is the completion callback function to a Fabric Discover
7455 * (FDISC) ELS command. Since all the FDISC ELS commands are issued
7456 * single threaded, each FDISC completion callback function will reset
7457 * the discovery timer for all vports such that the timers will not get
7458 * unnecessary timeout. The function checks the FDISC IOCB status. If error
7459 * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
7460 * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
7461 * assigned to the vport has been changed with the completion of the FDISC
7462 * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
7463 * are unregistered from the HBA, and then the lpfc_register_new_vport()
7464 * routine is invoked to register new vport with the HBA. Otherwise, the
7465 * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
7466 * Server for State Change Request (SCR).
7467 **/
92d7f7b0
JS
7468static void
7469lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7470 struct lpfc_iocbq *rspiocb)
7471{
7472 struct lpfc_vport *vport = cmdiocb->vport;
7473 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7474 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
7475 struct lpfc_nodelist *np;
7476 struct lpfc_nodelist *next_np;
7477 IOCB_t *irsp = &rspiocb->iocb;
7478 struct lpfc_iocbq *piocb;
92494144
JS
7479 struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
7480 struct serv_parm *sp;
7481 uint8_t fabric_param_changed;
92d7f7b0 7482
e8b62011
JS
7483 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7484 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
7485 irsp->ulpStatus, irsp->un.ulpWord[4],
7486 vport->fc_prevDID);
92d7f7b0
JS
7487 /* Since all FDISCs are being single threaded, we
7488 * must reset the discovery timer for ALL vports
7489 * waiting to send FDISC when one completes.
7490 */
7491 list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
7492 lpfc_set_disctmo(piocb->vport);
7493 }
7494
858c9f6c
JS
7495 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7496 "FDISC cmpl: status:x%x/x%x prevdid:x%x",
7497 irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
7498
92d7f7b0 7499 if (irsp->ulpStatus) {
695a814e
JS
7500
7501 if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
7502 lpfc_retry_pport_discovery(phba);
7503 goto out;
7504 }
7505
92d7f7b0
JS
7506 /* Check for retry */
7507 if (lpfc_els_retry(phba, cmdiocb, rspiocb))
7508 goto out;
92d7f7b0 7509 /* FDISC failed */
e8b62011 7510 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6b5151fd 7511 "0126 FDISC failed. (x%x/x%x)\n",
e8b62011 7512 irsp->ulpStatus, irsp->un.ulpWord[4]);
d7c255b2
JS
7513 goto fdisc_failed;
7514 }
d7c255b2 7515 spin_lock_irq(shost->host_lock);
695a814e 7516 vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
4b40c59e 7517 vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
d7c255b2 7518 vport->fc_flag |= FC_FABRIC;
76a95d75 7519 if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
d7c255b2
JS
7520 vport->fc_flag |= FC_PUBLIC_LOOP;
7521 spin_unlock_irq(shost->host_lock);
92d7f7b0 7522
d7c255b2
JS
7523 vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
7524 lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
92494144 7525 prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
a2fc4aef
JS
7526 if (!prsp)
7527 goto out;
92494144
JS
7528 sp = prsp->virt + sizeof(uint32_t);
7529 fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
7530 memcpy(&vport->fabric_portname, &sp->portName,
7531 sizeof(struct lpfc_name));
7532 memcpy(&vport->fabric_nodename, &sp->nodeName,
7533 sizeof(struct lpfc_name));
7534 if (fabric_param_changed &&
d7c255b2
JS
7535 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
7536 /* If our NportID changed, we need to ensure all
7537 * remaining NPORTs get unreg_login'ed so we can
7538 * issue unreg_vpi.
7539 */
7540 list_for_each_entry_safe(np, next_np,
7541 &vport->fc_nodes, nlp_listp) {
7542 if (!NLP_CHK_NODE_ACT(ndlp) ||
7543 (np->nlp_state != NLP_STE_NPR_NODE) ||
7544 !(np->nlp_flag & NLP_NPR_ADISC))
7545 continue;
09372820 7546 spin_lock_irq(shost->host_lock);
d7c255b2 7547 np->nlp_flag &= ~NLP_NPR_ADISC;
09372820 7548 spin_unlock_irq(shost->host_lock);
d7c255b2 7549 lpfc_unreg_rpi(vport, np);
92d7f7b0 7550 }
78730cfe 7551 lpfc_cleanup_pending_mbox(vport);
5af5eee7
JS
7552
7553 if (phba->sli_rev == LPFC_SLI_REV4)
7554 lpfc_sli4_unreg_all_rpis(vport);
7555
d7c255b2
JS
7556 lpfc_mbx_unreg_vpi(vport);
7557 spin_lock_irq(shost->host_lock);
7558 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
0f65ff68
JS
7559 if (phba->sli_rev == LPFC_SLI_REV4)
7560 vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
4b40c59e
JS
7561 else
7562 vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
d7c255b2 7563 spin_unlock_irq(shost->host_lock);
38b92ef8
JS
7564 } else if ((phba->sli_rev == LPFC_SLI_REV4) &&
7565 !(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
7566 /*
7567 * Driver needs to re-reg VPI in order for f/w
7568 * to update the MAC address.
7569 */
7570 lpfc_register_new_vport(phba, vport, ndlp);
5ac6b303 7571 goto out;
92d7f7b0
JS
7572 }
7573
ecfd03c6
JS
7574 if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
7575 lpfc_issue_init_vpi(vport);
7576 else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
d7c255b2
JS
7577 lpfc_register_new_vport(phba, vport, ndlp);
7578 else
7579 lpfc_do_scr_ns_plogi(phba, vport);
7580 goto out;
7581fdisc_failed:
7582 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
7583 /* Cancel discovery timer */
7584 lpfc_can_disctmo(vport);
7585 lpfc_nlp_put(ndlp);
92d7f7b0
JS
7586out:
7587 lpfc_els_free_iocb(phba, cmdiocb);
7588}
7589
e59058c4 7590/**
3621a710 7591 * lpfc_issue_els_fdisc - Issue a fdisc iocb command
e59058c4
JS
7592 * @vport: pointer to a virtual N_Port data structure.
7593 * @ndlp: pointer to a node-list data structure.
7594 * @retry: number of retries to the command IOCB.
7595 *
7596 * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
7597 * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
7598 * routine to issue the IOCB, which makes sure only one outstanding fabric
7599 * IOCB will be sent off HBA at any given time.
7600 *
7601 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7602 * will be incremented by 1 for holding the ndlp and the reference to ndlp
7603 * will be stored into the context1 field of the IOCB for the completion
7604 * callback function to the FDISC ELS command.
7605 *
7606 * Return code
7607 * 0 - Successfully issued fdisc iocb command
7608 * 1 - Failed to issue fdisc iocb command
7609 **/
a6ababd2 7610static int
92d7f7b0
JS
7611lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7612 uint8_t retry)
7613{
7614 struct lpfc_hba *phba = vport->phba;
7615 IOCB_t *icmd;
7616 struct lpfc_iocbq *elsiocb;
7617 struct serv_parm *sp;
7618 uint8_t *pcmd;
7619 uint16_t cmdsize;
7620 int did = ndlp->nlp_DID;
7621 int rc;
92d7f7b0 7622
5ffc266e 7623 vport->port_state = LPFC_FDISC;
6b5151fd 7624 vport->fc_myDID = 0;
92d7f7b0
JS
7625 cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
7626 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
7627 ELS_CMD_FDISC);
7628 if (!elsiocb) {
92d7f7b0 7629 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
7630 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7631 "0255 Issue FDISC: no IOCB\n");
92d7f7b0
JS
7632 return 1;
7633 }
7634
7635 icmd = &elsiocb->iocb;
7636 icmd->un.elsreq64.myID = 0;
7637 icmd->un.elsreq64.fl = 1;
7638
73d91e50
JS
7639 /*
7640 * SLI3 ports require a different context type value than SLI4.
7641 * Catch SLI3 ports here and override the prep.
7642 */
7643 if (phba->sli_rev == LPFC_SLI_REV3) {
f1126688
JS
7644 icmd->ulpCt_h = 1;
7645 icmd->ulpCt_l = 0;
7646 }
92d7f7b0
JS
7647
7648 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7649 *((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
7650 pcmd += sizeof(uint32_t); /* CSP Word 1 */
7651 memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
7652 sp = (struct serv_parm *) pcmd;
7653 /* Setup CSPs accordingly for Fabric */
7654 sp->cmn.e_d_tov = 0;
7655 sp->cmn.w2.r_a_tov = 0;
df9e1b59 7656 sp->cmn.virtual_fabric_support = 0;
92d7f7b0
JS
7657 sp->cls1.classValid = 0;
7658 sp->cls2.seqDelivery = 1;
7659 sp->cls3.seqDelivery = 1;
7660
7661 pcmd += sizeof(uint32_t); /* CSP Word 2 */
7662 pcmd += sizeof(uint32_t); /* CSP Word 3 */
7663 pcmd += sizeof(uint32_t); /* CSP Word 4 */
7664 pcmd += sizeof(uint32_t); /* Port Name */
7665 memcpy(pcmd, &vport->fc_portname, 8);
7666 pcmd += sizeof(uint32_t); /* Node Name */
7667 pcmd += sizeof(uint32_t); /* Node Name */
7668 memcpy(pcmd, &vport->fc_nodename, 8);
7669
7670 lpfc_set_disctmo(vport);
7671
7672 phba->fc_stat.elsXmitFDISC++;
7673 elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
7674
858c9f6c
JS
7675 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7676 "Issue FDISC: did:x%x",
7677 did, 0, 0);
7678
92d7f7b0
JS
7679 rc = lpfc_issue_fabric_iocb(phba, elsiocb);
7680 if (rc == IOCB_ERROR) {
7681 lpfc_els_free_iocb(phba, elsiocb);
92d7f7b0 7682 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
e8b62011
JS
7683 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
7684 "0256 Issue FDISC: Cannot send IOCB\n");
92d7f7b0
JS
7685 return 1;
7686 }
7687 lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
92d7f7b0
JS
7688 return 0;
7689}
7690
e59058c4 7691/**
3621a710 7692 * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
e59058c4
JS
7693 * @phba: pointer to lpfc hba data structure.
7694 * @cmdiocb: pointer to lpfc command iocb data structure.
7695 * @rspiocb: pointer to lpfc response iocb data structure.
7696 *
7697 * This routine is the completion callback function to the issuing of a LOGO
7698 * ELS command off a vport. It frees the command IOCB and then decrement the
7699 * reference count held on ndlp for this completion function, indicating that
7700 * the reference to the ndlp is no long needed. Note that the
7701 * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
7702 * callback function and an additional explicit ndlp reference decrementation
7703 * will trigger the actual release of the ndlp.
7704 **/
92d7f7b0
JS
7705static void
7706lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7707 struct lpfc_iocbq *rspiocb)
7708{
7709 struct lpfc_vport *vport = cmdiocb->vport;
858c9f6c 7710 IOCB_t *irsp;
e47c9093 7711 struct lpfc_nodelist *ndlp;
9589b062 7712 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
858c9f6c 7713
9589b062 7714 ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
858c9f6c
JS
7715 irsp = &rspiocb->iocb;
7716 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7717 "LOGO npiv cmpl: status:x%x/x%x did:x%x",
7718 irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
92d7f7b0
JS
7719
7720 lpfc_els_free_iocb(phba, cmdiocb);
7721 vport->unreg_vpi_cmpl = VPORT_ERROR;
e47c9093
JS
7722
7723 /* Trigger the release of the ndlp after logo */
7724 lpfc_nlp_put(ndlp);
9589b062
JS
7725
7726 /* NPIV LOGO completes to NPort <nlp_DID> */
7727 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7728 "2928 NPIV LOGO completes to NPort x%x "
7729 "Data: x%x x%x x%x x%x\n",
7730 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
7731 irsp->ulpTimeout, vport->num_disc_nodes);
7732
7733 if (irsp->ulpStatus == IOSTAT_SUCCESS) {
7734 spin_lock_irq(shost->host_lock);
7735 vport->fc_flag &= ~FC_FABRIC;
7736 spin_unlock_irq(shost->host_lock);
7737 }
92d7f7b0
JS
7738}
7739
e59058c4 7740/**
3621a710 7741 * lpfc_issue_els_npiv_logo - Issue a logo off a vport
e59058c4
JS
7742 * @vport: pointer to a virtual N_Port data structure.
7743 * @ndlp: pointer to a node-list data structure.
7744 *
7745 * This routine issues a LOGO ELS command to an @ndlp off a @vport.
7746 *
7747 * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7748 * will be incremented by 1 for holding the ndlp and the reference to ndlp
7749 * will be stored into the context1 field of the IOCB for the completion
7750 * callback function to the LOGO ELS command.
7751 *
7752 * Return codes
7753 * 0 - Successfully issued logo off the @vport
7754 * 1 - Failed to issue logo off the @vport
7755 **/
92d7f7b0
JS
7756int
7757lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
7758{
7759 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7760 struct lpfc_hba *phba = vport->phba;
92d7f7b0
JS
7761 IOCB_t *icmd;
7762 struct lpfc_iocbq *elsiocb;
7763 uint8_t *pcmd;
7764 uint16_t cmdsize;
7765
7766 cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
7767 elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
7768 ELS_CMD_LOGO);
7769 if (!elsiocb)
7770 return 1;
7771
7772 icmd = &elsiocb->iocb;
7773 pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7774 *((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
7775 pcmd += sizeof(uint32_t);
7776
7777 /* Fill in LOGO payload */
7778 *((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
7779 pcmd += sizeof(uint32_t);
7780 memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
7781
858c9f6c
JS
7782 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7783 "Issue LOGO npiv did:x%x flg:x%x",
7784 ndlp->nlp_DID, ndlp->nlp_flag, 0);
7785
92d7f7b0
JS
7786 elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
7787 spin_lock_irq(shost->host_lock);
7788 ndlp->nlp_flag |= NLP_LOGO_SND;
7789 spin_unlock_irq(shost->host_lock);
3772a991
JS
7790 if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7791 IOCB_ERROR) {
92d7f7b0
JS
7792 spin_lock_irq(shost->host_lock);
7793 ndlp->nlp_flag &= ~NLP_LOGO_SND;
7794 spin_unlock_irq(shost->host_lock);
7795 lpfc_els_free_iocb(phba, elsiocb);
7796 return 1;
7797 }
7798 return 0;
7799}
7800
e59058c4 7801/**
3621a710 7802 * lpfc_fabric_block_timeout - Handler function to the fabric block timer
e59058c4
JS
7803 * @ptr: holder for the timer function associated data.
7804 *
7805 * This routine is invoked by the fabric iocb block timer after
7806 * timeout. It posts the fabric iocb block timeout event by setting the
7807 * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
7808 * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
7809 * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
7810 * posted event WORKER_FABRIC_BLOCK_TMO.
7811 **/
92d7f7b0
JS
7812void
7813lpfc_fabric_block_timeout(unsigned long ptr)
7814{
7815 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
7816 unsigned long iflags;
7817 uint32_t tmo_posted;
5e9d9b82 7818
92d7f7b0
JS
7819 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
7820 tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
7821 if (!tmo_posted)
7822 phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
7823 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
7824
5e9d9b82
JS
7825 if (!tmo_posted)
7826 lpfc_worker_wake_up(phba);
7827 return;
92d7f7b0
JS
7828}
7829
e59058c4 7830/**
3621a710 7831 * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
e59058c4
JS
7832 * @phba: pointer to lpfc hba data structure.
7833 *
7834 * This routine issues one fabric iocb from the driver internal list to
7835 * the HBA. It first checks whether it's ready to issue one fabric iocb to
7836 * the HBA (whether there is no outstanding fabric iocb). If so, it shall
7837 * remove one pending fabric iocb from the driver internal list and invokes
7838 * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
7839 **/
92d7f7b0
JS
7840static void
7841lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
7842{
7843 struct lpfc_iocbq *iocb;
7844 unsigned long iflags;
7845 int ret;
92d7f7b0
JS
7846 IOCB_t *cmd;
7847
7848repeat:
7849 iocb = NULL;
7850 spin_lock_irqsave(&phba->hbalock, iflags);
7f5f3d0d 7851 /* Post any pending iocb to the SLI layer */
92d7f7b0
JS
7852 if (atomic_read(&phba->fabric_iocb_count) == 0) {
7853 list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
7854 list);
7855 if (iocb)
7f5f3d0d 7856 /* Increment fabric iocb count to hold the position */
92d7f7b0
JS
7857 atomic_inc(&phba->fabric_iocb_count);
7858 }
7859 spin_unlock_irqrestore(&phba->hbalock, iflags);
7860 if (iocb) {
7861 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
7862 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
7863 iocb->iocb_flag |= LPFC_IO_FABRIC;
7864
858c9f6c
JS
7865 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
7866 "Fabric sched1: ste:x%x",
7867 iocb->vport->port_state, 0, 0);
7868
3772a991 7869 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
92d7f7b0
JS
7870
7871 if (ret == IOCB_ERROR) {
7872 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
7873 iocb->fabric_iocb_cmpl = NULL;
7874 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
7875 cmd = &iocb->iocb;
7876 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
7877 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
7878 iocb->iocb_cmpl(phba, iocb, iocb);
7879
7880 atomic_dec(&phba->fabric_iocb_count);
7881 goto repeat;
7882 }
7883 }
7884
7885 return;
7886}
7887
e59058c4 7888/**
3621a710 7889 * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
e59058c4
JS
7890 * @phba: pointer to lpfc hba data structure.
7891 *
7892 * This routine unblocks the issuing fabric iocb command. The function
7893 * will clear the fabric iocb block bit and then invoke the routine
7894 * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
7895 * from the driver internal fabric iocb list.
7896 **/
92d7f7b0
JS
7897void
7898lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
7899{
7900 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7901
7902 lpfc_resume_fabric_iocbs(phba);
7903 return;
7904}
7905
e59058c4 7906/**
3621a710 7907 * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
e59058c4
JS
7908 * @phba: pointer to lpfc hba data structure.
7909 *
7910 * This routine blocks the issuing fabric iocb for a specified amount of
7911 * time (currently 100 ms). This is done by set the fabric iocb block bit
7912 * and set up a timeout timer for 100ms. When the block bit is set, no more
7913 * fabric iocb will be issued out of the HBA.
7914 **/
92d7f7b0
JS
7915static void
7916lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
7917{
7918 int blocked;
7919
7920 blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
7f5f3d0d 7921 /* Start a timer to unblock fabric iocbs after 100ms */
92d7f7b0 7922 if (!blocked)
256ec0d0
JS
7923 mod_timer(&phba->fabric_block_timer,
7924 jiffies + msecs_to_jiffies(100));
92d7f7b0
JS
7925
7926 return;
7927}
7928
e59058c4 7929/**
3621a710 7930 * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
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 callback function that is put to the fabric iocb's
7936 * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
7937 * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
7938 * function first restores and invokes the original iocb's callback function
7939 * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
7940 * fabric bound iocb from the driver internal fabric iocb list onto the wire.
7941 **/
92d7f7b0
JS
7942static void
7943lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7944 struct lpfc_iocbq *rspiocb)
7945{
7946 struct ls_rjt stat;
7947
7948 if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
7949 BUG();
7950
7951 switch (rspiocb->iocb.ulpStatus) {
7952 case IOSTAT_NPORT_RJT:
7953 case IOSTAT_FABRIC_RJT:
7954 if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
7955 lpfc_block_fabric_iocbs(phba);
ed957684 7956 }
92d7f7b0
JS
7957 break;
7958
7959 case IOSTAT_NPORT_BSY:
7960 case IOSTAT_FABRIC_BSY:
7961 lpfc_block_fabric_iocbs(phba);
7962 break;
7963
7964 case IOSTAT_LS_RJT:
7965 stat.un.lsRjtError =
7966 be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
7967 if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
7968 (stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
7969 lpfc_block_fabric_iocbs(phba);
7970 break;
7971 }
7972
7973 if (atomic_read(&phba->fabric_iocb_count) == 0)
7974 BUG();
7975
7976 cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
7977 cmdiocb->fabric_iocb_cmpl = NULL;
7978 cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
7979 cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
7980
7981 atomic_dec(&phba->fabric_iocb_count);
7982 if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
7f5f3d0d
JS
7983 /* Post any pending iocbs to HBA */
7984 lpfc_resume_fabric_iocbs(phba);
92d7f7b0
JS
7985 }
7986}
7987
e59058c4 7988/**
3621a710 7989 * lpfc_issue_fabric_iocb - Issue a fabric iocb command
e59058c4
JS
7990 * @phba: pointer to lpfc hba data structure.
7991 * @iocb: pointer to lpfc command iocb data structure.
7992 *
7993 * This routine is used as the top-level API for issuing a fabric iocb command
7994 * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
7995 * function makes sure that only one fabric bound iocb will be outstanding at
7996 * any given time. As such, this function will first check to see whether there
7997 * is already an outstanding fabric iocb on the wire. If so, it will put the
7998 * newly issued iocb onto the driver internal fabric iocb list, waiting to be
7999 * issued later. Otherwise, it will issue the iocb on the wire and update the
8000 * fabric iocb count it indicate that there is one fabric iocb on the wire.
8001 *
8002 * Note, this implementation has a potential sending out fabric IOCBs out of
8003 * order. The problem is caused by the construction of the "ready" boolen does
8004 * not include the condition that the internal fabric IOCB list is empty. As
8005 * such, it is possible a fabric IOCB issued by this routine might be "jump"
8006 * ahead of the fabric IOCBs in the internal list.
8007 *
8008 * Return code
8009 * IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
8010 * IOCB_ERROR - failed to issue fabric iocb
8011 **/
a6ababd2 8012static int
92d7f7b0
JS
8013lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
8014{
8015 unsigned long iflags;
92d7f7b0
JS
8016 int ready;
8017 int ret;
8018
8019 if (atomic_read(&phba->fabric_iocb_count) > 1)
8020 BUG();
8021
8022 spin_lock_irqsave(&phba->hbalock, iflags);
8023 ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
8024 !test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
8025
7f5f3d0d
JS
8026 if (ready)
8027 /* Increment fabric iocb count to hold the position */
8028 atomic_inc(&phba->fabric_iocb_count);
92d7f7b0
JS
8029 spin_unlock_irqrestore(&phba->hbalock, iflags);
8030 if (ready) {
8031 iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
8032 iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
8033 iocb->iocb_flag |= LPFC_IO_FABRIC;
8034
858c9f6c
JS
8035 lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
8036 "Fabric sched2: ste:x%x",
8037 iocb->vport->port_state, 0, 0);
8038
3772a991 8039 ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
92d7f7b0
JS
8040
8041 if (ret == IOCB_ERROR) {
8042 iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
8043 iocb->fabric_iocb_cmpl = NULL;
8044 iocb->iocb_flag &= ~LPFC_IO_FABRIC;
8045 atomic_dec(&phba->fabric_iocb_count);
8046 }
8047 } else {
8048 spin_lock_irqsave(&phba->hbalock, iflags);
8049 list_add_tail(&iocb->list, &phba->fabric_iocb_list);
8050 spin_unlock_irqrestore(&phba->hbalock, iflags);
8051 ret = IOCB_SUCCESS;
8052 }
8053 return ret;
8054}
8055
e59058c4 8056/**
3621a710 8057 * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
e59058c4
JS
8058 * @vport: pointer to a virtual N_Port data structure.
8059 *
8060 * This routine aborts all the IOCBs associated with a @vport from the
8061 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
8062 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
8063 * list, removes each IOCB associated with the @vport off the list, set the
8064 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
8065 * associated with the IOCB.
8066 **/
a6ababd2 8067static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
92d7f7b0
JS
8068{
8069 LIST_HEAD(completions);
8070 struct lpfc_hba *phba = vport->phba;
8071 struct lpfc_iocbq *tmp_iocb, *piocb;
92d7f7b0
JS
8072
8073 spin_lock_irq(&phba->hbalock);
8074 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
8075 list) {
8076
8077 if (piocb->vport != vport)
8078 continue;
8079
8080 list_move_tail(&piocb->list, &completions);
8081 }
8082 spin_unlock_irq(&phba->hbalock);
8083
a257bf90
JS
8084 /* Cancel all the IOCBs from the completions list */
8085 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8086 IOERR_SLI_ABORTED);
92d7f7b0
JS
8087}
8088
e59058c4 8089/**
3621a710 8090 * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
e59058c4
JS
8091 * @ndlp: pointer to a node-list data structure.
8092 *
8093 * This routine aborts all the IOCBs associated with an @ndlp from the
8094 * driver internal fabric IOCB list. The list contains fabric IOCBs to be
8095 * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
8096 * list, removes each IOCB associated with the @ndlp off the list, set the
8097 * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
8098 * associated with the IOCB.
8099 **/
92d7f7b0
JS
8100void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
8101{
8102 LIST_HEAD(completions);
a257bf90 8103 struct lpfc_hba *phba = ndlp->phba;
92d7f7b0
JS
8104 struct lpfc_iocbq *tmp_iocb, *piocb;
8105 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
92d7f7b0
JS
8106
8107 spin_lock_irq(&phba->hbalock);
8108 list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
8109 list) {
8110 if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
8111
8112 list_move_tail(&piocb->list, &completions);
ed957684 8113 }
dea3101e 8114 }
92d7f7b0
JS
8115 spin_unlock_irq(&phba->hbalock);
8116
a257bf90
JS
8117 /* Cancel all the IOCBs from the completions list */
8118 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8119 IOERR_SLI_ABORTED);
92d7f7b0
JS
8120}
8121
e59058c4 8122/**
3621a710 8123 * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
e59058c4
JS
8124 * @phba: pointer to lpfc hba data structure.
8125 *
8126 * This routine aborts all the IOCBs currently on the driver internal
8127 * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
8128 * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
8129 * list, removes IOCBs off the list, set the status feild to
8130 * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
8131 * the IOCB.
8132 **/
92d7f7b0
JS
8133void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
8134{
8135 LIST_HEAD(completions);
92d7f7b0
JS
8136
8137 spin_lock_irq(&phba->hbalock);
8138 list_splice_init(&phba->fabric_iocb_list, &completions);
8139 spin_unlock_irq(&phba->hbalock);
8140
a257bf90
JS
8141 /* Cancel all the IOCBs from the completions list */
8142 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8143 IOERR_SLI_ABORTED);
dea3101e 8144}
6fb120a7 8145
1151e3ec
JS
8146/**
8147 * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
8148 * @vport: pointer to lpfc vport data structure.
8149 *
8150 * This routine is invoked by the vport cleanup for deletions and the cleanup
8151 * for an ndlp on removal.
8152 **/
8153void
8154lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
8155{
8156 struct lpfc_hba *phba = vport->phba;
8157 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8158 unsigned long iflag = 0;
8159
8160 spin_lock_irqsave(&phba->hbalock, iflag);
8161 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
8162 list_for_each_entry_safe(sglq_entry, sglq_next,
8163 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
8164 if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
8165 sglq_entry->ndlp = NULL;
8166 }
8167 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
8168 spin_unlock_irqrestore(&phba->hbalock, iflag);
8169 return;
8170}
8171
6fb120a7
JS
8172/**
8173 * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
8174 * @phba: pointer to lpfc hba data structure.
8175 * @axri: pointer to the els xri abort wcqe structure.
8176 *
8177 * This routine is invoked by the worker thread to process a SLI4 slow-path
8178 * ELS aborted xri.
8179 **/
8180void
8181lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
8182 struct sli4_wcqe_xri_aborted *axri)
8183{
8184 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
19ca7609 8185 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
7851fe2c 8186 uint16_t lxri = 0;
19ca7609 8187
6fb120a7
JS
8188 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
8189 unsigned long iflag = 0;
19ca7609 8190 struct lpfc_nodelist *ndlp;
589a52d6 8191 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6fb120a7 8192
0f65ff68
JS
8193 spin_lock_irqsave(&phba->hbalock, iflag);
8194 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
6fb120a7
JS
8195 list_for_each_entry_safe(sglq_entry, sglq_next,
8196 &phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
8197 if (sglq_entry->sli4_xritag == xri) {
8198 list_del(&sglq_entry->list);
19ca7609
JS
8199 ndlp = sglq_entry->ndlp;
8200 sglq_entry->ndlp = NULL;
dafe8cea 8201 spin_lock(&pring->ring_lock);
6fb120a7
JS
8202 list_add_tail(&sglq_entry->list,
8203 &phba->sli4_hba.lpfc_sgl_list);
0f65ff68 8204 sglq_entry->state = SGL_FREED;
dafe8cea 8205 spin_unlock(&pring->ring_lock);
0f65ff68 8206 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
6fb120a7 8207 spin_unlock_irqrestore(&phba->hbalock, iflag);
ee0f4fe1
JS
8208 lpfc_set_rrq_active(phba, ndlp,
8209 sglq_entry->sli4_lxritag,
8210 rxid, 1);
589a52d6
JS
8211
8212 /* Check if TXQ queue needs to be serviced */
0e9bb8d7 8213 if (!(list_empty(&pring->txq)))
589a52d6 8214 lpfc_worker_wake_up(phba);
6fb120a7
JS
8215 return;
8216 }
8217 }
0f65ff68 8218 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
7851fe2c
JS
8219 lxri = lpfc_sli4_xri_inrange(phba, xri);
8220 if (lxri == NO_XRI) {
8221 spin_unlock_irqrestore(&phba->hbalock, iflag);
8222 return;
8223 }
dafe8cea 8224 spin_lock(&pring->ring_lock);
7851fe2c 8225 sglq_entry = __lpfc_get_active_sglq(phba, lxri);
0f65ff68 8226 if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
dafe8cea 8227 spin_unlock(&pring->ring_lock);
0f65ff68
JS
8228 spin_unlock_irqrestore(&phba->hbalock, iflag);
8229 return;
8230 }
8231 sglq_entry->state = SGL_XRI_ABORTED;
dafe8cea 8232 spin_unlock(&pring->ring_lock);
0f65ff68
JS
8233 spin_unlock_irqrestore(&phba->hbalock, iflag);
8234 return;
6fb120a7 8235}
086a345f
JS
8236
8237/* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
8238 * @vport: pointer to virtual port object.
8239 * @ndlp: nodelist pointer for the impacted node.
8240 *
8241 * The driver calls this routine in response to an SLI4 XRI ABORT CQE
8242 * or an SLI3 ASYNC_STATUS_CN event from the port. For either event,
8243 * the driver is required to send a LOGO to the remote node before it
8244 * attempts to recover its login to the remote node.
8245 */
8246void
8247lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
8248 struct lpfc_nodelist *ndlp)
8249{
8250 struct Scsi_Host *shost;
8251 struct lpfc_hba *phba;
8252 unsigned long flags = 0;
8253
8254 shost = lpfc_shost_from_vport(vport);
8255 phba = vport->phba;
8256 if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
8257 lpfc_printf_log(phba, KERN_INFO,
8258 LOG_SLI, "3093 No rport recovery needed. "
8259 "rport in state 0x%x\n", ndlp->nlp_state);
8260 return;
8261 }
8262 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8263 "3094 Start rport recovery on shost id 0x%x "
8264 "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
8265 "flags 0x%x\n",
8266 shost->host_no, ndlp->nlp_DID,
8267 vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
8268 ndlp->nlp_flag);
8269 /*
8270 * The rport is not responding. Remove the FCP-2 flag to prevent
8271 * an ADISC in the follow-up recovery code.
8272 */
8273 spin_lock_irqsave(shost->host_lock, flags);
8274 ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
8275 spin_unlock_irqrestore(shost->host_lock, flags);
8276 lpfc_issue_els_logo(vport, ndlp, 0);
8277 lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
8278}
8279
This page took 2.030687 seconds and 5 git commands to generate.