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