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