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